1
0
Fork 0

Launcher: fix compatability with older Qt

QQmlEngine::singletonInstance is >= 5.12. Work-around using a little
component+instance to achieve the same result.
This commit is contained in:
James Turner 2021-04-02 10:13:48 +01:00
parent 2913b72f43
commit 5dae2d129e
2 changed files with 39 additions and 11 deletions

View file

@ -913,14 +913,14 @@ void LauncherController::setMinWindowSize(QSize sz)
QUrl LauncherController::flyIconUrl() const QUrl LauncherController::flyIconUrl() const
{ {
if (m_aircraftType == Helicopter) { if (m_aircraftType == Helicopter) {
return QUrl{"qrc:///svg/toolbox-fly-heli"}; return QUrl{"image://colored-icon/toolbox-fly-heli"};
} else if (m_selectedAircraftInfo) { } else if (m_selectedAircraftInfo) {
if (m_selectedAircraftInfo->hasTag("spaceship")) { 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 QString LauncherController::flyButtonLabel() const

View file

@ -29,19 +29,47 @@ QmlColoredImageProvider::QmlColoredImageProvider() : QQuickImageProvider(QQmlIma
void QmlColoredImageProvider::loadStyleColors(QQmlEngine* engine, int styleTypeId) void QmlColoredImageProvider::loadStyleColors(QQmlEngine* engine, int styleTypeId)
{ {
QJSValue styleObject = engine->singletonInstance<QJSValue>(styleTypeId); #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
if (styleObject.isNull() || !styleObject.isQObject()) { QJSValue styleValue = engine->singletonInstance<QJSValue>(styleTypeId);
if (styleValue.isNull() || !styleValue.isQObject()) {
qWarning() << "Unable to load Style object"; qWarning() << "Unable to load Style object";
return; return;
} }
_themeColor = QColor{styleObject.property("themeColor").toString()}; QObject* styleObject = styleValue.toQObject();
_textColor = QColor{styleObject.property("baseTextColor").toString()}; #else
_themeContrastColor = QColor{styleObject.property("themeContrastTextColor").toString()}; // ugly version for Qt < 5.12 : parse and instantiate a dummy object to let
_activeColor = QColor{styleObject.property("activeColor").toString()}; // us access the Style singleton
_destructiveColor = QColor{styleObject.property("destructiveActionColor").toString()}; 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<QObject*>();
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) QImage QmlColoredImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize)
{ {