QmlDesigner: Support having diffrent url types by the collection editor

Task-number: QDS-11222
Change-Id: I94aac8a6cc5eb86d338b02a24886388188dc5def
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Ali Kianian
2023-11-13 18:28:10 +02:00
parent 9d0fd52a7c
commit a5a1152a76
8 changed files with 50 additions and 51 deletions

View File

@@ -4,6 +4,7 @@
#include "collectiondetailsmodel.h" #include "collectiondetailsmodel.h"
#include "collectioneditorconstants.h" #include "collectioneditorconstants.h"
#include "collectioneditorutils.h"
#include "modelnode.h" #include "modelnode.h"
#include "variantproperty.h" #include "variantproperty.h"
@@ -385,7 +386,7 @@ QStringList CollectionDetailsModel::typesList()
void CollectionDetailsModel::loadCollection(const ModelNode &sourceNode, const QString &collection) void CollectionDetailsModel::loadCollection(const ModelNode &sourceNode, const QString &collection)
{ {
QString fileName = sourceNode.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY).value().toString(); QString fileName = CollectionEditor::getSourceCollectionPath(sourceNode);
CollectionReference newReference{sourceNode, collection}; CollectionReference newReference{sourceNode, collection};
bool alreadyOpen = m_openedCollections.contains(newReference); bool alreadyOpen = m_openedCollections.contains(newReference);

View File

@@ -7,6 +7,8 @@
#include "bindingproperty.h" #include "bindingproperty.h"
#include "nodemetainfo.h" #include "nodemetainfo.h"
#include "propertymetainfo.h" #include "propertymetainfo.h"
#include "qmldesignerplugin.h"
#include "variantproperty.h"
#include <variant> #include <variant>
@@ -111,4 +113,16 @@ bool canAcceptCollectionAsModel(const ModelNode &node)
&& modelProperty.propertyType().isVariant(); && modelProperty.propertyType().isVariant();
} }
QString getSourceCollectionPath(const ModelNode &node)
{
QUrl nodeSource = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY).value().toUrl();
QString sourcePath = nodeSource.isLocalFile() ? nodeSource.toLocalFile() : nodeSource.toString();
return QmlDesignerPlugin::instance()
->currentDesignDocument()
->fileName()
.parentDir()
.resolvePath(sourcePath)
.toFSPathString();
}
} // namespace QmlDesigner::CollectionEditor } // namespace QmlDesigner::CollectionEditor

View File

@@ -11,6 +11,8 @@ bool variantIslessThan(const QVariant &a, const QVariant &b, CollectionDetails::
QString getSourceCollectionType(const QmlDesigner::ModelNode &node); QString getSourceCollectionType(const QmlDesigner::ModelNode &node);
QString getSourceCollectionPath(const QmlDesigner::ModelNode &node);
void assignCollectionSourceToNode(AbstractView *view, void assignCollectionSourceToNode(AbstractView *view,
const ModelNode &modelNode, const ModelNode &modelNode,
const ModelNode &collectionSourceNode = {}); const ModelNode &collectionSourceNode = {});

View File

@@ -121,7 +121,7 @@ ModelNode CollectionListModel::sourceNode() const
QString CollectionListModel::sourceAddress() const QString CollectionListModel::sourceAddress() const
{ {
return m_sourceNode.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY).value().toString(); return CollectionEditor::getSourceCollectionPath(m_sourceNode);
} }
bool CollectionListModel::contains(const QString &collectionName) const bool CollectionListModel::contains(const QString &collectionName) const

View File

@@ -26,7 +26,7 @@ QSharedPointer<QmlDesigner::CollectionListModel> loadCollection(
QSharedPointer<QmlDesigner::CollectionListModel> initialCollection = {}) QSharedPointer<QmlDesigner::CollectionListModel> initialCollection = {})
{ {
using namespace QmlDesigner::CollectionEditor; using namespace QmlDesigner::CollectionEditor;
QString sourceFileAddress = sourceNode.variantProperty(SOURCEFILE_PROPERTY).value().toString(); QString sourceFileAddress = getSourceCollectionPath(sourceNode);
QSharedPointer<QmlDesigner::CollectionListModel> collectionsList; QSharedPointer<QmlDesigner::CollectionListModel> collectionsList;
auto setupCollectionList = [&sourceNode, &initialCollection, &collectionsList]() { auto setupCollectionList = [&sourceNode, &initialCollection, &collectionsList]() {
@@ -286,9 +286,7 @@ bool CollectionSourceModel::addCollectionToSource(const ModelNode &node,
if (collectionExists(node, collectionName)) if (collectionExists(node, collectionName))
return returnError(tr("Model does not exist.")); return returnError(tr("Model does not exist."));
QString sourceFileAddress = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY) QString sourceFileAddress = CollectionEditor::getSourceCollectionPath(node);
.value()
.toString();
QFileInfo sourceFileInfo(sourceFileAddress); QFileInfo sourceFileInfo(sourceFileAddress);
if (!sourceFileInfo.isFile()) if (!sourceFileInfo.isFile())
@@ -442,9 +440,7 @@ void CollectionSourceModel::onCollectionNameChanged(const QString &oldName, cons
return; return;
} }
QString sourceFileAddress = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY) QString sourceFileAddress = CollectionEditor::getSourceCollectionPath(node);
.value()
.toString();
QFileInfo sourceFileInfo(sourceFileAddress); QFileInfo sourceFileInfo(sourceFileAddress);
if (!sourceFileInfo.isFile()) { if (!sourceFileInfo.isFile()) {
@@ -533,9 +529,7 @@ void CollectionSourceModel::onCollectionsRemoved(const QStringList &removedColle
return; return;
} }
QString sourceFileAddress = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY) QString sourceFileAddress = CollectionEditor::getSourceCollectionPath(node);
.value()
.toString();
QFileInfo sourceFileInfo(sourceFileAddress); QFileInfo sourceFileInfo(sourceFileAddress);
if (!sourceFileInfo.isFile()) { if (!sourceFileInfo.isFile()) {

View File

@@ -147,7 +147,15 @@ void CollectionView::addResource(const QUrl &url, const QString &name, const QSt
{ {
executeInTransaction(Q_FUNC_INFO, [this, &url, &name, &type]() { executeInTransaction(Q_FUNC_INFO, [this, &url, &name, &type]() {
ensureStudioModelImport(); ensureStudioModelImport();
QString sourceAddress = url.isLocalFile() ? url.toLocalFile() : url.toString(); QString sourceAddress;
if (url.isLocalFile()) {
Utils::FilePath fp = QmlDesignerPlugin::instance()->currentDesignDocument()->fileName().parentDir();
sourceAddress = Utils::FilePath::calcRelativePath(url.toLocalFile(),
fp.absoluteFilePath().toString());
} else {
sourceAddress = url.toString();
}
const NodeMetaInfo resourceMetaInfo = type.compare("json", Qt::CaseInsensitive) == 0 const NodeMetaInfo resourceMetaInfo = type.compare("json", Qt::CaseInsensitive) == 0
? jsonCollectionMetaInfo() ? jsonCollectionMetaInfo()
: csvCollectionMetaInfo(); : csvCollectionMetaInfo();

View File

@@ -39,21 +39,6 @@ QString collectionViewResourcesPath()
return Core::ICore::resourcePath("qmldesigner/collectionEditorQmlSource").toString(); return Core::ICore::resourcePath("qmldesigner/collectionEditorQmlSource").toString();
} }
QString urlToLocalPath(const QUrl &url)
{
QString localPath;
if (url.isLocalFile())
localPath = url.toLocalFile();
if (url.scheme() == QLatin1String("qrc")) {
const QString &path = url.path();
localPath = QStringLiteral(":") + path;
}
return localPath;
}
QString getPreferredCollectionName(const QUrl &url, const QString &collectionName) QString getPreferredCollectionName(const QUrl &url, const QString &collectionName)
{ {
if (collectionName.isEmpty()) { if (collectionName.isEmpty()) {
@@ -162,32 +147,27 @@ QSize CollectionWidget::minimumSizeHint() const
return {300, 400}; return {300, 400};
} }
bool CollectionWidget::loadJsonFile(const QString &jsonFileAddress, const QString &collectionName) bool CollectionWidget::loadJsonFile(const QUrl &url, const QString &collectionName)
{ {
if (!isJsonFile(jsonFileAddress)) if (!isJsonFile(url))
return false; return false;
m_view->addResource(jsonFileAddress, m_view->addResource(url, getPreferredCollectionName(url, collectionName), "json");
getPreferredCollectionName(jsonFileAddress, collectionName),
"json");
return true; return true;
} }
bool CollectionWidget::loadCsvFile(const QString &csvFileAddress, const QString &collectionName) bool CollectionWidget::loadCsvFile(const QUrl &url, const QString &collectionName)
{ {
m_view->addResource(csvFileAddress, m_view->addResource(url, getPreferredCollectionName(url, collectionName), "csv");
getPreferredCollectionName(csvFileAddress, collectionName),
"csv");
return true; return true;
} }
bool CollectionWidget::isJsonFile(const QString &jsonFileAddress) const bool CollectionWidget::isJsonFile(const QUrl &url) const
{ {
QUrl jsonUrl(jsonFileAddress); QString filePath = url.isLocalFile() ? url.toLocalFile() : url.toString();
QString fileAddress = jsonUrl.isLocalFile() ? jsonUrl.toLocalFile() : jsonUrl.toString(); QFile file(filePath);
QFile file(fileAddress);
if (!file.exists() || !file.open(QFile::ReadOnly)) if (!file.exists() || !file.open(QFile::ReadOnly))
return false; return false;
@@ -200,10 +180,9 @@ bool CollectionWidget::isJsonFile(const QString &jsonFileAddress) const
return true; return true;
} }
bool CollectionWidget::isCsvFile(const QString &csvFilePath) const bool CollectionWidget::isCsvFile(const QUrl &url) const
{ {
QUrl csvUrl(csvFilePath); QString filePath = url.isLocalFile() ? url.toLocalFile() : url.toString();
QString filePath = csvUrl.isLocalFile() ? csvUrl.toLocalFile() : csvUrl.toString();
QFile file(filePath); QFile file(filePath);
return file.exists() && file.fileName().endsWith(".csv"); return file.exists() && file.fileName().endsWith(".csv");
@@ -211,14 +190,15 @@ bool CollectionWidget::isCsvFile(const QString &csvFilePath) const
bool CollectionWidget::addCollection(const QString &collectionName, bool CollectionWidget::addCollection(const QString &collectionName,
const QString &collectionType, const QString &collectionType,
const QString &sourceAddress, const QUrl &sourceUrl,
const QVariant &sourceNode) const QVariant &sourceNode)
{ {
const ModelNode node = sourceNode.value<ModelNode>(); const ModelNode node = sourceNode.value<ModelNode>();
bool isNewCollection = !node.isValid(); bool isNewCollection = !node.isValid();
if (isNewCollection) { if (isNewCollection) {
QString sourcePath = ::urlToLocalPath(sourceAddress); QString sourcePath = sourceUrl.isLocalFile() ? sourceUrl.toLocalFile() : sourceUrl.toString();
if (collectionType == "json") { if (collectionType == "json") {
QJsonObject jsonObject; QJsonObject jsonObject;
QJsonObject initialObject; QJsonObject initialObject;

View File

@@ -34,13 +34,13 @@ public:
virtual QSize minimumSizeHint() const; virtual QSize minimumSizeHint() const;
Q_INVOKABLE bool loadJsonFile(const QString &jsonFileAddress, const QString &collectionName = {}); Q_INVOKABLE bool loadJsonFile(const QUrl &url, const QString &collectionName = {});
Q_INVOKABLE bool loadCsvFile(const QString &csvFileAddress, const QString &collectionName = {}); Q_INVOKABLE bool loadCsvFile(const QUrl &url, const QString &collectionName = {});
Q_INVOKABLE bool isJsonFile(const QString &jsonFileAddress) const; Q_INVOKABLE bool isJsonFile(const QUrl &url) const;
Q_INVOKABLE bool isCsvFile(const QString &csvFileAddress) const; Q_INVOKABLE bool isCsvFile(const QUrl &url) const;
Q_INVOKABLE bool addCollection(const QString &collectionName, Q_INVOKABLE bool addCollection(const QString &collectionName,
const QString &collectionType, const QString &collectionType,
const QString &sourceAddress, const QUrl &sourceUrl,
const QVariant &sourceNode); const QVariant &sourceNode);
Q_INVOKABLE void assignSourceNodeToSelectedItem(const QVariant &sourceNode); Q_INVOKABLE void assignSourceNodeToSelectedItem(const QVariant &sourceNode);