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

KDEUI

kpushbutton.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2000 Carsten Pfeiffer <pfeiffer@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kpushbutton.h"
00021 
00022 #include <QtGui/QDrag>
00023 #include <QtGui/QActionEvent>
00024 #include <QtGui/QMenu>
00025 #include <QtCore/QPointer>
00026 #include <QtGui/QStyle>
00027 #include <QtCore/QTimer>
00028 
00029 #include <config.h>
00030 
00031 #include <kconfig.h>
00032 #include <kglobal.h>
00033 #include <kglobalsettings.h>
00034 #include <kguiitem.h>
00035 #include <kicon.h>
00036 
00037 static bool s_useIcons = false;
00038 
00039 class KPushButton::KPushButtonPrivate
00040 {
00041 public:
00042     KPushButtonPrivate(KPushButton *_parent) : parent(_parent), m_dragEnabled( false )
00043     {
00044     }
00045 
00046     KPushButton *parent;
00047 
00048     KGuiItem item;
00049     KStandardGuiItem::StandardItem itemType;
00050     QPointer<QMenu> delayedMenu;
00051     QTimer * delayedMenuTimer;
00052     bool m_dragEnabled;
00053     QPoint startPos;
00054 
00055     void slotSettingsChanged( int );
00056     void slotPressedInternal();
00057     void slotClickedInternal();
00058     void slotDelayedMenuTimeout();
00059     void readSettings();
00060 };
00061 
00062 void KPushButton::KPushButtonPrivate::slotSettingsChanged( int /* category */ )
00063 {
00064     readSettings();
00065     parent->setIcon( item.icon() );
00066 }
00067 
00068 void KPushButton::KPushButtonPrivate::slotPressedInternal()
00069 {
00070     if (!delayedMenu.isNull()) {
00071         if (delayedMenuTimer==0) {
00072             delayedMenuTimer=new QTimer(parent);
00073             delayedMenuTimer->setSingleShot(true);
00074             connect(delayedMenuTimer,SIGNAL(timeout()),parent,SLOT(slotDelayedMenuTimeout()));
00075         }
00076         const int delay=parent->style()->styleHint(QStyle::SH_ToolButton_PopupDelay, 0, parent);
00077         delayedMenuTimer->start((delay<=0) ? 150:delay);
00078     }
00079 }
00080 
00081 void KPushButton::KPushButtonPrivate::slotClickedInternal()
00082 {
00083     if (delayedMenuTimer)
00084         delayedMenuTimer->stop();
00085 }
00086 
00087 void KPushButton::KPushButtonPrivate::slotDelayedMenuTimeout() {
00088     delayedMenuTimer->stop();
00089     if (!delayedMenu.isNull()) {
00090         parent->setMenu(delayedMenu);
00091         parent->showMenu();
00092         parent->setMenu(0);
00093     }
00094 }
00095 
00096 void KPushButton::KPushButtonPrivate::readSettings()
00097 {
00098     s_useIcons = KGlobalSettings::showIconsOnPushButtons();
00099 }
00100 
00101 
00102 
00103 KPushButton::KPushButton( QWidget *parent )
00104     : QPushButton( parent ), d( new KPushButtonPrivate(this) ) 
00105 {
00106     init( KGuiItem( "" ) );
00107 }
00108 
00109 KPushButton::KPushButton( const QString &text, QWidget *parent )
00110     : QPushButton( parent ), d( new KPushButtonPrivate(this) )
00111 {
00112     init( KGuiItem( text ) );
00113 }
00114 
00115 KPushButton::KPushButton( const KIcon &icon, const QString &text,
00116                           QWidget *parent )
00117     : QPushButton( text, parent ), d( new KPushButtonPrivate(this) )
00118 {
00119     init( KGuiItem( text, icon ) );
00120 }
00121 
00122 KPushButton::KPushButton( const KGuiItem &item, QWidget *parent )
00123     : QPushButton( parent ), d( new KPushButtonPrivate(this) )
00124 {
00125     init( item );
00126 }
00127 
00128 KPushButton::~KPushButton()
00129 {
00130     delete d;
00131 }
00132 
00133 void KPushButton::init( const KGuiItem &item )
00134 {
00135     d->item = item;
00136     d->itemType = (KStandardGuiItem::StandardItem) 0;
00137     d->delayedMenuTimer=0;
00138 
00139     connect(this,SIGNAL(pressed()), this, SLOT(slotPressedInternal()));
00140     connect(this,SIGNAL(clicked()), this, SLOT(slotClickedInternal()));
00141     // call QPushButton's implementation since we don't need to
00142     // set the GUI items text or check the state of the icon set
00143     QPushButton::setText( d->item.text() );
00144 
00145     static bool initialized = false;
00146     if ( !initialized ) {
00147         d->readSettings();
00148         initialized = true;
00149     }
00150 
00151     setIcon( d->item.icon() );
00152 
00153     setToolTip( item.toolTip() );
00154 
00155     setWhatsThis(item.whatsThis());
00156 
00157     connect( KGlobalSettings::self(), SIGNAL( settingsChanged(int) ),
00158              SLOT( slotSettingsChanged(int) ) );
00159 }
00160 
00161 bool KPushButton::isDragEnabled() const
00162 {
00163     return d->m_dragEnabled;
00164 }
00165 
00166 void KPushButton::setGuiItem( const KGuiItem& item )
00167 {
00168     d->item = item;
00169 
00170     // call QPushButton's implementation since we don't need to
00171     // set the GUI items text or check the state of the icon set
00172     QPushButton::setText( d->item.text() );
00173     setIcon( d->item.icon() );
00174     setToolTip( d->item.toolTip() );
00175     setEnabled( d->item.isEnabled() );
00176     setWhatsThis( d->item.whatsThis() );
00177 }
00178 
00179 void KPushButton::setGuiItem( KStandardGuiItem::StandardItem item )
00180 {
00181     setGuiItem( KStandardGuiItem::guiItem(item) );
00182     d->itemType = item;
00183 }
00184 
00185 KStandardGuiItem::StandardItem KPushButton::guiItem() const
00186 {
00187     return d->itemType;
00188 }
00189 
00190 void KPushButton::setText( const QString &text )
00191 {
00192     QPushButton::setText(text);
00193 
00194     // we need to re-evaluate the icon set when the text
00195     // is removed, or when it is supplied
00196     if (text.isEmpty() != d->item.text().isEmpty())
00197         setIcon(d->item.icon());
00198 
00199     d->item.setText(text);
00200 }
00201 
00202 void KPushButton::setIcon( const KIcon &icon )
00203 {
00204     d->item.setIcon(icon);
00205 
00206     if ( s_useIcons || text().isEmpty() )
00207         QPushButton::setIcon( icon );
00208     else
00209         QPushButton::setIcon( QIcon() );
00210 }
00211 
00212 void KPushButton::setIcon( const QIcon &qicon )
00213 {
00214     d->item.setIcon(KIcon(qicon));
00215 }
00216 
00217 void KPushButton::setDragEnabled( bool enable )
00218 {
00219     d->m_dragEnabled = enable;
00220 }
00221 
00222 void KPushButton::mousePressEvent( QMouseEvent *e )
00223 {
00224     if ( d->m_dragEnabled )
00225         d->startPos = e->pos();
00226     QPushButton::mousePressEvent( e );
00227 }
00228 
00229 void KPushButton::mouseMoveEvent( QMouseEvent *e )
00230 {
00231     if ( !d->m_dragEnabled )
00232     {
00233         QPushButton::mouseMoveEvent( e );
00234         return;
00235     }
00236 
00237     if ( (e->buttons() & Qt::LeftButton) &&
00238          (e->pos() - d->startPos).manhattanLength() >
00239          KGlobalSettings::dndEventDelay() )
00240     {
00241         startDrag();
00242         setDown( false );
00243     }
00244 }
00245 
00246 QDrag * KPushButton::dragObject()
00247 {
00248     return 0;
00249 }
00250 
00251 void KPushButton::startDrag()
00252 {
00253     QDrag *d = dragObject();
00254     if ( d )
00255         d->start();
00256 }
00257 
00258 void KPushButton::setDelayedMenu(QMenu *delayedMenu)
00259 {
00260     d->delayedMenu=delayedMenu;
00261 }
00262 
00263 QMenu* KPushButton::delayedMenu()
00264 {
00265     return d->delayedMenu;
00266 }
00267 
00268 #include "kpushbutton.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal