Debian Codegraph @

src/GUI/LauncherMainWindow.cxx

raw symbols SCIP
1 #include "config.h"
2
3 #include "LauncherMainWindow.hxx"
4
5 // Qt headers
6 #include <QMessageBox>
7 #include <QSettings>
8 #include <QDebug>
9 #include <QMenu>
10 #include <QMenuBar>
11
12 #include <QOpenGLContext>
13 #include <QQmlComponent>
14 #include <QQmlContext>
15 #include <QQmlEngine>
16 #include <QQmlError>
17 #include <QQmlFileSelector>
18 #include <QQuickItem>
19
20 // launcher headers
21 #include "AddOnsController.hxx"
22 #include "AircraftItemModel.hxx"
23 #include "DefaultAircraftLocator.hxx"
24 #include "GettingStartedTip.hxx"
25 #include "LaunchConfig.hxx"
26 #include "LauncherController.hxx"
27 #include "LauncherNotificationsController.hxx"
28 #include "LauncherPackageDelegate.hxx"
29 #include "LocalAircraftCache.hxx"
30 #include "LocationController.hxx"
31 #include "QmlColoredImageProvider.hxx"
32 #include "QtLauncher.hxx"
33 #include "UpdateChecker.hxx"
34
35 #include <Main/sentryIntegration.hxx>
36
37 //////////////////////////////////////////////////////////////////////////////
38
39 LauncherMainWindow::LauncherMainWindow(bool inSimMode) : QQuickView()
40 {
41 setTitle("FlightGear " FLIGHTGEAR_VERSION);
42
43 m_controller = new LauncherController(this, this);
44
45 int styleTypeId = 0;
46 m_controller->initQML(styleTypeId);
47
48 // use a direct connection to be notified synchronously when the render thread
49 // starts OpenGL. We use this to log the OpenGL information from the
50 // context at that time, for tracing purposes.
51 connect(this, &QQuickWindow::sceneGraphInitialized,
52 this, &LauncherMainWindow::renderTheadSceneGraphInitialized,
53 Qt::DirectConnection);
54
55 m_coloredIconProvider = new QmlColoredImageProvider;
56 engine()->addImageProvider("colored-icon", m_coloredIconProvider);
57
58 if (!inSimMode) {
59 #if defined(Q_OS_MAC)
60 QMenuBar* mb = new QMenuBar();
61
62 QMenu* fileMenu = mb->addMenu(tr("File"));
63 QAction* openAction = new QAction(tr("Open saved configuration..."));
64 openAction->setMenuRole(QAction::NoRole);
65 connect(openAction, &QAction::triggered,
66 m_controller, &LauncherController::openConfig);
67
68 QAction* saveAction = new QAction(tr("Save configuration as..."));
69 saveAction->setMenuRole(QAction::NoRole);
70 connect(saveAction, &QAction::triggered,
71 m_controller, &LauncherController::saveConfigAs);
72
73 fileMenu->addAction(openAction);
74 fileMenu->addAction(saveAction);
75
76 QMenu* toolsMenu = mb->addMenu(tr("Tools"));
77 QAction* restoreDefaultsAction = new QAction(tr("Restore defaults..."));
78 restoreDefaultsAction->setMenuRole(QAction::NoRole);
79 connect(restoreDefaultsAction, &QAction::triggered,
80 m_controller, &LauncherController::requestRestoreDefaults);
81
82 QAction* changeDataAction = new QAction(tr("Select data files location..."));
83 changeDataAction->setMenuRole(QAction::NoRole);
84 connect(changeDataAction, &QAction::triggered,
85 m_controller, &LauncherController::requestChangeDataPath);
86
87 QAction* viewCommandLineAction = new QAction(tr("View command-line"));
88 connect(viewCommandLineAction, &QAction::triggered,
89 m_controller, &LauncherController::viewCommandLine);
90
91 toolsMenu->addAction(restoreDefaultsAction);
92 toolsMenu->addAction(changeDataAction);
93 toolsMenu->addAction(viewCommandLineAction);
94 #endif
95
96 QAction* qa = new QAction(this);
97 qa->setMenuRole(QAction::QuitRole); // will be addeed accordingly
98 qa->setShortcut(QKeySequence("Ctrl+Q"));
99 connect(qa, &QAction::triggered, m_controller, &LauncherController::quit);
100 }
101
102 if (!checkQQC2Availability()) {
103 QMessageBox::critical(nullptr, "Missing required component",
104 tr("Your system is missing a required UI component (QtQuick Controls 2). "
105 "This normally occurs on Linux platforms where Qt is split into many small packages. "
106 "On Ubuntu/Debian systems, the package is called 'qml-module-qtquick-controls2'"));
107 }
108
109 m_coloredIconProvider->loadStyleColors(engine(), styleTypeId);
110
111 connect(this, &QQuickView::statusChanged, this, &LauncherMainWindow::onQuickStatusChanged);
112
113 m_controller->initialRestoreSettings();
114
115 ////////////
116 #if defined(Q_OS_WIN)
117 const QString osName("win");
118 #elif defined(Q_OS_MAC)
119 const QString osName("mac");
120 #else
121 const QString osName("unix");
122 #endif
123
124 setResizeMode(QQuickView::SizeRootObjectToView);
125 engine()->addImportPath("qrc:///");
126
127 // allow selecting different QML files based on the Qt version we are
128 // compiled against
129 auto selector = new QQmlFileSelector(engine(), this);
130 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
131 selector->setExtraSelectors({"qt6"});
132 #endif
133
134
135 QQmlContext* ctx = rootContext();
136 ctx->setContextProperty("_launcher", m_controller);
137 ctx->setContextProperty("_config", m_controller->config());
138 ctx->setContextProperty("_location", m_controller->location());
139 ctx->setContextProperty("_osName", osName);
140
141 auto updater = new UpdateChecker(this);
142 ctx->setContextProperty("_updates", updater);
143
144 auto packageDelegate = new LauncherPackageDelegate(this);
145 ctx->setContextProperty("_packages", packageDelegate);
146
147 auto notifications = new LauncherNotificationsController{this, engine()};
148 ctx->setContextProperty("_notifications", notifications);
149
150 if (!inSimMode) {
151 auto addOnsCtl = new AddOnsController(this, m_controller->config());
152 ctx->setContextProperty("_addOns", addOnsCtl);
153 }
154
155 auto weatherScenariosModel = new flightgear::WeatherScenariosModel(this);
156 ctx->setContextProperty("_weatherScenarios", weatherScenariosModel);
157
158 setSource(QUrl("qrc:///qml/Launcher.qml"));
159 }
160
161 void LauncherMainWindow::onQuickStatusChanged(QQuickView::Status status)
162 {
163 if (status == QQuickView::Error) {
164 QString errorString;
165
166 Q_FOREACH (auto err, errors()) {
167 errorString.append("\n" + err.toString());
168 }
169
170 QMessageBox::critical(nullptr, "UI loading failures.",
171 tr("Problems occurred loading the user interface. This is usually due to missing modules on your system. "
172 "Please report this error to the FlightGear developer list or forum, and take care to mention your system "
173 "distribution, etc. Please also include the information provided below.\n") +
174 errorString);
175 }
176 }
177
178 bool LauncherMainWindow::checkQQC2Availability()
179 {
180 QQmlComponent comp(engine());
181 comp.setData(R"(
182 import QtQuick.Controls 2.0
183 ScrollBar {
184 }
185 )",
186 {});
187 if (comp.isError()) {
188 return false;
189 }
190
191
192 auto item = comp.create();
193 const bool haveQQC2 = (item != nullptr);
194 if (item)
195 item->deleteLater();
196 return haveQQC2;
197 }
198
199 LauncherMainWindow::~LauncherMainWindow()
200 {
201 }
202
203 bool LauncherMainWindow::event(QEvent *event)
204 {
205 if (event->type() == QEvent::Close) {
206 m_controller->saveSettings();
207 }
208 return QQuickView::event(event);
209 }
210
211 bool LauncherMainWindow::execInApp()
212 {
213 m_controller->setInAppMode();
214
215 show();
216
217 while (m_controller->keepRunningInAppMode()) {
218 qApp->processEvents();
219 }
220
221 return m_controller->inAppResult();
222 }
223
224 // this slot runs in the context of the render thread. Don't modify
225 void LauncherMainWindow::renderTheadSceneGraphInitialized()
226 {
227 auto qContext = QOpenGLContext::currentContext();
228 if (!qContext) {
229 // can happen with alternative QML backends
230 qDebug() << Q_FUNC_INFO << "No current OpenGL context: Metal/D3D QML backend probably active.";
231 return;
232 }
233
234 std::string renderer = (char*)glGetString(GL_RENDERER);
235 // capture this to help with debugging this crash:
236 // https://sentry.io/share/issue/f98e38dceb4241dbaeed944d6ce4d746/
237 // https://bugreports.qt.io/browse/QTBUG-69703
238 flightgear::addSentryTag("qt-gl-vendor", (char*)glGetString(GL_VENDOR));
239 flightgear::addSentryTag("qt-gl-renderer", renderer.c_str());
240 flightgear::addSentryTag("qt-gl-version", (char*)glGetString(GL_VERSION));
241 flightgear::addSentryTag("qt-glsl-version", (char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
242
243 const char* gltype[] = {"Desktop", "GLES 2", "GLES 1"};
244 flightgear::addSentryTag("qt-gl-module-type", gltype[QOpenGLContext::openGLModuleType()]);
245
246 // if necessary, borrow more code from:
247 // https://code.qt.io/cgit/qt/qtbase.git/tree/examples/opengl/contextinfo/widget.cpp?h=5.15#n358
248 // to expand what this reports
249 }