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

KDEUI

  • 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
42static bool s_useIcons = false;
43
44class KPushButton::KPushButtonPrivate
45{
46public:
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
71void KPushButton::KPushButtonPrivate::slotSettingsChanged( int /* category */ )
72{
73 readSettings();
74 parent->setIcon( item.icon() );
75}
76
77void 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
90void 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
110void KPushButton::KPushButtonPrivate::slotDelayedMenuTimeout() {
111 delayedMenuTimer->stop();
112 if (!delayedMenu.isNull()) {
113 parent->setMenu(delayedMenu);
114 parent->showMenu();
115 parent->setMenu(0);
116 }
117}
118
119void 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
145void KPushButton::KPushButtonPrivate::readSettings()
146{
147 s_useIcons = KGlobalSettings::showIconsOnPushButtons();
148}
149
150
151
152KPushButton::KPushButton( QWidget *parent )
153 : QPushButton( parent ), d( new KPushButtonPrivate(this) )
154{
155 init( KGuiItem( "" ) );
156}
157
158KPushButton::KPushButton( const QString &text, QWidget *parent )
159 : QPushButton( parent ), d( new KPushButtonPrivate(this) )
160{
161 init( KGuiItem( text ) );
162}
163
164KPushButton::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
171KPushButton::KPushButton( const KGuiItem &item, QWidget *parent )
172 : QPushButton( parent ), d( new KPushButtonPrivate(this) )
173{
174 init( item );
175}
176
177KPushButton::~KPushButton()
178{
179 delete d;
180}
181
182void 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
210bool KPushButton::isDragEnabled() const
211{
212 return d->m_dragEnabled;
213}
214
215void 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
228void KPushButton::setGuiItem( KStandardGuiItem::StandardItem item )
229{
230 setGuiItem( KStandardGuiItem::guiItem(item) );
231 d->itemType = item;
232}
233
234KStandardGuiItem::StandardItem KPushButton::guiItem() const
235{
236 return d->itemType;
237}
238
239void 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
251void 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
261void KPushButton::setIcon( const QIcon &qicon )
262{
263 setIcon(KIcon(qicon));
264}
265
266void KPushButton::setDragEnabled( bool enable )
267{
268 d->m_dragEnabled = enable;
269}
270
271void KPushButton::mousePressEvent( QMouseEvent *e )
272{
273 if ( d->m_dragEnabled )
274 d->startPos = e->pos();
275 QPushButton::mousePressEvent( e );
276}
277
278void 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
295QDrag * KPushButton::dragObject()
296{
297 return 0;
298}
299
300void KPushButton::startDrag()
301{
302 QDrag *d = dragObject();
303 if ( d )
304 d->start();
305}
306
307void KPushButton::setDelayedMenu(QMenu *delayedMenu)
308{
309 d->delayedMenu=delayedMenu;
310}
311
312QMenu* KPushButton::delayedMenu()
313{
314 return d->delayedMenu;
315}
316
317KAuth::Action *KPushButton::authAction() const
318{
319 return d->authAction;
320}
321
322void KPushButton::setAuthAction(const QString &actionName)
323{
324 if (actionName.isEmpty()) {
325 setAuthAction(0);
326 } else {
327 setAuthAction(new KAuth::Action(actionName));
328 }
329}
330
331void 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
360QSize 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
371void 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"
KAuth::Action
KAuth::Action::AuthStatus
AuthStatus
KAuth::Action::Denied
Denied
KAuth::Action::Authorized
Authorized
KAuth::Action::AuthRequired
AuthRequired
KGlobalSettings::dndEventDelay
static int dndEventDelay()
Returns a threshold in pixels for drag & drop operations.
Definition: kglobalsettings.cpp:227
KGlobalSettings::self
static KGlobalSettings * self()
Return the KGlobalSettings singleton.
Definition: kglobalsettings.cpp:188
KGlobalSettings::showIconsOnPushButtons
static bool showIconsOnPushButtons()
This function determines if the user wishes to see icons on the push buttons.
Definition: kglobalsettings.cpp:768
KGuiItem
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:37
KGuiItem::toolTip
QString toolTip() const
Definition: kguiitem.cpp:184
KGuiItem::whatsThis
QString whatsThis() const
Definition: kguiitem.cpp:189
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:41
KPushButton
A QPushButton with drag-support and KGuiItem support.
Definition: kpushbutton.h:47
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::~KPushButton
~KPushButton()
Destructs the button.
Definition: kpushbutton.cpp:177
KPushButton::delayedMenu
QMenu * delayedMenu()
returns a delayed popup menu since menu() isn't virtual
Definition: kpushbutton.cpp:312
KPushButton::setDelayedMenu
void setDelayedMenu(QMenu *delayed_menu)
Sets a delayed popup menu for consistency, since menu() isn't virtual.
Definition: kpushbutton.cpp:307
KPushButton::isDragEnabled
bool isDragEnabled
Definition: kpushbutton.h:49
KPushButton::KPushButton
KPushButton(QWidget *parent=0)
Default constructor.
Definition: kpushbutton.cpp:152
KPushButton::guiItem
KStandardGuiItem::StandardItem guiItem() const
Reads the standard KGuiItem for this button.
Definition: kpushbutton.cpp:234
KPushButton::setAuthAction
void setAuthAction(KAuth::Action *action)
Sets the action object associated with this button.
Definition: kpushbutton.cpp:331
KPushButton::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *)
Reimplemented to add drag-support.
Definition: kpushbutton.cpp:278
KPushButton::paintEvent
virtual void paintEvent(QPaintEvent *)
Reimplemented to add arrow for delayed menu.
Definition: kpushbutton.cpp:371
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
KPushButton::setGuiItem
void setGuiItem(const KGuiItem &item)
Sets the KGuiItem for this button.
Definition: kpushbutton.cpp:215
KPushButton::setDragEnabled
void setDragEnabled(bool enable)
Enables/disables drag-support.
Definition: kpushbutton.cpp:266
KPushButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *)
Reimplemented to add drag-support.
Definition: kpushbutton.cpp:271
KPushButton::setText
void setText(const QString &text)
Sets the text of the button.
Definition: kpushbutton.cpp:239
KPushButton::sizeHint
virtual QSize sizeHint() const
Reimplemented to add arrow for delayed menu.
Definition: kpushbutton.cpp:360
KPushButton::startDrag
virtual void startDrag()
Starts a drag (dragCopy() by default) using dragObject()
Definition: kpushbutton.cpp:300
QMenu
QPushButton
QWidget
kauthaction.h
kauthactionwatcher.h
kconfig.h
itemType
QString itemType(const QString &type)
kglobal.h
kglobalsettings.h
kguiitem.h
kicon.h
timeout
int timeout
s_useIcons
static bool s_useIcons
Definition: kpushbutton.cpp:42
kpushbutton.h
KStandardGuiItem::StandardItem
StandardItem
Definition: kstandardguiitem.h:49
KStandardGuiItem::guiItem
KGuiItem guiItem(StandardItem ui_enum)
Returns the gui item for the given identifier.
Definition: kstandardguiitem.cpp:29
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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