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  // Associate the application with our desktop entry
51  setDesktopFileName("org.pencil2d.Pencil2D.desktop");
52 
54 }
55 
57 {
58  CommandLineParser parser;
59  parser.process(arguments());
60 
61  QString inputPath = parser.inputPath();
62  QStringList outputPaths = parser.outputPaths();
63 
64  if (outputPaths.isEmpty())
65  {
66  prepareGuiStartup(inputPath);
67  return Status::OK;
68  }
69 
70  auto mainWindow = new MainWindow2;
71  CommandLineExporter exporter(mainWindow);
72  if (exporter.process(inputPath,
73  outputPaths,
74  parser.camera(),
75  parser.width(),
76  parser.height(),
77  parser.startFrame(),
78  parser.endFrame(),
79  parser.transparency()))
80  {
81  return Status::SAFE;
82  }
83  return Status::FAIL;
84 }
85 
86 bool Pencil2D::event(QEvent* event)
87 {
88  if (event->type() == QEvent::FileOpen)
89  {
90  auto fileOpenEvent = dynamic_cast<QFileOpenEvent*>(event);
91  Q_ASSERT(fileOpenEvent);
92  emit openFileRequested(fileOpenEvent->file());
93  return true;
94  }
95  return QApplication::event(event);
96 }
97 
99 {
100  QSettings setting(PENCIL2D, PENCIL2D);
101  QString userLocale = setting.value(SETTING_LANGUAGE).toString();
102  QLocale locale = userLocale.isEmpty() ? QLocale::system() : QLocale(userLocale);
103  QLocale::setDefault(locale);
104 
105  std::unique_ptr<QTranslator> qtTranslator(new QTranslator(this));
106  if (qtTranslator->load(locale, "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
107  {
108  installTranslator(qtTranslator.release());
109  }
110 
111  std::unique_ptr<QTranslator> pencil2DTranslator(new QTranslator(this));
112  if (pencil2DTranslator->load(locale, "pencil", "_", ":/qm/"))
113  {
114  installTranslator(pencil2DTranslator.release());
115  }
116 }
117 
118 void Pencil2D::prepareGuiStartup(const QString& inputPath)
119 {
120  PlatformHandler::configurePlatformSpecificSettings();
121 
122  auto mainWindow = new MainWindow2;
123  connect(this, &Pencil2D::openFileRequested, mainWindow, &MainWindow2::openFile);
124  mainWindow->show();
125 
126  if (!inputPath.isEmpty())
127  {
128  mainWindow->openFile(inputPath);
129  }
130 }
Status handleCommandLineOptions()
Parses supplied command line arguments and performs the appropriate actions, such as running the comm...
Definition: pencil2d.cpp:56
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:118
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:98
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)