2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
2015-09-18 11:34:48 +02:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
2016-01-15 14:57:40 +01:00
|
|
|
** 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.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-02-16 19:07:59 +02:00
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
#include "itemlibrarymodel.h"
|
2021-02-04 16:18:45 +02:00
|
|
|
#include "itemlibrarycategoriesmodel.h"
|
|
|
|
|
#include "itemlibraryimport.h"
|
|
|
|
|
#include "itemlibrarycategory.h"
|
2014-05-21 15:35:03 +02:00
|
|
|
#include "itemlibraryitem.h"
|
2021-02-04 16:18:45 +02:00
|
|
|
#include "itemlibraryinfo.h"
|
2014-05-21 12:34:36 +02:00
|
|
|
|
2021-02-22 22:10:53 +02:00
|
|
|
#include <designermcumanager.h>
|
2010-12-10 22:01:55 +01:00
|
|
|
#include <model.h>
|
2019-04-29 12:09:12 +02:00
|
|
|
#include <nodehints.h>
|
2010-12-10 22:01:55 +01:00
|
|
|
#include <nodemetainfo.h>
|
2021-02-22 22:10:53 +02:00
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
#include "qmldesignerplugin.h"
|
2016-06-14 15:04:14 +02:00
|
|
|
#include <utils/algorithm.h>
|
2020-03-30 15:23:46 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2016-06-14 15:04:14 +02:00
|
|
|
|
2021-02-17 23:26:45 +01:00
|
|
|
#include <QIODevice>
|
2016-07-27 11:30:35 +02:00
|
|
|
#include <QLoggingCategory>
|
2021-02-17 23:26:45 +01:00
|
|
|
#include <QMetaProperty>
|
2010-02-16 17:24:18 +02:00
|
|
|
#include <QMimeData>
|
2021-02-17 23:26:45 +01:00
|
|
|
#include <QVariant>
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
2021-02-13 01:27:20 +02:00
|
|
|
// sectionName can be an import url or a category name
|
|
|
|
|
void ItemLibraryModel::saveExpandedState(bool expanded, const QString §ionName)
|
2013-06-06 11:24:18 +02:00
|
|
|
{
|
2021-02-13 01:27:20 +02:00
|
|
|
expandedStateHash.insert(sectionName, expanded);
|
2021-02-04 16:18:45 +02:00
|
|
|
}
|
2013-06-06 11:24:18 +02:00
|
|
|
|
2021-02-13 01:27:20 +02:00
|
|
|
bool ItemLibraryModel::loadExpandedState(const QString §ionName)
|
2021-02-04 16:18:45 +02:00
|
|
|
{
|
2021-02-13 01:27:20 +02:00
|
|
|
return expandedStateHash.value(sectionName, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::expandAll()
|
|
|
|
|
{
|
2021-02-18 21:14:23 +02:00
|
|
|
int i = 0;
|
2021-02-13 01:27:20 +02:00
|
|
|
for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) {
|
2021-02-18 21:14:23 +02:00
|
|
|
if (!import->importExpanded()) {
|
2021-02-13 01:27:20 +02:00
|
|
|
import->setImportExpanded();
|
2021-02-18 21:14:23 +02:00
|
|
|
emit dataChanged(index(i), index(i), {m_roleNames.key("importExpanded")});
|
2021-02-13 01:27:20 +02:00
|
|
|
saveExpandedState(true, import->importUrl());
|
|
|
|
|
}
|
2021-02-18 21:14:23 +02:00
|
|
|
import->expandCategories(true);
|
|
|
|
|
++i;
|
2021-02-13 01:27:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::collapseAll()
|
|
|
|
|
{
|
2021-02-18 21:14:23 +02:00
|
|
|
int i = 0;
|
2021-02-13 01:27:20 +02:00
|
|
|
for (const QPointer<ItemLibraryImport> &import : std::as_const(m_importList)) {
|
|
|
|
|
if (import->hasCategories() && import->importExpanded()) {
|
|
|
|
|
import->setImportExpanded(false);
|
2021-02-18 21:14:23 +02:00
|
|
|
emit dataChanged(index(i), index(i), {m_roleNames.key("importExpanded")});
|
2021-02-13 01:27:20 +02:00
|
|
|
saveExpandedState(false, import->importUrl());
|
|
|
|
|
}
|
2021-02-18 21:14:23 +02:00
|
|
|
++i;
|
2021-02-13 01:27:20 +02:00
|
|
|
}
|
2013-06-06 11:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-29 12:28:30 +01:00
|
|
|
void ItemLibraryModel::setFlowMode(bool b)
|
|
|
|
|
{
|
|
|
|
|
m_flowMode = b;
|
|
|
|
|
bool changed;
|
|
|
|
|
updateVisibility(&changed);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibraryModel::ItemLibraryModel(QObject *parent)
|
2014-06-23 17:54:46 +02:00
|
|
|
: QAbstractListModel(parent)
|
2010-02-16 17:24:18 +02:00
|
|
|
{
|
2014-05-15 17:52:13 +02:00
|
|
|
addRoleNames();
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ItemLibraryModel::~ItemLibraryModel()
|
|
|
|
|
{
|
2014-05-15 17:52:13 +02:00
|
|
|
clearSections();
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-15 17:52:13 +02:00
|
|
|
int ItemLibraryModel::rowCount(const QModelIndex & /*parent*/) const
|
|
|
|
|
{
|
2021-02-04 16:18:45 +02:00
|
|
|
return m_importList.count();
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibraryModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
2021-02-13 01:27:20 +02:00
|
|
|
if (!index.isValid() || index.row() >= m_importList.count())
|
2021-02-04 16:18:45 +02:00
|
|
|
return {};
|
2014-05-15 17:52:13 +02:00
|
|
|
|
|
|
|
|
if (m_roleNames.contains(role)) {
|
2021-02-04 16:18:45 +02:00
|
|
|
QVariant value = m_importList.at(index.row())->property(m_roleNames.value(role));
|
2014-05-15 17:52:13 +02:00
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
auto model = qobject_cast<ItemLibraryCategoriesModel *>(value.value<QObject *>());
|
2014-05-15 17:52:13 +02:00
|
|
|
if (model)
|
|
|
|
|
return QVariant::fromValue(model);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "invalid role requested";
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
return {};
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2021-02-13 01:27:20 +02:00
|
|
|
bool ItemLibraryModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
|
{
|
|
|
|
|
// currently only importExpanded property is updatable
|
|
|
|
|
if (index.isValid() && m_roleNames.contains(role)) {
|
|
|
|
|
QVariant currValue = m_importList.at(index.row())->property(m_roleNames.value(role));
|
|
|
|
|
if (currValue != value) {
|
|
|
|
|
m_importList[index.row()]->setProperty(m_roleNames.value(role), value);
|
|
|
|
|
if (m_roleNames.value(role) == "importExpanded")
|
|
|
|
|
saveExpandedState(value.toBool(), m_importList[index.row()]->importUrl());
|
|
|
|
|
emit dataChanged(index, index, {role});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-28 17:33:47 +02:00
|
|
|
QHash<int, QByteArray> ItemLibraryModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
return m_roleNames;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
QString ItemLibraryModel::searchText() const
|
|
|
|
|
{
|
|
|
|
|
return m_searchText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::setSearchText(const QString &searchText)
|
|
|
|
|
{
|
|
|
|
|
QString lowerSearchText = searchText.toLower();
|
|
|
|
|
|
|
|
|
|
if (m_searchText != lowerSearchText) {
|
|
|
|
|
m_searchText = lowerSearchText;
|
|
|
|
|
|
2016-06-07 19:58:38 +02:00
|
|
|
bool changed = false;
|
2021-03-08 17:01:40 +02:00
|
|
|
updateVisibility(&changed, !m_searchText.isEmpty());
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 17:15:59 +02:00
|
|
|
Import ItemLibraryModel::entryToImport(const ItemLibraryEntry &entry)
|
2010-12-10 22:01:55 +01:00
|
|
|
{
|
2013-01-15 13:48:01 +01:00
|
|
|
if (entry.majorVersion() == -1 && entry.minorVersion() == -1)
|
|
|
|
|
return Import::createFileImport(entry.requiredImport());
|
|
|
|
|
|
2011-05-16 16:12:29 +02:00
|
|
|
return Import::createLibraryImport(entry.requiredImport(), QString::number(entry.majorVersion()) + QLatin1Char('.') +
|
|
|
|
|
QString::number(entry.minorVersion()));
|
|
|
|
|
|
2010-12-10 22:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-05 13:11:57 +02:00
|
|
|
// Returns true if first import version is higher or equal to second import version
|
|
|
|
|
static bool compareVersions(const QString &version1, const QString &version2)
|
|
|
|
|
{
|
|
|
|
|
if (version2.isEmpty() || version1 == version2)
|
|
|
|
|
return true;
|
|
|
|
|
const QStringList version1List = version1.split(QLatin1Char('.'));
|
|
|
|
|
const QStringList version2List = version2.split(QLatin1Char('.'));
|
|
|
|
|
if (version1List.count() == 2 && version2List.count() == 2) {
|
|
|
|
|
int major1 = version1List.constFirst().toInt();
|
|
|
|
|
int major2 = version2List.constFirst().toInt();
|
|
|
|
|
if (major1 > major2) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (major1 == major2) {
|
|
|
|
|
int minor1 = version1List.constLast().toInt();
|
|
|
|
|
int minor2 = version2List.constLast().toInt();
|
|
|
|
|
if (minor1 >= minor2)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-10 22:01:55 +01:00
|
|
|
void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
|
2010-02-16 17:24:18 +02:00
|
|
|
{
|
2013-02-12 14:42:14 +01:00
|
|
|
if (!model)
|
|
|
|
|
return;
|
|
|
|
|
|
2016-06-07 19:58:38 +02:00
|
|
|
beginResetModel();
|
2014-05-15 17:52:13 +02:00
|
|
|
clearSections();
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2021-02-22 22:10:53 +02:00
|
|
|
Utils::FilePath qmlFileName = QmlDesignerPlugin::instance()->currentDesignDocument()->fileName();
|
|
|
|
|
ProjectExplorer::Project *project = ProjectExplorer::SessionManager::projectForFile(qmlFileName);
|
|
|
|
|
QString projectName = project ? project->displayName() : "";
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
// create import sections
|
2021-03-05 13:11:57 +02:00
|
|
|
QHash<QString, ItemLibraryImport *> importHash;
|
2021-02-04 16:18:45 +02:00
|
|
|
for (const Import &import : model->imports()) {
|
2021-02-22 22:10:53 +02:00
|
|
|
if (import.isLibraryImport() && import.url() != projectName) {
|
2021-03-05 13:11:57 +02:00
|
|
|
bool addNew = true;
|
2021-03-11 22:49:39 +02:00
|
|
|
bool isQuick3DAsset = import.url().startsWith("Quick3DAssets.");
|
|
|
|
|
QString importUrl = isQuick3DAsset ? ItemLibraryImport::quick3DAssetsTitle() : import.url();
|
|
|
|
|
ItemLibraryImport *oldImport = importHash.value(importUrl);
|
|
|
|
|
if (oldImport && oldImport->sectionType() == ItemLibraryImport::SectionType::Quick3DAssets
|
|
|
|
|
&& isQuick3DAsset) {
|
|
|
|
|
addNew = false; // add only 1 Quick3DAssets import section
|
|
|
|
|
} else if (oldImport && oldImport->importEntry().url() == import.url()) {
|
2021-03-05 13:11:57 +02:00
|
|
|
// Retain the higher version if multiples exist
|
|
|
|
|
if (compareVersions(oldImport->importEntry().version(), import.version()))
|
|
|
|
|
addNew = false;
|
|
|
|
|
else
|
|
|
|
|
delete oldImport;
|
|
|
|
|
}
|
2021-03-11 22:49:39 +02:00
|
|
|
|
2021-03-05 13:11:57 +02:00
|
|
|
if (addNew) {
|
2021-03-11 22:49:39 +02:00
|
|
|
auto sectionType = isQuick3DAsset ? ItemLibraryImport::SectionType::Quick3DAssets
|
|
|
|
|
: ItemLibraryImport::SectionType::Default;
|
|
|
|
|
ItemLibraryImport *itemLibImport = new ItemLibraryImport(import, this, sectionType);
|
|
|
|
|
importHash.insert(importUrl, itemLibImport);
|
2021-03-05 13:11:57 +02:00
|
|
|
}
|
2021-02-04 16:18:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-07-27 11:30:35 +02:00
|
|
|
|
2021-03-05 13:11:57 +02:00
|
|
|
for (const auto itemLibImport : qAsConst(importHash)) {
|
|
|
|
|
m_importList.append(itemLibImport);
|
2021-03-11 22:49:39 +02:00
|
|
|
itemLibImport->setImportExpanded(loadExpandedState(itemLibImport->importUrl()));
|
2021-03-05 13:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
const QList<ItemLibraryEntry> itemLibEntries = itemLibraryInfo->entries();
|
|
|
|
|
for (const ItemLibraryEntry &entry : itemLibEntries) {
|
2016-07-27 11:30:35 +02:00
|
|
|
NodeMetaInfo metaInfo = model->metaInfo(entry.typeName());
|
|
|
|
|
|
|
|
|
|
bool valid = metaInfo.isValid() && metaInfo.majorVersion() == entry.majorVersion();
|
|
|
|
|
bool isItem = valid && metaInfo.isSubclassOf("QtQuick.Item");
|
2021-03-02 17:15:59 +02:00
|
|
|
bool forceVisibility = valid && NodeHints::fromItemLibraryEntry(entry).visibleInLibrary();
|
2019-04-29 12:09:12 +02:00
|
|
|
|
2020-04-07 18:31:24 +02:00
|
|
|
if (m_flowMode && metaInfo.isValid()) {
|
|
|
|
|
isItem = metaInfo.isSubclassOf("FlowView.FlowItem")
|
|
|
|
|
|| metaInfo.isSubclassOf("FlowView.FlowWildcard")
|
|
|
|
|
|| metaInfo.isSubclassOf("FlowView.FlowDecision");
|
2021-03-02 17:15:59 +02:00
|
|
|
forceVisibility = isItem;
|
2020-01-29 12:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 17:15:59 +02:00
|
|
|
bool blocked = false;
|
2020-09-28 17:33:40 +02:00
|
|
|
const DesignerMcuManager &mcuManager = DesignerMcuManager::instance();
|
|
|
|
|
if (mcuManager.isMCUProject()) {
|
|
|
|
|
const QSet<QString> blockTypes = mcuManager.bannedItems();
|
|
|
|
|
|
|
|
|
|
if (blockTypes.contains(QString::fromUtf8(entry.typeName())))
|
2021-03-02 17:15:59 +02:00
|
|
|
blocked = true;
|
2020-08-31 20:03:34 +02:00
|
|
|
}
|
2020-01-29 12:28:30 +01:00
|
|
|
|
2021-03-02 17:15:59 +02:00
|
|
|
Import import = entryToImport(entry);
|
|
|
|
|
bool hasImport = model->hasImport(import, true, true);
|
|
|
|
|
bool isImportPossible = false;
|
|
|
|
|
if (!hasImport)
|
|
|
|
|
isImportPossible = model->isImportPossible(import, true, true);
|
|
|
|
|
bool isUsable = (valid && (isItem || forceVisibility))
|
|
|
|
|
&& (entry.requiredImport().isEmpty() || hasImport);
|
|
|
|
|
if (!blocked && (isUsable || isImportPossible)) {
|
2021-02-04 16:18:45 +02:00
|
|
|
ItemLibraryImport *importSection = nullptr;
|
|
|
|
|
QString catName = entry.category();
|
2021-03-02 17:15:59 +02:00
|
|
|
if (isUsable) {
|
|
|
|
|
if (catName == ItemLibraryImport::userComponentsTitle()) {
|
|
|
|
|
// create an import section for user components
|
|
|
|
|
importSection = importByUrl(ItemLibraryImport::userComponentsTitle());
|
|
|
|
|
if (!importSection) {
|
|
|
|
|
importSection = new ItemLibraryImport(
|
|
|
|
|
{}, this, ItemLibraryImport::SectionType::User);
|
|
|
|
|
m_importList.append(importSection);
|
|
|
|
|
importSection->setImportExpanded(loadExpandedState(catName));
|
|
|
|
|
}
|
2021-03-11 22:49:39 +02:00
|
|
|
} else if (catName == "My Quick3D Components") {
|
|
|
|
|
importSection = importByUrl(ItemLibraryImport::quick3DAssetsTitle());
|
2021-03-02 17:15:59 +02:00
|
|
|
} else {
|
|
|
|
|
if (catName.startsWith("Qt Quick - "))
|
|
|
|
|
catName = catName.mid(11); // remove "Qt Quick - "
|
|
|
|
|
importSection = importByUrl(entry.requiredImport());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
catName = ItemLibraryImport::unimportedComponentsTitle();
|
|
|
|
|
importSection = importByUrl(catName);
|
2021-02-04 16:18:45 +02:00
|
|
|
if (!importSection) {
|
2021-03-02 17:15:59 +02:00
|
|
|
importSection = new ItemLibraryImport(
|
|
|
|
|
{}, this, ItemLibraryImport::SectionType::Unimported);
|
2021-02-04 16:18:45 +02:00
|
|
|
m_importList.append(importSection);
|
2021-02-13 01:27:20 +02:00
|
|
|
importSection->setImportExpanded(loadExpandedState(catName));
|
2021-02-04 16:18:45 +02:00
|
|
|
}
|
2010-12-10 22:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 17:15:59 +02:00
|
|
|
if (!importSection) {
|
2021-02-04 16:18:45 +02:00
|
|
|
qWarning() << __FUNCTION__ << "No import section found! skipping entry: " << entry.name();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get or create category section
|
|
|
|
|
ItemLibraryCategory *categorySection = importSection->getCategorySection(catName);
|
|
|
|
|
if (!categorySection) {
|
|
|
|
|
categorySection = new ItemLibraryCategory(catName, importSection);
|
|
|
|
|
importSection->addCategory(categorySection);
|
2021-03-02 17:15:59 +02:00
|
|
|
if (importSection->sectionType() == ItemLibraryImport::SectionType::Default)
|
2021-02-13 01:27:20 +02:00
|
|
|
categorySection->setExpanded(loadExpandedState(categorySection->categoryName()));
|
2021-02-04 16:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create item
|
2021-03-02 17:15:59 +02:00
|
|
|
auto item = new ItemLibraryItem(entry, isUsable, categorySection);
|
2021-02-04 16:18:45 +02:00
|
|
|
categorySection->addItem(item);
|
2010-12-10 22:01:55 +01:00
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2014-07-03 12:00:15 +02:00
|
|
|
sortSections();
|
2016-06-07 19:58:38 +02:00
|
|
|
bool changed = false;
|
|
|
|
|
updateVisibility(&changed);
|
|
|
|
|
endResetModel();
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 17:50:34 +02:00
|
|
|
QMimeData *ItemLibraryModel::getMimeData(const ItemLibraryEntry &itemLibraryEntry)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2018-07-24 23:56:45 +02:00
|
|
|
auto mimeData = new QMimeData();
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
|
QDataStream stream(&data, QIODevice::WriteOnly);
|
2014-06-19 17:50:34 +02:00
|
|
|
stream << itemLibraryEntry;
|
2014-05-12 14:09:02 +02:00
|
|
|
mimeData->setData(QStringLiteral("application/vnd.bauhaus.itemlibraryinfo"), data);
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2014-05-12 14:09:02 +02:00
|
|
|
mimeData->removeFormat(QStringLiteral("text/plain"));
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
return mimeData;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-15 17:52:13 +02:00
|
|
|
void ItemLibraryModel::clearSections()
|
|
|
|
|
{
|
2021-02-04 16:18:45 +02:00
|
|
|
qDeleteAll(m_importList);
|
|
|
|
|
m_importList.clear();
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::registerQmlTypes()
|
|
|
|
|
{
|
2020-09-18 10:54:22 +02:00
|
|
|
qmlRegisterAnonymousType<QmlDesigner::ItemLibraryModel>("ItemLibraryModel", 1);
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
ItemLibraryImport *ItemLibraryModel::importByUrl(const QString &importUrl) const
|
2014-06-23 14:50:33 +02:00
|
|
|
{
|
2021-02-04 16:18:45 +02:00
|
|
|
for (ItemLibraryImport *itemLibraryImport : std::as_const(m_importList)) {
|
|
|
|
|
if (itemLibraryImport->importUrl() == importUrl
|
|
|
|
|
|| (importUrl.isEmpty() && itemLibraryImport->importUrl() == "QtQuick")
|
|
|
|
|
|| (importUrl == ItemLibraryImport::userComponentsTitle()
|
2021-03-02 17:15:59 +02:00
|
|
|
&& itemLibraryImport->sectionType() == ItemLibraryImport::SectionType::User)
|
2021-03-11 22:49:39 +02:00
|
|
|
|| (importUrl == ItemLibraryImport::quick3DAssetsTitle()
|
|
|
|
|
&& itemLibraryImport->sectionType() == ItemLibraryImport::SectionType::Quick3DAssets)
|
2021-03-02 17:15:59 +02:00
|
|
|
|| (importUrl == ItemLibraryImport::unimportedComponentsTitle()
|
|
|
|
|
&& itemLibraryImport->sectionType() == ItemLibraryImport::SectionType::Unimported)) {
|
2021-02-04 16:18:45 +02:00
|
|
|
return itemLibraryImport;
|
|
|
|
|
}
|
2014-06-23 14:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
return nullptr;
|
2014-06-23 14:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
void ItemLibraryModel::updateUsedImports(const QList<Import> &usedImports)
|
2010-02-16 17:24:18 +02:00
|
|
|
{
|
2021-02-04 16:18:45 +02:00
|
|
|
// imports in the excludeList are not marked used and thus can always be removed even when in use.
|
|
|
|
|
const QList<QString> excludeList = {"SimulinkConnector"};
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
for (ItemLibraryImport *importSection : std::as_const(m_importList)) {
|
|
|
|
|
if (!excludeList.contains(importSection->importUrl()))
|
|
|
|
|
importSection->setImportUsed(usedImports.contains(importSection->importEntry()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 17:01:40 +02:00
|
|
|
void ItemLibraryModel::updateVisibility(bool *changed, bool expand)
|
2021-02-04 16:18:45 +02:00
|
|
|
{
|
|
|
|
|
for (ItemLibraryImport *import : std::as_const(m_importList)) {
|
|
|
|
|
bool categoryChanged = false;
|
2021-03-08 17:01:40 +02:00
|
|
|
bool hasVisibleItems = import->updateCategoryVisibility(m_searchText, &categoryChanged, expand);
|
2021-02-04 16:18:45 +02:00
|
|
|
*changed |= categoryChanged;
|
2021-02-18 21:14:23 +02:00
|
|
|
|
2021-03-02 17:15:59 +02:00
|
|
|
if (import->sectionType() == ItemLibraryImport::SectionType::Unimported)
|
|
|
|
|
*changed |= import->setVisible(!m_searchText.isEmpty());
|
|
|
|
|
|
2021-02-18 21:14:23 +02:00
|
|
|
// expand import if it has an item matching search criteria
|
2021-03-08 17:01:40 +02:00
|
|
|
if (expand && hasVisibleItems && !import->importExpanded())
|
2021-02-18 21:14:23 +02:00
|
|
|
import->setImportExpanded();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 17:52:13 +02:00
|
|
|
void ItemLibraryModel::addRoleNames()
|
|
|
|
|
{
|
|
|
|
|
int role = 0;
|
2021-02-04 16:18:45 +02:00
|
|
|
const QMetaObject meta = ItemLibraryImport::staticMetaObject;
|
|
|
|
|
for (int i = meta.propertyOffset(); i < meta.propertyCount(); ++i)
|
|
|
|
|
m_roleNames.insert(role++, meta.property(i).name());
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 12:00:15 +02:00
|
|
|
void ItemLibraryModel::sortSections()
|
|
|
|
|
{
|
2021-02-04 16:18:45 +02:00
|
|
|
auto sectionSort = [](ItemLibraryImport *first, ItemLibraryImport *second) {
|
2020-04-28 22:05:05 +02:00
|
|
|
return QString::localeAwareCompare(first->sortingName(), second->sortingName()) < 0;
|
2014-07-03 12:00:15 +02:00
|
|
|
};
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
std::sort(m_importList.begin(), m_importList.end(), sectionSort);
|
2014-07-03 12:00:15 +02:00
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
for (ItemLibraryImport *itemLibImport : qAsConst(m_importList))
|
|
|
|
|
itemLibImport->sortCategorySections();
|
2013-08-06 16:45:59 +02:00
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
} // namespace QmlDesigner
|
2010-02-16 17:24:18 +02:00
|
|
|
|