1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Designer of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | // |
30 | // W A R N I N G |
31 | // ------------- |
32 | // |
33 | // This file is not part of the Qt API. It exists for the convenience |
34 | // of Qt Designer. This header |
35 | // file may change from version to version without notice, or even be removed. |
36 | // |
37 | // We mean it. |
38 | // |
39 | |
40 | #ifndef ACTIONREPOSITORY_H |
41 | #define ACTIONREPOSITORY_H |
42 | |
43 | #include "shared_global_p.h" |
44 | #include <QtCore/qmimedata.h> |
45 | #include <QtGui/qstandarditemmodel.h> |
46 | #include <QtWidgets/qtreeview.h> |
47 | #include <QtWidgets/qlistview.h> |
48 | #include <QtWidgets/qstackedwidget.h> |
49 | #include <QtGui/qicon.h> |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | class QPixmap; |
54 | |
55 | class QDesignerFormEditorInterface; |
56 | class QDesignerPropertySheetExtension; |
57 | |
58 | namespace qdesigner_internal { |
59 | |
60 | class PropertySheetKeySequenceValue; |
61 | |
62 | // Shared model of actions, to be used for several views (detailed/icon view). |
63 | class QDESIGNER_SHARED_EXPORT ActionModel: public QStandardItemModel |
64 | { |
65 | Q_OBJECT |
66 | public: |
67 | enum Columns { NameColumn, UsedColumn, TextColumn, ShortCutColumn, CheckedColumn, ToolTipColumn, NumColumns }; |
68 | enum { ActionRole = Qt::UserRole + 1000 }; |
69 | |
70 | explicit ActionModel(QWidget *parent = nullptr); |
71 | void initialize(QDesignerFormEditorInterface *core) { m_core = core; } |
72 | |
73 | void clearActions(); |
74 | QModelIndex addAction(QAction *a); |
75 | // remove row |
76 | void remove(int row); |
77 | // update the row from the underlying action |
78 | void update(int row); |
79 | |
80 | // return row of action or -1. |
81 | int findAction(QAction *) const; |
82 | |
83 | QString actionName(int row) const; |
84 | QAction *actionAt(const QModelIndex &index) const; |
85 | |
86 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
87 | QStringList mimeTypes() const override; |
88 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; |
89 | |
90 | // Find the associated menus and toolbars, ignore toolbuttons |
91 | static QWidgetList associatedWidgets(const QAction *action); |
92 | |
93 | // Retrieve shortcut via property sheet as it is a fake property |
94 | static PropertySheetKeySequenceValue actionShortCut(QDesignerFormEditorInterface *core, QAction *action); |
95 | static PropertySheetKeySequenceValue actionShortCut(const QDesignerPropertySheetExtension *ps); |
96 | |
97 | signals: |
98 | void resourceImageDropped(const QString &path, QAction *action); |
99 | |
100 | private: |
101 | using QStandardItemList = QList<QStandardItem *>; |
102 | |
103 | void (); |
104 | static void setItems(QDesignerFormEditorInterface *core, QAction *a, |
105 | const QIcon &defaultIcon, |
106 | QStandardItemList &sl); |
107 | |
108 | const QIcon m_emptyIcon; |
109 | |
110 | QDesignerFormEditorInterface *m_core = nullptr; |
111 | }; |
112 | |
113 | // Internal class that provides the detailed view of actions. |
114 | class ActionTreeView: public QTreeView |
115 | { |
116 | Q_OBJECT |
117 | public: |
118 | explicit ActionTreeView(ActionModel *model, QWidget *parent = nullptr); |
119 | QAction *currentAction() const; |
120 | |
121 | public slots: |
122 | void filter(const QString &text); |
123 | |
124 | signals: |
125 | void (QContextMenuEvent *event, QAction *); |
126 | void currentActionChanged(QAction *action); |
127 | void actionActivated(QAction *action, int column); |
128 | |
129 | protected slots: |
130 | void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override; |
131 | |
132 | protected: |
133 | void dragEnterEvent(QDragEnterEvent *event) override; |
134 | void dragMoveEvent(QDragMoveEvent *event) override; |
135 | void dropEvent(QDropEvent *event) override; |
136 | void focusInEvent(QFocusEvent *event) override; |
137 | void (QContextMenuEvent *event) override; |
138 | void startDrag(Qt::DropActions supportedActions) override; |
139 | |
140 | private slots: |
141 | void slotActivated(const QModelIndex &); |
142 | |
143 | private: |
144 | ActionModel *m_model; |
145 | }; |
146 | |
147 | // Internal class that provides the icon view of actions. |
148 | class ActionListView: public QListView |
149 | { |
150 | Q_OBJECT |
151 | public: |
152 | explicit ActionListView(ActionModel *model, QWidget *parent = nullptr); |
153 | QAction *currentAction() const; |
154 | |
155 | public slots: |
156 | void filter(const QString &text); |
157 | |
158 | signals: |
159 | void (QContextMenuEvent *event, QAction *); |
160 | void currentActionChanged(QAction *action); |
161 | void actionActivated(QAction *action); |
162 | |
163 | protected slots: |
164 | void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override; |
165 | |
166 | protected: |
167 | void dragEnterEvent(QDragEnterEvent *event) override; |
168 | void dragMoveEvent(QDragMoveEvent *event) override; |
169 | void dropEvent(QDropEvent *event) override; |
170 | void focusInEvent(QFocusEvent *event) override; |
171 | void (QContextMenuEvent *event) override; |
172 | void startDrag(Qt::DropActions supportedActions) override; |
173 | |
174 | private slots: |
175 | void slotActivated(const QModelIndex &); |
176 | |
177 | private: |
178 | ActionModel *m_model; |
179 | }; |
180 | |
181 | // Action View that can be switched between detailed and icon view |
182 | // using a QStackedWidget of ActionListView / ActionTreeView |
183 | // that share the item model and the selection model. |
184 | |
185 | class ActionView : public QStackedWidget { |
186 | Q_OBJECT |
187 | public: |
188 | // Separate initialize() function takes core argument to make this |
189 | // thing usable as promoted widget. |
190 | explicit ActionView(QWidget *parent = nullptr); |
191 | void initialize(QDesignerFormEditorInterface *core) { m_model->initialize(core); } |
192 | |
193 | // View mode |
194 | enum { DetailedView, IconView }; |
195 | int viewMode() const; |
196 | void setViewMode(int lm); |
197 | |
198 | void setSelectionMode(QAbstractItemView::SelectionMode sm); |
199 | QAbstractItemView::SelectionMode selectionMode() const; |
200 | |
201 | ActionModel *model() const { return m_model; } |
202 | |
203 | QAction *currentAction() const; |
204 | void setCurrentIndex(const QModelIndex &index); |
205 | |
206 | using ActionList = QList<QAction *>; |
207 | ActionList selectedActions() const; |
208 | QItemSelection selection() const; |
209 | |
210 | public slots: |
211 | void filter(const QString &text); |
212 | void selectAll(); |
213 | void clearSelection(); |
214 | |
215 | signals: |
216 | void (QContextMenuEvent *event, QAction *); |
217 | void currentChanged(QAction *action); |
218 | void activated(QAction *action, int column); |
219 | void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected); |
220 | void resourceImageDropped(const QString &data, QAction *action); |
221 | |
222 | private slots: |
223 | void slotCurrentChanged(QAction *action); |
224 | |
225 | private: |
226 | ActionModel *m_model; |
227 | ActionTreeView *m_actionTreeView; |
228 | ActionListView *m_actionListView; |
229 | }; |
230 | |
231 | class QDESIGNER_SHARED_EXPORT ActionRepositoryMimeData: public QMimeData |
232 | { |
233 | Q_OBJECT |
234 | public: |
235 | using ActionList = QList<QAction *>; |
236 | |
237 | ActionRepositoryMimeData(const ActionList &, Qt::DropAction dropAction); |
238 | ActionRepositoryMimeData(QAction *, Qt::DropAction dropAction); |
239 | |
240 | const ActionList &actionList() const { return m_actionList; } |
241 | QStringList formats() const override; |
242 | |
243 | static QPixmap actionDragPixmap(const QAction *action); |
244 | |
245 | // Utility to accept with right action |
246 | void accept(QDragMoveEvent *event) const; |
247 | private: |
248 | const Qt::DropAction m_dropAction; |
249 | ActionList m_actionList; |
250 | }; |
251 | |
252 | } // namespace qdesigner_internal |
253 | |
254 | QT_END_NAMESPACE |
255 | |
256 | #endif // ACTIONREPOSITORY_H |
257 | |