All Classes Namespaces Functions Variables Enumerations Properties Pages
commandlineparser.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew 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 
18 #include "commandlineparser.h"
19 
20 #include <QTextStream>
21 
22 CommandLineParser::CommandLineParser() : mParser(), mInputPath(), mOutputPaths(), mCamera()
23 {
24  mParser.setApplicationDescription(tr("Pencil2D is an animation/drawing software for Mac OS X, Windows, and Linux. "
25  "It lets you create traditional hand-drawn animation (cartoon) using both bitmap and vector graphics."));
26  mParser.addHelpOption();
27  mParser.addVersionOption();
28  mParser.addPositionalArgument("input", tr("Path to the input pencil file."));
29 
30  QCommandLineOption exportOutOption(QStringList() << "o" << "export",
31  tr("Render the file to <output_path>"),
32  tr("output_path"));
33  mParser.addOption(exportOutOption);
34 
35  // for backwards compatibility
36  QCommandLineOption exportSeqOption(QStringList() << "export-sequence",
37  tr("Render the file to <output_path>"),
38  tr("output_path"));
39 #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
40  exportSeqOption.setFlags(QCommandLineOption::HiddenFromHelp);
41 #endif
42  mParser.addOption(exportSeqOption);
43 
44  QCommandLineOption cameraOption(QStringList() << "camera",
45  tr("Name of the camera layer to use"),
46  tr("layer_name"));
47  mParser.addOption(cameraOption);
48 
49  QCommandLineOption widthOption(QStringList() << "width",
50  tr("Width of the output frames"),
51  tr("integer"));
52  mParser.addOption(widthOption);
53 
54  QCommandLineOption heightOption(QStringList() << "height",
55  tr("Height of the output frames"),
56  tr("integer"));
57  mParser.addOption(heightOption);
58 
59  QCommandLineOption startOption(QStringList() << "start",
60  tr("The first frame you want to include in the exported movie"),
61  tr("frame"));
62  mParser.addOption(startOption);
63 
64  QCommandLineOption endOption(QStringList() << "end",
65  tr("The last frame you want to include in the exported movie. "
66  "Can also be last or last-sound to automatically use the last "
67  "frame containing animation or sound, respectively"),
68  tr("frame"));
69  mParser.addOption(endOption);
70 
71  QCommandLineOption transparencyOption(QStringList() << "transparency",
72  tr("Render transparency when possible"));
73  mParser.addOption(transparencyOption);
74 }
75 
76 void CommandLineParser::process(QStringList arguments)
77 {
78  QTextStream out(stdout);
79  QTextStream err(stderr);
80 
81  arguments.removeOne("-NSDocumentRevisionsDebugMode");
82 
83  mParser.process(arguments);
84 
85  QStringList posArgs = mParser.positionalArguments();
86  if (!posArgs.isEmpty())
87  {
88  mInputPath = posArgs.at(0);
89  }
90 
91  mOutputPaths << mParser.values("export") << mParser.values("export-sequence");
92 
93  if (!mParser.value("width").isEmpty())
94  {
95  bool ok = false;
96  mWidth = mParser.value("width").toInt(&ok);
97  if (!ok)
98  {
99  err << tr("Warning: width value %1 is not an integer, ignoring.").arg(mParser.value("width")) << endl;
100  mWidth = -1;
101  }
102  }
103 
104  if (!mParser.value("height").isEmpty())
105  {
106  bool ok = false;
107  mHeight = mParser.value("height").toInt(&ok);
108  if (!ok)
109  {
110  err << tr("Warning: height value %1 is not an integer, ignoring.").arg(mParser.value("height")) << endl;
111  mHeight = -1;
112  }
113  }
114 
115  if (!mParser.value("start").isEmpty())
116  {
117  bool ok = false;
118  mStartFrame = mParser.value("start").toInt(&ok);
119  if (!ok)
120  {
121  err << tr("Warning: start value %1 is not an integer, ignoring.").arg(mParser.value("start")) << endl;
122  mStartFrame = 1;
123  }
124  if (mStartFrame < 1)
125  {
126  err << tr("Warning: start value must be at least 1, ignoring.") << endl;
127  mStartFrame = 1;
128  }
129  }
130 
131  if (!mParser.value("end").isEmpty())
132  {
133  if (mParser.value("end") == "last")
134  {
135  mEndFrame = -1;
136  }
137  else if (mParser.value("end") == "last-sound")
138  {
139  mEndFrame = -2;
140  }
141  else
142  {
143  bool ok = false;
144  mEndFrame = mParser.value("end").toInt(&ok);
145  if (!ok)
146  {
147  err << tr("Warning: end value %1 is not an integer, last or last-sound, ignoring.").arg(mParser.value("end")) << endl;
148  mEndFrame = -1;
149  }
150  }
151  if (mEndFrame > -1 && mEndFrame < mStartFrame)
152  {
153  err << tr("Warning: end value %1 is smaller than start value %2, ignoring.").arg(mEndFrame).arg(mStartFrame) << endl;
154  mEndFrame = mStartFrame;
155  }
156  }
157 
158  mTransparency = mParser.isSet("transparency");
159 
160  mCamera = mParser.value("camera");
161 }
QStringList positionalArguments() const const
const T & at(int i) const const
QTextStream & endl(QTextStream &stream)
QString tr(const char *sourceText, const char *disambiguation, int n)
QStringList values(const QString &optionName) const const
int toInt(bool *ok, int base) const const
bool isEmpty() const const
bool isEmpty() const const
bool isSet(const QString &name) const const
void process(const QStringList &arguments)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool removeOne(const T &value)
QString value(const QString &optionName) const const