All Classes Namespaces Functions Variables Enumerations Properties Pages
pencil2d.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 <memory>
19 
20 #include <QDebug>
21 #include <QFileOpenEvent>
22 #include <QIcon>
23 #include <QLibraryInfo>
24 #include <QSettings>
25 #include <QTranslator>
26 
27 #include "commandlineexporter.h"
28 #include "commandlineparser.h"
29 #include "mainwindow2.h"
30 #include "pencildef.h"
31 #include "platformhandler.h"
32 
33 #include "pencil2d.h"
34 
35 Pencil2D::Pencil2D(int& argc, char** argv) :
36  QApplication(argc, argv)
37 {
38  // Set organization and application name
39  setOrganizationName("Pencil2D");
40  setOrganizationDomain("pencil2d.org");
41  setApplicationName("Pencil2D");
42  setApplicationDisplayName("Pencil2D");
43 
44  // Set application version
45  setApplicationVersion(APP_VERSION);
46 
47  // Set application icon
48  setWindowIcon(QIcon(":/icons/icon.png"));
49 
50 #if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
51  // Associate the application with our desktop entry
52  setDesktopFileName("org.pencil2d.Pencil2D.desktop");
53 #endif
55 }
56 
57 Pencil2D::~Pencil2D()
58 {
59  // with a std::unique_ptr member variable,
60  // you need a non-default destructor to avoid compilation error.
61 }
62 
64 {
65  CommandLineParser parser;
66  parser.process(arguments());
67 
68  QString inputPath = parser.inputPath();
69  QStringList outputPaths = parser.outputPaths();
70 
71  if (outputPaths.isEmpty())
72  {
73  prepareGuiStartup(inputPath);
74  return Status::OK;
75  }
76 
77  // Can't construct the editor directly, need to make a main window instead because... reasons
78  mainWindow.reset(new MainWindow2);
79  CommandLineExporter exporter(mainWindow->mEditor);
80  if (exporter.process(inputPath,
81  outputPaths,
82  parser.camera(),
83  parser.width(),
84  parser.height(),
85  parser.startFrame(),
86  parser.endFrame(),
87  parser.transparency()))
88  {
89  return Status::SAFE;
90  }
91  return Status::FAIL;
92 }
93 
94 bool Pencil2D::event(QEvent* event)
95 {
96  if (event->type() == QEvent::FileOpen)
97  {
98  auto fileOpenEvent = dynamic_cast<QFileOpenEvent*>(event);
99  Q_ASSERT(fileOpenEvent);
100  emit openFileRequested(fileOpenEvent->file());
101  return true;
102  }
103  return QApplication::event(event);
104 }
105 
107 {
108  QSettings setting(PENCIL2D, PENCIL2D);
109  QString userLocale = setting.value(SETTING_LANGUAGE).toString();
110  QLocale locale = userLocale.isEmpty() ? QLocale::system() : QLocale(userLocale);
111  QLocale::setDefault(locale);
112 
113  std::unique_ptr<QTranslator> qtTranslator(new QTranslator(this));
114  if (qtTranslator->load(locale, "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
115  {
116  installTranslator(qtTranslator.release());
117  }
118 
119  std::unique_ptr<QTranslator> pencil2DTranslator(new QTranslator(this));
120  if (pencil2DTranslator->load(locale, "pencil", "_", ":/i18n/"))
121  {
122  installTranslator(pencil2DTranslator.release());
123  }
124 }
125 
126 void Pencil2D::prepareGuiStartup(const QString& inputPath)
127 {
128  PlatformHandler::configurePlatformSpecificSettings();
129 
130  mainWindow.reset(new MainWindow2);
131  connect(this, &Pencil2D::openFileRequested, mainWindow.get(), &MainWindow2::openFile);
132  mainWindow->show();
133 
134  if (!inputPath.isEmpty())
135  {
136  mainWindow->openFile(inputPath);
137  }
138 }
Status handleCommandLineOptions()
Parses supplied command line arguments and performs the appropriate actions, such as running the comm...
Definition: pencil2d.cpp:63
QEvent::Type type() const const
Pencil2D(int &argc, char **argv)
Initializes the application with the given command line arguments.
Definition: pencil2d.cpp:35
void openFileRequested(QString filename)
Emitted when the operating system requests that a file should be opened.
Handles command line export jobs.
void setOrganizationDomain(const QString &orgDomain)
QLocale system()
void setDesktopFileName(const QString &name)
void setApplicationVersion(const QString &version)
void setDefault(const QLocale &locale)
bool isEmpty() const const
bool isEmpty() const const
void prepareGuiStartup(const QString &inputPath)
Readies the graphical UI for entering the main loop.
Definition: pencil2d.cpp:126
bool installTranslator(QTranslator *translationFile)
QVariant value(const QString &key, const QVariant &defaultValue) const const
void installTranslators()
Sets up translators for the application locale configured by the user or the system locale...
Definition: pencil2d.cpp:106
virtual bool event(QEvent *e) override
void setWindowIcon(const QIcon &icon)
bool process(const QString &inputPath, const QStringList &outputPaths, const QString &camera, int width, int height, int startFrame, int endFrame, bool transparency)
Exports a Pencil2D file according to the specified options.
QStringList arguments()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString location(QLibraryInfo::LibraryLocation loc)
void setOrganizationName(const QString &orgName)
QString toString() const const
void setApplicationDisplayName(const QString &name)
void setApplicationName(const QString &application)