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 | #ifndef ACTIONPROVIDER_H |
30 | #define ACTIONPROVIDER_H |
31 | |
32 | // |
33 | // W A R N I N G |
34 | // ------------- |
35 | // |
36 | // This file is not part of the Qt API. It exists purely as an |
37 | // implementation detail. This header file may change from version to |
38 | // version without notice, or even be removed. |
39 | // |
40 | // We mean it. |
41 | // |
42 | |
43 | #include <QtDesigner/extension.h> |
44 | #include <QtCore/qpoint.h> |
45 | #include <QtCore/qrect.h> |
46 | #include <QtWidgets/qapplication.h> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | class QAction; |
51 | |
52 | class QDesignerActionProviderExtension |
53 | { |
54 | public: |
55 | virtual ~QDesignerActionProviderExtension() = default; |
56 | |
57 | virtual QRect actionGeometry(QAction *action) const = 0; |
58 | virtual QAction *actionAt(const QPoint &pos) const = 0; |
59 | |
60 | virtual void adjustIndicator(const QPoint &pos) = 0; |
61 | }; |
62 | |
63 | // Find action at the given position for a widget that has actionGeometry() (QToolBar, |
64 | // QMenuBar, QMenu). They usually have actionAt(), but that fails since Designer usually sets |
65 | // WA_TransparentForMouseEvents on the widgets. |
66 | template <class Widget> |
67 | int actionIndexAt(const Widget *w, const QPoint &pos, Qt::Orientation orientation) |
68 | { |
69 | const auto actions = w->actions(); |
70 | const int actionCount = actions.count(); |
71 | if (actionCount == 0) |
72 | return -1; |
73 | // actionGeometry() can be wrong sometimes; it returns a geometry that |
74 | // stretches to the end of the toolbar/menu bar. So, check from the beginning |
75 | // in the case of a horizontal right-to-left orientation. |
76 | const bool checkTopRight = orientation == Qt::Horizontal && w->layoutDirection() == Qt::RightToLeft; |
77 | const QPoint topRight = QPoint(w->rect().width(), 0); |
78 | for (int index = 0; index < actionCount; ++index) { |
79 | QRect g = w->actionGeometry(actions.at(index)); |
80 | if (checkTopRight) |
81 | g.setTopRight(topRight); |
82 | else |
83 | g.setTopLeft(QPoint(0, 0)); |
84 | |
85 | if (g.contains(p: pos)) |
86 | return index; |
87 | } |
88 | return -1; |
89 | } |
90 | |
91 | Q_DECLARE_EXTENSION_INTERFACE(QDesignerActionProviderExtension, "org.qt-project.Qt.Designer.ActionProvider" ) |
92 | |
93 | QT_END_NAMESPACE |
94 | |
95 | #endif // ACTIONPROVIDER_H |
96 | |