• Skip to content
  • Skip to link menu
KDE 4.1 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         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     setWhatsThis( d->item.whatsThis() );
00176 }
00177 
00178 void KPushButton::setGuiItem( KStandardGuiItem::StandardItem item )
00179 {
00180     setGuiItem( KStandardGuiItem::guiItem(item) );
00181     d->itemType = item;
00182 }
00183 
00184 KStandardGuiItem::StandardItem KPushButton::guiItem() const
00185 {
00186     return d->itemType;
00187 }
00188 
00189 void KPushButton::setText( const QString &text )
00190 {
00191     QPushButton::setText(text);
00192 
00193     // we need to re-evaluate the icon set when the text
00194     // is removed, or when it is supplied
00195     if (text.isEmpty() != d->item.text().isEmpty())
00196         setIcon(d->item.icon());
00197 
00198     d->item.setText(text);
00199 }
00200 
00201 void KPushButton::setIcon( const KIcon &icon )
00202 {
00203     d->item.setIcon(icon);
00204 
00205     if ( s_useIcons || text().isEmpty() )
00206         QPushButton::setIcon( icon );
00207     else
00208         QPushButton::setIcon( QIcon() );
00209 }
00210 
00211 void KPushButton::setIcon( const QIcon &qicon )
00212 {
00213     d->item.setIcon(KIcon(qicon));
00214 }
00215 
00216 void KPushButton::setDragEnabled( bool enable )
00217 {
00218     d->m_dragEnabled = enable;
00219 }
00220 
00221 void KPushButton::mousePressEvent( QMouseEvent *e )
00222 {
00223     if ( d->m_dragEnabled )
00224         d->startPos = e->pos();
00225     QPushButton::mousePressEvent( e );
00226 }
00227 
00228 void KPushButton::mouseMoveEvent( QMouseEvent *e )
00229 {
00230     if ( !d->m_dragEnabled )
00231     {
00232         QPushButton::mouseMoveEvent( e );
00233         return;
00234     }
00235 
00236     if ( (e->buttons() & Qt::LeftButton) &&
00237          (e->pos() - d->startPos).manhattanLength() >
00238          KGlobalSettings::dndEventDelay() )
00239     {
00240         startDrag();
00241         setDown( false );
00242     }
00243 }
00244 
00245 QDrag * KPushButton::dragObject()
00246 {
00247     return 0;
00248 }
00249 
00250 void KPushButton::startDrag()
00251 {
00252     QDrag *d = dragObject();
00253     if ( d )
00254         d->start();
00255 }
00256 
00257 void KPushButton::setDelayedMenu(QMenu *delayedMenu)
00258 {
00259     d->delayedMenu=delayedMenu;
00260 }
00261 
00262 QMenu* KPushButton::delayedMenu()
00263 {
00264     return d->delayedMenu;
00265 }
00266 
00267 #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
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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