qtoolbar.h 8.2 KB

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