diff --git a/app/backend/computermanager.h b/app/backend/computermanager.h index ae3bf3b3..517dff3a 100644 --- a/app/backend/computermanager.h +++ b/app/backend/computermanager.h @@ -242,11 +242,11 @@ class ComputerManager : public QObject public: explicit ComputerManager(QObject *parent = nullptr); - void startPolling(); + Q_INVOKABLE void startPolling(); - void stopPollingAsync(); + Q_INVOKABLE void stopPollingAsync(); - bool addNewHost(QString address, bool mdns); + Q_INVOKABLE bool addNewHost(QString address, bool mdns); QVector getComputers(); diff --git a/app/gui/AppView.qml b/app/gui/AppView.qml index 2ab7292c..9abd7b53 100644 --- a/app/gui/AppView.qml +++ b/app/gui/AppView.qml @@ -3,6 +3,8 @@ import QtQuick.Controls 2.2 import AppModel 1.0 +import ComputerManager 1.0 + GridView { property int computerIndex @@ -16,8 +18,8 @@ GridView { function createModel() { - var model = Qt.createQmlObject('import AppModel 1.0; AppModel {}', parent, "") - model.initialize(computerIndex) + var model = Qt.createQmlObject('import AppModel 1.0; AppModel {}', parent, '') + model.initialize(ComputerManager, computerIndex) return model } diff --git a/app/gui/PcView.qml b/app/gui/PcView.qml index d95e8d84..7efb221e 100644 --- a/app/gui/PcView.qml +++ b/app/gui/PcView.qml @@ -5,6 +5,8 @@ import QtQuick.Layouts 1.11 import ComputerModel 1.0 +import ComputerManager 1.0 + GridView { anchors.fill: parent anchors.leftMargin: 5 @@ -14,7 +16,24 @@ GridView { cellWidth: 350; cellHeight: 350; focus: true - model: ComputerModel {} + Component.onCompleted: { + // Start polling when this view is shown + ComputerManager.startPolling() + } + + Component.onDestruction: { + // Stop polling when this view is destroyed + ComputerManager.stopPollingAsync() + } + + function createModel() + { + var model = Qt.createQmlObject('import ComputerModel 1.0; ComputerModel {}', parent, '') + model.initialize(ComputerManager) + return model + } + + model: createModel() delegate: Item { width: 300; height: 300; diff --git a/app/gui/appmodel.cpp b/app/gui/appmodel.cpp index 3f8f647f..5bf5d88a 100644 --- a/app/gui/appmodel.cpp +++ b/app/gui/appmodel.cpp @@ -3,20 +3,20 @@ AppModel::AppModel(QObject *parent) : QAbstractListModel(parent) { - connect(&m_ComputerManager, &ComputerManager::computerStateChanged, - this, &AppModel::handleComputerStateChanged); connect(&m_BoxArtManager, &BoxArtManager::boxArtLoadComplete, this, &AppModel::handleBoxArtLoaded); } -void AppModel::initialize(int computerIndex) +void AppModel::initialize(ComputerManager* computerManager, int computerIndex) { - Q_ASSERT(computerIndex < m_ComputerManager.getComputers().count()); - m_Computer = m_ComputerManager.getComputers().at(computerIndex); + m_ComputerManager = computerManager; + connect(m_ComputerManager, &ComputerManager::computerStateChanged, + this, &AppModel::handleComputerStateChanged); + + Q_ASSERT(computerIndex < m_ComputerManager->getComputers().count()); + m_Computer = m_ComputerManager->getComputers().at(computerIndex); m_Apps = m_Computer->appList; m_CurrentGameId = m_Computer->currentGameId; - - m_ComputerManager.startPolling(); } int AppModel::rowCount(const QModelIndex &parent) const diff --git a/app/gui/appmodel.h b/app/gui/appmodel.h index 0d2b053a..a74de1b0 100644 --- a/app/gui/appmodel.h +++ b/app/gui/appmodel.h @@ -20,7 +20,7 @@ public: explicit AppModel(QObject *parent = nullptr); // Must be called before any QAbstractListModel functions - Q_INVOKABLE void initialize(int computerIndex); + Q_INVOKABLE void initialize(ComputerManager* computerManager, int computerIndex); QVariant data(const QModelIndex &index, int role) const override; @@ -36,7 +36,7 @@ private slots: private: NvComputer* m_Computer; BoxArtManager m_BoxArtManager; - ComputerManager m_ComputerManager; + ComputerManager* m_ComputerManager; QVector m_Apps; int m_CurrentGameId; }; diff --git a/app/gui/computermodel.cpp b/app/gui/computermodel.cpp index 2d5ea826..975ecf40 100644 --- a/app/gui/computermodel.cpp +++ b/app/gui/computermodel.cpp @@ -1,13 +1,16 @@ #include "computermodel.h" +#include "backend/nvpairingmanager.h" ComputerModel::ComputerModel(QObject* object) - : QAbstractListModel(object) + : QAbstractListModel(object) {} + +void ComputerModel::initialize(ComputerManager* computerManager) { - connect(&m_ComputerManager, &ComputerManager::computerStateChanged, + m_ComputerManager = computerManager; + connect(m_ComputerManager, &ComputerManager::computerStateChanged, this, &ComputerModel::handleComputerStateChanged); - m_Computers = m_ComputerManager.getComputers(); - m_ComputerManager.startPolling(); + m_Computers = m_ComputerManager->getComputers(); } QVariant ComputerModel::data(const QModelIndex& index, int role) const @@ -74,6 +77,21 @@ QHash ComputerModel::roleNames() const return names; } +void ComputerModel::deleteComputer(int computerIndex) +{ + Q_ASSERT(computerIndex < m_Computers.count()); + + beginRemoveRows(QModelIndex(), computerIndex, computerIndex); + + // m_Computer[computerIndex] will be deleted by this call + m_ComputerManager->deleteHost(m_Computers[computerIndex]); + + // Remove the now invalid item + m_Computers.removeAt(computerIndex); + + endRemoveRows(); +} + void ComputerModel::handleComputerStateChanged(NvComputer* computer) { // If this is an existing computer, we can report the data changed @@ -90,6 +108,6 @@ void ComputerModel::handleComputerStateChanged(NvComputer* computer) } // Our view of the world must be in sync with ComputerManager's - Q_ASSERT(m_Computers.count() == m_ComputerManager.getComputers().count()); + Q_ASSERT(m_Computers.count() == m_ComputerManager->getComputers().count()); } diff --git a/app/gui/computermodel.h b/app/gui/computermodel.h index 340da6df..a152a6b7 100644 --- a/app/gui/computermodel.h +++ b/app/gui/computermodel.h @@ -18,16 +18,21 @@ class ComputerModel : public QAbstractListModel public: explicit ComputerModel(QObject* object = nullptr); + // Must be called before any QAbstractListModel functions + Q_INVOKABLE void initialize(ComputerManager* computerManager); + QVariant data(const QModelIndex &index, int role) const override; int rowCount(const QModelIndex &parent) const override; virtual QHash roleNames() const override; + Q_INVOKABLE void deleteComputer(int computerIndex); + private slots: void handleComputerStateChanged(NvComputer* computer); private: QVector m_Computers; - ComputerManager m_ComputerManager; + ComputerManager* m_ComputerManager; }; diff --git a/app/gui/main.qml b/app/gui/main.qml index 1ec9da5e..d02127e9 100644 --- a/app/gui/main.qml +++ b/app/gui/main.qml @@ -4,9 +4,8 @@ import QtQuick.Controls 2.2 ApplicationWindow { id: window visible: true - width: 640 - height: 480 - title: qsTr("Stack") + width: 1280 + height: 600 color: "#333333" StackView { diff --git a/app/main.cpp b/app/main.cpp index b2fd4fe1..eeb4ecf3 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -30,6 +30,11 @@ int main(int argc, char *argv[]) // Register our C++ types for QML qmlRegisterType("ComputerModel", 1, 0, "ComputerModel"); qmlRegisterType("AppModel", 1, 0, "AppModel"); + qmlRegisterSingletonType("ComputerManager", 1, 0, + "ComputerManager", + [](QQmlEngine*, QJSEngine*) -> QObject* { + return new ComputerManager(); + }); // Load the main.qml file QQmlApplicationEngine engine;