File indexing completed on 2024-12-01 06:50:24
0001 // SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu> 0002 // 0003 // SPDX-License-Identifier: GPL-3.0-or-later 0004 0005 #include "arkade.h" 0006 0007 #include <QQmlEngine> 0008 #include <QQmlComponent> 0009 #include <QQmlContext> 0010 #include <QQuickItem> 0011 #include <KLocalizedContext> 0012 #include <KPackage/PackageLoader> 0013 0014 Arkade::Arkade(QQmlEngine *engine) 0015 : QObject(engine) 0016 { 0017 m_gamesModel = new GamesModel(this); 0018 m_contextObj = new KLocalizedContext(this); 0019 } 0020 0021 void Arkade::setGamesModel(GamesModel *gamesModel) 0022 { 0023 if (m_gamesModel == gamesModel) { 0024 return; 0025 } 0026 m_gamesModel = gamesModel; 0027 Q_EMIT gamesModelChanged(); 0028 } 0029 0030 GamesModel *Arkade::gamesModel() const 0031 { 0032 return m_gamesModel; 0033 } 0034 0035 QString Arkade::gameId() const 0036 { 0037 return m_gameId; 0038 } 0039 0040 void Arkade::setGameId(const QString &gameId) 0041 { 0042 if (m_gameId == gameId) { 0043 return; 0044 } 0045 0046 m_gameId = gameId; 0047 m_gamePackage = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Arkade/Game"), gameId); 0048 if (!m_gamePackage.isValid()) { 0049 Q_EMIT gameIdChanged(); 0050 return; 0051 } 0052 } 0053 0054 QString Arkade::qmlPath() 0055 { 0056 if (m_gamePackage.isValid()) { 0057 return m_gamePackage.filePath("ui", QStringLiteral("main.qml")); 0058 } else { 0059 return QString(); 0060 } 0061 } 0062 0063 QQuickItem *Arkade::gameItem() 0064 { 0065 return createGui(m_gamePackage.filePath("ui", QStringLiteral("main.qml"))); 0066 } 0067 0068 QQuickItem *Arkade::createGui(const QString &qmlPath) 0069 { 0070 QQmlEngine *engine = qmlEngine(this); 0071 QQmlComponent *component = new QQmlComponent(engine, QUrl(qmlPath), nullptr); 0072 if (component->status() != QQmlComponent::Ready) { 0073 qCritical() << "Error creating component:"; 0074 for (auto err : component->errors()) { 0075 qWarning() << err.toString(); 0076 } 0077 component->deleteLater(); 0078 return nullptr; 0079 } 0080 0081 QObject *guiObject = component->create(); 0082 QQuickItem *gui = qobject_cast<QQuickItem *>(guiObject); 0083 if (!gui) { 0084 qWarning() << "ERROR: QML gui" << guiObject << "not a QQuickItem instance"; 0085 guiObject->deleteLater(); 0086 return nullptr; 0087 } 0088 gui->setParent(this); 0089 return gui; 0090 } 0091 0092 #include "moc_arkade.cpp"