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 QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QDYNAMICTOOLBAR_H
41 #define QDYNAMICTOOLBAR_H
42 
43 #include <QtWidgets/qtwidgetsglobal.h>
44 #include <QtWidgets/qaction.h>
45 #include <QtWidgets/qwidget.h>
46 
47 QT_REQUIRE_CONFIG(toolbar);
48 
49 QT_BEGIN_NAMESPACE
50 
51 class QToolBarPrivate;
52 
53 class QAction;
54 class QIcon;
55 class QMainWindow;
56 class QStyleOptionToolBar;
57 
58 class Q_WIDGETS_EXPORT QToolBar : public QWidget
59 {
60     Q_OBJECT
61 
62     Q_PROPERTY(bool movable READ isMovable WRITE setMovable NOTIFY movableChanged)
63     Q_PROPERTY(Qt::ToolBarAreas allowedAreas READ allowedAreas WRITE setAllowedAreas NOTIFY allowedAreasChanged)
64     Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged)
65     Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
66     Q_PROPERTY(Qt::ToolButtonStyle toolButtonStyle READ toolButtonStyle WRITE setToolButtonStyle
67                NOTIFY toolButtonStyleChanged)
68     Q_PROPERTY(bool floating READ isFloating)
69     Q_PROPERTY(bool floatable READ isFloatable WRITE setFloatable)
70 
71 public:
72     explicit QToolBar(const QString &title, QWidget *parent = nullptr);
73     explicit QToolBar(QWidget *parent = nullptr);
74     ~QToolBar();
75 
76     void setMovable(bool movable);
77     bool isMovable() const;
78 
79     void setAllowedAreas(Qt::ToolBarAreas areas);
80     Qt::ToolBarAreas allowedAreas() const;
81 
isAreaAllowed(Qt::ToolBarArea area)82     inline bool isAreaAllowed(Qt::ToolBarArea area) const
83     { return (allowedAreas() & area) == area; }
84 
85     void setOrientation(Qt::Orientation orientation);
86     Qt::Orientation orientation() const;
87 
88     void clear();
89 
90     using QWidget::addAction;
91     QAction *addAction(const QString &text);
92     QAction *addAction(const QIcon &icon, const QString &text);
93     QAction *addAction(const QString &text, const QObject *receiver, const char* member);
94     QAction *addAction(const QIcon &icon, const QString &text,
95                        const QObject *receiver, const char* member);
96 #ifdef Q_CLANG_QDOC
97     template<typename Functor>
98     QAction *addAction(const QString &text, Functor functor);
99     template<typename Functor>
100     QAction *addAction(const QString &text, const QObject *context, Functor functor);
101     template<typename Functor>
102     QAction *addAction(const QIcon &icon, const QString &text, Functor functor);
103     template<typename Functor>
104     QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor);
105 #else
106     // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
107     template<class Obj, typename Func1>
108     inline typename std::enable_if<!std::is_same<const char*, Func1>::value
109         && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
addAction(const QString & text,const Obj * object,Func1 slot)110         addAction(const QString &text, const Obj *object, Func1 slot)
111     {
112         QAction *result = addAction(text);
113         connect(result, &QAction::triggered, object, std::move(slot));
114         return result;
115     }
116     // addAction(QString): Connect to a functor or function pointer (without context)
117     template <typename Func1>
addAction(const QString & text,Func1 slot)118     inline QAction *addAction(const QString &text, Func1 slot)
119     {
120         QAction *result = addAction(text);
121         connect(result, &QAction::triggered, slot);
122         return result;
123     }
124     // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
125     template<class Obj, typename Func1>
126     inline typename std::enable_if<!std::is_same<const char*, Func1>::value
127         && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
addAction(const QIcon & actionIcon,const QString & text,const Obj * object,Func1 slot)128         addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot)
129     {
130         QAction *result = addAction(actionIcon, text);
131         connect(result, &QAction::triggered, object, std::move(slot));
132         return result;
133     }
134     // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
135     template <typename Func1>
addAction(const QIcon & actionIcon,const QString & text,Func1 slot)136     inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot)
137     {
138         QAction *result = addAction(actionIcon, text);
139         connect(result, &QAction::triggered, slot);
140         return result;
141     }
142 #endif // !Q_CLANG_QDOC
143 
144     QAction *addSeparator();
145     QAction *insertSeparator(QAction *before);
146 
147     QAction *addWidget(QWidget *widget);
148     QAction *insertWidget(QAction *before, QWidget *widget);
149 
150     QRect actionGeometry(QAction *action) const;
151     QAction *actionAt(const QPoint &p) const;
152     inline QAction *actionAt(int x, int y) const;
153 
154     QAction *toggleViewAction() const;
155 
156     QSize iconSize() const;
157     Qt::ToolButtonStyle toolButtonStyle() const;
158 
159     QWidget *widgetForAction(QAction *action) const;
160 
161     bool isFloatable() const;
162     void setFloatable(bool floatable);
163     bool isFloating() const;
164 
165 public Q_SLOTS:
166     void setIconSize(const QSize &iconSize);
167     void setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle);
168 
169 Q_SIGNALS:
170     void actionTriggered(QAction *action);
171     void movableChanged(bool movable);
172     void allowedAreasChanged(Qt::ToolBarAreas allowedAreas);
173     void orientationChanged(Qt::Orientation orientation);
174     void iconSizeChanged(const QSize &iconSize);
175     void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle);
176     void topLevelChanged(bool topLevel);
177     void visibilityChanged(bool visible);
178 
179 protected:
180     void actionEvent(QActionEvent *event) override;
181     void changeEvent(QEvent *event) override;
182     void paintEvent(QPaintEvent *event) override;
183     bool event(QEvent *event) override;
184     void initStyleOption(QStyleOptionToolBar *option) const;
185 
186 
187 private:
188     Q_DECLARE_PRIVATE(QToolBar)
189     Q_DISABLE_COPY(QToolBar)
190     Q_PRIVATE_SLOT(d_func(), void _q_toggleView(bool))
191     Q_PRIVATE_SLOT(d_func(), void _q_updateIconSize(const QSize &))
192     Q_PRIVATE_SLOT(d_func(), void _q_updateToolButtonStyle(Qt::ToolButtonStyle))
193 
194     friend class QMainWindow;
195     friend class QMainWindowLayout;
196     friend class QToolBarLayout;
197     friend class QToolBarAreaLayout;
198 };
199 
actionAt(int ax,int ay)200 inline QAction *QToolBar::actionAt(int ax, int ay) const
201 { return actionAt(QPoint(ax, ay)); }
202 
203 QT_END_NAMESPACE
204 
205 #endif // QDYNAMICTOOLBAR_H
206