diff --git a/DeviceTypesSettingsPage.qml b/DeviceTypesSettingsPage.qml index 45bec39..7dec727 100644 --- a/DeviceTypesSettingsPage.qml +++ b/DeviceTypesSettingsPage.qml @@ -20,11 +20,13 @@ ColumnLayout { model: deviceTypesModel - onAddClicked: (index) => console.log('added', index); + onAddClicked: (index) => deviceTypesModel.insertRow(index < 0 ? 0 : index + 1); onRemoveClicked: (index) => deviceTypesModel.removeRow(index) } ColumnLayout { + enabled: listView.currentIndex !== -1 + GridLayout { Layout.preferredWidth: 300 Layout.maximumWidth: 300 diff --git a/DevicesSettingsPage.qml b/DevicesSettingsPage.qml index 86acb88..f61d519 100644 --- a/DevicesSettingsPage.qml +++ b/DevicesSettingsPage.qml @@ -20,11 +20,13 @@ ColumnLayout { model: devicesModel - onAddClicked: (index) => console.log('added', index) + onAddClicked: (index) => devicesModel.insertRow(index < 0 ? 0 : index + 1); onRemoveClicked: (index) => devicesModel.removeRow(index) } ColumnLayout { + enabled: listView.currentIndex !== -1 + GridLayout { Layout.preferredWidth: 300 Layout.maximumWidth: 300 diff --git a/devicesmodel.cpp b/devicesmodel.cpp index 072a6be..5b89204 100644 --- a/devicesmodel.cpp +++ b/devicesmodel.cpp @@ -234,6 +234,41 @@ bool DevicesModel::setData(const QModelIndex &index, const QVariant &value, int return false; } +bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent) +{ + if (parent.isValid()) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + auto &lights = m_controller->lightProject().lights; + + if (row < 0) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + if (row > lights.size()) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + beginInsertRows({}, row, row+count-1); + lights.insert(std::begin(lights) + row, count, LightConfig{ .id=99, .name="", .lightTypeId=0, .address=0, .position={} }); + endInsertRows(); + + return true; +} + bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent) { if (parent.isValid()) @@ -250,6 +285,12 @@ bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent) auto &lights = m_controller->lightProject().lights; + if (row < 0) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + if (row >= lights.size()) { qWarning() << "hilfe" << __LINE__; diff --git a/devicesmodel.h b/devicesmodel.h index ffca2ff..353b94d 100644 --- a/devicesmodel.h +++ b/devicesmodel.h @@ -25,6 +25,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/devicetypesmodel.cpp b/devicetypesmodel.cpp index 0f77687..551192c 100644 --- a/devicetypesmodel.cpp +++ b/devicetypesmodel.cpp @@ -195,6 +195,41 @@ bool DeviceTypesModel::setData(const QModelIndex &index, const QVariant &value, return false; } +bool DeviceTypesModel::insertRows(int row, int count, const QModelIndex &parent) +{ + if (parent.isValid()) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + if (!m_controller) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + auto &lightTypes = m_controller->lightProject().lightTypes; + + if (row < 0) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + if (row > lightTypes.size()) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + + beginInsertRows({}, row, row+count-1); + lightTypes.insert(std::begin(lightTypes) + row, count, LightTypeConfig{ .id=99, .name="" }); + endInsertRows(); + + return true; +} + bool DeviceTypesModel::removeRows(int row, int count, const QModelIndex &parent) { if (parent.isValid()) @@ -211,6 +246,12 @@ bool DeviceTypesModel::removeRows(int row, int count, const QModelIndex &parent) auto &lightTypes = m_controller->lightProject().lightTypes; + if (row < 0) + { + qWarning() << "hilfe" << __LINE__; + return false; + } + if (row >= lightTypes.size()) { qWarning() << "hilfe" << __LINE__; diff --git a/devicetypesmodel.h b/devicetypesmodel.h index a933464..798968f 100644 --- a/devicetypesmodel.h +++ b/devicetypesmodel.h @@ -25,6 +25,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: