1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 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 "itemlibrarycategory.h"
27 
28 #include "itemlibraryitem.h"
29 
30 namespace QmlDesigner {
31 
ItemLibraryCategory(const QString & groupName,QObject * parent)32 ItemLibraryCategory::ItemLibraryCategory(const QString &groupName, QObject *parent)
33     : QObject(parent),
34       m_ownerImport(qobject_cast<ItemLibraryImport *>(parent)),
35       m_name(groupName)
36 {
37 }
38 
categoryName() const39 QString ItemLibraryCategory::categoryName() const
40 {
41     return m_name;
42 }
43 
categoryExpanded() const44 bool ItemLibraryCategory::categoryExpanded() const
45 {
46     return m_categoryExpanded;
47 }
48 
sortingName() const49 QString ItemLibraryCategory::sortingName() const
50 {
51     if (ItemLibraryModel::categorySortingHash.contains(categoryName()))
52         return ItemLibraryModel::categorySortingHash.value(categoryName());
53 
54     return categoryName();
55 }
56 
addItem(ItemLibraryItem * itemEntry)57 void ItemLibraryCategory::addItem(ItemLibraryItem *itemEntry)
58 {
59     m_itemModel.addItem(itemEntry);
60 }
61 
itemModel()62 QObject *ItemLibraryCategory::itemModel()
63 {
64     return &m_itemModel;
65 }
66 
updateItemVisibility(const QString & searchText,bool * changed)67 bool ItemLibraryCategory::updateItemVisibility(const QString &searchText, bool *changed)
68 {
69     bool hasVisibleItems = false;
70 
71     *changed = false;
72 
73     for (const auto &item : m_itemModel.items()) {
74         bool itemVisible = item->itemName().toLower().contains(searchText)
75                         || item->typeName().toLower().contains(searchText);
76 
77         if (searchText.isEmpty() && !item->isUsable())
78             itemVisible = false;
79         bool itemChanged = item->setVisible(itemVisible);
80 
81         *changed |= itemChanged;
82 
83         if (itemVisible)
84             hasVisibleItems = true;
85     }
86 
87     // expand category if it has an item matching search criteria
88     if (!searchText.isEmpty() && hasVisibleItems && !categoryExpanded())
89         setExpanded(true);
90 
91     return hasVisibleItems;
92 }
93 
setCategoryVisible(bool isVisible)94 void ItemLibraryCategory::setCategoryVisible(bool isVisible)
95 {
96     if (isVisible != m_isVisible) {
97         m_isVisible = isVisible;
98         emit categoryVisibilityChanged();
99     }
100 }
101 
setVisible(bool isVisible)102 bool ItemLibraryCategory::setVisible(bool isVisible)
103 {
104     if (isVisible != m_isVisible) {
105         m_isVisible = isVisible;
106         return true;
107     }
108 
109     return false;
110 }
111 
isCategoryVisible() const112 bool ItemLibraryCategory::isCategoryVisible() const
113 {
114     return m_isVisible;
115 }
116 
sortItems()117 void ItemLibraryCategory::sortItems()
118 {
119     m_itemModel.sortItems();
120 }
121 
setExpanded(bool expanded)122 void ItemLibraryCategory::setExpanded(bool expanded)
123 {
124     m_categoryExpanded = expanded;
125 }
126 
127 } // namespace QmlDesigner
128