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 "itemlibraryinfo.h"
29 #include "import.h"
30 
31 #include <utils/fancylineedit.h>
32 #include <utils/dropsupport.h>
33 #include <previewtooltip/previewtooltipbackend.h>
34 #include "itemlibraryassetsmodel.h"
35 
36 #include <QFrame>
37 #include <QToolButton>
38 #include <QFileIconProvider>
39 #include <QQuickWidget>
40 #include <QQmlPropertyMap>
41 #include <QTimer>
42 #include <QPointF>
43 
44 #include <memory>
45 
46 QT_BEGIN_NAMESPACE
47 class QStackedWidget;
48 class QShortcut;
49 QT_END_NAMESPACE
50 
51 namespace Utils { class FileSystemWatcher; }
52 
53 namespace QmlDesigner {
54 
55 class MetaInfo;
56 class ItemLibraryEntry;
57 class Model;
58 class CustomFileSystemModel;
59 
60 class ItemLibraryModel;
61 class ItemLibraryAssetsIconProvider;
62 class ItemLibraryAssetsModel;
63 class ItemLibraryAddImportModel;
64 class ItemLibraryResourceView;
65 class SynchronousImageCache;
66 class AsynchronousImageCache;
67 class ImageCacheCollector;
68 
69 class ItemLibraryWidget : public QFrame
70 {
71     Q_OBJECT
72 
73 public:
74     ItemLibraryWidget(AsynchronousImageCache &imageCache,
75                       AsynchronousImageCache &asynchronousFontImageCache,
76                       SynchronousImageCache &synchronousFontImageCache);
77     ~ItemLibraryWidget();
78 
79     void setItemLibraryInfo(ItemLibraryInfo *itemLibraryInfo);
80     QList<QToolButton *> createToolBarWidgets();
81 
82     static QString qmlSourcesPath();
83     void clearSearchFilter();
84 
85     void delayedUpdateModel();
86     void updateModel();
87     void updatePossibleImports(const QList<Import> &possibleImports);
88     void updateUsedImports(const QList<Import> &usedImports);
89 
90     void setResourcePath(const QString &resourcePath);
91     void setModel(Model *model);
92     void setFlowMode(bool b);
93     static QPair<QString, QByteArray> getAssetTypeAndData(const QString &assetPath);
94 
95     Q_INVOKABLE void startDragAndDrop(const QVariant &itemLibEntry, const QPointF &mousePos);
96     Q_INVOKABLE void startDragAsset(const QStringList &assetPaths, const QPointF &mousePos);
97     Q_INVOKABLE void removeImport(const QString &importUrl);
98     Q_INVOKABLE void addImportForItem(const QString &importUrl);
99     Q_INVOKABLE void handleTabChanged(int index);
100     Q_INVOKABLE void handleAddModule();
101     Q_INVOKABLE void handleAddAsset();
102     Q_INVOKABLE void handleSearchfilterChanged(const QString &filterText);
103     Q_INVOKABLE void handleAddImport(int index);
104     Q_INVOKABLE bool isSearchActive() const;
105     Q_INVOKABLE void handleFilesDrop(const QStringList &filesPaths);
supportedSuffixes()106     Q_INVOKABLE QSet<QString> supportedSuffixes() const { return m_assetsModel->supportedSuffixes(); };
107 
108 signals:
109     void itemActivated(const QString& itemName);
110 
111 protected:
112     bool eventFilter(QObject *obj, QEvent *event) override;
113 
114 private:
115     void reloadQmlSource();
116 
117     void addResources(const QStringList &files);
118     void updateSearch();
119     void handlePriorityImportsChanged();
120 
121     QTimer m_compressionTimer;
122     QTimer m_assetCompressionTimer;
123     QSize m_itemIconSize;
124 
125     SynchronousImageCache &m_fontImageCache;
126     QPointer<ItemLibraryInfo> m_itemLibraryInfo;
127 
128     QPointer<ItemLibraryModel> m_itemLibraryModel;
129     QPointer<ItemLibraryAddImportModel> m_itemLibraryAddImportModel;
130     ItemLibraryAssetsIconProvider *m_assetsIconProvider = nullptr;
131     Utils::FileSystemWatcher *m_fileSystemWatcher = nullptr;
132     QPointer<ItemLibraryAssetsModel> m_assetsModel;
133 
134     QPointer<QStackedWidget> m_stackedWidget;
135 
136     QScopedPointer<QQuickWidget> m_headerWidget;
137     QScopedPointer<QQuickWidget> m_addImportWidget;
138     QScopedPointer<QQuickWidget> m_itemViewQuickWidget;
139     QScopedPointer<QQuickWidget> m_assetsWidget;
140     std::unique_ptr<PreviewTooltipBackend> m_previewTooltipBackend;
141     std::unique_ptr<PreviewTooltipBackend> m_fontPreviewTooltipBackend;
142 
143     QShortcut *m_qmlSourceUpdateShortcut;
144     AsynchronousImageCache &m_imageCache;
145     QPointer<Model> m_model;
146     QVariant m_itemToDrag;
147     QStringList m_assetsToDrag;
148     QPair<QString, QByteArray> m_assetToDragTypeAndData;
149     bool m_updateRetry = false;
150     QString m_filterText;
151     QPoint m_dragStartPoint;
152 };
153 
154 } // namespace QmlDesigner
155