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
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
#include "itemlibraryitemsmodel.h"
|
2014-05-21 15:35:03 +02:00
|
|
|
#include "itemlibraryitem.h"
|
2020-03-30 15:23:46 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2017-10-06 10:09:16 +02:00
|
|
|
#include <QDebug>
|
2021-02-04 16:18:45 +02:00
|
|
|
#include <QMetaProperty>
|
2017-10-06 10:09:16 +02:00
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
ItemLibraryItemsModel::ItemLibraryItemsModel(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
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
ItemLibraryItemsModel::~ItemLibraryItemsModel()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
int ItemLibraryItemsModel::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
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
QVariant ItemLibraryItemsModel::data(const QModelIndex &index, int role) const
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2021-02-04 16:18:45 +02:00
|
|
|
if (!index.isValid() || index.row() >= m_itemList.count()) {
|
2014-05-19 16:29:38 +02:00
|
|
|
qDebug() << Q_FUNC_INFO << "invalid index requested";
|
2021-02-04 16:18:45 +02:00
|
|
|
return {};
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
if (m_roleNames.contains(role))
|
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";
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
return {};
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
QHash<int, QByteArray> ItemLibraryItemsModel::roleNames() const
|
2014-08-28 17:33:47 +02:00
|
|
|
{
|
|
|
|
|
return m_roleNames;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
void ItemLibraryItemsModel::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
|
|
|
|
2021-03-02 17:15:59 +02:00
|
|
|
element->setVisible(element->isUsable());
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
const QList<QPointer<ItemLibraryItem>> &ItemLibraryItemsModel::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
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
void ItemLibraryItemsModel::sortItems()
|
2014-07-03 12:00:15 +02:00
|
|
|
{
|
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) {
|
2020-04-28 22:05:05 +02:00
|
|
|
return QString::localeAwareCompare(first->itemName(), second->itemName()) < 0;
|
2014-07-03 12:00:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::sort(m_itemList.begin(), m_itemList.end(), itemSort);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
void ItemLibraryItemsModel::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
|
|
|
}
|
|
|
|
|
|
2021-02-04 16:18:45 +02:00
|
|
|
void ItemLibraryItemsModel::addRoleNames()
|
2014-05-19 16:29:38 +02:00
|
|
|
{
|
2014-05-22 17:16:50 +02:00
|
|
|
int role = 0;
|
2021-02-04 16:18:45 +02:00
|
|
|
const QMetaObject meta = ItemLibraryItem::staticMetaObject;
|
|
|
|
|
for (int i = meta.propertyOffset(); i < meta.propertyCount(); ++i)
|
|
|
|
|
m_roleNames.insert(role++, meta.property(i).name());
|
2014-05-19 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-21 12:34:36 +02:00
|
|
|
} // namespace QmlDesigner
|