KDELibs4Support

kpushbutton.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Carsten Pfeiffer <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kpushbutton.h"
21 #include <QStyleOptionToolButton>
22 #include <QStylePainter>
23 
24 #include <QDrag>
25 #include <QActionEvent>
26 #include <QMenu>
27 #include <QPointer>
28 #include <QStyle>
29 #include <QTimer>
30 #include <QApplication>
31 
32 #include <kguiitem.h>
33 
34 #include <kauth/objectdecorator.h>
35 
36 class Q_DECL_HIDDEN KPushButton::KPushButtonPrivate
37 {
38 public:
39  KPushButtonPrivate(KPushButton *_parent) : parent(_parent), m_dragEnabled(false), decorator(nullptr)
40  {
41  }
42 
43  KPushButton *parent;
44 
46  QPointer<QMenu> delayedMenu;
47  QTimer *delayedMenuTimer;
48  bool m_dragEnabled;
49  QPoint startPos;
50  KAuth::ObjectDecorator *decorator;
51 
52  void slotPressedInternal();
53  void slotClickedInternal();
54  void authStatusChanged(KAuth::Action::AuthStatus status);
55  void slotDelayedMenuTimeout();
56 };
57 
58 void KPushButton::KPushButtonPrivate::slotPressedInternal()
59 {
60  if (!delayedMenu.isNull()) {
61  if (delayedMenuTimer == nullptr) {
62  delayedMenuTimer = new QTimer(parent);
63  delayedMenuTimer->setSingleShot(true);
64  connect(delayedMenuTimer, SIGNAL(timeout()), parent, SLOT(slotDelayedMenuTimeout()));
65  }
66  const int delay = parent->style()->styleHint(QStyle::SH_ToolButton_PopupDelay, nullptr, parent);
67  delayedMenuTimer->start((delay <= 0) ? 150 : delay);
68  }
69 }
70 
71 void KPushButton::KPushButtonPrivate::slotClickedInternal()
72 {
73  if (delayedMenuTimer) {
74  delayedMenuTimer->stop();
75  }
76 }
77 
78 void KPushButton::KPushButtonPrivate::slotDelayedMenuTimeout()
79 {
80  delayedMenuTimer->stop();
81  if (!delayedMenu.isNull()) {
82  parent->setMenu(delayedMenu);
83  parent->showMenu();
84  parent->setMenu(nullptr);
85  }
86 }
87 
89  : QPushButton(parent), d(new KPushButtonPrivate(this))
90 {
91  initWidget(KGuiItem(""));
92 }
93 
95  : QPushButton(parent), d(new KPushButtonPrivate(this))
96 {
97  initWidget(KGuiItem(text));
98 }
99 
100 KPushButton::KPushButton(const QIcon &icon, const QString &text,
101  QWidget *parent)
102  : QPushButton(text, parent), d(new KPushButtonPrivate(this))
103 {
104  initWidget(KGuiItem(text, icon));
105 }
106 
108  : QPushButton(parent), d(new KPushButtonPrivate(this))
109 {
110  initWidget(item);
111 }
112 
114 {
115  delete d;
116 }
117 
118 void KPushButton::initWidget(const KGuiItem &item)
119 {
120  d->decorator = new KAuth::ObjectDecorator(this);
121  connect(d->decorator, SIGNAL(authorized(KAuth::Action)),
122  this, SIGNAL(authorized(KAuth::Action)));
123 
124  d->itemType = (KStandardGuiItem::StandardItem) 0;
125  d->delayedMenuTimer = nullptr;
126 
127  connect(this, SIGNAL(pressed()), this, SLOT(slotPressedInternal()));
128  connect(this, SIGNAL(clicked()), this, SLOT(slotClickedInternal()));
129  KGuiItem::assign(this, item);
130 }
131 
132 bool KPushButton::isDragEnabled() const
133 {
134  return d->m_dragEnabled;
135 }
136 
138 {
139  KGuiItem::assign(this, item);
140 }
141 
143 {
145  d->itemType = item;
146 }
147 
149 {
150  return d->itemType;
151 }
152 
153 void KPushButton::setText(const QString &text)
154 {
156 }
157 
158 void KPushButton::setIcon(const QIcon &icon)
159 {
160  const bool useIcons = style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons, nullptr, this);
161  if (useIcons || text().isEmpty()) {
163  } else {
165  }
166 }
167 
169 {
170  d->m_dragEnabled = enable;
171 }
172 
174 {
175  if (d->m_dragEnabled) {
176  d->startPos = e->pos();
177  }
179 }
180 
182 {
183  if (!d->m_dragEnabled) {
185  return;
186  }
187 
188  if ((e->buttons() & Qt::LeftButton) &&
189  (e->pos() - d->startPos).manhattanLength() >
191  startDrag();
192  setDown(false);
193  }
194 }
195 
197 {
198  return nullptr;
199 }
200 
202 {
203  QDrag *d = dragObject();
204  if (d) {
205  d->start();
206  }
207 }
208 
210 {
211  d->delayedMenu = delayedMenu;
212 }
213 
215 {
216  return d->delayedMenu;
217 }
218 
220 {
221  return d->decorator->authAction();
222 }
223 
224 void KPushButton::setAuthAction(const QString &actionName)
225 {
226  d->decorator->setAuthAction(actionName);
227 }
228 
230 {
231  d->decorator->setAuthAction(action);
232 }
233 
235 {
236  const bool tempSetMenu = !menu() && d->delayedMenu;
237  if (tempSetMenu) {
238  const_cast<KPushButton *>(this)->setMenu(d->delayedMenu);
239  }
240  const QSize sz = QPushButton::sizeHint();
241  if (tempSetMenu) {
242  const_cast<KPushButton *>(this)->setMenu(nullptr);
243  }
244  return sz;
245 }
246 
248 {
249  QStylePainter p(this);
250  QStyleOptionButton option;
251  initStyleOption(&option);
252 
253  if (d->delayedMenu) {
254  option.features |= QStyleOptionButton::HasMenu;
255  }
256 
258 }
259 
260 #include "moc_kpushbutton.cpp"
QPoint pos() const const
virtual QSize sizeHint() const const override
void setMenu(QMenu *menu)
QMenu * delayedMenu()
returns a delayed popup menu since menu() isn't virtual
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:47
void clicked(bool checked)
void paintEvent(QPaintEvent *) override
Reimplemented to add arrow for delayed menu.
void drawControl(QStyle::ControlElement ce, const QStyleOption &option)
KGuiItem guiItem(StandardItem id)
LeftButton
~KPushButton() override
Destructs the button.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QMenu * menu() const const
void setDelayedMenu(QMenu *delayed_menu)
Sets a delayed popup menu for consistency, since menu() isn't virtual.
virtual void startDrag()
Starts a drag (dragCopy() by default) using dragObject()
QSize sizeHint() const override
Reimplemented to add arrow for delayed menu.
void setDown(bool)
static void assign(QPushButton *button, const KGuiItem &item)
void setDragEnabled(bool enable)
Enables/disables drag-support.
KAuth::Action authAction() const
Returns the action object associated with this button, or 0 if it does not have one.
QStyle * style() const const
void mouseMoveEvent(QMouseEvent *) override
Reimplemented to add drag-support.
void setGuiItem(const KGuiItem &item)
Sets the KGuiItem for this button.
Qt::MouseButtons buttons() const const
KStandardGuiItem::StandardItem guiItem() const
Reads the standard KGuiItem for this button.
Q_SCRIPTABLE CaptureState status()
Get the current networking status If the result is Unknown, the backend may be unconfigured or otherw...
Definition: networking.cpp:40
void authorized(const KAuth::Action &action)
Signal emitted when the button is triggered and authorized.
virtual QDrag * dragObject()
Reimplement this and return the QDrag object that should be used for the drag.
virtual int styleHint(QStyle::StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const const=0
void setText(const QString &text)
Sets the text of the button.
SH_ToolButton_PopupDelay
void initStyleOption(QStyleOptionButton *option) const const
virtual void mousePressEvent(QMouseEvent *e) override
void setAuthAction(const KAuth::Action &action)
Sets the action object associated with this button.
void setIcon(const QIcon &icon)
Sets the Icon Set for this button.
void mousePressEvent(QMouseEvent *) override
Reimplemented to add drag-support.
Qt::DropAction start(Qt::DropActions request)
virtual void mouseMoveEvent(QMouseEvent *e) override
KPushButton(QWidget *parent=nullptr)
Default constructor.
Definition: kpushbutton.cpp:88
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 03:54:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.