2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-02-16 19:07:59 +02:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 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"
|
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() {
|
|
|
|
|
qmlRegisterType<QmlDesigner::Internal::ItemLibrarySortedModel>();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
|
|
|
|
namespace Internal {
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-06-06 11:24:18 +02:00
|
|
|
static QHash<QString, bool> collapsedStateHash;
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibrarySortedModel::ItemLibrarySortedModel(QObject *parent) :
|
|
|
|
|
QAbstractListModel(parent)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibrarySortedModel::~ItemLibrarySortedModel()
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
clearElements();
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
int ItemLibrarySortedModel::rowCount(const QModelIndex &) const
|
|
|
|
|
{
|
|
|
|
|
return m_privList.count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibrarySortedModel::data(const QModelIndex &index, int role) const
|
|
|
|
|
{
|
|
|
|
|
if (!index.isValid() || index.row()+1 > m_privList.count()) {
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "invalid index requested";
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_roleNames.contains(role)) {
|
|
|
|
|
QVariant value = m_privList.at(index.row())->property(m_roleNames.value(role));
|
|
|
|
|
|
|
|
|
|
if (ItemLibrarySortedModel* model = qobject_cast<ItemLibrarySortedModel *>(value.value<QObject*>())) {
|
|
|
|
|
return QVariant::fromValue(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_privList.at(index.row())->property(m_roleNames.value(role));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "invalid role requested";
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibrarySortedModel::clearElements()
|
2010-02-16 17:24:18 +02:00
|
|
|
{
|
2013-08-06 16:45:59 +02:00
|
|
|
beginResetModel();
|
2010-02-16 17:24:18 +02:00
|
|
|
while (m_elementOrder.count() > 0)
|
|
|
|
|
removeElement(m_elementOrder.at(0).libId);
|
2013-08-06 16:45:59 +02:00
|
|
|
endResetModel();
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
static bool compareFunction(QObject *first, QObject *second)
|
|
|
|
|
{
|
|
|
|
|
static const char sortRoleName[] = "sortingRole";
|
|
|
|
|
|
|
|
|
|
return first->property(sortRoleName).toString() < second->property(sortRoleName).toString();
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void ItemLibrarySortedModel::addElement(QObject *element, int libId)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
struct order_struct orderEntry;
|
|
|
|
|
orderEntry.libId = libId;
|
|
|
|
|
orderEntry.visible = false;
|
|
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
|
while ((pos < m_elementOrder.count()) &&
|
2013-08-06 16:45:59 +02:00
|
|
|
compareFunction(m_elementModels.value(m_elementOrder.at(pos).libId), element))
|
2010-02-16 17:24:18 +02:00
|
|
|
++pos;
|
|
|
|
|
|
|
|
|
|
m_elementModels.insert(libId, element);
|
|
|
|
|
m_elementOrder.insert(pos, orderEntry);
|
|
|
|
|
|
|
|
|
|
setElementVisible(libId, true);
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void ItemLibrarySortedModel::removeElement(int libId)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2013-08-06 16:45:59 +02:00
|
|
|
QObject *element = m_elementModels.value(libId);
|
2010-02-16 17:24:18 +02:00
|
|
|
int pos = findElement(libId);
|
|
|
|
|
|
|
|
|
|
setElementVisible(libId, false);
|
|
|
|
|
|
|
|
|
|
m_elementModels.remove(libId);
|
|
|
|
|
m_elementOrder.removeAt(pos);
|
|
|
|
|
|
|
|
|
|
delete element;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
bool ItemLibrarySortedModel::elementVisible(int libId) const
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
int pos = findElement(libId);
|
|
|
|
|
return m_elementOrder.at(pos).visible;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
bool ItemLibrarySortedModel::setElementVisible(int libId, bool visible)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-03-10 12:45:49 +02:00
|
|
|
int pos = findElement(libId);
|
2010-02-16 17:24:18 +02:00
|
|
|
if (m_elementOrder.at(pos).visible == visible)
|
2010-03-10 12:45:49 +02:00
|
|
|
return false;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
int visiblePos = visibleElementPosition(libId);
|
2013-08-06 16:45:59 +02:00
|
|
|
if (visible) {
|
|
|
|
|
privateInsert(visiblePos, (m_elementModels.value(libId)));
|
|
|
|
|
} else {
|
|
|
|
|
privateRemove(visiblePos);
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
m_elementOrder[pos].visible = visible;
|
2010-03-10 12:45:49 +02:00
|
|
|
return true;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void ItemLibrarySortedModel::privateInsert(int pos, QObject *element)
|
|
|
|
|
{
|
|
|
|
|
QObject *object = element;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < object->metaObject()->propertyCount(); ++i) {
|
|
|
|
|
QMetaProperty property = object->metaObject()->property(i);
|
|
|
|
|
addRoleName(property.name());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_privList.insert(pos, element);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibrarySortedModel::privateRemove(int pos)
|
|
|
|
|
{
|
|
|
|
|
m_privList.removeAt(pos);
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
const QMap<int, QObject *> &ItemLibrarySortedModel::elements() const
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
return m_elementModels;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
template<typename T>
|
|
|
|
|
const QList<T> ItemLibrarySortedModel::elementsByType() const
|
|
|
|
|
{
|
|
|
|
|
QList<T> objectList;
|
|
|
|
|
|
|
|
|
|
foreach (QObject *item, elements()) {
|
|
|
|
|
T object = qobject_cast<T>(item);
|
|
|
|
|
if (object)
|
|
|
|
|
objectList.append(object);
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
return objectList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QObject *ItemLibrarySortedModel::element(int libId)
|
2010-02-16 17:24:18 +02:00
|
|
|
{
|
|
|
|
|
return m_elementModels.value(libId);
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
template<typename T>
|
|
|
|
|
T ItemLibrarySortedModel::elementByType(int libId)
|
|
|
|
|
{
|
|
|
|
|
return qobject_cast<T>(element(libId));
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
int ItemLibrarySortedModel::findElement(int libId) const
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
int i = 0;
|
|
|
|
|
QListIterator<struct order_struct> it(m_elementOrder);
|
|
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
if (it.next().libId == libId)
|
|
|
|
|
return i;
|
|
|
|
|
++i;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
int ItemLibrarySortedModel::visibleElementPosition(int libId) const
|
2010-03-10 12:45:49 +02:00
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
QListIterator<struct order_struct> it(m_elementOrder);
|
|
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
struct order_struct order = it.next();
|
|
|
|
|
if (order.libId == libId)
|
|
|
|
|
return i;
|
|
|
|
|
if (order.visible)
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void ItemLibrarySortedModel::resetModel()
|
|
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void ItemLibrarySortedModel::addRoleName(const QByteArray &roleName)
|
|
|
|
|
{
|
|
|
|
|
if (m_roleNames.values().contains(roleName)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int key = m_roleNames.count();
|
|
|
|
|
m_roleNames.insert(key, roleName);
|
|
|
|
|
setRoleNames(m_roleNames);
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibraryItemModel::ItemLibraryItemModel(int itemLibId, const QString &itemName, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
2010-02-16 17:24:18 +02:00
|
|
|
m_libId(itemLibId),
|
|
|
|
|
m_name(itemName),
|
|
|
|
|
m_iconSize(64, 64)
|
|
|
|
|
{
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
ItemLibraryItemModel::~ItemLibraryItemModel()
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2013-08-06 16:45:59 +02:00
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
int ItemLibraryItemModel::itemLibId() const
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
return m_libId;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
QString ItemLibraryItemModel::itemName() const
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
return m_name;
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
QString ItemLibraryItemModel::itemLibraryIconPath() const
|
|
|
|
|
{
|
|
|
|
|
//Prepend image provider prefix
|
|
|
|
|
return QLatin1String("image://qmldesigner_itemlibrary/")+ m_iconPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibraryItemModel::sortingRole() const
|
|
|
|
|
{
|
|
|
|
|
return itemName();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 10:11:05 +10:00
|
|
|
void ItemLibraryItemModel::setItemIconPath(const QString &iconPath)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-07-13 10:11:05 +10:00
|
|
|
m_iconPath = iconPath;
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
void ItemLibraryItemModel::setItemIconSize(const QSize &itemIconSize)
|
|
|
|
|
{
|
|
|
|
|
m_iconSize = itemIconSize;
|
2010-07-13 10:11:05 +10:00
|
|
|
setItemIconPath(m_iconPath);
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibrarySectionModel::ItemLibrarySectionModel(int sectionLibId, const QString §ionName, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
2010-02-16 17:24:18 +02:00
|
|
|
m_name(sectionName),
|
2013-08-06 16:45:59 +02:00
|
|
|
m_sectionLibId(sectionLibId),
|
|
|
|
|
m_sectionExpanded(true),
|
2010-02-16 17:24:18 +02:00
|
|
|
m_sectionEntries(parent)
|
|
|
|
|
{
|
2013-08-06 16:45:59 +02:00
|
|
|
if (collapsedStateHash.contains(sectionName))
|
|
|
|
|
m_sectionExpanded= collapsedStateHash.value(sectionName);
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
QString ItemLibrarySectionModel::sectionName() const
|
|
|
|
|
{
|
|
|
|
|
return m_name;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
int ItemLibrarySectionModel::sectionLibId() const
|
|
|
|
|
{
|
|
|
|
|
return m_sectionLibId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::sectionExpanded() const
|
|
|
|
|
{
|
|
|
|
|
return m_sectionExpanded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibrarySectionModel::sortingRole() const
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (sectionName() == QLatin1String("QML Components")) //Qml Components always come first
|
|
|
|
|
return QVariant(QLatin1String("AA.this_comes_first"));
|
|
|
|
|
|
|
|
|
|
return sectionName();
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
void ItemLibrarySectionModel::addSectionEntry(ItemLibraryItemModel *sectionEntry)
|
|
|
|
|
{
|
|
|
|
|
m_sectionEntries.addElement(sectionEntry, sectionEntry->itemLibId());
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
void ItemLibrarySectionModel::removeSectionEntry(int itemLibId)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
m_sectionEntries.removeElement(itemLibId);
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
QObject *ItemLibrarySectionModel::sectionEntries()
|
|
|
|
|
{
|
|
|
|
|
return &m_sectionEntries;
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
int ItemLibrarySectionModel::visibleItemIndex(int itemLibId)
|
|
|
|
|
{
|
|
|
|
|
return m_sectionEntries.visibleElementPosition(itemLibId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::isItemVisible(int itemLibId)
|
|
|
|
|
{
|
|
|
|
|
return m_sectionEntries.elementVisible(itemLibId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::updateSectionVisibility(const QString &searchText, bool *changed)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
bool haveVisibleItems = false;
|
2010-03-10 12:45:49 +02:00
|
|
|
|
|
|
|
|
*changed = false;
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
QMap<int, QObject *>::const_iterator itemIt = m_sectionEntries.elements().constBegin();
|
2010-02-16 17:24:18 +02:00
|
|
|
while (itemIt != m_sectionEntries.elements().constEnd()) {
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
bool itemVisible = m_sectionEntries.elementByType<ItemLibraryItemModel*>(
|
|
|
|
|
itemIt.key())->itemName().toLower().contains(searchText);
|
|
|
|
|
|
|
|
|
|
bool itemChanged = false;
|
2010-03-10 12:45:49 +02:00
|
|
|
itemChanged = m_sectionEntries.setElementVisible(itemIt.key(), itemVisible);
|
|
|
|
|
|
|
|
|
|
*changed |= itemChanged;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
if (itemVisible)
|
|
|
|
|
haveVisibleItems = true;
|
|
|
|
|
|
|
|
|
|
++itemIt;
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
m_sectionEntries.resetModel();
|
|
|
|
|
|
|
|
|
|
emit sectionEntriesChanged();
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
return haveVisibleItems;
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
void ItemLibrarySectionModel::updateItemIconSize(const QSize &itemIconSize)
|
|
|
|
|
{
|
2013-08-06 16:45:59 +02:00
|
|
|
foreach (ItemLibraryItemModel* itemLibraryItemModel, m_sectionEntries.elementsByType<ItemLibraryItemModel*>()) {
|
|
|
|
|
itemLibraryItemModel->setItemIconSize(itemIconSize);
|
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)
|
|
|
|
|
: ItemLibrarySortedModel(parent),
|
2010-02-16 17:24:18 +02:00
|
|
|
m_searchText(""),
|
|
|
|
|
m_itemIconSize(64, 64),
|
|
|
|
|
m_nextLibId(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ItemLibraryModel::~ItemLibraryModel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ItemLibraryModel::setItemIconSize(const QSize &itemIconSize)
|
|
|
|
|
{
|
|
|
|
|
m_itemIconSize = itemIconSize;
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
foreach (ItemLibrarySectionModel* itemLibrarySectionModel, sections()) {
|
|
|
|
|
itemLibrarySectionModel->updateItemIconSize(itemIconSize);
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
int ItemLibraryModel::getItemSectionIndex(int itemLibId)
|
|
|
|
|
{
|
|
|
|
|
if (m_sections.contains(itemLibId))
|
2013-08-06 16:45:59 +02:00
|
|
|
return section(m_sections.value(itemLibId))->visibleItemIndex(itemLibId);
|
2010-03-10 12:45:49 +02:00
|
|
|
else
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ItemLibraryModel::getSectionLibId(int itemLibId)
|
|
|
|
|
{
|
|
|
|
|
return m_sections.value(itemLibId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ItemLibraryModel::isItemVisible(int itemLibId)
|
|
|
|
|
{
|
|
|
|
|
if (!m_sections.contains(itemLibId))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
int sectionLibId = m_sections.value(itemLibId);
|
|
|
|
|
if (!elementVisible(sectionLibId))
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
return section(sectionLibId)->isItemVisible(itemLibId);
|
2010-03-10 12:45:49 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
clearElements();
|
|
|
|
|
m_itemInfos.clear();
|
2010-03-10 12:45:49 +02:00
|
|
|
m_sections.clear();
|
|
|
|
|
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
|
|
|
|
2012-12-20 15:33:53 +01:00
|
|
|
if ((valid || entry.forceImport())
|
|
|
|
|
&& (entry.requiredImport().isEmpty()
|
|
|
|
|
|| model->hasImport(entryToImport(entry), true, false) || entry.forceImport())) {
|
2010-12-10 22:01:55 +01:00
|
|
|
QString itemSectionName = entry.category();
|
|
|
|
|
ItemLibrarySectionModel *sectionModel;
|
|
|
|
|
ItemLibraryItemModel *itemModel;
|
|
|
|
|
int itemId = m_nextLibId++, sectionId;
|
|
|
|
|
|
|
|
|
|
if (sections.contains(itemSectionName)) {
|
|
|
|
|
sectionId = sections.value(itemSectionName);
|
2013-08-06 16:45:59 +02:00
|
|
|
sectionModel = section(sectionId);
|
2010-12-10 22:01:55 +01:00
|
|
|
} else {
|
|
|
|
|
sectionId = m_nextLibId++;
|
2013-08-06 16:45:59 +02:00
|
|
|
sectionModel = new ItemLibrarySectionModel(sectionId, itemSectionName, this);
|
2010-12-10 22:01:55 +01:00
|
|
|
addElement(sectionModel, sectionId);
|
|
|
|
|
sections.insert(itemSectionName, sectionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_itemInfos.insert(itemId, entry);
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
itemModel = new ItemLibraryItemModel(itemId, entry.name(), sectionModel);
|
2010-12-10 22:01:55 +01:00
|
|
|
|
|
|
|
|
// delayed creation of (default) icons
|
|
|
|
|
if (entry.iconPath().isEmpty())
|
|
|
|
|
entry.setIconPath(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
|
|
|
|
|
if (entry.dragIcon().isNull())
|
|
|
|
|
entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));
|
|
|
|
|
|
|
|
|
|
itemModel->setItemIconPath(entry.iconPath());
|
|
|
|
|
itemModel->setItemIconSize(m_itemIconSize);
|
|
|
|
|
sectionModel->addSectionEntry(itemModel);
|
|
|
|
|
m_sections.insert(itemId, sectionId);
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
updateVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString ItemLibraryModel::getTypeName(int libId)
|
|
|
|
|
{
|
|
|
|
|
return m_itemInfos.value(libId).typeName();
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
QMimeData *ItemLibraryModel::getMimeData(int libId)
|
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);
|
|
|
|
|
stream << m_itemInfos.value(libId);
|
|
|
|
|
mimeData->setData(QLatin1String("application/vnd.bauhaus.itemlibraryinfo"), data);
|
|
|
|
|
|
|
|
|
|
const QIcon icon = m_itemInfos.value(libId).dragIcon();
|
|
|
|
|
if (!icon.isNull()) {
|
|
|
|
|
const QList<QSize> sizes = icon.availableSizes();
|
|
|
|
|
if (!sizes.isEmpty())
|
|
|
|
|
mimeData->setImageData(icon.pixmap(sizes.front()).toImage());
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
mimeData->removeFormat(QLatin1String("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
|
|
|
}
|
|
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
|
|
|
|
|
QIcon ItemLibraryModel::getIcon(int libId)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
2010-02-16 17:24:18 +02:00
|
|
|
return m_itemInfos.value(libId).icon();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibrarySectionModel *ItemLibraryModel::section(int libId)
|
|
|
|
|
{
|
|
|
|
|
return elementByType<ItemLibrarySectionModel*>(libId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<ItemLibrarySectionModel *> ItemLibraryModel::sections() const
|
|
|
|
|
{
|
|
|
|
|
return elementsByType<ItemLibrarySectionModel*>();
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
void ItemLibraryModel::updateVisibility()
|
|
|
|
|
{
|
2013-08-06 16:45:59 +02:00
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
2010-03-10 12:45:49 +02:00
|
|
|
bool changed = false;
|
|
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
QMap<int, QObject *>::const_iterator sectionIt = elements().constBegin();
|
2010-02-16 17:24:18 +02:00
|
|
|
while (sectionIt != elements().constEnd()) {
|
2013-08-06 16:45:59 +02:00
|
|
|
ItemLibrarySectionModel *sectionModel = section(sectionIt.key());
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
QString sectionSearchText = m_searchText;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-02-16 17:24:18 +02:00
|
|
|
if (sectionModel->sectionName().toLower().contains(m_searchText))
|
|
|
|
|
sectionSearchText = "";
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
bool sectionChanged = false,
|
|
|
|
|
sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
|
|
|
|
|
§ionChanged);
|
|
|
|
|
if (sectionChanged) {
|
|
|
|
|
changed = true;
|
|
|
|
|
if (sectionVisibility)
|
|
|
|
|
emit sectionVisibilityChanged(sectionIt.key());
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
changed |= setElementVisible(sectionIt.key(), sectionVisibility);
|
2010-02-16 17:24:18 +02:00
|
|
|
++sectionIt;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-10 12:45:49 +02:00
|
|
|
if (changed)
|
|
|
|
|
emit visibilityChanged();
|
2010-02-16 17:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
return 64;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-17 12:04:51 +02:00
|
|
|
QPixmap ItemLibraryModel::createDragPixmap(int , int )
|
2010-04-28 10:11:28 +02:00
|
|
|
{
|
2011-05-09 14:36:11 +02:00
|
|
|
QImage dragImage(10, 10, QImage::Format_ARGB32); // TODO: draw item drag icon
|
|
|
|
|
dragImage.fill(0x00ffffff); //### todo for now we disable the preview image
|
2010-04-28 10:11:28 +02:00
|
|
|
QPainter p(&dragImage);
|
|
|
|
|
QPen pen(Qt::gray);
|
2011-05-09 14:36:11 +02:00
|
|
|
// pen.setWidth(2);
|
|
|
|
|
// p.setPen(pen);
|
|
|
|
|
// p.drawRect(1, 1, dragImage.width() - 2, dragImage.height() - 2);
|
2010-04-28 10:11:28 +02:00
|
|
|
return QPixmap::fromImage(dragImage);
|
|
|
|
|
}
|
2010-02-16 17:24:18 +02:00
|
|
|
|
2013-08-06 16:45:59 +02:00
|
|
|
void registerQmlTypes()
|
|
|
|
|
{
|
|
|
|
|
registerItemLibrarySortedModel();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QmlDesigner
|
2010-02-16 17:24:18 +02:00
|
|
|
|