KWidgetsAddons

kactionmenu.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <[email protected]>
4  SPDX-FileCopyrightText: 1999 Simon Hausmann <[email protected]>
5  SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haad[email protected]>
6  SPDX-FileCopyrightText: 2000 Kurt Granroth <[email protected]>
7  SPDX-FileCopyrightText: 2000 Michael Koch <[email protected]>
8  SPDX-FileCopyrightText: 2001 Holger Freyther <[email protected]>
9  SPDX-FileCopyrightText: 2002 Ellis Whitehead <[email protected]>
10  SPDX-FileCopyrightText: 2002 Joseph Wenninger <[email protected]>
11  SPDX-FileCopyrightText: 2003 Andras Mantia <[email protected]>
12  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
13 
14  SPDX-License-Identifier: LGPL-2.0-only
15 */
16 
17 #include "kactionmenu.h"
18 
19 #include <QMenu>
20 #include <QToolBar>
21 
22 class KActionMenuPrivate
23 {
24 public:
25  KActionMenuPrivate()
26  {
27  }
28  ~KActionMenuPrivate()
29  {
30  }
31 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 77)
32  bool m_stickyMenu = true;
33 #endif
35 };
36 
37 KActionMenu::KActionMenu(QObject *parent)
38  : QWidgetAction(parent)
39  , d(new KActionMenuPrivate)
40 {
41  setMenu(new QMenu);
42  setProperty("isShortcutConfigurable", false);
43 }
44 
45 KActionMenu::KActionMenu(const QString &text, QObject *parent)
46  : QWidgetAction(parent)
47  , d(new KActionMenuPrivate)
48 {
49  setMenu(new QMenu);
50  setProperty("isShortcutConfigurable", false);
51  setText(text);
52 }
53 
54 KActionMenu::KActionMenu(const QIcon &icon, const QString &text, QObject *parent)
55  : QWidgetAction(parent)
56  , d(new KActionMenuPrivate)
57 {
58  setMenu(new QMenu);
59  setProperty("isShortcutConfigurable", false);
60  setIcon(icon);
61  setText(text);
62 }
63 
64 KActionMenu::~KActionMenu()
65 {
66  delete menu();
67 }
68 
69 QWidget *KActionMenu::createWidget(QWidget *_parent)
70 {
71  QToolBar *parent = qobject_cast<QToolBar *>(_parent);
72  if (!parent) {
73  return QWidgetAction::createWidget(_parent);
74  }
75  QToolButton *button = new QToolButton(parent);
76  button->setAutoRaise(true);
77  button->setFocusPolicy(Qt::NoFocus);
78  button->setIconSize(parent->iconSize());
79  button->setToolButtonStyle(parent->toolButtonStyle());
82  button->setDefaultAction(this);
83  button->setPopupMode(popupMode());
85 
86  return button;
87 }
88 
89 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 0)
91 {
92  if (cmd) {
93  menu()->removeAction(cmd);
94  }
95 }
96 #endif
97 
99 {
100  menu()->addAction(action);
101 }
102 
103 QAction *KActionMenu::addSeparator()
104 {
105  QAction *separator = new QAction(this);
106  separator->setSeparator(true);
107  addAction(separator);
108  return separator;
109 }
110 
111 QAction *KActionMenu::insertSeparator(QAction *before)
112 {
113  QAction *separator = new QAction(this);
114  separator->setSeparator(true);
115  insertAction(before, separator);
116  return separator;
117 }
118 
119 void KActionMenu::insertAction(QAction *before, QAction *action)
120 {
121  menu()->insertAction(before, action);
122 }
123 
124 void KActionMenu::removeAction(QAction *action)
125 {
126  menu()->removeAction(action);
127 }
128 
129 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 77)
130 bool KActionMenu::delayed() const
131 {
132  return popupMode() == QToolButton::DelayedPopup;
133 }
134 #endif
135 
136 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 77)
137 void KActionMenu::setDelayed(bool _delayed)
138 {
139  if (_delayed) {
141  } else {
142  if (d->m_stickyMenu) {
144  } else {
146  }
147  }
148 }
149 #endif
150 
151 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 77)
152 bool KActionMenu::stickyMenu() const
153 {
154  if (popupMode() == QToolButton::DelayedPopup) {
155  return d->m_stickyMenu;
156  } else if (popupMode() == QToolButton::InstantPopup) {
157  return true;
158  } else { // MenuButtonPopup
159  return false;
160  }
161 }
162 #endif
163 
164 #if KWIDGETSADDONS_BUILD_DEPRECATED_SINCE(5, 77)
165 void KActionMenu::setStickyMenu(bool sticky)
166 {
167  if (popupMode() != QToolButton::DelayedPopup) {
168  if (sticky) {
170  } else {
172  }
173  }
174 
175  d->m_stickyMenu = sticky;
176 }
177 #endif
178 
179 QToolButton::ToolButtonPopupMode KActionMenu::popupMode() const
180 {
181  return d->m_popupMode;
182 }
183 
185 {
186  d->m_popupMode = popupMode;
187 }
void remove(QAction *)
Definition: kactionmenu.cpp:90
void actionTriggered(QAction *action)
void setFocusPolicy(Qt::FocusPolicy policy)
void addAction(QAction *action)
Adds action to this KActionMenu.
Definition: kactionmenu.cpp:98
void iconSizeChanged(const QSize &iconSize)
void setDelayed(bool delayed)
If set to true, this action will create a delayed popup menu when plugged in a KToolBar.
QCA_EXPORT void setProperty(const QString &name, const QVariant &value)
QAction(QObject *parent)
void setStickyMenu(bool sticky)
If set to true, this action will create a sticky popup menu when plugged in a KToolBar.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void insertAction(QAction *before, QAction *action)
QAction * addAction(const QString &text)
void setPopupMode(QToolButton::ToolButtonPopupMode popupMode)
Determines the popup mode when plugged in a KToolBar.
QMenu * menu() const const
void setAutoRaise(bool enable)
void setDefaultAction(QAction *action)
void removeAction(QAction *action)
void setIconSize(const QSize &size)
void setToolButtonStyle(Qt::ToolButtonStyle style)
virtual QWidget * createWidget(QWidget *parent)
void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)
void triggered(QAction *action)
void setSeparator(bool b)
void setPopupMode(QToolButton::ToolButtonPopupMode mode)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:08:01 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.