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 }
00129 
00130 void KPushButton::setGuiItem( KStdGuiItem::StdItem item )
00131 {
00132     setGuiItem( KStdGuiItem::guiItem(item) );
00133     d->itemType = item;
00134 }
00135 
00136 KStdGuiItem::StdItem KPushButton::guiItem() const
00137 {
00138     return d->itemType;
00139 }
00140 
00141 void KPushButton::setText( const QString &text )
00142 {
00143     QPushButton::setText(text);
00144 
00145     // we need to re-evaluate the icon set when the text
00146     // is removed, or when it is supplied
00147     if (text.isEmpty() != d->item.text().isEmpty())
00148         setIconSet(d->item.iconSet());
00149 
00150     d->item.setText(text);
00151 }
00152 
00153 void KPushButton::setIconSet( const QIconSet &iconSet )
00154 {
00155     d->item.setIconSet(iconSet);
00156 
00157     if ( s_useIcons || text().isEmpty() )
00158         QPushButton::setIconSet( iconSet );
00159     else
00160         QPushButton::setIconSet( QIconSet() );
00161 }
00162 
00163 void KPushButton::slotSettingsChanged( int /* category */ )
00164 {
00165     readSettings();
00166     setIconSet( d->item.iconSet() );
00167 }
00168 
00169 void KPushButton::setDragEnabled( bool enable )
00170 {
00171     m_dragEnabled = enable;
00172 }
00173 
00174 void KPushButton::mousePressEvent( QMouseEvent *e )
00175 {
00176     if ( m_dragEnabled )
00177     startPos = e->pos();
00178     QPushButton::mousePressEvent( e );
00179 }
00180 
00181 void KPushButton::mouseMoveEvent( QMouseEvent *e )
00182 {
00183     if ( !m_dragEnabled )
00184     {
00185         QPushButton::mouseMoveEvent( e );
00186         return;
00187     }
00188 
00189     if ( (e->state() & LeftButton) &&
00190          (e->pos() - startPos).manhattanLength() >
00191          KGlobalSettings::dndEventDelay() )
00192     {
00193         startDrag();
00194         setDown( false );
00195     }
00196 }
00197 
00198 QDragObject * KPushButton::dragObject()
00199 {
00200     return 0L;
00201 }
00202 
00203 void KPushButton::startDrag()
00204 {
00205     QDragObject *d = dragObject();
00206     if ( d )
00207     d->dragCopy();
00208 }
00209 
00210 void KPushButton::virtual_hook( int, void* )
00211 { /*BASE::virtual_hook( id, data );*/ }
00212 
00213 #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 Tue Aug 2 12:04:26 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003