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 #pragma once
27 
28 #include <QAbstractListModel>
29 #include <QDateTime>
30 #include <QDir>
31 #include <QHash>
32 #include <QIcon>
33 #include <QPair>
34 #include <QSet>
35 #include <QTimer>
36 
37 #include "itemlibraryassetsdir.h"
38 
39 namespace Utils { class FileSystemWatcher; }
40 
41 namespace QmlDesigner {
42 
43 class SynchronousImageCache;
44 
45 class ItemLibraryAssetsModel : public QAbstractListModel
46 {
47     Q_OBJECT
48 
49 public:
50     ItemLibraryAssetsModel(QmlDesigner::SynchronousImageCache &fontImageCache,
51                            Utils::FileSystemWatcher *fileSystemWatcher,
52                            QObject *parent = nullptr);
53 
54     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
55     int rowCount(const QModelIndex & parent = QModelIndex()) const override;
56     QHash<int, QByteArray> roleNames() const override;
57 
58     void refresh();
59     void setRootPath(const QString &path);
60     void setSearchText(const QString &searchText);
61 
62     static const QStringList &supportedImageSuffixes();
63     static const QStringList &supportedFragmentShaderSuffixes();
64     static const QStringList &supportedShaderSuffixes();
65     static const QStringList &supportedFontSuffixes();
66     static const QStringList &supportedAudioSuffixes();
67     static const QStringList &supportedTexture3DSuffixes();
68 
69     const QSet<QString> &supportedSuffixes() const;
70     const QSet<QString> &previewableSuffixes() const;
71 
72     static void saveExpandedState(bool expanded, const QString &assetPath);
73     static bool loadExpandedState(const QString &assetPath);
74 
75     enum class DirExpandState {
76         SomeExpanded,
77         AllExpanded,
78         AllCollapsed
79     };
80     Q_ENUM(DirExpandState)
81 
82     Q_INVOKABLE void toggleExpandAll(bool expand);
83     Q_INVOKABLE DirExpandState getAllExpandedState() const;
84     Q_INVOKABLE void removeFile(const QString &filePath);
85 
86 private:
87     SynchronousImageCache &m_fontImageCache;
88     QHash<QString, QPair<QDateTime, QIcon>> m_iconCache;
89 
90     QString m_searchText;
91     Utils::FileSystemWatcher *m_fileSystemWatcher = nullptr;
92     ItemLibraryAssetsDir *m_assetsDir = nullptr;
93 
94     QHash<int, QByteArray> m_roleNames;
95     inline static QHash<QString, bool> m_expandedStateHash; // <assetPath, isExpanded>
96 };
97 
98 } // namespace QmlDesigner
99