forked from qt-creator/qt-creator
QmlDesigner: Add import difference
In some cases we want to use only a sub set of imports. In that case we compute the differences on demand. To compute the differences the imports have to be sorted. Task-number: QDS-9542 Change-Id: I8b3e501b7657522317847b0b788cd3ff5b47b003 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -920,21 +920,25 @@ void Edit3DView::addQuick3DImport()
|
|||||||
{
|
{
|
||||||
DesignDocument *document = QmlDesignerPlugin::instance()->currentDesignDocument();
|
DesignDocument *document = QmlDesignerPlugin::instance()->currentDesignDocument();
|
||||||
if (document && !document->inFileComponentModelActive() && model()) {
|
if (document && !document->inFileComponentModelActive() && model()) {
|
||||||
const Imports imports = model()->possibleImports();
|
Import qtQuick3DImport;
|
||||||
for (const auto &import : imports) {
|
differenceCall(model()->possibleImports(), model()->imports(), [&](const auto &import) {
|
||||||
if (import.url() == "QtQuick3D") {
|
if (import.url() == "QtQuick3D")
|
||||||
if (!import.version().isEmpty() && import.majorVersion() >= 6) {
|
qtQuick3DImport = import;
|
||||||
|
});
|
||||||
|
if (!qtQuick3DImport.isEmpty()) {
|
||||||
|
if (!qtQuick3DImport.version().isEmpty() && qtQuick3DImport.majorVersion() >= 6) {
|
||||||
// Prefer empty version number in Qt6 and beyond
|
// Prefer empty version number in Qt6 and beyond
|
||||||
model()->changeImports({Import::createLibraryImport(
|
model()->changeImports({Import::createLibraryImport(qtQuick3DImport.url(),
|
||||||
import.url(), {}, import.alias(),
|
{},
|
||||||
import.importPaths())}, {});
|
qtQuick3DImport.alias(),
|
||||||
|
qtQuick3DImport.importPaths())},
|
||||||
|
{});
|
||||||
} else {
|
} else {
|
||||||
model()->changeImports({import}, {});
|
model()->changeImports({qtQuick3DImport}, {});
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Core::AsynchronousMessageBox::warning(tr("Failed to Add Import"),
|
Core::AsynchronousMessageBox::warning(tr("Failed to Add Import"),
|
||||||
tr("Could not add QtQuick3D import to project."));
|
tr("Could not add QtQuick3D import to project."));
|
||||||
}
|
}
|
||||||
|
@@ -62,7 +62,7 @@ void ItemLibraryView::modelAttached(Model *model)
|
|||||||
m_widget->setModel(model);
|
m_widget->setModel(model);
|
||||||
updateImports();
|
updateImports();
|
||||||
if (model)
|
if (model)
|
||||||
m_widget->updatePossibleImports(model->possibleImports());
|
m_widget->updatePossibleImports(difference(model->possibleImports(), model->imports()));
|
||||||
m_hasErrors = !rewriterView()->errors().isEmpty();
|
m_hasErrors = !rewriterView()->errors().isEmpty();
|
||||||
m_widget->setFlowMode(QmlItemNode(rootModelNode()).isFlowView());
|
m_widget->setFlowMode(QmlItemNode(rootModelNode()).isFlowView());
|
||||||
}
|
}
|
||||||
|
@@ -368,7 +368,7 @@ void ItemLibraryWidget::handlePriorityImportsChanged()
|
|||||||
{
|
{
|
||||||
if (!m_itemLibraryInfo.isNull()) {
|
if (!m_itemLibraryInfo.isNull()) {
|
||||||
m_addModuleModel->setPriorityImports(m_itemLibraryInfo->priorityImports());
|
m_addModuleModel->setPriorityImports(m_itemLibraryInfo->priorityImports());
|
||||||
m_addModuleModel->update(m_model->possibleImports());
|
m_addModuleModel->update(difference(m_model->possibleImports(), m_model->imports()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -901,7 +901,8 @@ void NavigatorTreeModel::addImport(const QString &importName)
|
|||||||
{
|
{
|
||||||
Import import = Import::createLibraryImport(importName);
|
Import import = Import::createLibraryImport(importName);
|
||||||
if (!m_view->model()->hasImport(import, true, true)) {
|
if (!m_view->model()->hasImport(import, true, true)) {
|
||||||
const Imports possImports = m_view->model()->possibleImports();
|
const Imports possImports = difference(m_view->model()->possibleImports(),
|
||||||
|
m_view->model()->imports());
|
||||||
for (const auto &possImport : possImports) {
|
for (const auto &possImport : possImports) {
|
||||||
if (possImport.url() == import.url()) {
|
if (possImport.url() == import.url()) {
|
||||||
import = possImport;
|
import = possImport;
|
||||||
|
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <utils/set_algorithm.h>
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
@@ -26,16 +28,15 @@ public:
|
|||||||
bool hasVersion() const { return !m_version.isEmpty(); }
|
bool hasVersion() const { return !m_version.isEmpty(); }
|
||||||
bool hasAlias() const { return !m_alias.isEmpty(); }
|
bool hasAlias() const { return !m_alias.isEmpty(); }
|
||||||
|
|
||||||
QString url() const { return m_url; }
|
const QString &url() const { return m_url; }
|
||||||
QString file() const { return m_file; }
|
const QString &file() const { return m_file; }
|
||||||
QString version() const { return m_version; }
|
const QString &version() const { return m_version; }
|
||||||
QString alias() const { return m_alias; }
|
const QString &alias() const { return m_alias; }
|
||||||
QStringList importPaths() const { return m_importPathList; }
|
const QStringList &importPaths() const { return m_importPathList; }
|
||||||
|
|
||||||
QString toString(bool skipAlias = false, bool skipVersion = false) const;
|
QString toString(bool skipAlias = false, bool skipVersion = false) const;
|
||||||
QString toImportString() const;
|
QString toImportString() const;
|
||||||
|
|
||||||
bool operator==(const Import &other) const;
|
|
||||||
bool isSameModule(const Import &other) const;
|
bool isSameModule(const Import &other) const;
|
||||||
|
|
||||||
int majorVersion() const;
|
int majorVersion() const;
|
||||||
@@ -43,6 +44,18 @@ public:
|
|||||||
static int majorFromVersion(const QString &version);
|
static int majorFromVersion(const QString &version);
|
||||||
static int minorFromVersion(const QString &version);
|
static int minorFromVersion(const QString &version);
|
||||||
|
|
||||||
|
friend bool operator==(const Import &first, const Import &second)
|
||||||
|
{
|
||||||
|
return first.m_url == second.m_url && first.m_file == second.m_file
|
||||||
|
&& (first.m_version == second.m_version || first.m_version.isEmpty()
|
||||||
|
|| second.m_version.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
friend bool operator<(const Import &first, const Import &second)
|
||||||
|
{
|
||||||
|
return std::tie(first.m_url, first.m_file) < std::tie(second.m_url, second.m_file);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Import(const QString &url, const QString &file, const QString &version, const QString &alias, const QStringList &importPaths);
|
Import(const QString &url, const QString &file, const QString &version, const QString &alias, const QStringList &importPaths);
|
||||||
|
|
||||||
@@ -58,6 +71,21 @@ QMLDESIGNERCORE_EXPORT size_t qHash(const Import &import);
|
|||||||
|
|
||||||
using Imports = QList<Import>;
|
using Imports = QList<Import>;
|
||||||
|
|
||||||
|
QMLDESIGNERCORE_EXPORT Imports difference(const Imports &first, const Imports &second);
|
||||||
|
|
||||||
|
template<typename Callable>
|
||||||
|
void differenceCall(const Imports &first, const Imports &second, Callable callable)
|
||||||
|
{
|
||||||
|
Imports difference;
|
||||||
|
difference.reserve(first.size());
|
||||||
|
|
||||||
|
std::set_difference(first.begin(),
|
||||||
|
first.end(),
|
||||||
|
second.begin(),
|
||||||
|
second.end(),
|
||||||
|
Utils::make_iterator(callable));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QmlDesigner::Import)
|
Q_DECLARE_METATYPE(QmlDesigner::Import)
|
||||||
|
@@ -119,8 +119,8 @@ public:
|
|||||||
const Imports &possibleImports() const;
|
const Imports &possibleImports() const;
|
||||||
const Imports &usedImports() const;
|
const Imports &usedImports() const;
|
||||||
void changeImports(const Imports &importsToBeAdded, const Imports &importsToBeRemoved);
|
void changeImports(const Imports &importsToBeAdded, const Imports &importsToBeRemoved);
|
||||||
void setPossibleImports(const Imports &possibleImports);
|
void setPossibleImports(Imports possibleImports);
|
||||||
void setUsedImports(const Imports &usedImports);
|
void setUsedImports(Imports usedImports);
|
||||||
bool hasImport(const Import &import, bool ignoreAlias = true, bool allowHigherVersion = false) const;
|
bool hasImport(const Import &import, bool ignoreAlias = true, bool allowHigherVersion = false) const;
|
||||||
bool isImportPossible(const Import &import, bool ignoreAlias = true, bool allowHigherVersion = false) const;
|
bool isImportPossible(const Import &import, bool ignoreAlias = true, bool allowHigherVersion = false) const;
|
||||||
QString pathForImport(const Import &import);
|
QString pathForImport(const Import &import);
|
||||||
|
@@ -62,11 +62,6 @@ QString Import::toString(bool skipAlias, bool skipVersion) const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Import::operator==(const Import &other) const
|
|
||||||
{
|
|
||||||
return url() == other.url() && file() == other.file() && (version() == other.version() || version().isEmpty() || other.version().isEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Import::isSameModule(const Import &other) const
|
bool Import::isSameModule(const Import &other) const
|
||||||
{
|
{
|
||||||
if (isLibraryImport())
|
if (isLibraryImport())
|
||||||
@@ -107,4 +102,17 @@ size_t qHash(const Import &import)
|
|||||||
return ::qHash(import.url()) ^ ::qHash(import.file()) ^ ::qHash(import.version()) ^ ::qHash(import.alias());
|
return ::qHash(import.url()) ^ ::qHash(import.file()) ^ ::qHash(import.version()) ^ ::qHash(import.alias());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Imports difference(const Imports &first, const Imports &second)
|
||||||
|
{
|
||||||
|
Imports difference;
|
||||||
|
difference.reserve(first.size());
|
||||||
|
|
||||||
|
std::set_difference(first.begin(),
|
||||||
|
first.end(),
|
||||||
|
second.begin(),
|
||||||
|
second.end(),
|
||||||
|
std::back_inserter(difference));
|
||||||
|
|
||||||
|
return difference;
|
||||||
|
}
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
@@ -127,6 +127,8 @@ void ModelPrivate::changeImports(const Imports &toBeAddedImportList,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::sort(m_imports.begin(), m_imports.end());
|
||||||
|
|
||||||
if (!removedImportList.isEmpty() || !addedImportList.isEmpty())
|
if (!removedImportList.isEmpty() || !addedImportList.isEmpty())
|
||||||
notifyImportsChanged(addedImportList, removedImportList);
|
notifyImportsChanged(addedImportList, removedImportList);
|
||||||
}
|
}
|
||||||
@@ -1419,19 +1421,23 @@ void Model::changeImports(const Imports &importsToBeAdded, const Imports &import
|
|||||||
d->changeImports(importsToBeAdded, importsToBeRemoved);
|
d->changeImports(importsToBeAdded, importsToBeRemoved);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::setPossibleImports(const Imports &possibleImports)
|
void Model::setPossibleImports(Imports possibleImports)
|
||||||
{
|
{
|
||||||
|
std::sort(possibleImports.begin(), possibleImports.end());
|
||||||
|
|
||||||
if (d->m_possibleImportList != possibleImports) {
|
if (d->m_possibleImportList != possibleImports) {
|
||||||
d->m_possibleImportList = possibleImports;
|
d->m_possibleImportList = std::move(possibleImports);
|
||||||
d->notifyPossibleImportsChanged(possibleImports);
|
d->notifyPossibleImportsChanged(d->m_possibleImportList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Model::setUsedImports(const Imports &usedImports)
|
void Model::setUsedImports(Imports usedImports)
|
||||||
{
|
{
|
||||||
|
std::sort(usedImports.begin(), usedImports.end());
|
||||||
|
|
||||||
if (d->m_usedImportList != usedImports) {
|
if (d->m_usedImportList != usedImports) {
|
||||||
d->m_usedImportList = usedImports;
|
d->m_usedImportList = std::move(usedImports);
|
||||||
d->notifyUsedImportsChanged(usedImports);
|
d->notifyUsedImportsChanged(d->m_usedImportList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -203,8 +203,6 @@ public:
|
|||||||
|
|
||||||
// Imports:
|
// Imports:
|
||||||
const Imports &imports() const { return m_imports; }
|
const Imports &imports() const { return m_imports; }
|
||||||
void addImport(const Import &import);
|
|
||||||
void removeImport(const Import &import);
|
|
||||||
void changeImports(const Imports &importsToBeAdded, const Imports &importToBeRemoved);
|
void changeImports(const Imports &importsToBeAdded, const Imports &importToBeRemoved);
|
||||||
void notifyImportsChanged(const Imports &addedImports, const Imports &removedImports);
|
void notifyImportsChanged(const Imports &addedImports, const Imports &removedImports);
|
||||||
void notifyPossibleImportsChanged(const Imports &possibleImports);
|
void notifyPossibleImportsChanged(const Imports &possibleImports);
|
||||||
|
Reference in New Issue
Block a user