1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "itemlibraryitemsmodel.h"
27 #include "itemlibraryitem.h"
28 #include <utils/qtcassert.h>
29 #include <QDebug>
30 #include <QMetaProperty>
31
32 namespace QmlDesigner {
33
ItemLibraryItemsModel(QObject * parent)34 ItemLibraryItemsModel::ItemLibraryItemsModel(QObject *parent) :
35 QAbstractListModel(parent)
36 {
37 addRoleNames();
38 }
39
~ItemLibraryItemsModel()40 ItemLibraryItemsModel::~ItemLibraryItemsModel()
41 {
42 }
43
rowCount(const QModelIndex &) const44 int ItemLibraryItemsModel::rowCount(const QModelIndex &) const
45 {
46 return m_itemList.count();
47 }
48
data(const QModelIndex & index,int role) const49 QVariant ItemLibraryItemsModel::data(const QModelIndex &index, int role) const
50 {
51 if (!index.isValid() || index.row() >= m_itemList.count()) {
52 qDebug() << Q_FUNC_INFO << "invalid index requested";
53 return {};
54 }
55
56 if (m_roleNames.contains(role))
57 return m_itemList.at(index.row())->property(m_roleNames.value(role));
58
59 qWarning() << Q_FUNC_INFO << "invalid role requested";
60
61 return {};
62 }
63
roleNames() const64 QHash<int, QByteArray> ItemLibraryItemsModel::roleNames() const
65 {
66 return m_roleNames;
67 }
68
addItem(ItemLibraryItem * element)69 void ItemLibraryItemsModel::addItem(ItemLibraryItem *element)
70 {
71 m_itemList.append(element);
72
73 element->setVisible(element->isUsable());
74 }
75
items() const76 const QList<QPointer<ItemLibraryItem>> &ItemLibraryItemsModel::items() const
77 {
78 return m_itemList;
79 }
80
sortItems()81 void ItemLibraryItemsModel::sortItems()
82 {
83 int nullPointerSectionCount = m_itemList.removeAll(QPointer<ItemLibraryItem>());
84 QTC_ASSERT(nullPointerSectionCount == 0,;);
85 auto itemSort = [](ItemLibraryItem *first, ItemLibraryItem *second) {
86 return QString::localeAwareCompare(first->itemName(), second->itemName()) < 0;
87 };
88
89 std::sort(m_itemList.begin(), m_itemList.end(), itemSort);
90 }
91
resetModel()92 void ItemLibraryItemsModel::resetModel()
93 {
94 beginResetModel();
95 endResetModel();
96 }
97
addRoleNames()98 void ItemLibraryItemsModel::addRoleNames()
99 {
100 int role = 0;
101 const QMetaObject meta = ItemLibraryItem::staticMetaObject;
102 for (int i = meta.propertyOffset(); i < meta.propertyCount(); ++i)
103 m_roleNames.insert(role++, meta.property(i).name());
104 }
105
106 } // namespace QmlDesigner
107