From e627f8ebcabdd6f5f35cf3fcd02e91b2750d4109 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Feb 2023 23:48:28 +0100 Subject: [PATCH] Add iconName property to model --- CMakeLists.txt | 1 + DeviceTypesSettingsPage.qml | 36 +++++++++++++++--------------------- IconComboBox.qml | 32 ++++++++++++++++++++++++++++++++ devicetypesmodel.cpp | 18 +++++++++++++++--- dmxcontroller.cpp | 7 ++++++- lightproject.h | 1 + 6 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 IconComboBox.qml diff --git a/CMakeLists.txt b/CMakeLists.txt index 02a8e93..533285e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ qt_add_qml_module(applightcontrol StatusBar.qml RegisterGroupsSettingsPage.qml RegistersSettingsItem.qml + IconComboBox.qml ) set_target_properties(applightcontrol PROPERTIES diff --git a/DeviceTypesSettingsPage.qml b/DeviceTypesSettingsPage.qml index 6b9f87f..879fef4 100644 --- a/DeviceTypesSettingsPage.qml +++ b/DeviceTypesSettingsPage.qml @@ -77,38 +77,32 @@ ColumnLayout { enabled: false Layout.fillWidth: true value: listView.currentData ? listView.currentData.id : -1 - onValueModified: listView.currentData.id = value + onValueModified: if (listView.currentData) listView.currentData.id = value; else console.warn('discarded'); } Label { text: qsTr("Name:") } TextField { Layout.fillWidth: true text: listView.currentData ? listView.currentData.name : '' - onTextEdited: listView.currentData.name = text + onTextEdited: if (listView.currentData) listView.currentData.name = text; else console.warn('discarded'); } Label { text: qsTr("Icon:") } - ComboBox { + IconComboBox { + id: iconComboBox + Layout.fillWidth: true Layout.preferredHeight: 64 - id: iconCombobox - textRole: "fileName" - valueRole: "fileUrl" - delegate: ItemDelegate { - height: 64 - anchors.left: parent.left - anchors.right: parent.right - contentItem: IconChooserDelegateLayout { - anchors.top: parent.top - anchors.bottom: parent.bottom - text: fileName - iconSource: fileUrl - } - } - contentItem: IconChooserDelegateLayout { - text: iconCombobox.currentText - iconSource: iconCombobox.currentValue - } + + textRole: "fileBaseName" + valueRole: "fileBaseName" + iconSourceRole: "fileUrl" model: iconsModel + + currentIndex: listView.currentData ? iconComboBox.indexOfValue(listView.currentData.iconName) : -1 + onActivated: { + console.log(currentValue); + if (listView.currentData) listView.currentData.iconName = currentValue; else console.warn('discarded'); + } } Label { text: qsTr("Registers:") } RegistersSettingsItem { diff --git a/IconComboBox.qml b/IconComboBox.qml new file mode 100644 index 0000000..ff3a647 --- /dev/null +++ b/IconComboBox.qml @@ -0,0 +1,32 @@ +import QtQuick +import QtQuick.Controls +import QtQuick.Controls.Material + +ComboBox { + id: comboBox + + property string iconSourceRole + + delegate: ItemDelegate { + height: 64 + anchors.left: parent.left + anchors.right: parent.right + contentItem: IconChooserDelegateLayout { + anchors.top: parent.top + anchors.bottom: parent.bottom + text: model[comboBox.textRole] + iconSource: model[comboBox.iconSourceRole] + } + } + contentItem: IconChooserDelegateLayout { + text: comboBox.currentText + iconSource: { + if (!comboBox.model) + return ''; + const url = comboBox.model.get(comboBox.currentIndex, "fileUrl"); + if (!url) + return ''; + return url; + } + } +} diff --git a/devicetypesmodel.cpp b/devicetypesmodel.cpp index 6a75fc2..79159ae 100644 --- a/devicetypesmodel.cpp +++ b/devicetypesmodel.cpp @@ -6,7 +6,7 @@ enum { IdRole = Qt::UserRole, - IconRole + IconNameRole }; void DeviceTypesModel::setController(DmxController *controller) @@ -69,6 +69,7 @@ QVariant DeviceTypesModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: case Qt::EditRole: return deviceType.name; case IdRole: return deviceType.id; + case IconNameRole: return deviceType.iconName; } return {}; @@ -106,7 +107,8 @@ QMap DeviceTypesModel::itemData(const QModelIndex &index) const return { { Qt::DisplayRole, deviceType.name }, - { IdRole, deviceType.id } + { IdRole, deviceType.id }, + { IconNameRole, deviceType.iconName } }; } @@ -114,7 +116,8 @@ QHash DeviceTypesModel::roleNames() const { return { { Qt::DisplayRole, "name" }, - { IdRole, "id" } + { IdRole, "id" }, + { IconNameRole, "iconName" } }; } @@ -167,6 +170,15 @@ bool DeviceTypesModel::setData(const QModelIndex &index, const QVariant &value, deviceType.id = value.toInt(); emit dataChanged(index, index, { IdRole }); return true; + case IconNameRole: + if (value.userType() != QMetaType::QString) + { + qWarning() << "hilfe" << __LINE__ << value.userType(); + return false; + } + deviceType.iconName = value.toString(); + emit dataChanged(index, index, { IconNameRole }); + return true; } return false; diff --git a/dmxcontroller.cpp b/dmxcontroller.cpp index 00cbafe..0850806 100644 --- a/dmxcontroller.cpp +++ b/dmxcontroller.cpp @@ -14,11 +14,13 @@ DmxController::DmxController(QObject *parent) : .deviceTypes { { .id=0, - .name="Stairville MH-X50+" + .name="Stairville MH-X50+", + .iconName="movinghead" }, { .id=1, .name="RGBW Strahler Klein", + .iconName="rgbstrahler", .registers { DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Dimmer }, DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Red }, @@ -32,6 +34,7 @@ DmxController::DmxController(QObject *parent) : { .id=2, .name="RGB Strahler", + .iconName="rgbstrahler", .registers { DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Red }, DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Green }, @@ -41,6 +44,7 @@ DmxController::DmxController(QObject *parent) : { .id=3, .name="Nebelmaschine", + .iconName="nebelmaschine", .registers { DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Dimmer } } @@ -48,6 +52,7 @@ DmxController::DmxController(QObject *parent) : { .id=4, .name="RGBW Strahler Groß", + .iconName="rgbstrahler", .registers { DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Dimmer }, DeviceTypeRegisterConfig { .type = DeviceTypeRegisterType::Red }, diff --git a/lightproject.h b/lightproject.h index 486abf7..5157cfc 100644 --- a/lightproject.h +++ b/lightproject.h @@ -37,6 +37,7 @@ struct DeviceTypeConfig { int id; QString name; + QString iconName; std::vector registers; };