From 7957e95d5735102500733a9d1181d66b01012320 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Feb 2023 22:55:40 +0100 Subject: [PATCH] Model improvements (synchronization) --- devicetyperegistersmodel.cpp | 83 ++++++++++++++++++++++++++++++++---- devicetyperegistersmodel.h | 5 +++ dmxcontroller.h | 4 ++ 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/devicetyperegistersmodel.cpp b/devicetyperegistersmodel.cpp index 7c3a072..05dd937 100644 --- a/devicetyperegistersmodel.cpp +++ b/devicetyperegistersmodel.cpp @@ -12,8 +12,31 @@ void DeviceTypeRegistersModel::setController(DmxController *controller) return; beginResetModel(); + + if (m_controller) + { + disconnect(m_controller, &DmxController::deviceTypeRegisterInserted, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterInserted); + disconnect(m_controller, &DmxController::deviceTypeRegisterRemoved, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterRemoved); + disconnect(m_controller, &DmxController::deviceTypeRegisterTypeChanged, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterTypeChanged); + } + m_controller = controller; + + if (m_controller) + { + connect(m_controller, &DmxController::deviceTypeRegisterInserted, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterInserted); + connect(m_controller, &DmxController::deviceTypeRegisterRemoved, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterRemoved); + connect(m_controller, &DmxController::deviceTypeRegisterTypeChanged, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterTypeChanged); + } + endResetModel(); + emit controllerChanged(m_controller); } @@ -172,26 +195,26 @@ bool DeviceTypeRegistersModel::setData(const QModelIndex &index, const QVariant if (!index.isValid()) { qWarning() << "hilfe" << __LINE__; - return true; + return false; } if (!m_controller) { qWarning() << "hilfe" << __LINE__; - return true; + return false; } if (m_deviceTypeId == -1) { qWarning() << "hilfe" << __LINE__; - return true; + return false; } auto deviceTypePtr = m_controller->lightProject().deviceTypes.findById(m_deviceTypeId); if (!deviceTypePtr) { qWarning() << "hilfe" << __LINE__; - return true; + return false; } auto &deviceType = *deviceTypePtr; @@ -199,20 +222,35 @@ bool DeviceTypeRegistersModel::setData(const QModelIndex &index, const QVariant if (index.row() < 0 || index.row() >= deviceType.registers.size()) { qWarning() << "hilfe" << __LINE__; - return true; + return false; } if (index.column() != 0) { qWarning() << "hilfe" << __LINE__; - return true; + return false; } auto &deviceTypeRegister = deviceType.registers.at(index.row()); - deviceTypeRegister.type = value.value(); - emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); - return true; + switch (role) + { + case Qt::EditRole: + deviceTypeRegister.type = value.value(); + emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); + + disconnect(m_controller, &DmxController::deviceTypeRegisterTypeChanged, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterTypeChanged); + emit m_controller->deviceTypeRegisterTypeChanged(deviceType, index.row(), deviceTypeRegister.type); + connect(m_controller, &DmxController::deviceTypeRegisterTypeChanged, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterTypeChanged); + + return true; + + default: + qWarning() << "hilfe" << __LINE__; + return false; + } } bool DeviceTypeRegistersModel::insertRows(int row, int count, const QModelIndex &parent) @@ -258,6 +296,12 @@ bool DeviceTypeRegistersModel::insertRows(int row, int count, const QModelIndex iter = registers.insert(iter, DeviceTypeRegisterConfig{ .type = DeviceTypeRegisterType::Dummy }) + 1; endInsertRows(); + disconnect(m_controller, &DmxController::deviceTypeRegisterInserted, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterInserted); + emit m_controller->deviceTypeRegisterInserted(deviceType, row, row+count-1); + connect(m_controller, &DmxController::deviceTypeRegisterInserted, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterInserted); + return true; } @@ -316,9 +360,30 @@ bool DeviceTypeRegistersModel::removeRows(int row, int count, const QModelIndex registers.erase(begin, end); endRemoveRows(); + disconnect(m_controller, &DmxController::deviceTypeRegisterRemoved, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterRemoved); + emit m_controller->deviceTypeRegisterRemoved(deviceType, row, row+count-1); + connect(m_controller, &DmxController::deviceTypeRegisterRemoved, + this, &DeviceTypeRegistersModel::otherDeviceTypeRegisterRemoved); + return true; } +void DeviceTypeRegistersModel::otherDeviceTypeRegisterInserted(const DeviceTypeConfig &deviceType, int first, int last) +{ + // TODO +} + +void DeviceTypeRegistersModel::otherDeviceTypeRegisterRemoved(const DeviceTypeConfig &deviceType, int first, int last) +{ + // TODO +} + +void DeviceTypeRegistersModel::otherDeviceTypeRegisterTypeChanged(const DeviceTypeConfig &deviceType, int index, DeviceTypeRegisterType type) +{ + // TODO +} + namespace { void registerDenShit() { diff --git a/devicetyperegistersmodel.h b/devicetyperegistersmodel.h index c4d9459..717f984 100644 --- a/devicetyperegistersmodel.h +++ b/devicetyperegistersmodel.h @@ -33,6 +33,11 @@ signals: void controllerChanged(DmxController *controller); void deviceTypeIdChanged(int deviceTypeId); +private slots: + void otherDeviceTypeRegisterInserted(const DeviceTypeConfig &deviceType, int first, int last); + void otherDeviceTypeRegisterRemoved(const DeviceTypeConfig &deviceType, int first, int last); + void otherDeviceTypeRegisterTypeChanged(const DeviceTypeConfig &deviceType, int index, DeviceTypeRegisterType type); + private: DmxController *m_controller{}; int m_deviceTypeId{-1}; diff --git a/dmxcontroller.h b/dmxcontroller.h index 3e9d84e..c76f4b9 100644 --- a/dmxcontroller.h +++ b/dmxcontroller.h @@ -30,6 +30,10 @@ public: signals: void performanceChanged(int performance); + 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); + protected: friend class DmxControllerThread;