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 QDESIGNER_MENU_H
41 #define QDESIGNER_MENU_H
42 
43 #include "shared_global_p.h"
44 
45 #include <QtWidgets/qaction.h>
46 #include <QtWidgets/qmenu.h>
47 #include <QtGui/qpixmap.h>
48 #include <QtCore/qhash.h>
49 
50 QT_BEGIN_NAMESPACE
51 
52 class QTimer;
53 class QLineEdit;
54 
55 class QDesignerFormWindowInterface;
56 class QDesignerActionProviderExtension;
57 class QDesignerMenu;
58 class QDesignerMenuBar;
59 class QPainter;
60 class QMimeData;
61 
62 namespace qdesigner_internal {
63     class CreateSubmenuCommand;
64     class ActionInsertionCommand;
65 }
66 
67 class QDESIGNER_SHARED_EXPORT QDesignerMenu: public QMenu
68 {
69     Q_OBJECT
70 public:
71     QDesignerMenu(QWidget *parent = nullptr);
72     ~QDesignerMenu() override;
73 
74     bool eventFilter(QObject *object, QEvent *event) override;
75 
76     QDesignerFormWindowInterface *formWindow() const;
77     QDesignerActionProviderExtension *actionProvider();
78 
79     QDesignerMenu *parentMenu() const;
80     QDesignerMenuBar *parentMenuBar() const;
81 
82     void setVisible(bool visible) override;
83 
84     void adjustSpecialActions();
85 
86     void createRealMenuAction(QAction *action);
87     void removeRealMenu(QAction *action);
88 
89     static void drawSelection(QPainter *p, const QRect &r);
90 
91     bool dragging() const;
92 
93     void closeMenuChain();
94 
95     void moveLeft();
96     void moveRight();
97     void moveUp(bool ctrl);
98     void moveDown(bool ctrl);
99 
100     // Helper for MenuTaskMenu extension
101     void deleteAction(QAction *a);
102 
103 private slots:
104     void slotAddSeparator();
105     void slotRemoveSelectedAction();
106     void slotShowSubMenuNow();
107     void slotDeactivateNow();
108     void slotAdjustSizeNow();
109 
110 protected:
111     void actionEvent(QActionEvent *event) override;
112     void dragEnterEvent(QDragEnterEvent *event) override;
113     void dragMoveEvent(QDragMoveEvent *event) override;
114     void dragLeaveEvent(QDragLeaveEvent *event) override;
115     void dropEvent(QDropEvent *event) override;
116     void paintEvent(QPaintEvent *event) override;
117     void keyPressEvent(QKeyEvent *event) override;
118     void keyReleaseEvent(QKeyEvent *event) override;
119     void showEvent(QShowEvent *event) override;
120 
121     bool handleEvent(QWidget *widget, QEvent *event);
122     bool handleMouseDoubleClickEvent(QWidget *widget, QMouseEvent *event);
123     bool handleMousePressEvent(QWidget *widget, QMouseEvent *event);
124     bool handleMouseReleaseEvent(QWidget *widget, QMouseEvent *event);
125     bool handleMouseMoveEvent(QWidget *widget, QMouseEvent *event);
126     bool handleContextMenuEvent(QWidget *widget, QContextMenuEvent *event);
127     bool handleKeyPressEvent(QWidget *widget, QKeyEvent *event);
128 
129     void startDrag(const QPoint &pos, Qt::KeyboardModifiers modifiers);
130 
131     void adjustIndicator(const QPoint &pos);
132     int findAction(const QPoint &pos) const;
133 
134     QAction *currentAction() const;
135     int realActionCount() const;
136     enum ActionDragCheck { NoActionDrag, ActionDragOnSubMenu, AcceptActionDrag };
137     ActionDragCheck checkAction(QAction *action) const;
138 
139     void showSubMenu(QAction *action);
140 
141     enum LeaveEditMode {
142         Default = 0,
143         ForceAccept
144     };
145 
146     void enterEditMode();
147     void leaveEditMode(LeaveEditMode mode);
148     void showLineEdit();
149 
150     QAction *createAction(const QString &text, bool separator = false);
151     QDesignerMenu *findOrCreateSubMenu(QAction *action);
152 
153     QAction *safeActionAt(int index) const;
154     QAction *safeMenuAction(QDesignerMenu *menu) const;
155     bool swap(int a, int b);
156 
157     void hideSubMenu();
158     void deleteAction();
159     void deactivateMenu();
160 
161     bool canCreateSubMenu(QAction *action) const;
162     QDesignerMenu *findRootMenu() const;
163     QDesignerMenu *findActivatedMenu() const;
164 
165     QRect subMenuPixmapRect(QAction *action) const;
166     bool hasSubMenuPixmap(QAction *action) const;
167 
168     void selectCurrentAction();
169 
170 private:
171     bool hideSubMenuOnCursorKey();
172     bool showSubMenuOnCursorKey();
173     const QPixmap m_subMenuPixmap;
174 
175     QPoint m_startPosition;
176     int m_currentIndex = 0;
177     QAction *m_addItem;
178     QAction *m_addSeparator;
179     QHash<QAction*, QDesignerMenu*> m_subMenus;
180     QTimer *m_showSubMenuTimer;
181     QTimer *m_deactivateWindowTimer;
182     QTimer *m_adjustSizeTimer;
183     QLineEdit *m_editor;
184     bool m_dragging = false;
185     int m_lastSubMenuIndex = -1;
186 
187     friend class qdesigner_internal::CreateSubmenuCommand;
188     friend class qdesigner_internal::ActionInsertionCommand;
189 };
190 
191 QT_END_NAMESPACE
192 
193 #endif // QDESIGNER_MENU_H
194