forked from qt-creator/qt-creator
QmlDesigner: Set texRole when assigning a collection to a node
Task-number: QDS-11461 Change-Id: Ia03c8267cc6f6b93860aa853afb048650f1acb3f Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
@@ -162,6 +162,20 @@ bool canAcceptCollectionAsModel(const ModelNode &node)
|
|||||||
&& modelProperty.propertyType().isVariant();
|
&& modelProperty.propertyType().isVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool hasTextRoleProperty(const ModelNode &node)
|
||||||
|
{
|
||||||
|
const NodeMetaInfo nodeMetaInfo = node.metaInfo();
|
||||||
|
if (!nodeMetaInfo.isValid())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const PropertyMetaInfo textRoleProperty = nodeMetaInfo.property("textRole");
|
||||||
|
if (!textRoleProperty.isValid())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return textRoleProperty.isWritable() && !textRoleProperty.isPrivate()
|
||||||
|
&& textRoleProperty.propertyType().isString();
|
||||||
|
}
|
||||||
|
|
||||||
QString getSourceCollectionPath(const ModelNode &dataStoreNode)
|
QString getSourceCollectionPath(const ModelNode &dataStoreNode)
|
||||||
{
|
{
|
||||||
using Utils::FilePath;
|
using Utils::FilePath;
|
||||||
@@ -380,4 +394,81 @@ QJsonArray loadAsCsvCollection(const QUrl &url)
|
|||||||
return elements;
|
return elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString getFirstColumnName(const QString &collectionName)
|
||||||
|
{
|
||||||
|
Utils::FilePath dataStorePath = CollectionEditorUtils::dataStoreJsonFilePath();
|
||||||
|
|
||||||
|
if (!dataStorePath.exists())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
Utils::FileReader dataStoreFile;
|
||||||
|
if (!dataStoreFile.fetch(dataStorePath))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
QJsonParseError jsonError;
|
||||||
|
QJsonDocument dataStoreDocument = QJsonDocument::fromJson(dataStoreFile.data(), &jsonError);
|
||||||
|
if (jsonError.error == QJsonParseError::NoError) {
|
||||||
|
QJsonObject rootObject = dataStoreDocument.object();
|
||||||
|
if (rootObject.contains(collectionName)) {
|
||||||
|
QJsonArray collectionArray = rootObject.value(collectionName).toArray();
|
||||||
|
for (const QJsonValue &elementValue : std::as_const(collectionArray)) {
|
||||||
|
const QJsonObject elementObject = elementValue.toObject();
|
||||||
|
QJsonObject::ConstIterator element = elementObject.constBegin();
|
||||||
|
if (element != elementObject.constEnd())
|
||||||
|
return element.key();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qWarning() << Q_FUNC_INFO << __LINE__
|
||||||
|
<< QString("Collection \"%1\" not found.").arg(collectionName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qWarning() << Q_FUNC_INFO << __LINE__ << "Problem in reading json file."
|
||||||
|
<< jsonError.errorString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool collectionHasColumn(const QString &collectionName, const QString &columnName)
|
||||||
|
{
|
||||||
|
Utils::FilePath dataStorePath = CollectionEditorUtils::dataStoreJsonFilePath();
|
||||||
|
|
||||||
|
if (!dataStorePath.exists())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Utils::FileReader dataStoreFile;
|
||||||
|
if (!dataStoreFile.fetch(dataStorePath))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QJsonParseError jsonError;
|
||||||
|
QJsonDocument dataStoreDocument = QJsonDocument::fromJson(dataStoreFile.data(), &jsonError);
|
||||||
|
if (jsonError.error == QJsonParseError::NoError) {
|
||||||
|
QJsonObject rootObject = dataStoreDocument.object();
|
||||||
|
if (rootObject.contains(collectionName)) {
|
||||||
|
QJsonArray collectionArray = rootObject.value(collectionName).toArray();
|
||||||
|
for (const QJsonValue &elementValue : std::as_const(collectionArray)) {
|
||||||
|
const QJsonObject elementObject = elementValue.toObject();
|
||||||
|
QJsonObject::ConstIterator element = elementObject.constBegin();
|
||||||
|
const QJsonObject::ConstIterator stopItem = elementObject.constEnd();
|
||||||
|
|
||||||
|
while (element != stopItem) {
|
||||||
|
const QString keyName = element.key();
|
||||||
|
++element;
|
||||||
|
|
||||||
|
if (columnName == keyName)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qWarning() << Q_FUNC_INFO << __LINE__
|
||||||
|
<< QString("Collection \"%1\" not found.").arg(collectionName);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qWarning() << Q_FUNC_INFO << __LINE__ << "Problem in reading json file."
|
||||||
|
<< jsonError.errorString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner::CollectionEditorUtils
|
} // namespace QmlDesigner::CollectionEditorUtils
|
||||||
|
@@ -34,10 +34,16 @@ bool ensureDataStoreExists(bool &justCreated);
|
|||||||
|
|
||||||
bool canAcceptCollectionAsModel(const ModelNode &node);
|
bool canAcceptCollectionAsModel(const ModelNode &node);
|
||||||
|
|
||||||
|
bool hasTextRoleProperty(const ModelNode &node);
|
||||||
|
|
||||||
QJsonArray defaultCollectionArray();
|
QJsonArray defaultCollectionArray();
|
||||||
|
|
||||||
QJsonArray loadAsSingleJsonCollection(const QUrl &url);
|
QJsonArray loadAsSingleJsonCollection(const QUrl &url);
|
||||||
|
|
||||||
QJsonArray loadAsCsvCollection(const QUrl &url);
|
QJsonArray loadAsCsvCollection(const QUrl &url);
|
||||||
|
|
||||||
|
QString getFirstColumnName(const QString &collectionName);
|
||||||
|
|
||||||
|
bool collectionHasColumn(const QString &collectionName, const QString &columnName);
|
||||||
|
|
||||||
} // namespace QmlDesigner::CollectionEditorUtils
|
} // namespace QmlDesigner::CollectionEditorUtils
|
||||||
|
@@ -450,12 +450,27 @@ void DataStoreModelNode::assignCollectionToNode(AbstractView *view,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
BindingProperty modelProperty = targetNode.bindingProperty("model");
|
view->executeInTransaction("assignCollectionToNode", [&]() {
|
||||||
|
QString identifier = QString("DataStore.%1").arg(QString::fromLatin1(sourceProperty.name()));
|
||||||
QString identifier = QString("DataStore.%1").arg(QString::fromLatin1(sourceProperty.name()));
|
BindingProperty modelProperty = targetNode.bindingProperty("model");
|
||||||
|
|
||||||
view->executeInTransaction("assignCollectionToNode", [&modelProperty, &identifier]() {
|
|
||||||
modelProperty.setExpression(identifier);
|
modelProperty.setExpression(identifier);
|
||||||
|
if (CollectionEditorUtils::hasTextRoleProperty(targetNode)) {
|
||||||
|
VariantProperty textRoleProperty = targetNode.variantProperty("textRole");
|
||||||
|
const QVariant currentTextRoleValue = textRoleProperty.value();
|
||||||
|
|
||||||
|
if (currentTextRoleValue.isValid() && !currentTextRoleValue.isNull()) {
|
||||||
|
if (currentTextRoleValue.type() == QVariant::String) {
|
||||||
|
const QString currentTextRole = currentTextRoleValue.toString();
|
||||||
|
if (CollectionEditorUtils::collectionHasColumn(collectionName, currentTextRole))
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString textRoleValue = CollectionEditorUtils::getFirstColumnName(collectionName);
|
||||||
|
textRoleProperty.setValue(textRoleValue);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user