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