kdeui Library API Documentation

kpushbutton.cpp

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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "kpushbutton.h"
00021 
00022 #include <qdragobject.h>
00023 #include <qwhatsthis.h>
00024 #include <qtooltip.h>
00025 
00026 #include "config.h"
00027 
00028 #include <kglobalsettings.h>
00029 #include <kconfig.h>
00030 #include <kglobal.h>
00031 #include <kipc.h> 
00032 #include <kapplication.h>
00033 
00034 class KPushButton::KPushButtonPrivate
00035 {
00036 public:
00037     KGuiItem item;
00038     KStdGuiItem::StdItem itemType;
00039 };
00040 
00041 bool KPushButton::s_useIcons = false;
00042 
00043 KPushButton::KPushButton( QWidget *parent, const char *name )
00044     : QPushButton( parent, name ),
00045       m_dragEnabled( false )
00046 {
00047     init( KGuiItem( "" ) );
00048 }
00049 
00050 KPushButton::KPushButton( const QString &text, QWidget *parent,
00051                           const char *name)
00052     : QPushButton( parent, name ),
00053       m_dragEnabled( false )
00054 {
00055     init( KGuiItem( text ) );
00056 }
00057 
00058 KPushButton::KPushButton( const QIconSet &icon, const QString &text,
00059                           QWidget *parent, const char *name )
00060     : QPushButton( text, parent, name ),
00061       m_dragEnabled( false )
00062 {
00063     init( KGuiItem( text, icon ) );
00064 }
00065 
00066 KPushButton::KPushButton( const KGuiItem &item, QWidget *parent,
00067                           const char *name )
00068     : QPushButton( parent, name ),
00069       m_dragEnabled( false )
00070 {
00071     init( item );
00072 }
00073 
00074 KPushButton::~KPushButton()
00075 {
00076     if( d )
00077     {
00078         delete d;
00079         d = 0L;
00080     }
00081 }
00082 
00083 void KPushButton::init( const KGuiItem &item )
00084 {
00085     d = new KPushButtonPrivate;
00086     d->item = item;
00087     d->itemType = (KStdGuiItem::StdItem) 0;
00088 
00089     // call QPushButton's implementation since we don't need to 
00090     // set the GUI items text or check the state of the icon set
00091     QPushButton::setText( d->item.text() );
00092 
00093     static bool initialized = false;
00094     if ( !initialized ) {
00095         readSettings();
00096         initialized = true;
00097     }
00098 
00099     setIconSet( d->item.iconSet() );
00100 
00101     setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );
00102 
00103     QToolTip::add( this, item.toolTip() );
00104 
00105     QWhatsThis::add( this, item.whatsThis() );
00106 
00107     if (kapp)
00108     {
00109        connect( kapp, SIGNAL( settingsChanged(int) ),
00110                SLOT( slotSettingsChanged(int) ) );
00111        kapp->addKipcEventMask( KIPC::SettingsChanged );
00112     }
00113 }
00114 
00115 void KPushButton::readSettings()
00116 {
00117     s_useIcons = KGlobalSettings::showIconsOnPushButtons();
00118 }
00119 
00120 void KPushButton::setGuiItem( const KGuiItem& item )
00121 {
00122     d->item = item;
00123 
00124     // call QPushButton's implementation since we don't need to 
00125     // set the GUI items text or check the state of the icon set
00126     QPushButton::setText( d->item.text() );
00127     setIconSet( d->item.iconSet() );
00128     QToolTip::add( this, d->item.toolTip() ); 
00129     QWhatsThis::add( this, d->item.whatsThis() );
00130 }
00131 
00132 void KPushButton::setGuiItem( KStdGuiItem::StdItem item )
00133 {
00134     setGuiItem( KStdGuiItem::guiItem(item) );
00135     d->itemType = item;
00136 }
00137 
00138 KStdGuiItem::StdItem KPushButton::guiItem() const
00139 {
00140     return d->itemType;
00141 }
00142 
00143 void KPushButton::setText( const QString &text )
00144 {
00145     QPushButton::setText(text);
00146 
00147     // we need to re-evaluate the icon set when the text
00148     // is removed, or when it is supplied
00149     if (text.isEmpty() != d->item.text().isEmpty())
00150         setIconSet(d->item.iconSet());
00151 
00152     d->item.setText(text);
00153 }
00154 
00155 void KPushButton::setIconSet( const QIconSet &iconSet )
00156 {
00157     d->item.setIconSet(iconSet);
00158 
00159     if ( s_useIcons || text().isEmpty() )
00160         QPushButton::setIconSet( iconSet );
00161     else
00162         QPushButton::setIconSet( QIconSet() );
00163 }
00164 
00165 void KPushButton::slotSettingsChanged( int /* category */ )
00166 {
00167     readSettings();
00168     setIconSet( d->item.iconSet() );
00169 }
00170 
00171 void KPushButton::setDragEnabled( bool enable )
00172 {
00173     m_dragEnabled = enable;
00174 }
00175 
00176 void KPushButton::mousePressEvent( QMouseEvent *e )
00177 {
00178     if ( m_dragEnabled )
00179     startPos = e->pos();
00180     QPushButton::mousePressEvent( e );
00181 }
00182 
00183 void KPushButton::mouseMoveEvent( QMouseEvent *e )
00184 {
00185     if ( !m_dragEnabled )
00186     {
00187         QPushButton::mouseMoveEvent( e );
00188         return;
00189     }
00190 
00191     if ( (e->state() & LeftButton) &&
00192          (e->pos() - startPos).manhattanLength() >
00193          KGlobalSettings::dndEventDelay() )
00194     {
00195         startDrag();
00196         setDown( false );
00197     }
00198 }
00199 
00200 QDragObject * KPushButton::dragObject()
00201 {
00202     return 0L;
00203 }
00204 
00205 void KPushButton::startDrag()
00206 {
00207     QDragObject *d = dragObject();
00208     if ( d )
00209     d->dragCopy();
00210 }
00211 
00212 void KPushButton::virtual_hook( int, void* )
00213 { /*BASE::virtual_hook( id, data );*/ }
00214 
00215 #include "kpushbutton.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Sep 16 06:49:58 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003