-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathapplication.cpp
More file actions
208 lines (174 loc) · 7.25 KB
/
application.cpp
File metadata and controls
208 lines (174 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
Copyright 2023, Mitch Curtis
This file is part of Slate.
Slate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Slate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Slate. If not, see <http://www.gnu.org/licenses/>.
*/
#include "application.h"
#include <QApplication>
#include <QFontDatabase>
#include <QLibraryInfo>
#include <QLoggingCategory>
#include <QQmlFileSelector>
#include <QTranslator>
#include <QUndoStack>
#include "canvaspane.h"
#include "canvaspaneitem.h"
#include "imagecanvas.h"
#include "imagelayer.h"
#include "imageproject.h"
#include "layeredimagecanvas.h"
#include "layeredimageproject.h"
#include "project.h"
#include "projectimageprovider.h"
#include "projectmanager.h"
#include "spriteimageprovider.h"
#include "tilecanvas.h"
#include "tilecanvaspaneitem.h"
#include "tileset.h"
#include "tilesetproject.h"
Q_LOGGING_CATEGORY(lcApplication, "app.application")
static QGuiApplication *createApplication(int &argc, char **argv, const QString &applicationName)
{
// TODO: move the test rules to test code if possible - e.g. testhelper.cpp
QLoggingCategory::setFilterRules("app.* = false\ntests.* = false\nui.* = false");
// High-DPI scaling is always enabled in Qt 6.
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QApplication::setOrganizationName("Mitch Curtis");
QApplication::setApplicationName(applicationName);
QApplication::setOrganizationDomain("mitchcurtis");
QApplication::setApplicationDisplayName("Slate - Pixel Art Editor");
QApplication::setApplicationVersion(APP_VERSION);
QApplication *app = new QApplication(argc, argv);
return app;
}
Application::Application(int &argc, char **argv, const QString &applicationName) :
mApplication(createApplication(argc, argv, applicationName)),
mSettings(new ApplicationSettings),
mEngine(new QQmlApplicationEngine)
{
qCDebug(lcApplication) << "constructing Application...";
registerQmlTypes();
addFonts();
installTranslators();
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
QQmlFileSelector fileSelector(mEngine.data());
fileSelector.setExtraSelectors(QStringList() << QLatin1String("nativemenubar"));
#endif
mProjectManager.setApplicationSettings(mSettings.data());
mEngine->addImageProvider("sprite", new SpriteImageProvider);
mEngine->addImageProvider("project", new ProjectImageProvider(&mProjectManager));
mEngine->rootContext()->setContextProperty("settings", mSettings.data());
mEngine->rootContext()->setContextProperty("qtVersion", QT_VERSION_STR);
qCDebug(lcApplication) << "Loading main.qml...";
mEngine->setInitialProperties({
{ "projectManager", QVariant::fromValue(&mProjectManager) }
});
mEngine->load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
if (!mEngine->rootObjects().isEmpty())
qCDebug(lcApplication) << "... loaded main.qml";
else
qCDebug(lcApplication) << "... failed to load main.qml";
qCDebug(lcApplication) << "... constructed Application";
}
Application::~Application()
{
// Context properties should outlive QML.
// Usually this wouldn't be a problem with a traditional main(),
// but since we're trying to encapsulate as much as possible into Application
// to be able to reuse it in tests, we run into the problem where
// the project manager (a context property) is destroyed before the engine.
// Give the engine a little helping hand and ensure that it's
// destroyed before the project manager, otherwise we get binding errors.
mEngine.reset();
}
int Application::run()
{
return mApplication->exec();
}
ApplicationSettings *Application::settings() const
{
return mSettings.data();
}
QQmlApplicationEngine *Application::qmlEngine() const
{
return mEngine.data();
}
ProjectManager *Application::projectManager()
{
return &mProjectManager;
}
void Application::registerQmlTypes()
{
qRegisterMetaType<ApplicationSettings*>();
qRegisterMetaType<ImageLayer*>();
qRegisterMetaType<Project::Type>();
// For some reason, only when debugging, I get
// QMetaProperty::read: Unable to handle unregistered datatype 'QUndoStack*' for property 'Project_QML_108::undoStack'
// if I don't do this.
qRegisterMetaType<QUndoStack*>();
qRegisterMetaType<Tile*>();
qRegisterMetaType<Tileset*>();
qRegisterMetaType<QVector<QColor>>();
}
void Application::addFonts()
{
// It's especially important to ensure that all fonts we use
// are available, otherwise Qt will have to search for "font family aliases",
// which can take 1 second (observable with qt.qpa.fonts.warning = true).
const QVector<QString> fontsToLoad = {
QStringLiteral(":/fonts/FontAwesome.otf"),
QStringLiteral(":/fonts/Roboto/Roboto-Bold.ttf"),
QStringLiteral(":/fonts/Roboto/Roboto-Regular.ttf")
};
for (const QString &fontPath : fontsToLoad) {
if (QFontDatabase::addApplicationFont(fontPath) == -1) {
qWarning() << "Failed to load font:" << fontPath;
}
}
}
void Application::installTranslators()
{
// Install translators for the current language.
const QLocale locale(mSettings->language());
QDir slateTranslationsDir = QDir::current();
#if defined(Q_OS_WIN32)
slateTranslationsDir.cd(QStringLiteral("translations"));
#elif defined(Q_OS_MAC)
slateTranslationsDir.cdUp();
slateTranslationsDir.cd(QStringLiteral("Translations"));
#else
slateTranslationsDir.cd(QStringLiteral("translations"));
#endif
const QStringList tsFiles = QString::fromLatin1(TS_FILES).split(' ');
qCDebug(lcApplication) << "looking for translation for"
<< locale.name() << "locale in" << slateTranslationsDir.absolutePath()
<< "with the following known translations:" << tsFiles;
QTranslator *slateTranslator = new QTranslator(mApplication.data());
if (slateTranslator->load(locale, QStringLiteral("slate_"), QString(), slateTranslationsDir.absolutePath())) {
mApplication->installTranslator(slateTranslator);
} else {
const auto tsFileIt = std::find_if(tsFiles.constBegin(), tsFiles.constEnd(), [locale](const QString &tsFile) {
return tsFile.contains(locale.name());
});
if (tsFileIt != tsFiles.constEnd()) {
// We have a translation for this locale, so it should have loaded.
qWarning() << "Failed to load slate_* translation for locale"
<< locale.name() << "from" << slateTranslationsDir.absolutePath();
}
}
const QString qtTranslationsDir = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
std::unique_ptr<QTranslator> qtTranslator(new QTranslator(mApplication.data()));
if (qtTranslator->load(locale, QStringLiteral("qt_"), QString(), qtTranslationsDir))
mApplication->installTranslator(qtTranslator.get());
}