Keep a singleton ComputerManager outside of the Models

This commit is contained in:
Cameron Gutman
2018-07-05 22:08:55 -07:00
parent 0d26ef7e5c
commit 1b1ad86271
9 changed files with 72 additions and 24 deletions
+3 -3
View File
@@ -242,11 +242,11 @@ class ComputerManager : public QObject
public: public:
explicit ComputerManager(QObject *parent = nullptr); 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<NvComputer*> getComputers(); QVector<NvComputer*> getComputers();
+4 -2
View File
@@ -3,6 +3,8 @@ import QtQuick.Controls 2.2
import AppModel 1.0 import AppModel 1.0
import ComputerManager 1.0
GridView { GridView {
property int computerIndex property int computerIndex
@@ -16,8 +18,8 @@ GridView {
function createModel() function createModel()
{ {
var model = Qt.createQmlObject('import AppModel 1.0; AppModel {}', parent, "") var model = Qt.createQmlObject('import AppModel 1.0; AppModel {}', parent, '')
model.initialize(computerIndex) model.initialize(ComputerManager, computerIndex)
return model return model
} }
+20 -1
View File
@@ -5,6 +5,8 @@ import QtQuick.Layouts 1.11
import ComputerModel 1.0 import ComputerModel 1.0
import ComputerManager 1.0
GridView { GridView {
anchors.fill: parent anchors.fill: parent
anchors.leftMargin: 5 anchors.leftMargin: 5
@@ -14,7 +16,24 @@ GridView {
cellWidth: 350; cellHeight: 350; cellWidth: 350; cellHeight: 350;
focus: true 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 { delegate: Item {
width: 300; height: 300; width: 300; height: 300;
+7 -7
View File
@@ -3,20 +3,20 @@
AppModel::AppModel(QObject *parent) AppModel::AppModel(QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
{ {
connect(&m_ComputerManager, &ComputerManager::computerStateChanged,
this, &AppModel::handleComputerStateChanged);
connect(&m_BoxArtManager, &BoxArtManager::boxArtLoadComplete, connect(&m_BoxArtManager, &BoxArtManager::boxArtLoadComplete,
this, &AppModel::handleBoxArtLoaded); this, &AppModel::handleBoxArtLoaded);
} }
void AppModel::initialize(int computerIndex) void AppModel::initialize(ComputerManager* computerManager, int computerIndex)
{ {
Q_ASSERT(computerIndex < m_ComputerManager.getComputers().count()); m_ComputerManager = computerManager;
m_Computer = m_ComputerManager.getComputers().at(computerIndex); 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_Apps = m_Computer->appList;
m_CurrentGameId = m_Computer->currentGameId; m_CurrentGameId = m_Computer->currentGameId;
m_ComputerManager.startPolling();
} }
int AppModel::rowCount(const QModelIndex &parent) const int AppModel::rowCount(const QModelIndex &parent) const
+2 -2
View File
@@ -20,7 +20,7 @@ public:
explicit AppModel(QObject *parent = nullptr); explicit AppModel(QObject *parent = nullptr);
// Must be called before any QAbstractListModel functions // 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; QVariant data(const QModelIndex &index, int role) const override;
@@ -36,7 +36,7 @@ private slots:
private: private:
NvComputer* m_Computer; NvComputer* m_Computer;
BoxArtManager m_BoxArtManager; BoxArtManager m_BoxArtManager;
ComputerManager m_ComputerManager; ComputerManager* m_ComputerManager;
QVector<NvApp> m_Apps; QVector<NvApp> m_Apps;
int m_CurrentGameId; int m_CurrentGameId;
}; };
+23 -5
View File
@@ -1,13 +1,16 @@
#include "computermodel.h" #include "computermodel.h"
#include "backend/nvpairingmanager.h"
ComputerModel::ComputerModel(QObject* object) 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); this, &ComputerModel::handleComputerStateChanged);
m_Computers = m_ComputerManager.getComputers(); m_Computers = m_ComputerManager->getComputers();
m_ComputerManager.startPolling();
} }
QVariant ComputerModel::data(const QModelIndex& index, int role) const QVariant ComputerModel::data(const QModelIndex& index, int role) const
@@ -74,6 +77,21 @@ QHash<int, QByteArray> ComputerModel::roleNames() const
return names; 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) void ComputerModel::handleComputerStateChanged(NvComputer* computer)
{ {
// If this is an existing computer, we can report the data changed // 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 // 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());
} }
+6 -1
View File
@@ -18,16 +18,21 @@ class ComputerModel : public QAbstractListModel
public: public:
explicit ComputerModel(QObject* object = nullptr); 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; QVariant data(const QModelIndex &index, int role) const override;
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
virtual QHash<int, QByteArray> roleNames() const override; virtual QHash<int, QByteArray> roleNames() const override;
Q_INVOKABLE void deleteComputer(int computerIndex);
private slots: private slots:
void handleComputerStateChanged(NvComputer* computer); void handleComputerStateChanged(NvComputer* computer);
private: private:
QVector<NvComputer*> m_Computers; QVector<NvComputer*> m_Computers;
ComputerManager m_ComputerManager; ComputerManager* m_ComputerManager;
}; };
+2 -3
View File
@@ -4,9 +4,8 @@ import QtQuick.Controls 2.2
ApplicationWindow { ApplicationWindow {
id: window id: window
visible: true visible: true
width: 640 width: 1280
height: 480 height: 600
title: qsTr("Stack")
color: "#333333" color: "#333333"
StackView { StackView {
+5
View File
@@ -30,6 +30,11 @@ int main(int argc, char *argv[])
// Register our C++ types for QML // Register our C++ types for QML
qmlRegisterType<ComputerModel>("ComputerModel", 1, 0, "ComputerModel"); qmlRegisterType<ComputerModel>("ComputerModel", 1, 0, "ComputerModel");
qmlRegisterType<AppModel>("AppModel", 1, 0, "AppModel"); qmlRegisterType<AppModel>("AppModel", 1, 0, "AppModel");
qmlRegisterSingletonType<ComputerManager>("ComputerManager", 1, 0,
"ComputerManager",
[](QQmlEngine*, QJSEngine*) -> QObject* {
return new ComputerManager();
});
// Load the main.qml file // Load the main.qml file
QQmlApplicationEngine engine; QQmlApplicationEngine engine;