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"
|
2010-05-12 11:56:23 +02:00
|
|
|
#include "itemlibraryinfo.h"
|
2014-05-21 18:24:30 +02:00
|
|
|
#include "itemlibrarysection.h"
|
2014-05-21 15:35:03 +02:00
|
|
|
#include "itemlibraryitem.h"
|
2014-05-21 18:27:08 +02:00
|
|
|
#include "itemlibrarysection.h"
|
2014-05-21 12:34:36 +02:00
|
|
|
|
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>
|
2010-01-07 12:14:35 +01:00
|
|
|
|
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
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
#include <QVariant>
|
2013-08-06 16:45:59 +02:00
|
|
|
#include <QMetaProperty>
|
2016-07-27 11:30:35 +02:00
|
|
|
#include <QLoggingCategory>
|
2010-02-16 17:24:18 +02:00
|
|
|
#include <QMimeData>
|
2010-01-07 12:14:35 +01:00
|
|
|
#include <QPainter>
|
2010-02-16 17:24:18 +02:00
|
|
|
#include <QPen>
|
|
|
|
|
#include <qdebug.h>
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2018-10-12 09:33:30 +03:00
|
|
|
static Q_LOGGING_CATEGORY(itemlibraryPopulate, "qtc.itemlibrary.populate", QtWarningMsg)
|
2016-07-27 11:30:35 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
static bool inline registerItemLibrarySortedModel() {
|
2014-05-21 18:24:30 +02:00
|
|
|
qmlRegisterType<QmlDesigner::ItemLibrarySectionModel>();
|
2013-08-06 16:45:59 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
2013-06-06 11:24:18 +02:00
|
|
|
static QHash<QString, bool> collapsedStateHash;
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-06-06 11:24:18 +02:00
|
|
|
void ItemLibraryModel::setExpanded(bool expanded, const QString §ion)
|
|
|
|
|
{
|
|
|
|
|
if (collapsedStateHash.contains(section))
|
|
|
|
|
collapsedStateHash.remove(section);
|
|
|
|
|
|
|
|
|
|
if (!expanded) //default is true
|
|
|
|
|
collapsedStateHash.insert(section, expanded);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2014-06-23 17:54:46 +02:00
|
|
|
return m_sections.count();
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibraryModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
2014-06-23 17:54:46 +02:00
|
|
|
if (!index.isValid() || index.row() +1 > m_sections.count())
|
2014-05-15 17:52:13 +02:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_roleNames.contains(role)) {
|
2014-06-23 17:54:46 +02:00
|
|
|
QVariant value = m_sections.at(index.row())->property(m_roleNames.value(role));
|
2014-05-15 17:52:13 +02:00
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
auto model = qobject_cast<ItemLibrarySectionModel *>(value.value<QObject*>());
|
2014-05-15 17:52:13 +02:00
|
|
|
if (model)
|
|
|
|
|
return QVariant::fromValue(model);
|
|
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
auto model2 = qobject_cast<ItemLibraryModel *>(value.value<QObject*>());
|
2014-05-15 17:52:13 +02:00
|
|
|
if (model2)
|
|
|
|
|
return QVariant::fromValue(model2);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "invalid role requested";
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
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;
|
|
|
|
|
emit searchTextChanged();
|
|
|
|
|
|
2016-06-07 19:58:38 +02:00
|
|
|
bool changed = false;
|
|
|
|
|
updateVisibility(&changed);
|
|
|
|
|
if (changed)
|
2019-01-16 18:06:21 +01:00
|
|
|
emit dataChanged(QModelIndex(), QModelIndex());
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-16 16:12:29 +02:00
|
|
|
Import 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
|
|
|
}
|
|
|
|
|
|
2017-01-03 13:17:46 +01:00
|
|
|
bool sectionExapanded(const QString §ionName)
|
|
|
|
|
{
|
|
|
|
|
if (collapsedStateHash.contains(sectionName))
|
|
|
|
|
return collapsedStateHash.value(sectionName);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2010-12-10 22:01:55 +01:00
|
|
|
QStringList imports;
|
|
|
|
|
foreach (const Import &import, model->imports())
|
|
|
|
|
if (import.isLibraryImport())
|
2011-02-25 15:27:13 +01:00
|
|
|
imports << import.url() + QLatin1Char(' ') + import.version();
|
2010-05-07 16:33:02 +02:00
|
|
|
|
2016-07-27 11:30:35 +02:00
|
|
|
|
|
|
|
|
qCInfo(itemlibraryPopulate) << Q_FUNC_INFO;
|
2010-12-10 22:01:55 +01:00
|
|
|
foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
|
2010-05-07 16:33:02 +02:00
|
|
|
|
2016-07-27 11:30:35 +02:00
|
|
|
qCInfo(itemlibraryPopulate) << entry.typeName() << entry.majorVersion() << entry.minorVersion();
|
|
|
|
|
|
|
|
|
|
NodeMetaInfo metaInfo = model->metaInfo(entry.typeName());
|
|
|
|
|
|
|
|
|
|
qCInfo(itemlibraryPopulate) << "valid: " << metaInfo.isValid() << metaInfo.majorVersion() << metaInfo.minorVersion();
|
2016-06-03 14:31:50 +02:00
|
|
|
|
2016-07-27 11:30:35 +02:00
|
|
|
bool valid = metaInfo.isValid() && metaInfo.majorVersion() == entry.majorVersion();
|
|
|
|
|
bool isItem = valid && metaInfo.isSubclassOf("QtQuick.Item");
|
|
|
|
|
|
|
|
|
|
qCInfo(itemlibraryPopulate) << "isItem: " << isItem;
|
|
|
|
|
|
|
|
|
|
qCInfo(itemlibraryPopulate) << "required import: " << entry.requiredImport() << entryToImport(entry).toImportString();
|
|
|
|
|
|
2019-04-29 12:09:12 +02:00
|
|
|
bool forceVisiblity = valid && NodeHints::fromItemLibraryEntry(entry).visibleInLibrary();
|
|
|
|
|
|
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");
|
|
|
|
|
forceVisiblity = isItem;
|
2020-01-29 12:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-07-27 11:30:35 +02:00
|
|
|
if (valid
|
2019-04-29 12:09:12 +02:00
|
|
|
&& (isItem || forceVisiblity) //We can change if the navigator does support pure QObjects
|
2016-07-27 11:30:35 +02:00
|
|
|
&& (entry.requiredImport().isEmpty()
|
|
|
|
|
|| model->hasImport(entryToImport(entry), true, true))) {
|
2010-12-10 22:01:55 +01:00
|
|
|
QString itemSectionName = entry.category();
|
2016-07-27 11:30:35 +02:00
|
|
|
qCInfo(itemlibraryPopulate) << "Adding:" << entry.typeName() << "to:" << entry.category();
|
2014-06-23 14:50:33 +02:00
|
|
|
ItemLibrarySection *sectionModel = sectionByName(itemSectionName);
|
|
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
if (sectionModel == nullptr) {
|
2014-06-23 14:50:33 +02:00
|
|
|
sectionModel = new ItemLibrarySection(itemSectionName, this);
|
2014-06-23 17:54:46 +02:00
|
|
|
m_sections.append(sectionModel);
|
2017-01-03 13:17:46 +01:00
|
|
|
sectionModel->setSectionExpanded(sectionExapanded(itemSectionName));
|
2010-12-10 22:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
auto item = new ItemLibraryItem(sectionModel);
|
2014-07-03 12:00:15 +02:00
|
|
|
item->setItemLibraryEntry(entry);
|
|
|
|
|
sectionModel->addSectionEntry(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()
|
|
|
|
|
{
|
2014-06-23 17:54:46 +02:00
|
|
|
qDeleteAll(m_sections);
|
|
|
|
|
m_sections.clear();
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::registerQmlTypes()
|
|
|
|
|
{
|
2014-05-21 18:24:30 +02:00
|
|
|
qmlRegisterType<QmlDesigner::ItemLibrarySectionModel>();
|
2014-05-15 17:52:13 +02:00
|
|
|
qmlRegisterType<QmlDesigner::ItemLibraryModel>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-23 14:50:33 +02:00
|
|
|
ItemLibrarySection *ItemLibraryModel::sectionByName(const QString §ionName)
|
|
|
|
|
{
|
2014-06-23 17:54:46 +02:00
|
|
|
foreach (ItemLibrarySection *itemLibrarySection, m_sections) {
|
2014-06-23 14:50:33 +02:00
|
|
|
if (itemLibrarySection->sectionName() == sectionName)
|
|
|
|
|
return itemLibrarySection;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
return nullptr;
|
2014-06-23 14:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
2016-06-07 19:58:38 +02:00
|
|
|
void ItemLibraryModel::updateVisibility(bool *changed)
|
2010-02-16 17:24:18 +02:00
|
|
|
{
|
2014-06-23 17:54:46 +02:00
|
|
|
foreach (ItemLibrarySection *itemLibrarySection, m_sections) {
|
2010-02-16 17:24:18 +02:00
|
|
|
QString sectionSearchText = m_searchText;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2014-06-23 17:48:15 +02:00
|
|
|
bool sectionChanged = false;
|
|
|
|
|
bool sectionVisibility = itemLibrarySection->updateSectionVisibility(sectionSearchText,
|
|
|
|
|
§ionChanged);
|
2020-01-29 12:28:30 +01:00
|
|
|
|
2016-06-07 19:58:38 +02:00
|
|
|
*changed |= sectionChanged;
|
|
|
|
|
*changed |= itemLibrarySection->setVisible(sectionVisibility);
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 17:52:13 +02:00
|
|
|
void ItemLibraryModel::addRoleNames()
|
|
|
|
|
{
|
|
|
|
|
int role = 0;
|
2014-05-21 18:27:08 +02:00
|
|
|
for (int propertyIndex = 0; propertyIndex < ItemLibrarySection::staticMetaObject.propertyCount(); ++propertyIndex) {
|
|
|
|
|
QMetaProperty property = ItemLibrarySection::staticMetaObject.property(propertyIndex);
|
2014-05-15 17:52:13 +02:00
|
|
|
m_roleNames.insert(role, property.name());
|
|
|
|
|
++role;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 12:00:15 +02:00
|
|
|
void ItemLibraryModel::sortSections()
|
|
|
|
|
{
|
2020-03-30 15:23:46 +02:00
|
|
|
int nullPointerSectionCount = m_sections.removeAll(QPointer<ItemLibrarySection>());
|
|
|
|
|
QTC_ASSERT(nullPointerSectionCount == 0,;);
|
2014-07-03 12:00:15 +02:00
|
|
|
auto sectionSort = [](ItemLibrarySection *first, ItemLibrarySection *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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort(m_sections.begin(), m_sections.end(), sectionSort);
|
|
|
|
|
|
2020-06-13 23:04:31 +02:00
|
|
|
for (const auto itemLibrarySection : m_sections)
|
2014-07-03 12:00:15 +02:00
|
|
|
itemLibrarySection->sortItems();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void registerQmlTypes()
|
|
|
|
|
{
|
|
|
|
|
registerItemLibrarySortedModel();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
} // namespace QmlDesigner
|
2010-02-16 17:24:18 +02:00
|
|
|
|