2014-05-21 12:34:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** 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
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "itemlibrarysectionmodel.h"
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
#include "itemlibraryitem.h"
|
2014-05-21 12:35:27 +02:00
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
2014-05-19 16:29:38 +02:00
|
|
|
ItemLibrarySortedModel::ItemLibrarySortedModel(QObject *parent) :
|
|
|
|
|
QAbstractListModel(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ItemLibrarySortedModel::~ItemLibrarySortedModel()
|
|
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
clearItems();
|
2014-05-19 16:29:38 +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";
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
void ItemLibrarySortedModel::clearItems()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
beginResetModel();
|
2014-05-21 14:49:34 +02:00
|
|
|
while (m_itemOrder.count() > 0)
|
|
|
|
|
removeItem(m_itemOrder.at(0).libId);
|
2014-05-19 16:29:38 +02:00
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool compareFunction(QObject *first, QObject *second)
|
|
|
|
|
{
|
|
|
|
|
static const char sortRoleName[] = "sortingRole";
|
|
|
|
|
|
|
|
|
|
return first->property(sortRoleName).toString() < second->property(sortRoleName).toString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
void ItemLibrarySortedModel::addItem(ItemLibraryItem *element, int libId)
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
struct order_struct orderEntry;
|
|
|
|
|
orderEntry.libId = libId;
|
|
|
|
|
orderEntry.visible = false;
|
|
|
|
|
|
|
|
|
|
int pos = 0;
|
2014-05-21 14:49:34 +02:00
|
|
|
while ((pos < m_itemOrder.count()) &&
|
|
|
|
|
compareFunction(m_itemModels.value(m_itemOrder.at(pos).libId), element))
|
2014-05-19 16:29:38 +02:00
|
|
|
++pos;
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
m_itemModels.insert(libId, element);
|
|
|
|
|
m_itemOrder.insert(pos, orderEntry);
|
2014-05-19 16:29:38 +02:00
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
setItemVisible(libId, true);
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
void ItemLibrarySortedModel::removeItem(int libId)
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
QObject *element = m_itemModels.value(libId);
|
|
|
|
|
int pos = findItem(libId);
|
2014-05-19 16:29:38 +02:00
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
setItemVisible(libId, false);
|
2014-05-19 16:29:38 +02:00
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
m_itemModels.remove(libId);
|
|
|
|
|
m_itemOrder.removeAt(pos);
|
2014-05-19 16:29:38 +02:00
|
|
|
|
|
|
|
|
delete element;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
bool ItemLibrarySortedModel::itemVisible(int libId) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
int pos = findItem(libId);
|
|
|
|
|
return m_itemOrder.at(pos).visible;
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
bool ItemLibrarySortedModel::setItemVisible(int libId, bool visible)
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
int pos = findItem(libId);
|
|
|
|
|
if (m_itemOrder.at(pos).visible == visible)
|
2014-05-19 16:29:38 +02:00
|
|
|
return false;
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
int visiblePos = visibleItemPosition(libId);
|
2014-05-19 16:29:38 +02:00
|
|
|
if (visible)
|
2014-05-21 14:49:34 +02:00
|
|
|
privateInsert(visiblePos, (m_itemModels.value(libId)));
|
2014-05-19 16:29:38 +02:00
|
|
|
else
|
|
|
|
|
privateRemove(visiblePos);
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
m_itemOrder[pos].visible = visible;
|
2014-05-19 16:29:38 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
const QMap<int, ItemLibraryItem*> &ItemLibrarySortedModel::items() const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
return m_itemModels;
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
const QList<ItemLibraryItem *> &ItemLibrarySortedModel::itemList() const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
return m_itemModels.values();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
ItemLibraryItem *ItemLibrarySortedModel::item(int libId)
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
return m_itemModels.value(libId);
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
int ItemLibrarySortedModel::findItem(int libId) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
int i = 0;
|
2014-05-21 14:49:34 +02:00
|
|
|
QListIterator<struct order_struct> it(m_itemOrder);
|
2014-05-19 16:29:38 +02:00
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
if (it.next().libId == libId)
|
|
|
|
|
return i;
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
int ItemLibrarySortedModel::visibleItemPosition(int libId) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
int i = 0;
|
2014-05-21 14:49:34 +02:00
|
|
|
QListIterator<struct order_struct> it(m_itemOrder);
|
2014-05-19 16:29:38 +02:00
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
struct order_struct order = it.next();
|
|
|
|
|
if (order.libId == libId)
|
|
|
|
|
return i;
|
|
|
|
|
if (order.visible)
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ItemLibrarySortedModel::resetModel()
|
|
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
ItemLibrarySectionModel::ItemLibrarySectionModel(int sectionLibId, const QString §ionName, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
m_name(sectionName),
|
2014-05-15 17:52:13 +02:00
|
|
|
m_sectionLibraryId(sectionLibId),
|
2014-05-21 12:34:36 +02:00
|
|
|
m_sectionExpanded(true),
|
2014-05-15 17:52:13 +02:00
|
|
|
m_sectionEntries(parent),
|
|
|
|
|
m_isVisible(false)
|
2014-05-21 12:34:36 +02:00
|
|
|
{
|
|
|
|
|
// if (collapsedStateHash.contains(sectionName))
|
|
|
|
|
// m_sectionExpanded= collapsedStateHash.value(sectionName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString ItemLibrarySectionModel::sectionName() const
|
|
|
|
|
{
|
|
|
|
|
return m_name;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-15 17:52:13 +02:00
|
|
|
int ItemLibrarySectionModel::sectionLibraryId() const
|
2014-05-21 12:34:36 +02:00
|
|
|
{
|
2014-05-15 17:52:13 +02:00
|
|
|
return m_sectionLibraryId;
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::sectionExpanded() const
|
|
|
|
|
{
|
|
|
|
|
return m_sectionExpanded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ItemLibrarySectionModel::sortingRole() const
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (sectionName() == QStringLiteral("QML Components")) //Qml Components always come first
|
|
|
|
|
return QVariant(QStringLiteral("AA.this_comes_first"));
|
|
|
|
|
|
|
|
|
|
return sectionName();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
void ItemLibrarySectionModel::addSectionEntry(ItemLibraryItem *sectionEntry)
|
2014-05-21 12:34:36 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
m_sectionEntries.addItem(sectionEntry, sectionEntry->itemLibId());
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ItemLibrarySectionModel::removeSectionEntry(int itemLibId)
|
|
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
m_sectionEntries.removeItem(itemLibId);
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QObject *ItemLibrarySectionModel::sectionEntries()
|
|
|
|
|
{
|
|
|
|
|
return &m_sectionEntries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ItemLibrarySectionModel::visibleItemIndex(int itemLibId)
|
|
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
return m_sectionEntries.visibleItemPosition(itemLibId);
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::isItemVisible(int itemLibId)
|
|
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
return m_sectionEntries.itemVisible(itemLibId);
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::updateSectionVisibility(const QString &searchText, bool *changed)
|
|
|
|
|
{
|
|
|
|
|
bool haveVisibleItems = false;
|
|
|
|
|
|
|
|
|
|
*changed = false;
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
QMap<int, ItemLibraryItem*>::const_iterator itemIterator = m_sectionEntries.items().constBegin();
|
2014-05-21 14:49:34 +02:00
|
|
|
while (itemIterator != m_sectionEntries.items().constEnd()) {
|
2014-05-21 12:34:36 +02:00
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
bool itemVisible = m_sectionEntries.item(itemIterator.key())->itemName().toLower().contains(searchText);
|
2014-05-21 12:34:36 +02:00
|
|
|
|
|
|
|
|
bool itemChanged = false;
|
2014-05-21 14:49:34 +02:00
|
|
|
itemChanged = m_sectionEntries.setItemVisible(itemIterator.key(), itemVisible);
|
2014-05-21 12:34:36 +02:00
|
|
|
|
|
|
|
|
*changed |= itemChanged;
|
|
|
|
|
|
2014-05-19 16:29:38 +02:00
|
|
|
if (itemVisible)
|
|
|
|
|
haveVisibleItems = true;
|
2014-05-21 12:34:36 +02:00
|
|
|
|
2014-05-21 14:49:34 +02:00
|
|
|
++itemIterator;
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sectionEntries.resetModel();
|
|
|
|
|
|
|
|
|
|
emit sectionEntriesChanged();
|
|
|
|
|
|
|
|
|
|
return haveVisibleItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ItemLibrarySectionModel::updateItemIconSize(const QSize &itemIconSize)
|
|
|
|
|
{
|
2014-05-21 15:35:03 +02:00
|
|
|
foreach (ItemLibraryItem* itemLibraryItemModel, m_sectionEntries.itemList()) {
|
2014-05-21 14:49:34 +02:00
|
|
|
itemLibraryItemModel->setItemIconSize(itemIconSize);
|
|
|
|
|
}
|
2014-05-15 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::setVisible(bool isVisible)
|
|
|
|
|
{
|
|
|
|
|
if (isVisible != m_isVisible) {
|
|
|
|
|
m_isVisible = isVisible;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ItemLibrarySectionModel::isVisible() const
|
|
|
|
|
{
|
|
|
|
|
return m_isVisible;
|
2014-05-21 12:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace QmlDesigner
|