From 9d3c42c40ca4287fd28d03e0ce2d6403f6c5bd77 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Feb 2023 20:02:29 +0100 Subject: [PATCH] Implemented adding of new registers to device types --- LightControlWindow.qml | 1 + RegistersSettingsItem.qml | 1 + devicetyperegistersmodel.cpp | 52 ++++++++++++++++++++++++++++++++++++ devicetyperegistersmodel.h | 1 + main.cpp | 4 --- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/LightControlWindow.qml b/LightControlWindow.qml index 02ca722..4a8a0f3 100644 --- a/LightControlWindow.qml +++ b/LightControlWindow.qml @@ -27,6 +27,7 @@ ApplicationWindow { ListModel { id: deviceTypeRegisterTypesModel + ListElement { text: qsTr('Dummy'); value: DeviceTypeRegisterType.Dummy } ListElement { text: qsTr('Dimmer'); value: DeviceTypeRegisterType.Dimmer } ListElement { text: qsTr('Red'); value: DeviceTypeRegisterType.Red } ListElement { text: qsTr('Green'); value: DeviceTypeRegisterType.Green } diff --git a/RegistersSettingsItem.qml b/RegistersSettingsItem.qml index 79038da..e7f413d 100644 --- a/RegistersSettingsItem.qml +++ b/RegistersSettingsItem.qml @@ -31,6 +31,7 @@ Pane { model: deviceTypeRegistersModel + onAddClicked: (index) => deviceTypeRegistersModel.insertRow(index < 0 ? 0 : index + 1); onRemoveClicked: (index) => deviceTypeRegistersModel.removeRow(index) } } diff --git a/devicetyperegistersmodel.cpp b/devicetyperegistersmodel.cpp index 59004a2..7c3a072 100644 --- a/devicetyperegistersmodel.cpp +++ b/devicetyperegistersmodel.cpp @@ -215,8 +215,60 @@ bool DeviceTypeRegistersModel::setData(const QModelIndex &index, const QVariant return true; } +bool DeviceTypeRegistersModel::insertRows(int row, int count, const QModelIndex &parent) +{ + if (parent.isValid()) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return true; + } + + if (m_deviceTypeId == -1) + { + qWarning() << "hilfe" << __LINE__; + return true; + } + + auto deviceTypePtr = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId); + if (!deviceTypePtr) + { + qWarning() << "hilfe" << __LINE__; + return true; + } + + auto &deviceType = *deviceTypePtr; + + if (row < 0) + { + qWarning() << "hilfe" << __LINE__; + return true; + } + + auto ®isters = deviceType.registers; + + beginInsertRows({}, row, row+count-1); + auto iter = std::begin(registers) + row; + for (; count > 0; count--) + iter = registers.insert(iter, DeviceTypeRegisterConfig{ .type = DeviceTypeRegisterType::Dummy }) + 1; + endInsertRows(); + + return true; +} + bool DeviceTypeRegistersModel::removeRows(int row, int count, const QModelIndex &parent) { + if (parent.isValid()) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + if (!m_controller) { qWarning() << "hilfe" << __LINE__; diff --git a/devicetyperegistersmodel.h b/devicetyperegistersmodel.h index 29b2eba..c4d9459 100644 --- a/devicetyperegistersmodel.h +++ b/devicetyperegistersmodel.h @@ -26,6 +26,7 @@ public: QHash roleNames() const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; + bool insertRows(int row, int count, const QModelIndex &parent) override; bool removeRows(int row, int count, const QModelIndex &parent) override; signals: diff --git a/main.cpp b/main.cpp index 4f893d6..f86c2df 100644 --- a/main.cpp +++ b/main.cpp @@ -3,8 +3,6 @@ #include #include #include -#include - #include "dmxcontroller.h" int main(int argc, char *argv[]) @@ -24,8 +22,6 @@ int main(int argc, char *argv[]) QGuiApplication app{argc, argv}; - qDebug() << QDir{":/lightcontrol"}.entryInfoList(); - QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption();