2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
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-05-15 17:52:13 +02:00
|
|
|
: QAbstractListModel(parent),
|
2010-02-16 17:24:18 +02:00
|
|
|
m_nextLibId(0)
|
|
|
|
|
{
|
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
|
|
|
|
|
{
|
|
|
|
|
return visibleSectionCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibraryModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid() || index.row() +1 > visibleSectionCount())
|
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_roleNames.contains(role)) {
|
|
|
|
|
QVariant value = visibleSections().at(index.row())->property(m_roleNames.value(role));
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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-03-10 12:45:49 +02:00
|
|
|
m_nextLibId = 0;
|
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
|
|
|
|
2012-12-20 15:33:53 +01:00
|
|
|
NodeMetaInfo metaInfo = model->metaInfo(entry.typeName(), -1, -1);
|
|
|
|
|
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-05-21 15:35:03 +02:00
|
|
|
ItemLibraryItem *itemModel;
|
2014-06-23 14:50:33 +02:00
|
|
|
|
|
|
|
|
if (sectionModel == 0) {
|
|
|
|
|
sectionModel = new ItemLibrarySection(itemSectionName, this);
|
|
|
|
|
m_sectionModels.append(sectionModel);
|
2010-12-10 22:01:55 +01:00
|
|
|
}
|
|
|
|
|
|
2014-06-23 13:09:26 +02:00
|
|
|
itemModel = new ItemLibraryItem(sectionModel);
|
2014-06-18 18:03:52 +02:00
|
|
|
itemModel->setItemLibraryEntry(entry);
|
2010-12-10 22:01:55 +01:00
|
|
|
sectionModel->addSectionEntry(itemModel);
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
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 14:50:33 +02:00
|
|
|
return m_sectionModels;
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::clearSections()
|
|
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
qDeleteAll(m_sectionModels);
|
|
|
|
|
m_sectionModels.clear();
|
|
|
|
|
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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ItemLibraryModel::visibleSectionCount() const
|
|
|
|
|
{
|
|
|
|
|
int visibleCount = 0;
|
|
|
|
|
|
2014-06-23 14:50:33 +02:00
|
|
|
foreach (ItemLibrarySection *section, m_sectionModels) {
|
|
|
|
|
if (section->isVisible())
|
2014-05-15 17:52:13 +02:00
|
|
|
++visibleCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return visibleCount;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:27:08 +02:00
|
|
|
QList<ItemLibrarySection *> ItemLibraryModel::visibleSections() const
|
2014-05-15 17:52:13 +02:00
|
|
|
{
|
2014-05-21 18:27:08 +02:00
|
|
|
QList<ItemLibrarySection *> visibleSectionList;
|
2014-05-15 17:52:13 +02:00
|
|
|
|
2014-06-23 14:50:33 +02:00
|
|
|
foreach (ItemLibrarySection *section, m_sectionModels) {
|
|
|
|
|
if (section->isVisible())
|
|
|
|
|
visibleSectionList.append(section);
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return visibleSectionList;
|
2013-08-06 16:45:59 +02:00
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2014-06-23 14:50:33 +02:00
|
|
|
ItemLibrarySection *ItemLibraryModel::sectionByName(const QString §ionName)
|
|
|
|
|
{
|
|
|
|
|
foreach (ItemLibrarySection *itemLibrarySection, m_sectionModels) {
|
|
|
|
|
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 14:50:33 +02:00
|
|
|
foreach (ItemLibrarySection *itemLibrarySection, m_sectionModels) {
|
2010-02-16 17:24:18 +02:00
|
|
|
QString sectionSearchText = m_searchText;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2014-06-23 14:50:33 +02:00
|
|
|
if (itemLibrarySection->sectionName().toLower().contains(m_searchText))
|
2014-05-15 17:52:13 +02:00
|
|
|
sectionSearchText.clear();
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
bool sectionChanged = false,
|
2014-06-23 14:50:33 +02:00
|
|
|
sectionVisibility = itemLibrarySection->updateSectionVisibility(sectionSearchText,
|
2010-03-10 12:45:49 +02:00
|
|
|
§ionChanged);
|
2014-06-23 14:50:33 +02:00
|
|
|
if (sectionChanged)
|
2010-03-10 12:45:49 +02:00
|
|
|
changed = true;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2014-06-23 14:50:33 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setRoleNames(m_roleNames);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-07 16:33:02 +02:00
|
|
|
int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
|
2010-04-28 10:11:28 +02:00
|
|
|
{
|
2010-05-07 12:49:13 +02:00
|
|
|
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
|
2010-04-28 10:11:28 +02:00
|
|
|
{
|
2013-03-05 12:19:19 +01:00
|
|
|
if (property.name() == "width")
|
2010-04-28 10:11:28 +02:00
|
|
|
return property.value().toInt();
|
|
|
|
|
}
|
2014-05-15 17:52:13 +02:00
|
|
|
|
2010-04-28 10:11:28 +02:00
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-07 16:33:02 +02:00
|
|
|
int ItemLibraryModel::getHeight(const ItemLibraryEntry &itemLibraryEntry)
|
2010-04-28 10:11:28 +02:00
|
|
|
{
|
2010-05-07 12:49:13 +02:00
|
|
|
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
|
2010-04-28 10:11:28 +02:00
|
|
|
{
|
2013-03-05 12:19:19 +01:00
|
|
|
if (property.name() == "height")
|
2010-04-28 10:11:28 +02:00
|
|
|
return property.value().toInt();
|
|
|
|
|
}
|
2014-05-15 17:52:13 +02:00
|
|
|
|
2010-04-28 10:11:28 +02:00
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|