From 5dae2d129e60b15b79d99c3d7a5956c52d712d7b Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 2 Apr 2021 10:13:48 +0100 Subject: [PATCH] Launcher: fix compatability with older Qt QQmlEngine::singletonInstance is >= 5.12. Work-around using a little component+instance to achieve the same result. --- src/GUI/LauncherController.cxx | 6 ++-- src/GUI/QmlColoredImageProvider.cxx | 44 +++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/GUI/LauncherController.cxx b/src/GUI/LauncherController.cxx index 0081d5aec..aea6779d0 100644 --- a/src/GUI/LauncherController.cxx +++ b/src/GUI/LauncherController.cxx @@ -913,14 +913,14 @@ void LauncherController::setMinWindowSize(QSize sz) QUrl LauncherController::flyIconUrl() const { if (m_aircraftType == Helicopter) { - return QUrl{"qrc:///svg/toolbox-fly-heli"}; + return QUrl{"image://colored-icon/toolbox-fly-heli"}; } else if (m_selectedAircraftInfo) { if (m_selectedAircraftInfo->hasTag("spaceship")) { - return QUrl{"qrc:///svg/toolbox-fly-alt"}; + return QUrl{"image://colored-icon/toolbox-fly-alt"}; } } - return QUrl{"qrc:///svg/toolbox-fly"}; + return QUrl{"image://colored-icon/toolbox-fly"}; } QString LauncherController::flyButtonLabel() const diff --git a/src/GUI/QmlColoredImageProvider.cxx b/src/GUI/QmlColoredImageProvider.cxx index 48dfcfb5a..6b8ff7dc9 100644 --- a/src/GUI/QmlColoredImageProvider.cxx +++ b/src/GUI/QmlColoredImageProvider.cxx @@ -29,19 +29,47 @@ QmlColoredImageProvider::QmlColoredImageProvider() : QQuickImageProvider(QQmlIma void QmlColoredImageProvider::loadStyleColors(QQmlEngine* engine, int styleTypeId) { - QJSValue styleObject = engine->singletonInstance(styleTypeId); - if (styleObject.isNull() || !styleObject.isQObject()) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) + QJSValue styleValue = engine->singletonInstance(styleTypeId); + if (styleValue.isNull() || !styleValue.isQObject()) { qWarning() << "Unable to load Style object"; return; } - _themeColor = QColor{styleObject.property("themeColor").toString()}; - _textColor = QColor{styleObject.property("baseTextColor").toString()}; - _themeContrastColor = QColor{styleObject.property("themeContrastTextColor").toString()}; - _activeColor = QColor{styleObject.property("activeColor").toString()}; - _destructiveColor = QColor{styleObject.property("destructiveActionColor").toString()}; -} + QObject* styleObject = styleValue.toQObject(); +#else + // ugly version for Qt < 5.12 : parse and instantiate a dummy object to let + // us access the Style singleton + QQmlComponent comp(engine); + comp.setData(R"( + import QtQuick 2.0 + import FlightGear 1.0 + QtObject { + readonly property var styleObject: Style + } + )", + {}); + if (comp.isError()) { + qWarning() << Q_FUNC_INFO << "Failed to create style accessor component" << comp.errors(); + return; + } + auto item = comp.create(); + if (!item) { + qWarning() << Q_FUNC_INFO << "Failed to create component instance"; + return; + } + + QObject* styleObject = item->property("styleObject").value(); + item->deleteLater(); +#endif + + _themeColor = QColor{styleObject->property("themeColor").toString()}; + _textColor = QColor{styleObject->property("baseTextColor").toString()}; + _themeContrastColor = QColor{styleObject->property("themeContrastTextColor").toString()}; + _activeColor = QColor{styleObject->property("activeColor").toString()}; + _destructiveColor = QColor{styleObject->property("destructiveActionColor").toString()}; +} QImage QmlColoredImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize) {