forked from qt-creator/qt-creator
QmlDesigner: Assign a single collection model to the selected node
Task-number: QDS-11217
Change-Id: I35eb536540faa2299a51d152a29f07c2c36abe41
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
(cherry picked from commit 206ad75359
)
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -16,6 +16,7 @@ Item {
|
||||
|
||||
property color textColor
|
||||
property string sourceType
|
||||
property bool hasSelectedTarget
|
||||
|
||||
signal selectItem(int itemIndex)
|
||||
signal deleteItem()
|
||||
@@ -125,6 +126,12 @@ Item {
|
||||
shortcut: StandardKey.Replace
|
||||
onTriggered: renameDialog.open()
|
||||
}
|
||||
|
||||
StudioControls.MenuItem {
|
||||
text: qsTr("Assign to the selected node")
|
||||
enabled: root.hasSelectedTarget
|
||||
onTriggered: rootView.assignCollectionToSelectedNode(collectionName)
|
||||
}
|
||||
}
|
||||
|
||||
component Spacer: Item {
|
||||
|
@@ -111,7 +111,6 @@ Item {
|
||||
implicitWidth: sourceListView.width
|
||||
onDeleteItem: root.model.removeRow(index)
|
||||
hasSelectedTarget: root.rootView.targetNodeSelected
|
||||
onAssignToSelected: root.rootView.assignSourceNodeToSelectedItem(sourceNode)
|
||||
onAddCollection: (collectionName) => {
|
||||
root.rootView.addCollection(collectionName,
|
||||
sourceCollectionType,
|
||||
|
@@ -24,7 +24,6 @@ Item {
|
||||
|
||||
signal selectItem(int itemIndex)
|
||||
signal deleteItem()
|
||||
signal assignToSelected()
|
||||
signal addCollection(string collectionName)
|
||||
|
||||
function toggleExpanded() {
|
||||
@@ -161,6 +160,7 @@ Item {
|
||||
delegate: CollectionItem {
|
||||
width: collectionListView.width
|
||||
sourceType: collectionListView.model.sourceType
|
||||
hasSelectedTarget: root.hasSelectedTarget
|
||||
onDeleteItem: collectionListView.model.removeRow(index)
|
||||
}
|
||||
}
|
||||
@@ -188,12 +188,6 @@ Item {
|
||||
shortcut: StandardKey.Replace
|
||||
onTriggered: renameDialog.open()
|
||||
}
|
||||
|
||||
StudioControls.MenuItem {
|
||||
text: qsTr("Assign to the selected node")
|
||||
enabled: root.hasSelectedTarget
|
||||
onTriggered: root.assignToSelected()
|
||||
}
|
||||
}
|
||||
|
||||
component Spacer: Item {
|
||||
|
@@ -7,7 +7,6 @@
|
||||
#include "bindingproperty.h"
|
||||
#include "nodemetainfo.h"
|
||||
#include "propertymetainfo.h"
|
||||
#include "qmldesignerplugin.h"
|
||||
#include "variantproperty.h"
|
||||
|
||||
#include <variant>
|
||||
@@ -100,20 +99,30 @@ QString getSourceCollectionType(const ModelNode &node)
|
||||
return {};
|
||||
}
|
||||
|
||||
void assignCollectionSourceToNode(AbstractView *view,
|
||||
const ModelNode &modelNode,
|
||||
const ModelNode &collectionSourceNode)
|
||||
void assignCollectionToNode(AbstractView *view,
|
||||
const ModelNode &modelNode,
|
||||
const ModelNode &collectionSourceNode,
|
||||
const QString &collectionName)
|
||||
{
|
||||
QTC_ASSERT(modelNode.isValid() && collectionSourceNode.isValid(), return);
|
||||
|
||||
if (collectionSourceNode.id().isEmpty() || !canAcceptCollectionAsModel(modelNode))
|
||||
QString sourceId = isDataStoreNode(collectionSourceNode) ? "DataStore"
|
||||
: collectionSourceNode.id();
|
||||
|
||||
if (sourceId.isEmpty() || !canAcceptCollectionAsModel(modelNode))
|
||||
return;
|
||||
|
||||
VariantProperty sourceProperty = collectionSourceNode.variantProperty(collectionName.toLatin1());
|
||||
if (!sourceProperty.exists())
|
||||
return;
|
||||
|
||||
BindingProperty modelProperty = modelNode.bindingProperty("model");
|
||||
|
||||
view->executeInTransaction("CollectionEditor::assignCollectionSourceToNode",
|
||||
[&modelProperty, &collectionSourceNode]() {
|
||||
modelProperty.setExpression(collectionSourceNode.id());
|
||||
QString identifier = QString("%1.%2").arg(sourceId, QString::fromLatin1(sourceProperty.name()));
|
||||
|
||||
view->executeInTransaction("CollectionEditor::assignCollectionToNode",
|
||||
[&modelProperty, &identifier]() {
|
||||
modelProperty.setExpression(identifier);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -148,6 +157,22 @@ QString getSourceCollectionPath(const ModelNode &dataStoreNode)
|
||||
return {};
|
||||
}
|
||||
|
||||
bool isDataStoreNode(const ModelNode &dataStoreNode)
|
||||
{
|
||||
using Utils::FilePath;
|
||||
ProjectExplorer::Project *currentProject = ProjectExplorer::ProjectManager::startupProject();
|
||||
|
||||
if (!currentProject || !dataStoreNode.isValid())
|
||||
return false;
|
||||
|
||||
const FilePath expectedFile = currentProject->projectDirectory().pathAppended(
|
||||
"/imports/" + currentProject->displayName() + "/DataStore.qml");
|
||||
|
||||
FilePath modelPath = FilePath::fromUserInput(dataStoreNode.model()->fileUrl().toLocalFile());
|
||||
|
||||
return modelPath.isSameFile(expectedFile);
|
||||
}
|
||||
|
||||
QJsonArray defaultCollectionArray()
|
||||
{
|
||||
QJsonObject initialObject;
|
||||
|
@@ -20,9 +20,12 @@ QString getSourceCollectionType(const QmlDesigner::ModelNode &node);
|
||||
|
||||
QString getSourceCollectionPath(const QmlDesigner::ModelNode &dataStoreNode);
|
||||
|
||||
void assignCollectionSourceToNode(AbstractView *view,
|
||||
const ModelNode &modelNode,
|
||||
const ModelNode &collectionSourceNode = {});
|
||||
void assignCollectionToNode(AbstractView *view,
|
||||
const ModelNode &modelNode,
|
||||
const ModelNode &collectionSourceNode,
|
||||
const QString &collectionName);
|
||||
|
||||
bool isDataStoreNode(const ModelNode &dataStoreNode);
|
||||
|
||||
bool canAcceptCollectionAsModel(const ModelNode &node);
|
||||
|
||||
|
@@ -334,20 +334,19 @@ bool CollectionWidget::addCollectionToDataStore(const QString &collectionName)
|
||||
return added;
|
||||
}
|
||||
|
||||
void CollectionWidget::assignSourceNodeToSelectedItem(const QVariant &sourceNode)
|
||||
void CollectionWidget::assignCollectionToSelectedNode(const QString collectionName)
|
||||
{
|
||||
ModelNode sourceModel = sourceNode.value<ModelNode>();
|
||||
ModelNode dsNode = dataStoreNode();
|
||||
ModelNode targetNode = m_view->singleSelectedModelNode();
|
||||
|
||||
QTC_ASSERT(sourceModel.isValid() && targetNode.isValid(), return);
|
||||
QTC_ASSERT(dsNode.isValid() && targetNode.isValid(), return);
|
||||
|
||||
if (sourceModel.id().isEmpty()) {
|
||||
warn(tr("Assigning the model group"),
|
||||
tr("The model group must have a valid id to be assigned."));
|
||||
if (dsNode.id().isEmpty()) {
|
||||
warn(tr("Assigning the model"), tr("The model must have a valid id to be assigned."));
|
||||
return;
|
||||
}
|
||||
|
||||
CollectionEditor::assignCollectionSourceToNode(m_view, targetNode, sourceModel);
|
||||
CollectionEditor::assignCollectionToNode(m_view, targetNode, dsNode, collectionName);
|
||||
}
|
||||
|
||||
ModelNode CollectionWidget::dataStoreNode() const
|
||||
|
@@ -52,7 +52,7 @@ public:
|
||||
|
||||
Q_INVOKABLE bool addCollectionToDataStore(const QString &collectionName);
|
||||
|
||||
Q_INVOKABLE void assignSourceNodeToSelectedItem(const QVariant &sourceNode);
|
||||
Q_INVOKABLE void assignCollectionToSelectedNode(const QString collectionName);
|
||||
|
||||
Q_INVOKABLE ModelNode dataStoreNode() const;
|
||||
|
||||
|
Reference in New Issue
Block a user