diff options
author | Eike Ziller <eike.ziller@qt.io> | 2022-09-21 10:59:59 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2022-09-21 11:16:29 +0200 |
commit | 3011548066174e964dad6617e37792679941a2d6 (patch) | |
tree | d9edc436c9d4b4b39bacc50ded2f9c23b9a874b0 /src/plugins/qmldesigner/components | |
parent | d696f3380cb7191b2b6239894b9c6c4b4a0fc043 (diff) | |
parent | 5ebb467cc622a9dc91ff4ec0f02de257f19d69a0 (diff) | |
download | qt-creator-3011548066174e964dad6617e37792679941a2d6.tar.gz |
Merge remote-tracking branch 'origin/8.0'
Conflicts:
src/libs/utils/deviceshell.cpp
src/plugins/clangcodemodel/clangdclient.cpp
src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.cpp
src/plugins/remotelinux/linuxdevice.cpp
Change-Id: Idc2edf428c98e9cbc891f1833b271ce2def9a835
Diffstat (limited to 'src/plugins/qmldesigner/components')
18 files changed, 913 insertions, 49 deletions
diff --git a/src/plugins/qmldesigner/components/edit3d/edit3dview.cpp b/src/plugins/qmldesigner/components/edit3d/edit3dview.cpp index f0789bd6d9..d9a0ad2824 100644 --- a/src/plugins/qmldesigner/components/edit3d/edit3dview.cpp +++ b/src/plugins/qmldesigner/components/edit3d/edit3dview.cpp @@ -1,15 +1,16 @@ // Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 +#include "edit3dview.h" +#include "backgroundcolorselection.h" #include "edit3dactions.h" #include "edit3dcanvas.h" -#include "edit3dview.h" -#include "edit3dwidget.h" #include "edit3dviewconfig.h" -#include "backgroundcolorselection.h" +#include "edit3dwidget.h" #include "metainfo.h" -#include "seekerslider.h" #include "nodehints.h" +#include "seekerslider.h" +#include "view3dactioncommand.h" #include <auxiliarydataproperties.h> #include <coreplugin/icore.h> @@ -287,6 +288,8 @@ void Edit3DView::nodeAtPosReady(const ModelNode &modelNode, const QVector3D &pos assignMaterialTo3dModel(modelNode, m_droppedMaterial); }); } + } else if (m_nodeAtPosReqType == NodeAtPosReqType::BundleMaterialDrop) { + emitCustomNotification("drop_bundle_material", {modelNode}); // To MaterialBrowserView } m_nodeAtPosReqType = NodeAtPosReqType::None; } @@ -837,4 +840,11 @@ void Edit3DView::dropMaterial(const ModelNode &matNode, const QPointF &pos) emitView3DAction(View3DActionType::GetNodeAtPos, pos); } +void Edit3DView::dropBundleMaterial(const QPointF &pos) +{ + m_nodeAtPosReqType = NodeAtPosReqType::BundleMaterialDrop; + QmlDesignerPlugin::instance()->viewManager().nodeInstanceView()->view3DAction( + View3DActionType::GetNodeAtPos, pos); +} + } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/edit3d/edit3dview.h b/src/plugins/qmldesigner/components/edit3d/edit3dview.h index bd866098e5..94c88ce5a2 100644 --- a/src/plugins/qmldesigner/components/edit3d/edit3dview.h +++ b/src/plugins/qmldesigner/components/edit3d/edit3dview.h @@ -58,12 +58,14 @@ public: void addQuick3DImport(); void startContextMenu(const QPoint &pos); void dropMaterial(const ModelNode &matNode, const QPointF &pos); + void dropBundleMaterial(const QPointF &pos); private slots: void onEntriesChanged(); private: enum class NodeAtPosReqType { + BundleMaterialDrop, MaterialDrop, ContextMenu, None diff --git a/src/plugins/qmldesigner/components/edit3d/edit3dwidget.cpp b/src/plugins/qmldesigner/components/edit3d/edit3dwidget.cpp index 2c497a425f..2f02c1c620 100644 --- a/src/plugins/qmldesigner/components/edit3d/edit3dwidget.cpp +++ b/src/plugins/qmldesigner/components/edit3d/edit3dwidget.cpp @@ -309,7 +309,8 @@ void Edit3DWidget::dragEnterEvent(QDragEnterEvent *dragEnterEvent) const DesignerActionManager &actionManager = QmlDesignerPlugin::instance() ->viewManager().designerActionManager(); if (actionManager.externalDragHasSupportedAssets(dragEnterEvent->mimeData()) - || dragEnterEvent->mimeData()->hasFormat(Constants::MIME_TYPE_MATERIAL)) { + || dragEnterEvent->mimeData()->hasFormat(Constants::MIME_TYPE_MATERIAL) + || dragEnterEvent->mimeData()->hasFormat(Constants::MIME_TYPE_BUNDLE_MATERIAL)) { dragEnterEvent->acceptProposedAction(); } } @@ -329,6 +330,12 @@ void Edit3DWidget::dropEvent(QDropEvent *dropEvent) return; } + // handle dropping bundle materials + if (dropEvent->mimeData()->hasFormat(Constants::MIME_TYPE_BUNDLE_MATERIAL)) { + m_view->dropBundleMaterial(dropEvent->position()); + return; + } + // handle dropping external assets const DesignerActionManager &actionManager = QmlDesignerPlugin::instance() ->viewManager().designerActionManager(); diff --git a/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.cpp b/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.cpp index 357be8db45..422288c8b2 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.cpp +++ b/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.cpp @@ -33,9 +33,9 @@ #include <qmljs/qmljsmodelmanagerinterface.h> -#include <QFile> #include <QFileInfo> -#include <QSaveFile> +#include <QJsonDocument> +#include <QJsonObject> #include <QStringList> using namespace Utils; @@ -65,20 +65,14 @@ BundleImporter::BundleImporter(const QString &bundleDir, QString BundleImporter::importComponent(const QString &qmlFile, const QStringList &files) { - FilePath bundleImportPath = QmlDesignerPlugin::instance()->documentManager().currentProjectDirPath(); + FilePath bundleImportPath = resolveBundleImportPath(); if (bundleImportPath.isEmpty()) - return "Failed to resolve current project path"; + return "Failed to resolve bundle import folder"; - const QString projectBundlePath = QStringLiteral("%1%2/%3").arg( - QLatin1String(Constants::DEFAULT_ASSET_IMPORT_FOLDER), - QLatin1String(Constants::COMPONENT_BUNDLES_FOLDER), - m_bundleId).mid(1); // Chop leading slash - bundleImportPath = bundleImportPath.resolvePath(projectBundlePath); + bool bundleImportPathExists = bundleImportPath.exists(); - if (!bundleImportPath.exists()) { - if (!bundleImportPath.createDir()) - return QStringLiteral("Failed to create bundle import folder: '%1'").arg(bundleImportPath.toString()); - } + if (!bundleImportPathExists && !bundleImportPath.createDir()) + return QStringLiteral("Failed to create bundle import folder: '%1'").arg(bundleImportPath.toString()); for (const QString &file : qAsConst(m_sharedFiles)) { FilePath target = bundleImportPath.resolvePath(file); @@ -93,37 +87,24 @@ QString BundleImporter::importComponent(const QString &qmlFile, } FilePath qmldirPath = bundleImportPath.resolvePath(QStringLiteral("qmldir")); - QFile qmldirFile(qmldirPath.toString()); - - QString qmldirContent; - if (qmldirPath.exists()) { - if (!qmldirFile.open(QIODeviceBase::ReadOnly)) - return QStringLiteral("Failed to open qmldir file for reading: '%1'").arg(qmldirPath.toString()); - qmldirContent = QString::fromUtf8(qmldirFile.readAll()); - qmldirFile.close(); - } else { + QString qmldirContent = QString::fromUtf8(qmldirPath.fileContents().value_or(QByteArray())); + if (qmldirContent.isEmpty()) { qmldirContent.append("module "); qmldirContent.append(m_moduleName); qmldirContent.append('\n'); } - FilePath qmlSourceFile = FilePath::fromString(qmlFile); + FilePath qmlSourceFile = bundleImportPath.resolvePath(FilePath::fromString(qmlFile)); const bool qmlFileExists = qmlSourceFile.exists(); const QString qmlType = qmlSourceFile.baseName(); m_pendingTypes.append(QStringLiteral("%1.%2") .arg(QLatin1String(Constants::COMPONENT_BUNDLES_FOLDER).mid(1), qmlType)); if (!qmldirContent.contains(qmlFile)) { - QSaveFile qmldirSaveFile(qmldirPath.toString()); - if (!qmldirSaveFile.open(QIODeviceBase::WriteOnly | QIODeviceBase::Truncate)) - return QStringLiteral("Failed to open qmldir file for writing: '%1'").arg(qmldirPath.toString()); - qmldirContent.append(qmlType); qmldirContent.append(" 1.0 "); qmldirContent.append(qmlFile); qmldirContent.append('\n'); - - qmldirSaveFile.write(qmldirContent.toUtf8()); - qmldirSaveFile.commit(); + qmldirPath.writeFileContents(qmldirContent.toUtf8()); } QStringList allFiles; @@ -145,6 +126,19 @@ QString BundleImporter::importComponent(const QString &qmlFile, return QStringLiteral("Failed to copy file: '%1'").arg(source.toString()); } + QVariantHash assetRefMap = loadAssetRefMap(bundleImportPath); + bool writeAssetRefs = false; + for (const QString &assetFile : files) { + QStringList assets = assetRefMap[assetFile].toStringList(); + if (!assets.contains(qmlFile)) { + assets.append(qmlFile); + writeAssetRefs = true; + } + assetRefMap[assetFile] = assets; + } + if (writeAssetRefs) + writeAssetRefMap(bundleImportPath, assetRefMap); + m_fullReset = !qmlFileExists; auto doc = QmlDesignerPlugin::instance()->currentDesignDocument(); Model *model = doc ? doc->currentModel() : nullptr; @@ -163,10 +157,12 @@ QString BundleImporter::importComponent(const QString &qmlFile, } } else { // If import is not yet possible, import statement needs to be added asynchronously to - // avoid errors, as code model update takes a while. Full reset is not necessary - // in this case, as new import directory appearing will trigger scanning of it. + // avoid errors, as code model update takes a while. m_importAddPending = true; - m_fullReset = false; + + // Full reset is not necessary if new import directory appearing will trigger scanning, + // but if directory existed but was not valid possible import, we need to do a reset. + m_fullReset = bundleImportPathExists; } } m_importTimerCount = 0; @@ -232,4 +228,114 @@ void BundleImporter::handleImportTimer() } } +QVariantHash BundleImporter::loadAssetRefMap(const Utils::FilePath &bundlePath) +{ + FilePath assetRefPath = bundlePath.resolvePath(QLatin1String(Constants::COMPONENT_BUNDLES_ASSET_REF_FILE)); + const std::optional<QByteArray> content = assetRefPath.fileContents(); + if (content) { + QJsonParseError error; + QJsonDocument bundleDataJsonDoc = QJsonDocument::fromJson(*content, &error); + if (bundleDataJsonDoc.isNull()) { + // Failure to read asset refs is not considred fatal, so just print error + qWarning() << "Failed to parse bundle asset ref file:" << error.errorString(); + } else { + return bundleDataJsonDoc.object().toVariantHash(); + } + } + return {}; +} + +void BundleImporter::writeAssetRefMap(const Utils::FilePath &bundlePath, + const QVariantHash &assetRefMap) +{ + FilePath assetRefPath = bundlePath.resolvePath(QLatin1String(Constants::COMPONENT_BUNDLES_ASSET_REF_FILE)); + QJsonObject jsonObj = QJsonObject::fromVariantHash(assetRefMap); + if (!assetRefPath.writeFileContents(QJsonDocument{jsonObj}.toJson())) { + // Failure to write asset refs is not considred fatal, so just print error + qWarning() << QStringLiteral("Failed to save bundle asset ref file: '%1'").arg(assetRefPath.toString()) ; + } +} + +QString BundleImporter::unimportComponent(const QString &qmlFile) +{ + FilePath bundleImportPath = resolveBundleImportPath(); + if (bundleImportPath.isEmpty()) + return "Failed to resolve bundle import folder"; + + if (!bundleImportPath.exists()) + return {}; + + FilePath qmlFilePath = bundleImportPath.resolvePath(qmlFile); + if (!qmlFilePath.exists()) + return {}; + + QStringList removedFiles; + removedFiles.append(qmlFile); + + FilePath qmldirPath = bundleImportPath.resolvePath(QStringLiteral("qmldir")); + const std::optional<QByteArray> qmldirContent = qmldirPath.fileContents(); + QByteArray newContent; + if (qmldirContent) { + QByteArray qmlType = qmlFilePath.baseName().toUtf8(); + int typeIndex = qmldirContent->indexOf(qmlType); + if (typeIndex != -1) { + int newLineIndex = qmldirContent->indexOf('\n', typeIndex); + newContent = qmldirContent->left(typeIndex); + if (newLineIndex != -1) + newContent.append(qmldirContent->mid(newLineIndex + 1)); + } + if (newContent != qmldirContent) { + if (!qmldirPath.writeFileContents(newContent)) + return QStringLiteral("Failed to write qmldir file: '%1'").arg(qmldirPath.toString()); + } + } + + QVariantHash assetRefMap = loadAssetRefMap(bundleImportPath); + bool writeAssetRefs = false; + const auto keys = assetRefMap.keys(); + for (const QString &assetFile : keys) { + QStringList assets = assetRefMap[assetFile].toStringList(); + if (assets.contains(qmlFile)) { + assets.removeAll(qmlFile); + writeAssetRefs = true; + } + if (!assets.isEmpty()) { + assetRefMap[assetFile] = assets; + } else { + removedFiles.append(assetFile); + assetRefMap.remove(assetFile); + writeAssetRefs = true; + } + } + + for (const QString &removedFile : removedFiles) { + FilePath removedFilePath = bundleImportPath.resolvePath(removedFile); + if (removedFilePath.exists()) + removedFilePath.removeFile(); + } + + if (writeAssetRefs) + writeAssetRefMap(bundleImportPath, assetRefMap); + + m_fullReset = true; + m_importTimerCount = 0; + m_importTimer.start(); + + return {}; +} + +FilePath BundleImporter::resolveBundleImportPath() +{ + FilePath bundleImportPath = QmlDesignerPlugin::instance()->documentManager().currentProjectDirPath(); + if (bundleImportPath.isEmpty()) + return bundleImportPath; + + const QString projectBundlePath = QStringLiteral("%1%2/%3").arg( + QLatin1String(Constants::DEFAULT_ASSET_IMPORT_FOLDER), + QLatin1String(Constants::COMPONENT_BUNDLES_FOLDER), + m_bundleId).mid(1); // Chop leading slash + + return bundleImportPath.resolvePath(projectBundlePath); +} + } // namespace QmlDesigner::Internal diff --git a/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.h b/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.h index 840c4c672f..b22d0edd59 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.h +++ b/src/plugins/qmldesigner/components/materialbrowser/bundleimporter.h @@ -30,6 +30,7 @@ #include "nodemetainfo.h" #include <QTimer> +#include <QVariantHash> QT_BEGIN_NAMESPACE QT_END_NAMESPACE @@ -49,6 +50,8 @@ public: QString importComponent(const QString &qmlFile, const QStringList &files); + QString unimportComponent(const QString &qmlFile); + signals: // The metaInfo parameter will be invalid if an error was encountered during // asynchronous part of the import. In this case all remaining pending imports have been @@ -57,6 +60,9 @@ signals: private: void handleImportTimer(); + QVariantHash loadAssetRefMap(const Utils::FilePath &bundlePath); + void writeAssetRefMap(const Utils::FilePath &bundlePath, const QVariantHash &assetRefMap); + Utils::FilePath resolveBundleImportPath(); Utils::FilePath m_bundleDir; QString m_bundleId; diff --git a/src/plugins/qmldesigner/components/materialbrowser/bundlematerial.cpp b/src/plugins/qmldesigner/components/materialbrowser/bundlematerial.cpp new file mode 100644 index 0000000000..75549945f4 --- /dev/null +++ b/src/plugins/qmldesigner/components/materialbrowser/bundlematerial.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "bundlematerial.h" + +namespace QmlDesigner { + +BundleMaterial::BundleMaterial(QObject *parent, + const QString &name, + const QString &qml, + const QUrl &icon, + const QStringList &files) + : QObject(parent), m_name(name), m_qml(qml), m_icon(icon), m_files(files) {} + +bool BundleMaterial::filter(const QString &searchText) +{ + if (m_visible != m_name.contains(searchText, Qt::CaseInsensitive)) { + m_visible = !m_visible; + emit materialVisibleChanged(); + } + + return m_visible; +} + +QUrl BundleMaterial::icon() const +{ + return m_icon; +} + +QString BundleMaterial::qml() const +{ + return m_qml; +} + +QStringList BundleMaterial::files() const +{ + return m_files; +} + +bool BundleMaterial::visible() const +{ + return m_visible; +} + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/bundlematerial.h b/src/plugins/qmldesigner/components/materialbrowser/bundlematerial.h new file mode 100644 index 0000000000..04f4ae2656 --- /dev/null +++ b/src/plugins/qmldesigner/components/materialbrowser/bundlematerial.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include <QDataStream> +#include <QObject> +#include <QUrl> + +namespace QmlDesigner { + +class BundleMaterial : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString bundleMaterialName MEMBER m_name CONSTANT) + Q_PROPERTY(QUrl bundleMaterialIcon MEMBER m_icon CONSTANT) + Q_PROPERTY(bool bundleMaterialVisible MEMBER m_visible NOTIFY materialVisibleChanged) + +public: + BundleMaterial(QObject *parent, + const QString &name, + const QString &qml, + const QUrl &icon, + const QStringList &files); + + bool filter(const QString &searchText); + + QUrl icon() const; + QString qml() const; + QStringList files() const; + bool visible() const; + +signals: + void materialVisibleChanged(); + +private: + QString m_name; + QString m_qml; + QUrl m_icon; + QStringList m_files; + + bool m_visible = true; +}; + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/bundlematerialcategory.cpp b/src/plugins/qmldesigner/components/materialbrowser/bundlematerialcategory.cpp new file mode 100644 index 0000000000..9a18ff7501 --- /dev/null +++ b/src/plugins/qmldesigner/components/materialbrowser/bundlematerialcategory.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "bundlematerialcategory.h" + +#include "bundlematerial.h" + +namespace QmlDesigner { + +BundleMaterialCategory::BundleMaterialCategory(QObject *parent, const QString &name) + : QObject(parent), m_name(name) {} + +void BundleMaterialCategory::addBundleMaterial(BundleMaterial *bundleMat) +{ + m_categoryMaterials.append(bundleMat); +} + +bool BundleMaterialCategory::filter(const QString &searchText) +{ + bool visible = false; + for (BundleMaterial *mat : std::as_const(m_categoryMaterials)) + visible |= mat->filter(searchText); + + if (visible != m_visible) { + m_visible = visible; + emit categoryVisibleChanged(); + return true; + } + + return false; +} + +QString BundleMaterialCategory::name() const +{ + return m_name; +} + +bool BundleMaterialCategory::visible() const +{ + return m_visible; +} + +QList<BundleMaterial *> BundleMaterialCategory::categoryMaterials() const +{ + return m_categoryMaterials; +} + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/bundlematerialcategory.h b/src/plugins/qmldesigner/components/materialbrowser/bundlematerialcategory.h new file mode 100644 index 0000000000..0a3a0f7c02 --- /dev/null +++ b/src/plugins/qmldesigner/components/materialbrowser/bundlematerialcategory.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include <QObject> + +namespace QmlDesigner { + +class BundleMaterial; + +class BundleMaterialCategory : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString categoryName MEMBER m_name CONSTANT) + Q_PROPERTY(bool categoryVisible MEMBER m_visible NOTIFY categoryVisibleChanged) + +public: + BundleMaterialCategory(QObject *parent, const QString &name); + + void addBundleMaterial(BundleMaterial *bundleMat); + bool filter(const QString &searchText); + + QString name() const; + bool visible() const; + QList<BundleMaterial *> categoryMaterials() const; + +signals: + void categoryVisibleChanged(); + +private: + QString m_name; + bool m_visible = true; + + QList<BundleMaterial *> m_categoryMaterials; +}; + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.cpp b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.cpp new file mode 100644 index 0000000000..1632fc14fa --- /dev/null +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.cpp @@ -0,0 +1,229 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#include "materialbrowserbundlemodel.h" + +#include "bundleimporter.h" +#include "bundlematerial.h" +#include "bundlematerialcategory.h" +#include "utils/qtcassert.h" + +#include <QCoreApplication> +#include <QJsonArray> +#include <QJsonDocument> +#include <QUrl> + +namespace QmlDesigner { + +MaterialBrowserBundleModel::MaterialBrowserBundleModel(QObject *parent) + : QAbstractListModel(parent) +{ + loadMaterialBundle(); +} + +int MaterialBrowserBundleModel::rowCount(const QModelIndex &) const +{ + return m_bundleCategories.size(); +} + +QVariant MaterialBrowserBundleModel::data(const QModelIndex &index, int role) const +{ + QTC_ASSERT(index.isValid() && index.row() < m_bundleCategories.count(), return {}); + QTC_ASSERT(roleNames().contains(role), return {}); + + QByteArray roleName = roleNames().value(role); + if (roleName == "bundleCategory") + return m_bundleCategories.at(index.row())->name(); + + if (roleName == "bundleCategoryVisible") + return m_bundleCategories.at(index.row())->visible(); + + if (roleName == "bundleMaterialsModel") + return QVariant::fromValue(m_bundleCategories.at(index.row())->categoryMaterials()); + + return {}; +} + +bool MaterialBrowserBundleModel::isValidIndex(int idx) const +{ + return idx > -1 && idx < rowCount(); +} + +QHash<int, QByteArray> MaterialBrowserBundleModel::roleNames() const +{ + static const QHash<int, QByteArray> roles { + {Qt::UserRole + 1, "bundleCategory"}, + {Qt::UserRole + 2, "bundleCategoryVisible"}, + {Qt::UserRole + 3, "bundleMaterialsModel"} + }; + return roles; +} + +void MaterialBrowserBundleModel::loadMaterialBundle() +{ + if (m_matBundleExists || m_probeMatBundleDir) + return; + + QDir matBundleDir(qEnvironmentVariable("MATERIAL_BUNDLE_PATH")); + + // search for matBundleDir from exec dir and up + if (matBundleDir.dirName() == ".") { + m_probeMatBundleDir = true; // probe only once + + matBundleDir.setPath(QCoreApplication::applicationDirPath()); + while (!matBundleDir.cd("material_bundle") && matBundleDir.cdUp()) + ; // do nothing + + if (matBundleDir.dirName() != "material_bundle") // bundlePathDir not found + return; + } + + QString matBundlePath = matBundleDir.filePath("material_bundle.json"); + + if (m_matBundleObj.isEmpty()) { + QFile matPropsFile(matBundlePath); + + if (!matPropsFile.open(QIODevice::ReadOnly)) { + qWarning("Couldn't open material_bundle.json"); + return; + } + + QJsonDocument matBundleJsonDoc = QJsonDocument::fromJson(matPropsFile.readAll()); + if (matBundleJsonDoc.isNull()) { + qWarning("Invalid material_bundle.json file"); + return; + } else { + m_matBundleObj = matBundleJsonDoc.object(); + } + } + + m_matBundleExists = true; + + const QJsonObject catsObj = m_matBundleObj.value("categories").toObject(); + const QStringList categories = catsObj.keys(); + for (const QString &cat : categories) { + auto category = new BundleMaterialCategory(this, cat); + + const QJsonObject matsObj = catsObj.value(cat).toObject(); + const QStringList mats = matsObj.keys(); + for (const QString &mat : mats) { + const QJsonObject matObj = matsObj.value(mat).toObject(); + + QStringList files; + const QJsonArray assetsArr = matObj.value("files").toArray(); + for (const QJsonValueRef &asset : assetsArr) + files.append(asset.toString()); + + auto bundleMat = new BundleMaterial(category, mat, matObj.value("qml").toString(), + QUrl::fromLocalFile(matBundleDir.filePath(matObj.value("icon").toString())), files); + + category->addBundleMaterial(bundleMat); + } + m_bundleCategories.append(category); + } + + QStringList sharedFiles; + const QJsonArray sharedFilesArr = m_matBundleObj.value("sharedFiles").toArray(); + for (const QJsonValueRef &file : sharedFilesArr) + sharedFiles.append(file.toString()); + + m_importer = new Internal::BundleImporter(matBundleDir.path(), "MaterialBundle", sharedFiles); + connect(m_importer, &Internal::BundleImporter::importFinished, this, [&](const QmlDesigner::NodeMetaInfo &metaInfo) { + if (metaInfo.isValid()) + emit addBundleMaterialToProjectRequested(metaInfo); + }); +} + +bool MaterialBrowserBundleModel::hasQuick3DImport() const +{ + return m_hasQuick3DImport; +} + +void MaterialBrowserBundleModel::setHasQuick3DImport(bool b) +{ + if (b == m_hasQuick3DImport) + return; + + m_hasQuick3DImport = b; + emit hasQuick3DImportChanged(); +} + +bool MaterialBrowserBundleModel::hasMaterialRoot() const +{ + return m_hasMaterialRoot; +} + +void MaterialBrowserBundleModel::setHasMaterialRoot(bool b) +{ + if (m_hasMaterialRoot == b) + return; + + m_hasMaterialRoot = b; + emit hasMaterialRootChanged(); +} + +void MaterialBrowserBundleModel::setSearchText(const QString &searchText) +{ + QString lowerSearchText = searchText.toLower(); + + if (m_searchText == lowerSearchText) + return; + + m_searchText = lowerSearchText; + + bool anyCatVisible = false; + bool catVisibilityChanged = false; + + for (BundleMaterialCategory *cat : std::as_const(m_bundleCategories)) { + catVisibilityChanged |= cat->filter(m_searchText); + anyCatVisible |= cat->visible(); + } + + if (anyCatVisible == m_isEmpty) { + m_isEmpty = !anyCatVisible; + emit isEmptyChanged(); + } + + if (catVisibilityChanged) + resetModel(); +} + +void MaterialBrowserBundleModel::resetModel() +{ + beginResetModel(); + endResetModel(); +} + +void MaterialBrowserBundleModel::applyToSelected(BundleMaterial *mat, bool add) +{ + emit applyToSelectedTriggered(mat, add); +} + +void MaterialBrowserBundleModel::addMaterial(BundleMaterial *mat) +{ + m_importer->importComponent(mat->qml(), mat->files()); +} + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.h b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.h new file mode 100644 index 0000000000..60b48f1c9b --- /dev/null +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include <modelnode.h> +#include <qmlobjectnode.h> + +#include <QAbstractListModel> +#include <QJsonObject> +#include <QObject> +#include <QPointer> + +namespace QmlDesigner { + +class BundleMaterial; +class BundleMaterialCategory; + +namespace Internal { +class BundleImporter; +} + +class MaterialBrowserBundleModel : public QAbstractListModel +{ + Q_OBJECT + + Q_PROPERTY(bool matBundleExists MEMBER m_matBundleExists CONSTANT) + Q_PROPERTY(bool isEmpty MEMBER m_isEmpty NOTIFY isEmptyChanged) + Q_PROPERTY(bool hasQuick3DImport READ hasQuick3DImport WRITE setHasQuick3DImport NOTIFY hasQuick3DImportChanged) + Q_PROPERTY(bool hasMaterialRoot READ hasMaterialRoot WRITE setHasMaterialRoot NOTIFY hasMaterialRootChanged) + +public: + MaterialBrowserBundleModel(QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QHash<int, QByteArray> roleNames() const override; + + void setSearchText(const QString &searchText); + + bool hasQuick3DImport() const; + void setHasQuick3DImport(bool b); + + bool hasMaterialRoot() const; + void setHasMaterialRoot(bool b); + + void resetModel(); + + Q_INVOKABLE void applyToSelected(QmlDesigner::BundleMaterial *mat, bool add = false); + Q_INVOKABLE void addMaterial(QmlDesigner::BundleMaterial *mat); + +signals: + void isEmptyChanged(); + void hasQuick3DImportChanged(); + void hasMaterialRootChanged(); + void materialVisibleChanged(); + void applyToSelectedTriggered(QmlDesigner::BundleMaterial *mat, bool add = false); + void addBundleMaterialToProjectRequested(const QmlDesigner::NodeMetaInfo &metaInfo); + +private: + void loadMaterialBundle(); + bool isValidIndex(int idx) const; + + QString m_searchText; + QList<BundleMaterialCategory *> m_bundleCategories; + QJsonObject m_matBundleObj; + Internal::BundleImporter *m_importer = nullptr; + + bool m_isEmpty = true; + bool m_hasQuick3DImport = false; + bool m_hasMaterialRoot = false; + bool m_matBundleExists = false; + bool m_probeMatBundleDir = false; +}; + +} // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowsermodel.cpp b/src/plugins/qmldesigner/components/materialbrowser/materialbrowsermodel.cpp index 8c0d5b345d..bbe47ec94c 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/materialbrowsermodel.cpp +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowsermodel.cpp @@ -213,6 +213,8 @@ void MaterialBrowserModel::setSearchText(const QString &searchText) isEmpty = !isValidIndex(m_selectedIndex + inc) && !isValidIndex(m_selectedIndex - inc); } + if (!isMaterialVisible(m_selectedIndex)) // handles the case of a single material + isEmpty = true; } if (isEmpty != m_isEmpty) { diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.cpp b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.cpp index c511e35488..aaf9391d38 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.cpp +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.cpp @@ -3,8 +3,10 @@ #include "materialbrowserview.h" #include "bindingproperty.h" +#include "bundlematerial.h" #include "materialbrowsermodel.h" #include "materialbrowserwidget.h" +#include "materialbrowserbundlemodel.h" #include "nodeabstractproperty.h" #include "nodemetainfo.h" #include "qmlobjectnode.h" @@ -14,9 +16,12 @@ #include <designmodecontext.h> #include <nodeinstanceview.h> #include <nodemetainfo.h> +#include <nodelistproperty.h> #include <qmldesignerconstants.h> #include <QQuickItem> +#include <QRegularExpression> +#include <QTimer> namespace QmlDesigner { @@ -93,6 +98,80 @@ WidgetInfo MaterialBrowserView::widgetInfo() } }); }); + + connect(m_widget, &MaterialBrowserWidget::bundleMaterialDragStarted, this, + [&] (QmlDesigner::BundleMaterial *bundleMat) { + m_draggedBundleMaterial = bundleMat; + }); + + MaterialBrowserBundleModel *matBrowserBundleModel = m_widget->materialBrowserBundleModel().data(); + + connect(matBrowserBundleModel, &MaterialBrowserBundleModel::applyToSelectedTriggered, this, + [&] (BundleMaterial *material, bool add) { + if (!m_selectedModel.isValid()) + return; + + m_bundleMaterialDropTarget = m_selectedModel; + m_bundleMaterialAddToSelected = add; + m_widget->materialBrowserBundleModel()->addMaterial(material); + }); + + connect(matBrowserBundleModel, &MaterialBrowserBundleModel::addBundleMaterialToProjectRequested, this, + [&] (const QmlDesigner::NodeMetaInfo &metaInfo) { + ModelNode matLib = materialLibraryNode(); + if (!matLib.isValid()) + return; + + executeInTransaction("MaterialBrowserView::widgetInfo", [&] { + ModelNode newMatNode = createModelNode(metaInfo.typeName(), metaInfo.majorVersion(), + metaInfo.minorVersion()); + matLib.defaultNodeListProperty().reparentHere(newMatNode); + + static QRegularExpression rgx("([A-Z])([a-z]*)"); + QString newName = QString::fromLatin1(metaInfo.simplifiedTypeName()).replace(rgx, " \\1\\2").trimmed(); + QString newId = model()->generateIdFromName(newName, "material"); + newMatNode.setIdWithRefactoring(newId); + + VariantProperty objNameProp = newMatNode.variantProperty("objectName"); + objNameProp.setValue(newName); + + if (m_bundleMaterialDropTarget.isValid()) { + QmlObjectNode qmlObjNode(m_bundleMaterialDropTarget); + if (m_bundleMaterialAddToSelected) { + // TODO: unify this logic as it exist elsewhere also + auto expToList = [](const QString &exp) { + QString copy = exp; + copy = copy.remove("[").remove("]"); + + QStringList tmp = copy.split(',', Qt::SkipEmptyParts); + for (QString &str : tmp) + str = str.trimmed(); + + return tmp; + }; + + auto listToExp = [](QStringList &stringList) { + if (stringList.size() > 1) + return QString("[" + stringList.join(",") + "]"); + + if (stringList.size() == 1) + return stringList.first(); + + return QString(); + }; + QStringList matList = expToList(qmlObjNode.expression("materials")); + matList.append(newMatNode.id()); + QString updatedExp = listToExp(matList); + qmlObjNode.setBindingProperty("materials", updatedExp); + } else { + qmlObjNode.setBindingProperty("materials", newMatNode.id()); + } + m_bundleMaterialDropTarget = {}; + } + + m_bundleMaterialAddToSelected = false; + }); + }); } return createWidgetInfo(m_widget.data(), @@ -162,24 +241,24 @@ void MaterialBrowserView::modelAboutToBeDetached(Model *model) void MaterialBrowserView::selectedNodesChanged(const QList<ModelNode> &selectedNodeList, [[maybe_unused]] const QList<ModelNode> &lastSelectedNodeList) { - ModelNode selectedModel; + m_selectedModel = {}; for (const ModelNode &node : selectedNodeList) { if (node.metaInfo().isQtQuick3DModel()) { - selectedModel = node; + m_selectedModel = node; break; } } - m_widget->materialBrowserModel()->setHasModelSelection(selectedModel.isValid()); + m_widget->materialBrowserModel()->setHasModelSelection(m_selectedModel.isValid()); if (!m_autoSelectModelMaterial) return; - if (selectedNodeList.size() > 1 || !selectedModel.isValid()) + if (selectedNodeList.size() > 1 || !m_selectedModel.isValid()) return; - QmlObjectNode qmlObjNode(selectedModel); + QmlObjectNode qmlObjNode(m_selectedModel); QString matExp = qmlObjNode.expression("materials"); if (matExp.isEmpty()) return; @@ -307,6 +386,10 @@ void MaterialBrowserView::customNotification(const AbstractView *view, }); } else if (identifier == "delete_selected_material") { m_widget->materialBrowserModel()->deleteSelectedMaterial(); + } else if (identifier == "drop_bundle_material") { + m_bundleMaterialDropTarget = nodeList.first(); + m_widget->materialBrowserBundleModel()->addMaterial(m_draggedBundleMaterial); + m_draggedBundleMaterial = nullptr; } } diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.h b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.h index 13bd3c36a3..6e5a45e246 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.h +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserview.h @@ -9,6 +9,7 @@ namespace QmlDesigner { +class BundleMaterial; class MaterialBrowserWidget; class MaterialBrowserView : public AbstractView @@ -43,13 +44,17 @@ public: private: void refreshModel(bool updateImages); bool isMaterial(const ModelNode &node) const; + void loadPropertyGroups(); QPointer<MaterialBrowserWidget> m_widget; + ModelNode m_bundleMaterialDropTarget; + ModelNode m_selectedModel; // first selected 3D model node + BundleMaterial *m_draggedBundleMaterial = nullptr; + bool m_bundleMaterialAddToSelected = false; bool m_hasQuick3DImport = false; bool m_autoSelectModelMaterial = false; // TODO: wire this to some action bool m_puppetResetPending = false; bool m_propertyGroupsLoaded = false; - void loadPropertyGroups(); }; } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.cpp b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.cpp index fe74692676..3f6f2f39f5 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.cpp +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.cpp @@ -2,25 +2,29 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #include "materialbrowserwidget.h" + +#include "bundlematerial.h" +#include "materialbrowserbundlemodel.h" #include "materialbrowsermodel.h" #include "materialbrowserview.h" -#include <theme.h> - #include <designeractionmanager.h> #include <designermcumanager.h> #include <documentmanager.h> #include <qmldesignerconstants.h> #include <qmldesignerplugin.h> +#include <theme.h> + #include <utils/algorithm.h> -#include <utils/stylehelper.h> #include <utils/qtcassert.h> +#include <utils/stylehelper.h> -#include <QImageReader> #include <QMenu> #include <QMimeData> #include <QMouseEvent> +#include <QJsonArray> +#include <QJsonDocument> #include <QQmlContext> #include <QQmlEngine> #include <QQuickImageProvider> @@ -91,7 +95,7 @@ bool MaterialBrowserWidget::eventFilter(QObject *obj, QEvent *event) if (m_materialToDrag.isValid()) { QMouseEvent *me = static_cast<QMouseEvent *>(event); - if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 10) { + if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 20) { QByteArray data; QMimeData *mimeData = new QMimeData; QDataStream stream(&data, QIODevice::WriteOnly); @@ -103,6 +107,17 @@ bool MaterialBrowserWidget::eventFilter(QObject *obj, QEvent *event) QString::number(m_materialToDrag.internalId()), nullptr, {128, 128})); m_materialToDrag = {}; } + } else if (m_bundleMaterialToDrag != nullptr) { + QMouseEvent *me = static_cast<QMouseEvent *>(event); + if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 20) { + QMimeData *mimeData = new QMimeData; + mimeData->setData(Constants::MIME_TYPE_BUNDLE_MATERIAL, {}); + mimeData->removeFormat("text/plain"); + + model->startDrag(mimeData, m_bundleMaterialToDrag->icon().toLocalFile()); + emit bundleMaterialDragStarted(m_bundleMaterialToDrag); + m_bundleMaterialToDrag = {}; + } } } @@ -112,6 +127,7 @@ bool MaterialBrowserWidget::eventFilter(QObject *obj, QEvent *event) MaterialBrowserWidget::MaterialBrowserWidget(MaterialBrowserView *view) : m_materialBrowserView(view) , m_materialBrowserModel(new MaterialBrowserModel(this)) + , m_materialBrowserBundleModel(new MaterialBrowserBundleModel(this)) , m_quickWidget(new QQuickWidget(this)) , m_previewImageProvider(new PreviewImageProvider()) { @@ -130,6 +146,7 @@ MaterialBrowserWidget::MaterialBrowserWidget(MaterialBrowserView *view) m_quickWidget->rootContext()->setContextProperties({ {"rootView", QVariant::fromValue(this)}, {"materialBrowserModel", QVariant::fromValue(m_materialBrowserModel.data())}, + {"materialBrowserBundleModel", QVariant::fromValue(m_materialBrowserBundleModel.data())}, }); m_quickWidget->engine()->addImageProvider("materialBrowser", m_previewImageProvider); @@ -189,6 +206,12 @@ void MaterialBrowserWidget::startDragMaterial(int index, const QPointF &mousePos m_dragStartPoint = mousePos.toPoint(); } +void MaterialBrowserWidget::startDragBundleMaterial(QmlDesigner::BundleMaterial *bundleMat, const QPointF &mousePos) +{ + m_bundleMaterialToDrag = bundleMat; + m_dragStartPoint = mousePos.toPoint(); +} + QString MaterialBrowserWidget::qmlSourcesPath() { #ifdef SHARE_QML_PATH @@ -216,6 +239,7 @@ void MaterialBrowserWidget::reloadQmlSource() void MaterialBrowserWidget::updateSearch() { m_materialBrowserModel->setSearchText(m_filterText); + m_materialBrowserBundleModel->setSearchText(m_filterText); m_quickWidget->update(); } @@ -229,4 +253,10 @@ QPointer<MaterialBrowserModel> MaterialBrowserWidget::materialBrowserModel() con return m_materialBrowserModel; } +QPointer<MaterialBrowserBundleModel> MaterialBrowserWidget::materialBrowserBundleModel() const +{ + return m_materialBrowserBundleModel; +} + + } // namespace QmlDesigner diff --git a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.h b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.h index 212265f002..1767286b49 100644 --- a/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.h +++ b/src/plugins/qmldesigner/components/materialbrowser/materialbrowserwidget.h @@ -11,6 +11,7 @@ #include <QFileIconProvider> #include <QFrame> +#include <QJsonObject> #include <QPointF> #include <QQmlPropertyMap> #include <QQuickWidget> @@ -26,8 +27,10 @@ QT_END_NAMESPACE namespace QmlDesigner { +class BundleMaterial; class MaterialBrowserView; class MaterialBrowserModel; +class MaterialBrowserBundleModel; class PreviewImageProvider; class MaterialBrowserWidget : public QFrame @@ -45,13 +48,18 @@ public: void clearSearchFilter(); QPointer<MaterialBrowserModel> materialBrowserModel() const; + QPointer<MaterialBrowserBundleModel> materialBrowserBundleModel() const; void updateMaterialPreview(const ModelNode &node, const QPixmap &pixmap); Q_INVOKABLE void handleSearchfilterChanged(const QString &filterText); Q_INVOKABLE void startDragMaterial(int index, const QPointF &mousePos); + Q_INVOKABLE void startDragBundleMaterial(QmlDesigner::BundleMaterial *bundleMat, const QPointF &mousePos); QQuickWidget *quickWidget() const; +signals: + void bundleMaterialDragStarted(QmlDesigner::BundleMaterial *bundleMat); + protected: bool eventFilter(QObject *obj, QEvent *event) override; @@ -61,6 +69,7 @@ private: QPointer<MaterialBrowserView> m_materialBrowserView; QPointer<MaterialBrowserModel> m_materialBrowserModel; + QPointer<MaterialBrowserBundleModel> m_materialBrowserBundleModel; QScopedPointer<QQuickWidget> m_quickWidget; QShortcut *m_qmlSourceUpdateShortcut = nullptr; @@ -70,6 +79,7 @@ private: QString m_filterText; ModelNode m_materialToDrag; + BundleMaterial *m_bundleMaterialToDrag = nullptr; QPoint m_dragStartPoint; }; diff --git a/src/plugins/qmldesigner/components/propertyeditor/dynamicpropertiesproxymodel.cpp b/src/plugins/qmldesigner/components/propertyeditor/dynamicpropertiesproxymodel.cpp index 9dd87e54c9..acc6744e2c 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/dynamicpropertiesproxymodel.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/dynamicpropertiesproxymodel.cpp @@ -35,6 +35,7 @@ #include <qmldesignerconstants.h> #include <qmldesignerplugin.h> +#include <coreplugin/messagebox.h> #include <utils/qtcassert.h> #include <QScopeGuard> @@ -150,6 +151,12 @@ void DynamicPropertiesProxyModel::createProperty(const QString &name, const QStr if (selectedNodes.count() == 1) { const ModelNode modelNode = selectedNodes.constFirst(); if (modelNode.isValid()) { + if (modelNode.hasProperty(name.toUtf8())) { + Core::AsynchronousMessageBox::warning(tr("Property already exists"), + tr("Property '%1' already exists") + .arg(name)); + return; + } try { if (Internal::DynamicPropertiesModel::isValueType(typeName)) { QVariant value = Internal::DynamicPropertiesModel::defaultValueForType(typeName); diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp index 188e4f7ed7..f56c3391c3 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp @@ -437,6 +437,9 @@ void PropertyEditorQmlBackend::setup(const QmlObjectNode &qmlObjectNode, const Q } ); + contextObject()->setHasMultiSelection( + !qmlObjectNode.view()->singleSelectedModelNode().isValid()); + qCInfo(propertyEditorBenchmark) << "anchors:" << time.elapsed(); qCInfo(propertyEditorBenchmark) << "context:" << time.elapsed(); |