2014-05-21 12:34:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://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
|
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.
|
2014-05-21 12:34:36 +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.
|
2014-05-21 12:34:36 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "itemlibrarysectionmodel.h"
|
|
|
|
|
|
2014-05-21 15:35:03 +02:00
|
|
|
#include "itemlibraryitem.h"
|
2014-05-21 12:35:27 +02:00
|
|
|
|
2020-03-30 15:23:46 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2017-10-06 10:09:16 +02:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
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 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
|
|
|
|
2018-07-24 23:56:45 +02:00
|
|
|
if (auto 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-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
|
|
|
}
|
|
|
|
|
|
2020-03-30 15:23:46 +02:00
|
|
|
const QList<QPointer<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()
|
|
|
|
|
{
|
2020-03-30 15:23:46 +02:00
|
|
|
int nullPointerSectionCount = m_itemList.removeAll(QPointer<ItemLibraryItem>());
|
|
|
|
|
QTC_ASSERT(nullPointerSectionCount == 0,;);
|
2014-07-03 12:00:15 +02:00
|
|
|
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
|