• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • widgets
kpushbutton.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Carsten Pfeiffer <pfeiffer@kde.org>
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 <QtGui/QDrag>
25 #include <QtGui/QActionEvent>
26 #include <QtGui/QMenu>
27 #include <QtCore/QPointer>
28 #include <QtGui/QStyle>
29 #include <QtCore/QTimer>
30 
31 #include <config.h>
32 
33 #include <kconfig.h>
34 #include <kglobal.h>
35 #include <kglobalsettings.h>
36 #include <kguiitem.h>
37 #include <kicon.h>
38 
39 #include "auth/kauthaction.h"
40 #include "auth/kauthactionwatcher.h"
41 
42 static bool s_useIcons = false;
43 
44 class KPushButton::KPushButtonPrivate
45 {
46 public:
47  KPushButtonPrivate(KPushButton *_parent) : parent(_parent), m_dragEnabled( false ), authAction(0)
48  {
49  }
50 
51  KPushButton *parent;
52 
53  KGuiItem item;
54  KStandardGuiItem::StandardItem itemType;
55  QPointer<QMenu> delayedMenu;
56  QTimer * delayedMenuTimer;
57  bool m_dragEnabled;
58  QPoint startPos;
59  KAuth::Action *authAction;
60  // TODO: Remove whenever QIcon overlays will get fixed
61  KIcon oldIcon;
62 
63  void slotSettingsChanged( int );
64  void slotPressedInternal();
65  void slotClickedInternal();
66  void authStatusChanged(int status);
67  void slotDelayedMenuTimeout();
68  void readSettings();
69 };
70 
71 void KPushButton::KPushButtonPrivate::slotSettingsChanged( int /* category */ )
72 {
73  readSettings();
74  parent->setIcon( item.icon() );
75 }
76 
77 void KPushButton::KPushButtonPrivate::slotPressedInternal()
78 {
79  if (!delayedMenu.isNull()) {
80  if (delayedMenuTimer==0) {
81  delayedMenuTimer=new QTimer(parent);
82  delayedMenuTimer->setSingleShot(true);
83  connect(delayedMenuTimer,SIGNAL(timeout()),parent,SLOT(slotDelayedMenuTimeout()));
84  }
85  const int delay=parent->style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, parent);
86  delayedMenuTimer->start((delay<=0) ? 150:delay);
87  }
88 }
89 
90 void KPushButton::KPushButtonPrivate::slotClickedInternal()
91 {
92  if (delayedMenuTimer)
93  delayedMenuTimer->stop();
94 
95  if (authAction) {
96  KAuth::Action::AuthStatus s = authAction->earlyAuthorize();
97  switch(s) {
98  case KAuth::Action::Denied:
99  parent->setEnabled(false);
100  break;
101  case KAuth::Action::Authorized:
102  emit parent->authorized(authAction);
103  break;
104  default:
105  break;
106  }
107  }
108 }
109 
110 void KPushButton::KPushButtonPrivate::slotDelayedMenuTimeout() {
111  delayedMenuTimer->stop();
112  if (!delayedMenu.isNull()) {
113  parent->setMenu(delayedMenu);
114  parent->showMenu();
115  parent->setMenu(0);
116  }
117 }
118 
119 void KPushButton::KPushButtonPrivate::authStatusChanged(int status)
120 {
121  KAuth::Action::AuthStatus s = (KAuth::Action::AuthStatus)status;
122 
123  switch(s) {
124  case KAuth::Action::Authorized:
125  parent->setEnabled(true);
126  if(!oldIcon.isNull()) {
127  parent->setIcon(oldIcon);
128  oldIcon = KIcon();
129  }
130  break;
131  case KAuth::Action::AuthRequired:
132  parent->setEnabled(true);
133  oldIcon = KIcon(parent->icon());
134  parent->setIcon(KIcon("dialog-password"));
135  break;
136  default:
137  parent->setEnabled(false);
138  if(!oldIcon.isNull()) {
139  parent->setIcon(oldIcon);
140  oldIcon = KIcon();
141  }
142  }
143 }
144 
145 void KPushButton::KPushButtonPrivate::readSettings()
146 {
147  s_useIcons = KGlobalSettings::showIconsOnPushButtons();
148 }
149 
150 
151 
152 KPushButton::KPushButton( QWidget *parent )
153  : QPushButton( parent ), d( new KPushButtonPrivate(this) )
154 {
155  init( KGuiItem( "" ) );
156 }
157 
158 KPushButton::KPushButton( const QString &text, QWidget *parent )
159  : QPushButton( parent ), d( new KPushButtonPrivate(this) )
160 {
161  init( KGuiItem( text ) );
162 }
163 
164 KPushButton::KPushButton( const KIcon &icon, const QString &text,
165  QWidget *parent )
166  : QPushButton( text, parent ), d( new KPushButtonPrivate(this) )
167 {
168  init( KGuiItem( text, icon ) );
169 }
170 
171 KPushButton::KPushButton( const KGuiItem &item, QWidget *parent )
172  : QPushButton( parent ), d( new KPushButtonPrivate(this) )
173 {
174  init( item );
175 }
176 
177 KPushButton::~KPushButton()
178 {
179  delete d;
180 }
181 
182 void KPushButton::init( const KGuiItem &item )
183 {
184  d->item = item;
185  d->itemType = (KStandardGuiItem::StandardItem) 0;
186  d->delayedMenuTimer=0;
187 
188  connect(this,SIGNAL(pressed()), this, SLOT(slotPressedInternal()));
189  connect(this,SIGNAL(clicked()), this, SLOT(slotClickedInternal()));
190  // call QPushButton's implementation since we don't need to
191  // set the GUI items text or check the state of the icon set
192  QPushButton::setText( d->item.text() );
193 
194  static bool initialized = false;
195  if ( !initialized ) {
196  d->readSettings();
197  initialized = true;
198  }
199 
200  setIcon( d->item.icon() );
201 
202  setToolTip( item.toolTip() );
203 
204  setWhatsThis(item.whatsThis());
205 
206  connect( KGlobalSettings::self(), SIGNAL(settingsChanged(int)),
207  SLOT(slotSettingsChanged(int)) );
208 }
209 
210 bool KPushButton::isDragEnabled() const
211 {
212  return d->m_dragEnabled;
213 }
214 
215 void KPushButton::setGuiItem( const KGuiItem& item )
216 {
217  d->item = item;
218 
219  // call QPushButton's implementation since we don't need to
220  // set the GUI items text or check the state of the icon set
221  QPushButton::setText( d->item.text() );
222  setIcon( d->item.icon() );
223  setToolTip( d->item.toolTip() );
224  setEnabled( d->item.isEnabled() );
225  setWhatsThis( d->item.whatsThis() );
226 }
227 
228 void KPushButton::setGuiItem( KStandardGuiItem::StandardItem item )
229 {
230  setGuiItem( KStandardGuiItem::guiItem(item) );
231  d->itemType = item;
232 }
233 
234 KStandardGuiItem::StandardItem KPushButton::guiItem() const
235 {
236  return d->itemType;
237 }
238 
239 void KPushButton::setText( const QString &text )
240 {
241  QPushButton::setText(text);
242 
243  // we need to re-evaluate the icon set when the text
244  // is removed, or when it is supplied
245  if (text.isEmpty() != d->item.text().isEmpty())
246  setIcon(d->item.icon());
247 
248  d->item.setText(text);
249 }
250 
251 void KPushButton::setIcon( const KIcon &icon )
252 {
253  d->item.setIcon(icon);
254 
255  if ( s_useIcons || text().isEmpty() )
256  QPushButton::setIcon( icon );
257  else
258  QPushButton::setIcon( QIcon() );
259 }
260 
261 void KPushButton::setIcon( const QIcon &qicon )
262 {
263  setIcon(KIcon(qicon));
264 }
265 
266 void KPushButton::setDragEnabled( bool enable )
267 {
268  d->m_dragEnabled = enable;
269 }
270 
271 void KPushButton::mousePressEvent( QMouseEvent *e )
272 {
273  if ( d->m_dragEnabled )
274  d->startPos = e->pos();
275  QPushButton::mousePressEvent( e );
276 }
277 
278 void KPushButton::mouseMoveEvent( QMouseEvent *e )
279 {
280  if ( !d->m_dragEnabled )
281  {
282  QPushButton::mouseMoveEvent( e );
283  return;
284  }
285 
286  if ( (e->buttons() & Qt::LeftButton) &&
287  (e->pos() - d->startPos).manhattanLength() >
288  KGlobalSettings::dndEventDelay() )
289  {
290  startDrag();
291  setDown( false );
292  }
293 }
294 
295 QDrag * KPushButton::dragObject()
296 {
297  return 0;
298 }
299 
300 void KPushButton::startDrag()
301 {
302  QDrag *d = dragObject();
303  if ( d )
304  d->start();
305 }
306 
307 void KPushButton::setDelayedMenu(QMenu *delayedMenu)
308 {
309  d->delayedMenu=delayedMenu;
310 }
311 
312 QMenu* KPushButton::delayedMenu()
313 {
314  return d->delayedMenu;
315 }
316 
317 KAuth::Action *KPushButton::authAction() const
318 {
319  return d->authAction;
320 }
321 
322 void KPushButton::setAuthAction(const QString &actionName)
323 {
324  if (actionName.isEmpty()) {
325  setAuthAction(0);
326  } else {
327  setAuthAction(new KAuth::Action(actionName));
328  }
329 }
330 
331 void KPushButton::setAuthAction(KAuth::Action *action)
332 {
333  if (d->authAction == action) {
334  return;
335  }
336 
337  if (d->authAction) {
338  disconnect(d->authAction->watcher(), SIGNAL(statusChanged(int)),
339  this, SLOT(authStatusChanged(int)));
340  //delete d->authAction;
341  d->authAction = 0;
342  if (!d->oldIcon.isNull()) {
343  setIcon(d->oldIcon);
344  d->oldIcon = KIcon();
345  }
346  }
347 
348  if (action != 0) {
349  d->authAction = action;
350 
351  // Set the parent widget
352  d->authAction->setParentWidget(this);
353 
354  connect(d->authAction->watcher(), SIGNAL(statusChanged(int)),
355  this, SLOT(authStatusChanged(int)));
356  d->authStatusChanged(d->authAction->status());
357  }
358 }
359 
360 QSize KPushButton::sizeHint() const
361 {
362  const bool tempSetMenu = !menu() && d->delayedMenu;
363  if (tempSetMenu)
364  const_cast<KPushButton *>(this)->setMenu(d->delayedMenu);
365  const QSize sz = QPushButton::sizeHint();
366  if (tempSetMenu)
367  const_cast<KPushButton *>(this)->setMenu(0);
368  return sz;
369 }
370 
371 void KPushButton::paintEvent( QPaintEvent * )
372 {
373  QStylePainter p(this);
374  QStyleOptionButton option;
375  initStyleOption(&option);
376 
377  if (d->delayedMenu)
378  option.features |= QStyleOptionButton::HasMenu;
379 
380  p.drawControl(QStyle::CE_PushButton, option);
381 }
382 
383 #include "kpushbutton.moc"
QStylePainter
QAbstractButton::setDown
void setDown(bool)
QPushButton::setMenu
void setMenu(QMenu *menu)
KStandardGuiItem::guiItem
KGuiItem guiItem(StandardItem ui_enum)
Returns the gui item for the given identifier.
Definition: kstandardguiitem.cpp:29
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:46
QWidget
KPushButton::authAction
KAuth::Action * authAction() const
Returns the action object associated with this button, or 0 if it does not have one.
Definition: kpushbutton.cpp:317
KGuiItem::whatsThis
QString whatsThis() const
Definition: kguiitem.cpp:189
KAuth::Action::AuthRequired
QPushButton::sizeHint
virtual QSize sizeHint() const
QDrag::start
Qt::DropAction start(QFlags< Qt::DropAction > request)
kglobalsettings.h
KPushButton::sizeHint
virtual QSize sizeHint() const
Reimplemented to add arrow for delayed menu.
Definition: kpushbutton.cpp:360
timeout
int timeout
KPushButton::paintEvent
virtual void paintEvent(QPaintEvent *)
Reimplemented to add arrow for delayed menu.
Definition: kpushbutton.cpp:371
kconfig.h
QAbstractButton::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
KPushButton::startDrag
virtual void startDrag()
Starts a drag (dragCopy() by default) using dragObject()
Definition: kpushbutton.cpp:300
QPointer< QMenu >
KPushButton::guiItem
KStandardGuiItem::StandardItem guiItem() const
Reads the standard KGuiItem for this button.
Definition: kpushbutton.cpp:234
kauthactionwatcher.h
KPushButton::setAuthAction
void setAuthAction(KAuth::Action *action)
Sets the action object associated with this button.
Definition: kpushbutton.cpp:331
QPoint
QMouseEvent
KStandardGuiItem::StandardItem
StandardItem
Definition: kstandardguiitem.h:48
QMouseEvent::buttons
Qt::MouseButtons buttons() const
KGlobalSettings::self
static KGlobalSettings * self()
Return the KGlobalSettings singleton.
Definition: kglobalsettings.cpp:188
KGuiItem::toolTip
QString toolTip() const
Definition: kguiitem.cpp:184
s_useIcons
static bool s_useIcons
Definition: kpushbutton.cpp:42
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
QAbstractButton::setIcon
void setIcon(const QIcon &icon)
QStylePainter::drawControl
void drawControl(QStyle::ControlElement ce, const QStyleOption &option)
QStyleOptionButton
QPushButton::initStyleOption
void initStyleOption(QStyleOptionButton *option) const
kglobal.h
QWidget::setEnabled
void setEnabled(bool)
KPushButton::setText
void setText(const QString &text)
Sets the text of the button.
Definition: kpushbutton.cpp:239
QTimer
KAuth::Action::Denied
QAbstractButton::pressed
void pressed()
KGuiItem
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:36
QDrag
QString::isEmpty
bool isEmpty() const
QAbstractButton::clicked
void clicked(bool checked)
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
KAuth::Action::Authorized
QString
QAbstractButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
kauthaction.h
KPushButton::setDelayedMenu
void setDelayedMenu(QMenu *delayed_menu)
Sets a delayed popup menu for consistency, since menu() isn't virtual.
Definition: kpushbutton.cpp:307
KAuth::Action::AuthStatus
AuthStatus
KGlobalSettings::showIconsOnPushButtons
static bool showIconsOnPushButtons()
This function determines if the user wishes to see icons on the push buttons.
Definition: kglobalsettings.cpp:768
QMenu
QSize
kpushbutton.h
KPushButton::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *)
Reimplemented to add drag-support.
Definition: kpushbutton.cpp:278
KPushButton::setDragEnabled
void setDragEnabled(bool enable)
Enables/disables drag-support.
Definition: kpushbutton.cpp:266
itemType
QString itemType(const QString &type)
QWidget::setWhatsThis
void setWhatsThis(const QString &)
KPushButton::dragObject
virtual QDrag * dragObject()
Reimplement this and return the QDrag object that should be used for the drag.
Definition: kpushbutton.cpp:295
KPushButton::setIcon
void setIcon(const KIcon &icon)
Sets the Icon Set for this button.
Definition: kpushbutton.cpp:251
KPushButton::setGuiItem
void setGuiItem(const KGuiItem &item)
Sets the KGuiItem for this button.
Definition: kpushbutton.cpp:215
QAbstractButton::setText
void setText(const QString &text)
QPushButton::menu
QMenu * menu() const
QPushButton
KPushButton::delayedMenu
QMenu * delayedMenu()
returns a delayed popup menu since menu() isn't virtual
Definition: kpushbutton.cpp:312
QMouseEvent::pos
const QPoint & pos() const
QWidget::setToolTip
void setToolTip(const QString &)
QPaintEvent
KAuth::Action
kicon.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
kguiitem.h
KPushButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *)
Reimplemented to add drag-support.
Definition: kpushbutton.cpp:271
KGlobalSettings::dndEventDelay
static int dndEventDelay()
Returns a threshold in pixels for drag & drop operations.
Definition: kglobalsettings.cpp:227
KPushButton::~KPushButton
~KPushButton()
Destructs the button.
Definition: kpushbutton.cpp:177
KPushButton::KPushButton
KPushButton(QWidget *parent=0)
Default constructor.
Definition: kpushbutton.cpp:152
QIcon
KPushButton::isDragEnabled
bool isDragEnabled() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal