2014-05-21 12:34:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2014-05-21 12:34:36 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2014-05-21 12:34:36 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2014-05-21 12:34:36 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2014-05-21 12:34:36 +02:00
|
|
|
** 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-21 18:24:30 +02:00
|
|
|
ItemLibrarySectionModel::ItemLibrarySectionModel(QObject *parent) :
|
2014-05-19 16:29:38 +02:00
|
|
|
QAbstractListModel(parent)
|
|
|
|
|
{
|
2014-05-22 17:16:50 +02:00
|
|
|
addRoleNames();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
ItemLibrarySectionModel::~ItemLibrarySectionModel()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-21 14:49:34 +02:00
|
|
|
clearItems();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
int ItemLibrarySectionModel::rowCount(const QModelIndex &) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-06-23 16:01:48 +02:00
|
|
|
return m_itemList.count();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
QVariant ItemLibrarySectionModel::data(const QModelIndex &index, int role) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-06-23 16:01:48 +02:00
|
|
|
if (!index.isValid() || index.row() + 1 > m_itemList.count()) {
|
2014-05-19 16:29:38 +02:00
|
|
|
qDebug() << Q_FUNC_INFO << "invalid index requested";
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_roleNames.contains(role)) {
|
2014-06-23 16:01:48 +02:00
|
|
|
QVariant value = m_itemList.at(index.row())->property(m_roleNames.value(role));
|
2014-05-19 16:29:38 +02:00
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
if (ItemLibrarySectionModel* model = qobject_cast<ItemLibrarySectionModel *>(value.value<QObject*>()))
|
2014-05-19 16:29:38 +02:00
|
|
|
return QVariant::fromValue(model);
|
|
|
|
|
|
2014-06-23 16:01:48 +02:00
|
|
|
return m_itemList.at(index.row())->property(m_roleNames.value(role));
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qWarning() << Q_FUNC_INFO << "invalid role requested";
|
|
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-28 17:33:47 +02:00
|
|
|
QHash<int, QByteArray> ItemLibrarySectionModel::roleNames() const
|
|
|
|
|
{
|
|
|
|
|
return m_roleNames;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
void ItemLibrarySectionModel::clearItems()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 14:06:28 +02:00
|
|
|
void ItemLibrarySectionModel::addItem(ItemLibraryItem *element)
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-26 14:05:12 +02:00
|
|
|
m_itemList.append(element);
|
2014-05-22 17:16:50 +02:00
|
|
|
|
|
|
|
|
element->setVisible(true);
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-26 14:05:12 +02:00
|
|
|
const QList<ItemLibraryItem *> &ItemLibrarySectionModel::items() const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-26 14:05:12 +02:00
|
|
|
return m_itemList;
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 12:00:15 +02:00
|
|
|
void ItemLibrarySectionModel::sortItems()
|
|
|
|
|
{
|
|
|
|
|
auto itemSort = [](ItemLibraryItem *first, ItemLibraryItem *second) {
|
|
|
|
|
return QString::localeAwareCompare(first->itemName(), second->itemName()) < 1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort(m_itemList.begin(), m_itemList.end(), itemSort);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-21 18:24:30 +02:00
|
|
|
void ItemLibrarySectionModel::resetModel()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-06-23 17:48:15 +02:00
|
|
|
beginResetModel();
|
|
|
|
|
endResetModel();
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-22 17:16:50 +02:00
|
|
|
void ItemLibrarySectionModel::addRoleNames()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-22 17:16:50 +02:00
|
|
|
int role = 0;
|
|
|
|
|
for (int propertyIndex = 0; propertyIndex < ItemLibraryItem::staticMetaObject.propertyCount(); ++propertyIndex) {
|
|
|
|
|
QMetaProperty property = ItemLibraryItem::staticMetaObject.property(propertyIndex);
|
|
|
|
|
m_roleNames.insert(role, property.name());
|
|
|
|
|
++role;
|
|
|
|
|
}
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
} // namespace QmlDesigner
|