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 #pragma once
27 
28 #include <QAbstractListModel>
29 #include <QtQml/qqml.h>
30 #include <import.h>
31 
QT_FORWARD_DECLARE_CLASS(QMimeData)32 QT_FORWARD_DECLARE_CLASS(QMimeData)
33 
34 namespace QmlDesigner {
35 
36 class ItemLibraryInfo;
37 class ItemLibraryEntry;
38 class Model;
39 class ItemLibraryImport;
40 
41 class ItemLibraryModel : public QAbstractListModel
42 {
43     Q_OBJECT
44     Q_PROPERTY(bool isAnyCategoryHidden READ isAnyCategoryHidden WRITE setIsAnyCategoryHidden NOTIFY isAnyCategoryHiddenChanged FINAL)
45 
46 public:
47     explicit ItemLibraryModel(QObject *parent = nullptr);
48     ~ItemLibraryModel() override;
49 
50     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
51     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
52     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
53     QHash<int, QByteArray> roleNames() const override;
54 
55     QString searchText() const;
56     ItemLibraryImport *importByUrl(const QString &importName) const;
57 
58     void update(ItemLibraryInfo *itemLibraryInfo, Model *model);
59     void updateUsedImports(const QList<Import> &usedImports);
60 
61     QMimeData *getMimeData(const ItemLibraryEntry &itemLibraryEntry);
62 
63     void setSearchText(const QString &searchText);
64     void setFlowMode(bool);
65 
66     bool isAnyCategoryHidden() const;
67     void setIsAnyCategoryHidden(bool state);
68 
69     static void registerQmlTypes();
70     static void saveExpandedState(bool expanded, const QString &sectionName);
71     static bool loadExpandedState(const QString &sectionName);
72     static void saveCategoryVisibleState(bool isVisible, const QString &categoryName, const QString
73                                          &importName);
74     static bool loadCategoryVisibleState(const QString &categoryName, const QString &importName);
75 
76     Q_INVOKABLE void expandAll();
77     Q_INVOKABLE void collapseAll();
78     Q_INVOKABLE void showHiddenCategories();
79     Q_INVOKABLE bool getIsAnyCategoryHidden() const;
80 
81     Import entryToImport(const ItemLibraryEntry &entry);
82 
83     inline static QHash<QString, QString> categorySortingHash;
84 
85 signals:
86     void isAnyCategoryHiddenChanged();
87 
88 private:
89     void updateVisibility(bool *changed);
90     void addRoleNames();
91     void sortSections();
92     void clearSections();
93 
94     QList<QPointer<ItemLibraryImport>> m_importList;
95     QHash<int, QByteArray> m_roleNames;
96 
97     QString m_searchText;
98     bool m_flowMode = false;
99     bool m_isAnyCategoryHidden = false;
100 
101     inline static QHash<QString, bool> expandedStateHash;
102     inline static QHash<QString, bool> categoryVisibleStateHash;
103 };
104 
105 } // namespace QmlDesigner
106