Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
main.cpp
1 /*
2 
3 Pencil - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2013-2017 Matt Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 #include <QTranslator>
18 #include <QLibraryInfo>
19 #include <QDir>
20 #include <QCommandLineParser>
21 #include <QCommandLineOption>
22 #include "editor.h"
23 #include "mainwindow2.h"
24 #include "pencilapplication.h"
25 #include <iostream>
26 #include <cstring>
27 
28 using std::cout;
29 using std::endl;
30 
31 void installTranslator( PencilApplication& app )
32 {
33  QSettings setting( PENCIL2D, PENCIL2D );
34  QString strUserLocale = setting.value( SETTING_LANGUAGE ).toString();
35  if ( strUserLocale.isEmpty() )
36  {
37  strUserLocale = QLocale::system().name();
38  }
39 
40  QString strQtLocale = strUserLocale;
41  strQtLocale.replace( "-", "_" );
42  QTranslator* qtTranslator = new QTranslator(&app);
43  qtTranslator->load( "qt_" + strUserLocale, QLibraryInfo::location( QLibraryInfo::TranslationsPath ) );
44  app.installTranslator( qtTranslator );
45 
46  strUserLocale.replace( "_", "-" );
47  qDebug() << "Detect locale =" << strUserLocale;
48 
49  QTranslator* pencil2DTranslator = new QTranslator(&app);
50  bool b = pencil2DTranslator->load( ":/qm/Language." + strUserLocale );
51 
52  qDebug() << "Load translation = " << b;
53 
54  b = app.installTranslator( pencil2DTranslator );
55 
56  qDebug() << "Install translation = " << b;
57 }
58 
59 int handleArguments( MainWindow2 & mainWindow )
60 {
61  QStringList args = PencilApplication::arguments();
62  QString inputPath;
63  QStringList outputPaths;
64  int width = -1, height = -1;
65  bool transparency = false;
66 
67  QCommandLineParser parser;
68  // TODO: Ignore -NSDocumentRevisionsDebugMode
69 
70  parser.setApplicationDescription( PencilApplication::tr("Pencil2D is an animation/drawing software for Mac OS X, Windows, and Linux. It lets you create traditional hand-drawn animation (cartoon) using both bitmap and vector graphics.") );
71  parser.addHelpOption();
72  parser.addVersionOption();
73  parser.addPositionalArgument( "input", PencilApplication::tr( "Path to the input pencil file." ) );
74 
75  QCommandLineOption exportSeqOption( QStringList() << "o" << "export-sequence",
76  PencilApplication::tr( "Render the file to <output_path>" ),
77  PencilApplication::tr( "output_path" ) );
78  parser.addOption( exportSeqOption );
79 
80  QCommandLineOption widthOption( QStringList() << "width",
81  PencilApplication::tr( "Width of the output frames" ),
82  PencilApplication::tr( "integer" ) );
83  parser.addOption( widthOption );
84 
85  QCommandLineOption heightOption( QStringList() << "height",
86  PencilApplication::tr( "Height of the output frames" ),
87  PencilApplication::tr( "integer" ) );
88  parser.addOption( heightOption );
89 
90  QCommandLineOption transparencyOption( QStringList() << "transparency",
91  PencilApplication::tr( "Render transparency when possible" ) );
92  parser.addOption( transparencyOption );
93 
94  parser.process( args );
95 
96  QStringList posArgs = parser.positionalArguments();
97  if ( !posArgs.isEmpty() )
98  {
99  inputPath = posArgs.at(0);
100  }
101 
102  outputPaths = parser.values( exportSeqOption );
103 
104  if ( !parser.value( widthOption ).isEmpty() )
105  {
106  bool ok = false;
107  width = parser.value( widthOption ).toInt( &ok );
108  if ( !ok )
109  {
110  qDebug() << PencilApplication::tr( "Warning: width value %1 is not an integer, ignoring." ).arg(parser.value( widthOption ));
111  width = -1;
112  }
113  }
114  if ( !parser.value( heightOption ).isEmpty() )
115  {
116  bool ok = false;
117  height = parser.value( heightOption ).toInt( &ok );
118  if ( !ok )
119  {
120  qDebug() << PencilApplication::tr( "Warning: height value %1 is not an integer, ignoring." ).arg(parser.value( heightOption ));
121  height = -1;
122  }
123  }
124  transparency = parser.isSet( transparencyOption );
125 
126  // If there are no output paths, open up the GUI (to the input path if there is one)
127  if ( outputPaths.isEmpty() )
128  {
129  mainWindow.show();
130  if( !inputPath.isEmpty() )
131  {
132  mainWindow.openFile(inputPath);
133  }
134  return PencilApplication::exec();
135  }
136  else if ( inputPath.isEmpty() )
137  {
138  // Error if there are output paths without an input path
139  qDebug() << PencilApplication::tr( "Error: No input file specified." );
140  return 1;
141  }
142 
143  std::cout << PencilApplication::tr( "Exporting image sequence..." ).constData() << std::endl;
144 
145  QFileInfo inputFileInfo(inputPath);
146  if(!inputFileInfo.exists()) {
147  qDebug() << PencilApplication::tr( "Error: the input file at '%1' does not exist" ).arg(inputPath);
148  return 1;
149  }
150  if ( !inputFileInfo.isFile() )
151  {
152  qDebug() << PencilApplication::tr( "Error: the input path '%1' is not a file" ).arg(inputPath);
153  return 1;
154  }
155 
156  for ( int i = 0; i < outputPaths.length(); i++ )
157  {
158  mainWindow.openFile( inputPath );
159 
160  // Detect format
161  QString format;
162  QMap<QString, QString> extensionMapping;
163  extensionMapping[ "png" ] = "PNG";
164  extensionMapping[ "jpg" ] = "JPG";
165  extensionMapping[ "jpeg" ] = "JPG";
166  extensionMapping[ "tif" ] = "TIF";
167  extensionMapping[ "tiff" ] = "TIF";
168  extensionMapping[ "bmp" ] = "BMP";
169  QString extension = outputPaths[i].mid( outputPaths[i].lastIndexOf( "." ) + 1 ).toLower();
170  //qDebug() << "Ext: " << outputPaths[i].lastIndexOf(".") << " " << extension << " " << outputPaths[i];
171  if ( inputPath.contains(".") && extensionMapping.contains( extension ) )
172  {
173  format = extensionMapping[extension];
174  }
175  else {
176  qDebug() << PencilApplication::tr( "Warning: Output format is not specified or unsupported. Using PNG." );
177  format = "PNG";
178  }
179 
180  mainWindow.mEditor->exportSeqCLI( outputPaths[i], format, width, height, transparency );
181  }
182  qDebug() << PencilApplication::tr( "Done." );
183 
184  return 0;
185 }
186 
187 bool isGUIMode(int argc, char* argv[] )
188 {
189  bool b = false;
190  b |= ( argc == 1 );
191  b |= ( argc <= 3 ) && QString( argv[ 1 ] ) == "-NSDocumentRevisionsDebugMode";
192  return b;
193 }
194 
195 int main(int argc, char* argv[])
196 {
197  PencilApplication app( argc, argv );
198 
199  installTranslator( app );
200 
201  MainWindow2 mainWindow;
202  mainWindow.setWindowTitle( PENCIL_WINDOW_TITLE );
203 
204  QObject::connect( &app, &PencilApplication::openFileRequested, &mainWindow, &MainWindow2::openFile );
205  app.emitOpenFileRequest();
206 
207  if ( isGUIMode( argc, argv ) )
208  {
209  mainWindow.show();
210  return app.exec();
211  }
212 
213  return handleArguments( mainWindow );
214 }