From 84669babf2baaa8061471841ddf17836370aa3b5 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 19 Feb 2023 03:48:22 +0100 Subject: [PATCH] More model improvements (synchronization in DeviceTypesModel) --- DeviceTypesSettingsPage.qml | 5 ++- DevicesSettingsPage.qml | 6 ++- LightControlWindow.qml | 5 --- devicetypesmodel.cpp | 77 +++++++++++++++++++++++++++++++++++++ devicetypesmodel.h | 5 +++ dmxcontroller.h | 5 +++ 6 files changed, 96 insertions(+), 7 deletions(-) diff --git a/DeviceTypesSettingsPage.qml b/DeviceTypesSettingsPage.qml index 16d8783..0672376 100644 --- a/DeviceTypesSettingsPage.qml +++ b/DeviceTypesSettingsPage.qml @@ -27,7 +27,10 @@ ColumnLayout { Layout.maximumWidth: 300 Layout.fillHeight: true - model: deviceTypesModel + model: DeviceTypesModel { + id: deviceTypesModel + controller: __controller + } onAddClicked: (index) => { const newIndex = index < 0 ? 0 : index + 1; if (deviceTypesModel.insertRow(newIndex)) currentIndex = newIndex; else console.warn('failed'); } onRemoveClicked: (index) => { diff --git a/DevicesSettingsPage.qml b/DevicesSettingsPage.qml index 8c8d693..8c9011d 100644 --- a/DevicesSettingsPage.qml +++ b/DevicesSettingsPage.qml @@ -3,6 +3,8 @@ import QtQuick.Controls import QtQuick.Controls.Material import QtQuick.Layouts +import com.büro + ColumnLayout { Label { text: qsTr("Devices Settings") @@ -83,7 +85,9 @@ ColumnLayout { ComboBox { id: deviceTypeCombobox Layout.fillWidth: true - model: deviceTypesModel + model: DeviceTypesModel { + controller: __controller + } textRole: "name" valueRole: "id" currentIndex: listView.currentData ? deviceTypeCombobox.indexOfValue(listView.currentData.deviceTypeId) : -1 diff --git a/LightControlWindow.qml b/LightControlWindow.qml index e042171..8814bfd 100644 --- a/LightControlWindow.qml +++ b/LightControlWindow.qml @@ -22,11 +22,6 @@ ApplicationWindow { property int masterWhite property int masterStrobo - DeviceTypesModel { - id: deviceTypesModel - controller: __controller - } - DevicesModel { id: devicesModel controller: __controller diff --git a/devicetypesmodel.cpp b/devicetypesmodel.cpp index 79159ae..199be5a 100644 --- a/devicetypesmodel.cpp +++ b/devicetypesmodel.cpp @@ -15,7 +15,29 @@ void DeviceTypesModel::setController(DmxController *controller) return; beginResetModel(); + + if (m_controller) + { + disconnect(m_controller, &DmxController::deviceTypeInserted, + this, &DeviceTypesModel::otherDeviceTypeInserted); + disconnect(m_controller, &DmxController::deviceTypeRemoved, + this, &DeviceTypesModel::otherDeviceTypeRemoved); + disconnect(m_controller, &DmxController::deviceTypeNameChanged, + this, &DeviceTypesModel::otherDeviceTypeNameChanged); + } + m_controller = controller; + + if (m_controller) + { + connect(m_controller, &DmxController::deviceTypeInserted, + this, &DeviceTypesModel::otherDeviceTypeInserted); + connect(m_controller, &DmxController::deviceTypeRemoved, + this, &DeviceTypesModel::otherDeviceTypeRemoved); + connect(m_controller, &DmxController::deviceTypeNameChanged, + this, &DeviceTypesModel::otherDeviceTypeNameChanged); + } + endResetModel(); emit controllerChanged(m_controller); } @@ -160,6 +182,13 @@ bool DeviceTypesModel::setData(const QModelIndex &index, const QVariant &value, } deviceType.name = value.toString(); emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); + + disconnect(m_controller, &DmxController::deviceTypeNameChanged, + this, &DeviceTypesModel::otherDeviceTypeNameChanged); + emit m_controller->deviceTypeNameChanged(index.row(), deviceType.name); + connect(m_controller, &DmxController::deviceTypeNameChanged, + this, &DeviceTypesModel::otherDeviceTypeNameChanged); + return true; case IdRole: if (value.userType() != QMetaType::Int) @@ -221,6 +250,12 @@ bool DeviceTypesModel::insertRows(int row, int count, const QModelIndex &parent) iter = deviceTypes.insert(iter, DeviceTypeConfig{ .id=id++, .name="" }) + 1; endInsertRows(); + disconnect(m_controller, &DmxController::deviceTypeInserted, + this, &DeviceTypesModel::otherDeviceTypeInserted); + emit m_controller->deviceTypeInserted(row, row+count-1); + connect(m_controller, &DmxController::deviceTypeInserted, + this, &DeviceTypesModel::otherDeviceTypeInserted); + return true; } @@ -264,9 +299,51 @@ bool DeviceTypesModel::removeRows(int row, int count, const QModelIndex &parent) deviceTypes.erase(begin, end); endRemoveRows(); + disconnect(m_controller, &DmxController::deviceTypeRemoved, + this, &DeviceTypesModel::otherDeviceTypeRemoved); + emit m_controller->deviceTypeRemoved(row, row+count-1); + connect(m_controller, &DmxController::deviceTypeRemoved, + this, &DeviceTypesModel::otherDeviceTypeRemoved); + return true; } +void DeviceTypesModel::otherDeviceTypeInserted(int first, int last) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + beginInsertRows({}, first, last); + endInsertRows(); +} + +void DeviceTypesModel::otherDeviceTypeRemoved(int first, int last) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + beginRemoveRows({}, first, last); + endRemoveRows(); +} + +void DeviceTypesModel::otherDeviceTypeNameChanged(int row, const QString &name) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + const auto index = this->index(row); + emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); +} + namespace { void registerDenShit() { diff --git a/devicetypesmodel.h b/devicetypesmodel.h index b2c43fe..789e7ba 100644 --- a/devicetypesmodel.h +++ b/devicetypesmodel.h @@ -28,6 +28,11 @@ public: signals: void controllerChanged(DmxController *controller); +private slots: + void otherDeviceTypeInserted(int first, int last); + void otherDeviceTypeRemoved(int first, int last); + void otherDeviceTypeNameChanged(int row, const QString &name); + private: DmxController *m_controller{}; }; diff --git a/dmxcontroller.h b/dmxcontroller.h index c76f4b9..7f3233a 100644 --- a/dmxcontroller.h +++ b/dmxcontroller.h @@ -30,6 +30,11 @@ public: signals: void performanceChanged(int performance); + void deviceTypeInserted(int first, int last); + void deviceTypeRemoved(int first, int last); + void deviceTypeNameChanged(int index, const QString &name); + void deviceTypeIconNameChanged(int index, const QString &iconName); + void deviceTypeRegisterInserted(const DeviceTypeConfig &deviceType, int first, int last); void deviceTypeRegisterRemoved(const DeviceTypeConfig &deviceType, int first, int last); void deviceTypeRegisterTypeChanged(const DeviceTypeConfig &deviceType, int index, DeviceTypeRegisterType type);