-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.cpp
More file actions
275 lines (227 loc) · 9.27 KB
/
main.cpp
File metadata and controls
275 lines (227 loc) · 9.27 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**************************************************************************
**
** Copyright (C) 2013 by Philip Schuchardt
** www.cavewhere.com
**
**************************************************************************/
//Qt includes
#include <QGuiApplication>
#include <QApplication>
#include <QFont>
#include <QQmlContext>
#include <QMessageBox>
#include <QQmlApplicationEngine>
#include <QThreadPool>
#include <QQuickWindow>
#include <QCommandLineParser>
#include <QFileInfo>
#include <QtQml/qqml.h>
#include <qpa/qplatformwindow.h>
#include <QQuickStyle>
#include <QStyleFactory>
//Our includes
//#include "cwMainWindow.h"
#include "cwImage.h"
#include "cwRootData.h"
#include "cwProject.h"
#include "cwQmlImageProviderBinder.h"
#include "cwOpenFileEventHandler.h"
#include "cwDeepLinkHandler.h"
#include "cwApplication.h"
#include "cwGlobals.h"
#include "cwMetaTypeSystem.h"
#include "cwTask.h"
#include "cwSettings.h"
#include "cwFontSettings.h"
//QuickQanave includes
#include <QuickQanava>
//std includes
#include <memory>
//QQuickGit includes
#include "GitConcurrent.h"
#include "GitCommitImageProvider.h"
#include "GitRepository.h"
//MarkScope
#include "MarkScope/FrameProfiler.h"
#ifndef CAVEWHERE_VERSION
#define CAVEWHERE_VERSION "Sauce-Release"
#endif
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
switch (type) {
case QtWarningMsg:
qDebug() << "QWarning triggered: " << msg;
if (msg.contains("No module named \"cavewherelib\" found")) {
// Breakpoint here for the debugger
qDebug() << "Breaking on QWarning...";
#ifdef Q_OS_WIN
__debugbreak(); // Windows
#else
__builtin_trap(); // macOS/Linux
#endif
}
break;
case QtCriticalMsg:
case QtFatalMsg:
// Handle other types of messages if necessary
break;
default:
break;
}
}
void handleCommandline(QCoreApplication& a, cwRootData* rootData) {
// Command-line argument parser
QCommandLineParser parser;
parser.setApplicationDescription("CaveWhere Application");
parser.addHelpOption();
// Adding --page option
QCommandLineOption pageOption(QStringList({"p", "page"}),
"Specify the page URL to open.",
"pageurl");
parser.addOption(pageOption);
// Adding optional filename argument
parser.addPositionalArgument("filename", "The optional file to open.");
// Parse the command-line arguments
parser.process(a);
// Check if --page was provided
QString pageUrl;
if (parser.isSet(pageOption)) {
pageUrl = parser.value(pageOption);
}
const QStringList positionalArgs = parser.positionalArguments();
if (!positionalArgs.isEmpty()) {
QString filename = positionalArgs.first();
const bool isDeepLink = filename.startsWith(QLatin1String("cavewhere://"));
if (isDeepLink) {
rootData->deepLinkHandler()->handleUrl(QUrl(filename));
} else {
rootData->project()->loadOrConvert(filename);
}
if(!pageUrl.isEmpty() && !isDeepLink) {
QObject* obj = new QObject();
struct ShouldLoad {
bool isPageViewLoaded = false;
bool isFileLoaded = false;
};
auto shouldLoad = std::make_shared<ShouldLoad>();
auto loadCommandLinePage = [obj, rootData, pageUrl, shouldLoad]() {
if(shouldLoad->isFileLoaded) {
//This is pretty unrealiable, it depends on the loading spead
QTimer::singleShot(250, [rootData, obj, pageUrl]() {
rootData->pageSelectionModel()->setCurrentPageAddress(pageUrl);
//This delete disconnects the connection
obj->deleteLater();
});
}
};
obj->connect(rootData->project(), &cwProject::loaded, obj, [shouldLoad, loadCommandLinePage]() {
shouldLoad->isFileLoaded = true;
loadCommandLinePage();
});
}
}
}
int main(int argc, char *argv[])
{
#ifdef Q_OS_LINUX
if (qEnvironmentVariableIsEmpty("QT_QPA_PLATFORMTHEME")) {
qputenv("QT_QPA_PLATFORMTHEME", "xdgdesktopportal");
}
#endif
// Fusion is the only built-in style that supports dark palettes on Windows and Linux.
// The native Windows Vista style always ignores the dark palette (Qt 6.5 blog).
// macOS native style handles dark mode correctly on its own.
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
QApplication::setStyle(QStyleFactory::create("Fusion")); // Qt Widgets
QQuickStyle::setStyle("Fusion"); // Qt Quick Controls
#endif
//This needs to be first for QSettings
QApplication::setOrganizationName("Vadose Solutions");
QApplication::setOrganizationDomain("cavewhere.com");
QApplication::setApplicationName("CaveWhere");
QApplication::setApplicationVersion(CAVEWHERE_VERSION);
cwApplication a(argc, argv);
//Register meta system
cwMetaTypeSystem::registerTypes();
//Load all the fonts
cwGlobals::loadFonts();
// Default QFont() resolves to .AppleSystemUIFont on macOS, which SVG
// viewers cannot render. Align C++ rendering with the user's configured
// font family (QML UI already follows Theme.fontFamily).
cwSettings::initialize();
auto syncAppFont = []() {
const QString configured = cwFontSettings::instance()->fontFamily();
const QString family = configured.isEmpty()
? cwFontSettings::fontEntries().first().family
: configured;
QFont f = QApplication::font();
f.setFamily(family);
QApplication::setFont(f);
};
syncAppFont();
QObject::connect(cwFontSettings::instance(), &cwFontSettings::fontFamilyChanged,
qApp, syncAppFont);
//Clear the settings for testing
QSettings settings;
// settings.clear();
// Configure multisample antialiasing
QSurfaceFormat format;
format.setSamples(4); // Adjust the sample count as needed
QSurfaceFormat::setDefaultFormat(format);
QQmlApplicationEngine* applicationEngine = new QQmlApplicationEngine();
//initilize cavewher lib, gitlib2
cwRootData::initCavewherelib();
//Use a single shared thread pool to avoid over-subscribing CPU cores
QQuickGit::GitConcurrent::setThreadPool(cwTask::threadPool());
// Add the macOS Resources directory to the QML import search path
QString resourcePath = QCoreApplication::applicationDirPath() + "/../Resources/qml";
applicationEngine->addImportPath(resourcePath);
applicationEngine->addImportPath(":/"); //This enable QuickQanava to load in qml correctly
//Initilize QuickQanava
QuickQanava::initialize(applicationEngine);
QQmlContext* context = applicationEngine->rootContext();
applicationEngine->loadFromModule(QStringLiteral("cavewherelib"),
QStringLiteral("CavewhereMainWindow"));
auto id = qmlTypeId("cavewherelib", 1, 0, "RootData");
cwRootData* rootData = applicationEngine->rootContext()->engine()->singletonInstance<cwRootData*>(id);
qDebug() << "CaveWhere built for" << (rootData->desktopBuild() ? "desktop" : "mobile");
MarkScope::FrameProfiler frameProfiler(applicationEngine);
//Handle command line args
handleCommandline(a, rootData);
//QPlatformWindow is needed because QQuickWindow has no public windowModified API
if (!applicationEngine->rootObjects().isEmpty()) {
auto* mainWindow = qobject_cast<QQuickWindow*>(applicationEngine->rootObjects().first());
if (mainWindow) {
auto updateModified = [mainWindow, rootData]() {
if (auto* platformWindow = mainWindow->handle()) {
platformWindow->setWindowModified(rootData->project()->modified());
}
};
QObject::connect(rootData->project(), &cwProject::modifiedChanged,
mainWindow, updateModified);
}
}
//Handles when the user clicks on a file in Finder(Mac OS X) or Explorer (Windows)
cwOpenFileEventHandler* openFileHandler = new cwOpenFileEventHandler(&a);
openFileHandler->setProject(rootData->project());
QObject::connect(openFileHandler, &cwOpenFileEventHandler::deepLinkReceived,
rootData->deepLinkHandler(), &cwDeepLinkHandler::handleUrl);
a.installEventFilter(openFileHandler);
//Creates image providers for the qml engine
new cwQmlImageProviderBinder(context->engine(), rootData, applicationEngine);
//Enables image://gitcommit/ URLs in QML for viewing images at any commit
QQuickGit::GitCommitImageProvider::registerOn(context->engine());
bool quitCalled = false;
auto quit = [&a, &quitCalled, applicationEngine]() {
if (quitCalled) { return; }
quitCalled = true;
delete applicationEngine;
QThreadPool::globalInstance()->waitForDone();
cwTask::threadPool()->waitForDone();
a.quit();
};
//Allow the engine to quit the application
QObject::connect(context->engine(), &QQmlEngine::quit, &a, quit, Qt::QueuedConnection);
int result = a.exec();
QQuickGit::GitRepository::shutdownGitEngine();
return result;
}