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
51QT_BEGIN_NAMESPACE
52
53class QPixmap;
54
55class QDesignerFormEditorInterface;
56class QDesignerPropertySheetExtension;
57
58namespace qdesigner_internal {
59
60class PropertySheetKeySequenceValue;
61
62// Shared model of actions, to be used for several views (detailed/icon view).
63class QDESIGNER_SHARED_EXPORT ActionModel: public QStandardItemModel
64{
65 Q_OBJECT
66public:
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
97signals:
98 void resourceImageDropped(const QString &path, QAction *action);
99
100private:
101 using QStandardItemList = QList<QStandardItem *>;
102
103 void initializeHeaders();
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.
114class ActionTreeView: public QTreeView
115{
116 Q_OBJECT
117public:
118 explicit ActionTreeView(ActionModel *model, QWidget *parent = nullptr);
119 QAction *currentAction() const;
120
121public slots:
122 void filter(const QString &text);
123
124signals:
125 void actionContextMenuRequested(QContextMenuEvent *event, QAction *);
126 void currentActionChanged(QAction *action);
127 void actionActivated(QAction *action, int column);
128
129protected slots:
130 void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
131
132protected:
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 contextMenuEvent(QContextMenuEvent *event) override;
138 void startDrag(Qt::DropActions supportedActions) override;
139
140private slots:
141 void slotActivated(const QModelIndex &);
142
143private:
144 ActionModel *m_model;
145};
146
147// Internal class that provides the icon view of actions.
148class ActionListView: public QListView
149{
150 Q_OBJECT
151public:
152 explicit ActionListView(ActionModel *model, QWidget *parent = nullptr);
153 QAction *currentAction() const;
154
155public slots:
156 void filter(const QString &text);
157
158signals:
159 void actionContextMenuRequested(QContextMenuEvent *event, QAction *);
160 void currentActionChanged(QAction *action);
161 void actionActivated(QAction *action);
162
163protected slots:
164 void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
165
166protected:
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 contextMenuEvent(QContextMenuEvent *event) override;
172 void startDrag(Qt::DropActions supportedActions) override;
173
174private slots:
175 void slotActivated(const QModelIndex &);
176
177private:
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
185class ActionView : public QStackedWidget {
186 Q_OBJECT
187public:
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
210public slots:
211 void filter(const QString &text);
212 void selectAll();
213 void clearSelection();
214
215signals:
216 void contextMenuRequested(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
222private slots:
223 void slotCurrentChanged(QAction *action);
224
225private:
226 ActionModel *m_model;
227 ActionTreeView *m_actionTreeView;
228 ActionListView *m_actionListView;
229};
230
231class QDESIGNER_SHARED_EXPORT ActionRepositoryMimeData: public QMimeData
232{
233 Q_OBJECT
234public:
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;
247private:
248 const Qt::DropAction m_dropAction;
249 ActionList m_actionList;
250};
251
252} // namespace qdesigner_internal
253
254QT_END_NAMESPACE
255
256#endif // ACTIONREPOSITORY_H
257

source code of qttools/src/designer/src/lib/shared/actionrepository_p.h