QmlDesigner: Enable name and id for texture ComboBoxes

Change-Id: Iefe9e68bf9c54753c38c6a43f3a6d3217f2293c0
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Shrief Gabr <shrief.gabr@qt.io>
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
This commit is contained in:
Ali Kianian
2024-06-26 18:29:47 +03:00
parent c6531e02cf
commit a80e45fa15
3 changed files with 92 additions and 14 deletions

View File

@@ -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
}
}
}

View File

@@ -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<ItemFilterModel>("HelperWidgets", 2, 0, "ItemFilterModel");

View File

@@ -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();