forked from qt-creator/qt-creator
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:
@@ -4,6 +4,7 @@
|
||||
#include "collectiondetailsmodel.h"
|
||||
|
||||
#include "collectioneditorconstants.h"
|
||||
#include "collectioneditorutils.h"
|
||||
#include "modelnode.h"
|
||||
#include "variantproperty.h"
|
||||
|
||||
@@ -385,7 +386,7 @@ QStringList CollectionDetailsModel::typesList()
|
||||
|
||||
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};
|
||||
bool alreadyOpen = m_openedCollections.contains(newReference);
|
||||
|
@@ -7,6 +7,8 @@
|
||||
#include "bindingproperty.h"
|
||||
#include "nodemetainfo.h"
|
||||
#include "propertymetainfo.h"
|
||||
#include "qmldesignerplugin.h"
|
||||
#include "variantproperty.h"
|
||||
|
||||
#include <variant>
|
||||
|
||||
@@ -111,4 +113,16 @@ bool canAcceptCollectionAsModel(const ModelNode &node)
|
||||
&& 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
|
||||
|
@@ -11,6 +11,8 @@ bool variantIslessThan(const QVariant &a, const QVariant &b, CollectionDetails::
|
||||
|
||||
QString getSourceCollectionType(const QmlDesigner::ModelNode &node);
|
||||
|
||||
QString getSourceCollectionPath(const QmlDesigner::ModelNode &node);
|
||||
|
||||
void assignCollectionSourceToNode(AbstractView *view,
|
||||
const ModelNode &modelNode,
|
||||
const ModelNode &collectionSourceNode = {});
|
||||
|
@@ -121,7 +121,7 @@ ModelNode CollectionListModel::sourceNode() 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
|
||||
|
@@ -26,7 +26,7 @@ QSharedPointer<QmlDesigner::CollectionListModel> loadCollection(
|
||||
QSharedPointer<QmlDesigner::CollectionListModel> initialCollection = {})
|
||||
{
|
||||
using namespace QmlDesigner::CollectionEditor;
|
||||
QString sourceFileAddress = sourceNode.variantProperty(SOURCEFILE_PROPERTY).value().toString();
|
||||
QString sourceFileAddress = getSourceCollectionPath(sourceNode);
|
||||
|
||||
QSharedPointer<QmlDesigner::CollectionListModel> collectionsList;
|
||||
auto setupCollectionList = [&sourceNode, &initialCollection, &collectionsList]() {
|
||||
@@ -286,9 +286,7 @@ bool CollectionSourceModel::addCollectionToSource(const ModelNode &node,
|
||||
if (collectionExists(node, collectionName))
|
||||
return returnError(tr("Model does not exist."));
|
||||
|
||||
QString sourceFileAddress = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY)
|
||||
.value()
|
||||
.toString();
|
||||
QString sourceFileAddress = CollectionEditor::getSourceCollectionPath(node);
|
||||
|
||||
QFileInfo sourceFileInfo(sourceFileAddress);
|
||||
if (!sourceFileInfo.isFile())
|
||||
@@ -442,9 +440,7 @@ void CollectionSourceModel::onCollectionNameChanged(const QString &oldName, cons
|
||||
return;
|
||||
}
|
||||
|
||||
QString sourceFileAddress = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY)
|
||||
.value()
|
||||
.toString();
|
||||
QString sourceFileAddress = CollectionEditor::getSourceCollectionPath(node);
|
||||
|
||||
QFileInfo sourceFileInfo(sourceFileAddress);
|
||||
if (!sourceFileInfo.isFile()) {
|
||||
@@ -533,9 +529,7 @@ void CollectionSourceModel::onCollectionsRemoved(const QStringList &removedColle
|
||||
return;
|
||||
}
|
||||
|
||||
QString sourceFileAddress = node.variantProperty(CollectionEditor::SOURCEFILE_PROPERTY)
|
||||
.value()
|
||||
.toString();
|
||||
QString sourceFileAddress = CollectionEditor::getSourceCollectionPath(node);
|
||||
|
||||
QFileInfo sourceFileInfo(sourceFileAddress);
|
||||
if (!sourceFileInfo.isFile()) {
|
||||
|
@@ -147,7 +147,15 @@ void CollectionView::addResource(const QUrl &url, const QString &name, const QSt
|
||||
{
|
||||
executeInTransaction(Q_FUNC_INFO, [this, &url, &name, &type]() {
|
||||
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
|
||||
? jsonCollectionMetaInfo()
|
||||
: csvCollectionMetaInfo();
|
||||
|
@@ -39,21 +39,6 @@ QString collectionViewResourcesPath()
|
||||
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)
|
||||
{
|
||||
if (collectionName.isEmpty()) {
|
||||
@@ -162,32 +147,27 @@ QSize CollectionWidget::minimumSizeHint() const
|
||||
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;
|
||||
|
||||
m_view->addResource(jsonFileAddress,
|
||||
getPreferredCollectionName(jsonFileAddress, collectionName),
|
||||
"json");
|
||||
m_view->addResource(url, getPreferredCollectionName(url, collectionName), "json");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CollectionWidget::loadCsvFile(const QString &csvFileAddress, const QString &collectionName)
|
||||
bool CollectionWidget::loadCsvFile(const QUrl &url, const QString &collectionName)
|
||||
{
|
||||
m_view->addResource(csvFileAddress,
|
||||
getPreferredCollectionName(csvFileAddress, collectionName),
|
||||
"csv");
|
||||
m_view->addResource(url, getPreferredCollectionName(url, collectionName), "csv");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CollectionWidget::isJsonFile(const QString &jsonFileAddress) const
|
||||
bool CollectionWidget::isJsonFile(const QUrl &url) const
|
||||
{
|
||||
QUrl jsonUrl(jsonFileAddress);
|
||||
QString fileAddress = jsonUrl.isLocalFile() ? jsonUrl.toLocalFile() : jsonUrl.toString();
|
||||
QFile file(fileAddress);
|
||||
QString filePath = url.isLocalFile() ? url.toLocalFile() : url.toString();
|
||||
QFile file(filePath);
|
||||
|
||||
if (!file.exists() || !file.open(QFile::ReadOnly))
|
||||
return false;
|
||||
@@ -200,10 +180,9 @@ bool CollectionWidget::isJsonFile(const QString &jsonFileAddress) const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CollectionWidget::isCsvFile(const QString &csvFilePath) const
|
||||
bool CollectionWidget::isCsvFile(const QUrl &url) const
|
||||
{
|
||||
QUrl csvUrl(csvFilePath);
|
||||
QString filePath = csvUrl.isLocalFile() ? csvUrl.toLocalFile() : csvUrl.toString();
|
||||
QString filePath = url.isLocalFile() ? url.toLocalFile() : url.toString();
|
||||
QFile file(filePath);
|
||||
|
||||
return file.exists() && file.fileName().endsWith(".csv");
|
||||
@@ -211,14 +190,15 @@ bool CollectionWidget::isCsvFile(const QString &csvFilePath) const
|
||||
|
||||
bool CollectionWidget::addCollection(const QString &collectionName,
|
||||
const QString &collectionType,
|
||||
const QString &sourceAddress,
|
||||
const QUrl &sourceUrl,
|
||||
const QVariant &sourceNode)
|
||||
{
|
||||
const ModelNode node = sourceNode.value<ModelNode>();
|
||||
bool isNewCollection = !node.isValid();
|
||||
|
||||
if (isNewCollection) {
|
||||
QString sourcePath = ::urlToLocalPath(sourceAddress);
|
||||
QString sourcePath = sourceUrl.isLocalFile() ? sourceUrl.toLocalFile() : sourceUrl.toString();
|
||||
|
||||
if (collectionType == "json") {
|
||||
QJsonObject jsonObject;
|
||||
QJsonObject initialObject;
|
||||
|
@@ -34,13 +34,13 @@ public:
|
||||
|
||||
virtual QSize minimumSizeHint() const;
|
||||
|
||||
Q_INVOKABLE bool loadJsonFile(const QString &jsonFileAddress, const QString &collectionName = {});
|
||||
Q_INVOKABLE bool loadCsvFile(const QString &csvFileAddress, const QString &collectionName = {});
|
||||
Q_INVOKABLE bool isJsonFile(const QString &jsonFileAddress) const;
|
||||
Q_INVOKABLE bool isCsvFile(const QString &csvFileAddress) const;
|
||||
Q_INVOKABLE bool loadJsonFile(const QUrl &url, const QString &collectionName = {});
|
||||
Q_INVOKABLE bool loadCsvFile(const QUrl &url, const QString &collectionName = {});
|
||||
Q_INVOKABLE bool isJsonFile(const QUrl &url) const;
|
||||
Q_INVOKABLE bool isCsvFile(const QUrl &url) const;
|
||||
Q_INVOKABLE bool addCollection(const QString &collectionName,
|
||||
const QString &collectionType,
|
||||
const QString &sourceAddress,
|
||||
const QUrl &sourceUrl,
|
||||
const QVariant &sourceNode);
|
||||
|
||||
Q_INVOKABLE void assignSourceNodeToSelectedItem(const QVariant &sourceNode);
|
||||
|
Reference in New Issue
Block a user