forked from qt-creator/qt-creator
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:
@@ -11,27 +11,32 @@ HelperWidgets.ComboBox {
|
|||||||
|
|
||||||
manualMapping: true
|
manualMapping: true
|
||||||
editable: 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*)/ }
|
validator: RegularExpressionValidator { regularExpression: /(^$|^[a-z_]\w*)/ }
|
||||||
|
|
||||||
HelperWidgets.ItemFilterModel {
|
HelperWidgets.ItemFilterModel {
|
||||||
id: itemFilterModel
|
id: itemFilterModel
|
||||||
modelNodeBackendProperty: modelNodeBackend
|
modelNodeBackendProperty: modelNodeBackend
|
||||||
|
onModelReset: optionsList.updateModel()
|
||||||
|
onRowsInserted: optionsList.updateModel()
|
||||||
|
onRowsRemoved: optionsList.updateModel()
|
||||||
}
|
}
|
||||||
|
|
||||||
property string defaultItem: qsTr("[None]")
|
property string defaultItem: qsTr("[None]")
|
||||||
property string textValue: comboBox.backendValue?.expression ?? ""
|
property string expressionValue: comboBox.backendValue?.expression ?? ""
|
||||||
property bool block: false
|
property bool block: false
|
||||||
property bool dirty: true
|
property bool dirty: true
|
||||||
|
|
||||||
onTextValueChanged: {
|
onExpressionValueChanged: {
|
||||||
if (comboBox.block)
|
if (comboBox.block)
|
||||||
return
|
return
|
||||||
|
|
||||||
comboBox.setCurrentText(comboBox.textValue)
|
comboBox.updateText()
|
||||||
}
|
}
|
||||||
onModelChanged: comboBox.setCurrentText(comboBox.textValue)
|
onModelChanged: comboBox.updateText()
|
||||||
onEditTextChanged: comboBox.dirty = true
|
onEditTextChanged: comboBox.dirty = true
|
||||||
onFocusChanged: {
|
onFocusChanged: {
|
||||||
if (comboBox.dirty)
|
if (comboBox.dirty)
|
||||||
@@ -41,7 +46,18 @@ HelperWidgets.ComboBox {
|
|||||||
onCompressedActivated: function(index, reason) { comboBox.handleActivate(index) }
|
onCompressedActivated: function(index, reason) { comboBox.handleActivate(index) }
|
||||||
onAccepted: comboBox.setCurrentText(comboBox.editText)
|
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) {
|
function handleActivate(index) {
|
||||||
if (!comboBox.__isCompleted || comboBox.backendValue === undefined)
|
if (!comboBox.__isCompleted || comboBox.backendValue === undefined)
|
||||||
@@ -69,18 +85,41 @@ HelperWidgets.ComboBox {
|
|||||||
comboBox.editText = comboBox.defaultItem
|
comboBox.editText = comboBox.defaultItem
|
||||||
}
|
}
|
||||||
|
|
||||||
if (comboBox.currentIndex === 0) {
|
if (comboBox.currentIndex < 1) {
|
||||||
comboBox.backendValue.resetValue()
|
comboBox.backendValue.resetValue()
|
||||||
} else {
|
} else {
|
||||||
if (comboBox.backendValue.expression !== comboBox.editText)
|
let valueData = (comboBox.valueRole === "")
|
||||||
comboBox.backendValue.expression = comboBox.editText
|
? comboBox.editText
|
||||||
|
: itemFilterModel.modelItemData(comboBox.currentIndex - 1)[comboBox.valueRole]
|
||||||
|
|
||||||
|
if (comboBox.backendValue.expression !== valueData)
|
||||||
|
comboBox.backendValue.expression = valueData
|
||||||
}
|
}
|
||||||
comboBox.dirty = false
|
comboBox.dirty = false
|
||||||
}
|
}
|
||||||
|
|
||||||
function addDefaultItem(arr) {
|
Repeater {
|
||||||
var copy = arr.slice()
|
id: optionsList
|
||||||
copy.unshift(comboBox.defaultItem)
|
|
||||||
return copy
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -119,6 +119,41 @@ QStringList ItemFilterModel::validationItems() const
|
|||||||
return m_validationItems;
|
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()
|
void ItemFilterModel::registerDeclarativeType()
|
||||||
{
|
{
|
||||||
qmlRegisterType<ItemFilterModel>("HelperWidgets", 2, 0, "ItemFilterModel");
|
qmlRegisterType<ItemFilterModel>("HelperWidgets", 2, 0, "ItemFilterModel");
|
||||||
|
@@ -30,7 +30,9 @@ class ItemFilterModel : public QAbstractListModel
|
|||||||
Q_PROPERTY(QStringList validationItems READ validationItems NOTIFY validationItemsChanged)
|
Q_PROPERTY(QStringList validationItems READ validationItems NOTIFY validationItemsChanged)
|
||||||
|
|
||||||
public:
|
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);
|
explicit ItemFilterModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
@@ -45,6 +47,8 @@ public:
|
|||||||
QStringList itemModel() const;
|
QStringList itemModel() const;
|
||||||
QStringList validationRoles() const;
|
QStringList validationRoles() const;
|
||||||
QStringList validationItems() const;
|
QStringList validationItems() const;
|
||||||
|
Q_INVOKABLE QVariant modelItemData(int row) const;
|
||||||
|
Q_INVOKABLE int indexFromId(const QString &id) const;
|
||||||
|
|
||||||
static void registerDeclarativeType();
|
static void registerDeclarativeType();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user