mirror of
https://github.com/moonlight-stream/moonlight-qt
synced 2024-11-10 13:44:17 +00:00
Keep a singleton ComputerManager outside of the Models
This commit is contained in:
parent
0d26ef7e5c
commit
1b1ad86271
9 changed files with 72 additions and 24 deletions
|
@ -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<NvComputer*> getComputers();
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<NvApp> m_Apps;
|
||||
int m_CurrentGameId;
|
||||
};
|
||||
|
|
|
@ -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<int, QByteArray> 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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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<int, QByteArray> roleNames() const override;
|
||||
|
||||
Q_INVOKABLE void deleteComputer(int computerIndex);
|
||||
|
||||
private slots:
|
||||
void handleComputerStateChanged(NvComputer* computer);
|
||||
|
||||
private:
|
||||
QVector<NvComputer*> m_Computers;
|
||||
ComputerManager m_ComputerManager;
|
||||
ComputerManager* m_ComputerManager;
|
||||
};
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -30,6 +30,11 @@ int main(int argc, char *argv[])
|
|||
// Register our C++ types for QML
|
||||
qmlRegisterType<ComputerModel>("ComputerModel", 1, 0, "ComputerModel");
|
||||
qmlRegisterType<AppModel>("AppModel", 1, 0, "AppModel");
|
||||
qmlRegisterSingletonType<ComputerManager>("ComputerManager", 1, 0,
|
||||
"ComputerManager",
|
||||
[](QQmlEngine*, QJSEngine*) -> QObject* {
|
||||
return new ComputerManager();
|
||||
});
|
||||
|
||||
// Load the main.qml file
|
||||
QQmlApplicationEngine engine;
|
||||
|
|
Loading…
Reference in a new issue