diff --git a/devicesmodel.cpp b/devicesmodel.cpp index ab117b6..7907e8c 100644 --- a/devicesmodel.cpp +++ b/devicesmodel.cpp @@ -19,7 +19,41 @@ void DevicesModel::setController(DmxController *controller) return; beginResetModel(); + + if (m_controller) + { + disconnect(m_controller, &DmxController::deviceInserted, + this, &DevicesModel::otherDeviceInserted); + disconnect(m_controller, &DmxController::deviceRemoved, + this, &DevicesModel::otherDeviceRemoved); + disconnect(m_controller, &DmxController::deviceNameChanged, + this, &DevicesModel::otherDeviceNameChanged); + disconnect(m_controller, &DmxController::deviceDeviceTypeIdChanged, + this, &DevicesModel::otherDeviceDeviceTypeIdChanged); + disconnect(m_controller, &DmxController::deviceAddressChanged, + this, &DevicesModel::otherDeviceAddressChanged); + disconnect(m_controller, &DmxController::devicePositionChanged, + this, &DevicesModel::otherDevicePositionChanged); + } + m_controller = controller; + + if (m_controller) + { + connect(m_controller, &DmxController::deviceInserted, + this, &DevicesModel::otherDeviceInserted); + connect(m_controller, &DmxController::deviceRemoved, + this, &DevicesModel::otherDeviceRemoved); + connect(m_controller, &DmxController::deviceNameChanged, + this, &DevicesModel::otherDeviceNameChanged); + connect(m_controller, &DmxController::deviceDeviceTypeIdChanged, + this, &DevicesModel::otherDeviceDeviceTypeIdChanged); + connect(m_controller, &DmxController::deviceAddressChanged, + this, &DevicesModel::otherDeviceAddressChanged); + connect(m_controller, &DmxController::devicePositionChanged, + this, &DevicesModel::otherDevicePositionChanged); + } + endResetModel(); emit controllerChanged(m_controller); } @@ -168,6 +202,13 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int } device.name = value.toString(); emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); + + disconnect(m_controller, &DmxController::deviceNameChanged, + this, &DevicesModel::otherDeviceNameChanged); + emit m_controller->deviceNameChanged(index.row(), device.name); + connect(m_controller, &DmxController::deviceNameChanged, + this, &DevicesModel::otherDeviceNameChanged); + return true; case IdRole: if (value.userType() != QMetaType::Int) @@ -186,6 +227,13 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int } device.deviceTypeId = value.toInt(); emit dataChanged(index, index, { DeviceTypeIdRole }); + + disconnect(m_controller, &DmxController::deviceDeviceTypeIdChanged, + this, &DevicesModel::otherDeviceDeviceTypeIdChanged); + emit m_controller->deviceDeviceTypeIdChanged(index.row(), device.deviceTypeId); + connect(m_controller, &DmxController::deviceDeviceTypeIdChanged, + this, &DevicesModel::otherDeviceDeviceTypeIdChanged); + return true; case AddressRole: if (value.userType() != QMetaType::Int) @@ -195,6 +243,13 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int } device.address = value.toInt(); emit dataChanged(index, index, { AddressRole }); + + disconnect(m_controller, &DmxController::deviceAddressChanged, + this, &DevicesModel::otherDeviceAddressChanged); + emit m_controller->deviceAddressChanged(index.row(), device.address); + connect(m_controller, &DmxController::deviceAddressChanged, + this, &DevicesModel::otherDeviceAddressChanged); + return true; case PositionRole: if (value.userType() != QMetaType::QVector3D) @@ -204,6 +259,13 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int } device.position = value.value(); emit dataChanged(index, index, { PositionRole }); + + disconnect(m_controller, &DmxController::devicePositionChanged, + this, &DevicesModel::otherDevicePositionChanged); + emit m_controller->devicePositionChanged(index.row(), device.position); + connect(m_controller, &DmxController::devicePositionChanged, + this, &DevicesModel::otherDevicePositionChanged); + return true; default: qWarning() << "hilfe" << __LINE__; @@ -244,10 +306,16 @@ bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent) beginInsertRows({}, row, row+count-1); auto iter = std::begin(devices) + row; - for (; count > 0; count--) + for (auto i = 0; i < count; i++) iter = devices.insert(iter, LightConfig{ .id=id++, .name="", .deviceTypeId=0, .address=0, .position={} }) + 1; endInsertRows(); + disconnect(m_controller, &DmxController::deviceInserted, + this, &DevicesModel::otherDeviceInserted); + emit m_controller->deviceInserted(row, row+count-1); + connect(m_controller, &DmxController::deviceInserted, + this, &DevicesModel::otherDeviceInserted); + return true; } @@ -291,9 +359,87 @@ bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent) devices.erase(begin, end); endRemoveRows(); + disconnect(m_controller, &DmxController::deviceRemoved, + this, &DevicesModel::otherDeviceRemoved); + emit m_controller->deviceRemoved(row, row+count-1); + connect(m_controller, &DmxController::deviceRemoved, + this, &DevicesModel::otherDeviceRemoved); + return true; } +void DevicesModel::otherDeviceInserted(int first, int last) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + beginInsertRows({}, first, last); + endInsertRows(); +} + +void DevicesModel::otherDeviceRemoved(int first, int last) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + beginRemoveRows({}, first, last); + endRemoveRows(); +} + +void DevicesModel::otherDeviceNameChanged(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 }); +} + +void DevicesModel::otherDeviceDeviceTypeIdChanged(int row, int deviceTypeId) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + const auto index = this->index(row); + emit dataChanged(index, index, { DeviceTypeIdRole }); +} + +void DevicesModel::otherDeviceAddressChanged(int row, int address) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + const auto index = this->index(row); + emit dataChanged(index, index, { AddressRole }); +} + +void DevicesModel::otherDevicePositionChanged(int row, const QVector3D &position) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + const auto index = this->index(row); + emit dataChanged(index, index, { PositionRole }); +} + namespace { void registrierDenShit() { diff --git a/devicesmodel.h b/devicesmodel.h index c0e2133..f3c8e10 100644 --- a/devicesmodel.h +++ b/devicesmodel.h @@ -28,6 +28,14 @@ public: signals: void controllerChanged(DmxController *controller); +private slots: + void otherDeviceInserted(int first, int last); + void otherDeviceRemoved(int first, int last); + void otherDeviceNameChanged(int row, const QString &name); + void otherDeviceDeviceTypeIdChanged(int row, int deviceTypeId); + void otherDeviceAddressChanged(int row, int address); + void otherDevicePositionChanged(int row, const QVector3D &position); + private: DmxController *m_controller{}; }; diff --git a/devicetypesmodel.cpp b/devicetypesmodel.cpp index 4ed33f0..1482bbe 100644 --- a/devicetypesmodel.cpp +++ b/devicetypesmodel.cpp @@ -24,6 +24,8 @@ void DeviceTypesModel::setController(DmxController *controller) this, &DeviceTypesModel::otherDeviceTypeRemoved); disconnect(m_controller, &DmxController::deviceTypeNameChanged, this, &DeviceTypesModel::otherDeviceTypeNameChanged); + disconnect(m_controller, &DmxController::deviceTypeIconNameChanged, + this, &DeviceTypesModel::otherDeviceTypeIconNameChanged); } m_controller = controller; @@ -36,6 +38,8 @@ void DeviceTypesModel::setController(DmxController *controller) this, &DeviceTypesModel::otherDeviceTypeRemoved); connect(m_controller, &DmxController::deviceTypeNameChanged, this, &DeviceTypesModel::otherDeviceTypeNameChanged); + connect(m_controller, &DmxController::deviceTypeIconNameChanged, + this, &DeviceTypesModel::otherDeviceTypeIconNameChanged); } endResetModel(); @@ -208,6 +212,13 @@ bool DeviceTypesModel::setData(const QModelIndex &index, const QVariant &value, } deviceType.iconName = value.toString(); emit dataChanged(index, index, { IconNameRole }); + + disconnect(m_controller, &DmxController::deviceTypeIconNameChanged, + this, &DeviceTypesModel::otherDeviceTypeIconNameChanged); + emit m_controller->deviceTypeIconNameChanged(index.row(), deviceType.iconName); + connect(m_controller, &DmxController::deviceTypeIconNameChanged, + this, &DeviceTypesModel::otherDeviceTypeIconNameChanged); + return true; default: qWarning() << "hilfe" << __LINE__; @@ -248,7 +259,7 @@ bool DeviceTypesModel::insertRows(int row, int count, const QModelIndex &parent) beginInsertRows({}, row, row+count-1); auto iter = std::begin(deviceTypes) + row; - for (; count > 0; count--) + for (auto i = 0; i < count; i++) iter = deviceTypes.insert(iter, DeviceTypeConfig{ .id=id++, .name="" }) + 1; endInsertRows(); @@ -346,6 +357,18 @@ void DeviceTypesModel::otherDeviceTypeNameChanged(int row, const QString &name) emit dataChanged(index, index, { Qt::DisplayRole, Qt::EditRole }); } +void DeviceTypesModel::otherDeviceTypeIconNameChanged(int row, const QString &name) +{ + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return; + } + + const auto index = this->index(row); + emit dataChanged(index, index, { IconNameRole }); +} + namespace { void registrierDenShit() { diff --git a/devicetypesmodel.h b/devicetypesmodel.h index 0c05e68..aacdc02 100644 --- a/devicetypesmodel.h +++ b/devicetypesmodel.h @@ -32,6 +32,7 @@ private slots: void otherDeviceTypeInserted(int first, int last); void otherDeviceTypeRemoved(int first, int last); void otherDeviceTypeNameChanged(int row, const QString &name); + void otherDeviceTypeIconNameChanged(int row, const QString &name); private: DmxController *m_controller{}; diff --git a/dmxcontroller.h b/dmxcontroller.h index 7f3233a..037b7e8 100644 --- a/dmxcontroller.h +++ b/dmxcontroller.h @@ -39,6 +39,13 @@ signals: void deviceTypeRegisterRemoved(const DeviceTypeConfig &deviceType, int first, int last); void deviceTypeRegisterTypeChanged(const DeviceTypeConfig &deviceType, int index, DeviceTypeRegisterType type); + void deviceInserted(int first, int last); + void deviceRemoved(int first, int last); + void deviceNameChanged(int index, const QString &name); + void deviceDeviceTypeIdChanged(int index, int deviceTypeId); + void deviceAddressChanged(int index, int address); + void devicePositionChanged(int index, const QVector3D &position); + protected: friend class DmxControllerThread;