From a80e45fa15f59c3e35c05bd0c40a9af9b485b18f Mon Sep 17 00:00:00 2001 From: Ali Kianian Date: Wed, 26 Jun 2024 18:29:47 +0300 Subject: [PATCH] QmlDesigner: Enable name and id for texture ComboBoxes Change-Id: Iefe9e68bf9c54753c38c6a43f3a6d3217f2293c0 Reviewed-by: Mahmoud Badri Reviewed-by: Shrief Gabr Reviewed-by: Miikka Heikkinen --- .../HelperWidgets/ItemFilterComboBox.qml | 65 +++++++++++++++---- .../propertyeditor/itemfiltermodel.cpp | 35 ++++++++++ .../propertyeditor/itemfiltermodel.h | 6 +- 3 files changed, 92 insertions(+), 14 deletions(-) diff --git a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ItemFilterComboBox.qml b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ItemFilterComboBox.qml index 316207e5f53..bb7d1307d81 100644 --- a/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ItemFilterComboBox.qml +++ b/share/qtcreator/qmldesigner/propertyEditorQmlSources/imports/HelperWidgets/ItemFilterComboBox.qml @@ -11,27 +11,32 @@ HelperWidgets.ComboBox { manualMapping: true editable: true - model: comboBox.addDefaultItem(itemFilterModel.itemModel) + model: optionsList.model + valueRole: "id" + textRole: (comboBox.typeFilter === "QtQuick3D.Texture") ? "idAndName" : "id" validator: RegularExpressionValidator { regularExpression: /(^$|^[a-z_]\w*)/ } HelperWidgets.ItemFilterModel { id: itemFilterModel modelNodeBackendProperty: modelNodeBackend + onModelReset: optionsList.updateModel() + onRowsInserted: optionsList.updateModel() + onRowsRemoved: optionsList.updateModel() } property string defaultItem: qsTr("[None]") - property string textValue: comboBox.backendValue?.expression ?? "" + property string expressionValue: comboBox.backendValue?.expression ?? "" property bool block: false property bool dirty: true - onTextValueChanged: { + onExpressionValueChanged: { if (comboBox.block) return - comboBox.setCurrentText(comboBox.textValue) + comboBox.updateText() } - onModelChanged: comboBox.setCurrentText(comboBox.textValue) + onModelChanged: comboBox.updateText() onEditTextChanged: comboBox.dirty = true onFocusChanged: { if (comboBox.dirty) @@ -41,7 +46,18 @@ HelperWidgets.ComboBox { onCompressedActivated: function(index, reason) { comboBox.handleActivate(index) } onAccepted: comboBox.setCurrentText(comboBox.editText) - Component.onCompleted: comboBox.setCurrentText(comboBox.textValue) + Component.onCompleted: comboBox.updateText() + + function updateText() { + let idx = itemFilterModel.indexFromId(comboBox.expressionValue) + if (idx < 0) { + comboBox.setCurrentText(comboBox.defaultItem) + return + } + + let textValue = itemFilterModel.modelItemData(idx)[comboBox.textRole] + comboBox.setCurrentText(textValue) + } function handleActivate(index) { if (!comboBox.__isCompleted || comboBox.backendValue === undefined) @@ -69,18 +85,41 @@ HelperWidgets.ComboBox { comboBox.editText = comboBox.defaultItem } - if (comboBox.currentIndex === 0) { + if (comboBox.currentIndex < 1) { comboBox.backendValue.resetValue() } else { - if (comboBox.backendValue.expression !== comboBox.editText) - comboBox.backendValue.expression = comboBox.editText + let valueData = (comboBox.valueRole === "") + ? comboBox.editText + : itemFilterModel.modelItemData(comboBox.currentIndex - 1)[comboBox.valueRole] + + if (comboBox.backendValue.expression !== valueData) + comboBox.backendValue.expression = valueData } comboBox.dirty = false } - function addDefaultItem(arr) { - var copy = arr.slice() - copy.unshift(comboBox.defaultItem) - return copy + Repeater { + id: optionsList + + property var localModel: [] + + function updateModel() { + optionsList.localModel = [] + + if (comboBox.textRole !== "" && comboBox.valueRole !== "") { + let defaultItem = {} + defaultItem[comboBox.textRole] = comboBox.defaultItem + defaultItem[comboBox.valueRole] = "" + optionsList.localModel.push(defaultItem) + } else { + optionsList.localModel.push(comboBox.defaultItem) + } + + let rows = itemFilterModel.rowCount() + for (let i = 0; i < rows; ++i) + optionsList.localModel.push(itemFilterModel.modelItemData(i)) + + optionsList.model = optionsList.localModel // trigger on change handler + } } } diff --git a/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp index 38acbafa9cb..dd6b4e92691 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.cpp @@ -119,6 +119,41 @@ QStringList ItemFilterModel::validationItems() const return m_validationItems; } +QVariant ItemFilterModel::modelItemData(int row) const +{ + QModelIndex idx = index(row, 0, {}); + if (!idx.isValid()) + return {}; + + QVariantMap mapItem; + + auto insertData = [&](Role role) { + mapItem.insert(QString::fromUtf8(roleNames().value(role)), idx.data(role)); + }; + + insertData(IdRole); + insertData(IdAndNameRole); + insertData(NameRole); + insertData(EnabledRole); + + return mapItem; +} + +int ItemFilterModel::indexFromId(const QString &id) const +{ + AbstractView *view = m_modelNode.view(); + if (!view || !view->model()) + return -1; + + int idx = -1; + for (const auto &internalId : std::as_const(m_modelInternalIds)) { + ++idx; + if (id == view->modelNodeForInternalId(internalId).id()) + return idx; + } + return -1; +} + void ItemFilterModel::registerDeclarativeType() { qmlRegisterType("HelperWidgets", 2, 0, "ItemFilterModel"); diff --git a/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h index 4bd45601a27..5ffb9d1deed 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h +++ b/src/plugins/qmldesigner/components/propertyeditor/itemfiltermodel.h @@ -30,7 +30,9 @@ class ItemFilterModel : public QAbstractListModel Q_PROPERTY(QStringList validationItems READ validationItems NOTIFY validationItemsChanged) public: - enum { IdRole = Qt::DisplayRole, NameRole = Qt::UserRole, IdAndNameRole, EnabledRole }; + enum Role { IdRole = Qt::DisplayRole, NameRole = Qt::UserRole, IdAndNameRole, EnabledRole }; + + Q_ENUM(Role) explicit ItemFilterModel(QObject *parent = nullptr); @@ -45,6 +47,8 @@ public: QStringList itemModel() const; QStringList validationRoles() const; QStringList validationItems() const; + Q_INVOKABLE QVariant modelItemData(int row) const; + Q_INVOKABLE int indexFromId(const QString &id) const; static void registerDeclarativeType();