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>
|
|
|
|
|
#include <nodemetainfo.h>
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
#include <QVariant>
|
2013-08-06 16:45:59 +02:00
|
|
|
#include <QMetaProperty>
|
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
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
ItemLibrarySectionModel* model = qobject_cast<ItemLibrarySectionModel *>(value.value<QObject*>());
|
2014-05-15 17:52:13 +02:00
|
|
|
if (model)
|
|
|
|
|
return QVariant::fromValue(model);
|
|
|
|
|
|
|
|
|
|
ItemLibraryModel* model2 = qobject_cast<ItemLibraryModel *>(value.value<QObject*>());
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
updateVisibility();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
QMap<QString, int> sections;
|
|
|
|
|
|
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
|
|
|
|
2010-12-10 22:01:55 +01:00
|
|
|
foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
|
2010-05-07 16:33:02 +02:00
|
|
|
|
2016-04-05 15:55:35 +02:00
|
|
|
NodeMetaInfo metaInfo = model->metaInfo(entry.typeName());
|
2012-12-20 15:33:53 +01:00
|
|
|
bool valid = metaInfo.isValid() && metaInfo.majorVersion() == entry.majorVersion();
|
2010-12-10 22:01:55 +01:00
|
|
|
|
2014-05-07 13:00:57 +02:00
|
|
|
if (valid
|
2012-12-20 15:33:53 +01:00
|
|
|
&& (entry.requiredImport().isEmpty()
|
2014-05-07 13:00:57 +02:00
|
|
|
|| model->hasImport(entryToImport(entry), true, true))) {
|
2010-12-10 22:01:55 +01:00
|
|
|
QString itemSectionName = entry.category();
|
2014-06-23 14:50:33 +02:00
|
|
|
ItemLibrarySection *sectionModel = sectionByName(itemSectionName);
|
2014-07-03 12:00:15 +02:00
|
|
|
ItemLibraryItem *item;
|
2014-06-23 14:50:33 +02:00
|
|
|
|
|
|
|
|
if (sectionModel == 0) {
|
|
|
|
|
sectionModel = new ItemLibrarySection(itemSectionName, this);
|
2014-06-23 17:54:46 +02:00
|
|
|
m_sections.append(sectionModel);
|
2010-12-10 22:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 12:00:15 +02:00
|
|
|
item = new ItemLibraryItem(sectionModel);
|
|
|
|
|
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();
|
2014-06-23 17:48:15 +02:00
|
|
|
resetModel();
|
2010-02-16 17:24:18 +02:00
|
|
|
updateVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-19 17:50:34 +02:00
|
|
|
QMimeData *ItemLibraryModel::getMimeData(const ItemLibraryEntry &itemLibraryEntry)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
QMimeData *mimeData = new QMimeData();
|
|
|
|
|
|
|
|
|
|
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-21 18:27:08 +02:00
|
|
|
QList<ItemLibrarySection *> ItemLibraryModel::sections() const
|
2013-08-06 16:45:59 +02:00
|
|
|
{
|
2014-06-23 17:54:46 +02:00
|
|
|
return m_sections;
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::clearSections()
|
|
|
|
|
{
|
|
|
|
|
beginResetModel();
|
2014-06-23 17:54:46 +02:00
|
|
|
qDeleteAll(m_sections);
|
|
|
|
|
m_sections.clear();
|
2014-05-15 17:52:13 +02:00
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
void ItemLibraryModel::updateVisibility()
|
|
|
|
|
{
|
2010-03-10 12:45:49 +02:00
|
|
|
bool changed = false;
|
|
|
|
|
|
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);
|
|
|
|
|
changed |= sectionChanged;
|
2014-06-23 14:50:33 +02:00
|
|
|
changed |= itemLibrarySection->setVisible(sectionVisibility);
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
2014-06-23 17:48:15 +02:00
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
|
resetModel();
|
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-06-23 17:48:15 +02:00
|
|
|
void ItemLibraryModel::resetModel()
|
|
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 12:00:15 +02:00
|
|
|
void ItemLibraryModel::sortSections()
|
|
|
|
|
{
|
|
|
|
|
auto sectionSort = [](ItemLibrarySection *first, ItemLibrarySection *second) {
|
|
|
|
|
return QString::localeAwareCompare(first->sortingName(), second->sortingName()) < 1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort(m_sections.begin(), m_sections.end(), sectionSort);
|
|
|
|
|
|
|
|
|
|
foreach (ItemLibrarySection *itemLibrarySection, m_sections)
|
|
|
|
|
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
|
|
|
|