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
47QT_REQUIRE_CONFIG(toolbar);
48
49QT_BEGIN_NAMESPACE
50
51class QToolBarPrivate;
52
53class QAction;
54class QIcon;
55class QMainWindow;
56class QStyleOptionToolBar;
57
58class Q_WIDGETS_EXPORT QToolBar : public QWidget
59{
60 Q_OBJECT
61
62 Q_PROPERTY(bool movable READ isMovable WRITE setMovable
63 DESIGNABLE (qobject_cast<QMainWindow *>(parentWidget()) != 0)
64 NOTIFY movableChanged)
65 Q_PROPERTY(Qt::ToolBarAreas allowedAreas READ allowedAreas WRITE setAllowedAreas
66 DESIGNABLE (qobject_cast<QMainWindow *>(parentWidget()) != 0)
67 NOTIFY allowedAreasChanged)
68 Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation
69 DESIGNABLE (qobject_cast<QMainWindow *>(parentWidget()) == 0)
70 NOTIFY orientationChanged)
71 Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
72 Q_PROPERTY(Qt::ToolButtonStyle toolButtonStyle READ toolButtonStyle WRITE setToolButtonStyle
73 NOTIFY toolButtonStyleChanged)
74 Q_PROPERTY(bool floating READ isFloating)
75 Q_PROPERTY(bool floatable READ isFloatable WRITE setFloatable)
76
77public:
78 explicit QToolBar(const QString &title, QWidget *parent = nullptr);
79 explicit QToolBar(QWidget *parent = nullptr);
80 ~QToolBar();
81
82 void setMovable(bool movable);
83 bool isMovable() const;
84
85 void setAllowedAreas(Qt::ToolBarAreas areas);
86 Qt::ToolBarAreas allowedAreas() const;
87
88 inline bool isAreaAllowed(Qt::ToolBarArea area) const
89 { return (allowedAreas() & area) == area; }
90
91 void setOrientation(Qt::Orientation orientation);
92 Qt::Orientation orientation() const;
93
94 void clear();
95
96 using QWidget::addAction;
97 QAction *addAction(const QString &text);
98 QAction *addAction(const QIcon &icon, const QString &text);
99 QAction *addAction(const QString &text, const QObject *receiver, const char* member);
100 QAction *addAction(const QIcon &icon, const QString &text,
101 const QObject *receiver, const char* member);
102#ifdef Q_CLANG_QDOC
103 template<typename Functor>
104 QAction *addAction(const QString &text, Functor functor);
105 template<typename Functor>
106 QAction *addAction(const QString &text, const QObject *context, Functor functor);
107 template<typename Functor>
108 QAction *addAction(const QIcon &icon, const QString &text, Functor functor);
109 template<typename Functor>
110 QAction *addAction(const QIcon &icon, const QString &text, const QObject *context, Functor functor);
111#else
112 // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
113 template<class Obj, typename Func1>
114 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
115 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
116 addAction(const QString &text, const Obj *object, Func1 slot)
117 {
118 QAction *result = addAction(text);
119 connect(result, &QAction::triggered, object, std::move(slot));
120 return result;
121 }
122 // addAction(QString): Connect to a functor or function pointer (without context)
123 template <typename Func1>
124 inline QAction *addAction(const QString &text, Func1 slot)
125 {
126 QAction *result = addAction(text);
127 connect(result, &QAction::triggered, slot);
128 return result;
129 }
130 // addAction(QString): Connect to a QObject slot / functor or function pointer (with context)
131 template<class Obj, typename Func1>
132 inline typename std::enable_if<!std::is_same<const char*, Func1>::value
133 && QtPrivate::IsPointerToTypeDerivedFromQObject<Obj*>::Value, QAction *>::type
134 addAction(const QIcon &actionIcon, const QString &text, const Obj *object, Func1 slot)
135 {
136 QAction *result = addAction(actionIcon, text);
137 connect(result, &QAction::triggered, object, std::move(slot));
138 return result;
139 }
140 // addAction(QIcon, QString): Connect to a functor or function pointer (without context)
141 template <typename Func1>
142 inline QAction *addAction(const QIcon &actionIcon, const QString &text, Func1 slot)
143 {
144 QAction *result = addAction(actionIcon, text);
145 connect(result, &QAction::triggered, slot);
146 return result;
147 }
148#endif // !Q_CLANG_QDOC
149
150 QAction *addSeparator();
151 QAction *insertSeparator(QAction *before);
152
153 QAction *addWidget(QWidget *widget);
154 QAction *insertWidget(QAction *before, QWidget *widget);
155
156 QRect actionGeometry(QAction *action) const;
157 QAction *actionAt(const QPoint &p) const;
158 inline QAction *actionAt(int x, int y) const;
159
160 QAction *toggleViewAction() const;
161
162 QSize iconSize() const;
163 Qt::ToolButtonStyle toolButtonStyle() const;
164
165 QWidget *widgetForAction(QAction *action) const;
166
167 bool isFloatable() const;
168 void setFloatable(bool floatable);
169 bool isFloating() const;
170
171public Q_SLOTS:
172 void setIconSize(const QSize &iconSize);
173 void setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle);
174
175Q_SIGNALS:
176 void actionTriggered(QAction *action);
177 void movableChanged(bool movable);
178 void allowedAreasChanged(Qt::ToolBarAreas allowedAreas);
179 void orientationChanged(Qt::Orientation orientation);
180 void iconSizeChanged(const QSize &iconSize);
181 void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle);
182 void topLevelChanged(bool topLevel);
183 void visibilityChanged(bool visible);
184
185protected:
186 void actionEvent(QActionEvent *event) override;
187 void changeEvent(QEvent *event) override;
188 void paintEvent(QPaintEvent *event) override;
189 bool event(QEvent *event) override;
190 void initStyleOption(QStyleOptionToolBar *option) const;
191
192
193private:
194 Q_DECLARE_PRIVATE(QToolBar)
195 Q_DISABLE_COPY(QToolBar)
196 Q_PRIVATE_SLOT(d_func(), void _q_toggleView(bool))
197 Q_PRIVATE_SLOT(d_func(), void _q_updateIconSize(const QSize &))
198 Q_PRIVATE_SLOT(d_func(), void _q_updateToolButtonStyle(Qt::ToolButtonStyle))
199
200 friend class QMainWindow;
201 friend class QMainWindowLayout;
202 friend class QToolBarLayout;
203 friend class QToolBarAreaLayout;
204};
205
206inline QAction *QToolBar::actionAt(int ax, int ay) const
207{ return actionAt(QPoint(ax, ay)); }
208
209QT_END_NAMESPACE
210
211#endif // QDYNAMICTOOLBAR_H
212