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., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, 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 QWhatsThis::add( this, d->item.whatsThis() ); 00129 00130 // Do not add a tooltip to the button automatically as 99% of the time the 00131 // tooltip is redundant to the button text and it results in QTipManager 00132 // invoking an eventHandler on the QApplication which breaks certain apps 00133 // like KDesktop which are sensitive to such things 00134 // QToolTip::add( this, d->item.toolTip() ); 00135 } 00136 00137 void KPushButton::setGuiItem( KStdGuiItem::StdItem item ) 00138 { 00139 setGuiItem( KStdGuiItem::guiItem(item) ); 00140 d->itemType = item; 00141 } 00142 00143 KStdGuiItem::StdItem KPushButton::guiItem() const 00144 { 00145 return d->itemType; 00146 } 00147 00148 void KPushButton::setText( const QString &text ) 00149 { 00150 QPushButton::setText(text); 00151 00152 // we need to re-evaluate the icon set when the text 00153 // is removed, or when it is supplied 00154 if (text.isEmpty() != d->item.text().isEmpty()) 00155 setIconSet(d->item.iconSet()); 00156 00157 d->item.setText(text); 00158 } 00159 00160 void KPushButton::setIconSet( const QIconSet &iconSet ) 00161 { 00162 d->item.setIconSet(iconSet); 00163 00164 if ( s_useIcons || text().isEmpty() ) 00165 QPushButton::setIconSet( iconSet ); 00166 else 00167 QPushButton::setIconSet( QIconSet() ); 00168 } 00169 00170 void KPushButton::slotSettingsChanged( int /* category */ ) 00171 { 00172 readSettings(); 00173 setIconSet( d->item.iconSet() ); 00174 } 00175 00176 void KPushButton::setDragEnabled( bool enable ) 00177 { 00178 m_dragEnabled = enable; 00179 } 00180 00181 void KPushButton::mousePressEvent( QMouseEvent *e ) 00182 { 00183 if ( m_dragEnabled ) 00184 startPos = e->pos(); 00185 QPushButton::mousePressEvent( e ); 00186 } 00187 00188 void KPushButton::mouseMoveEvent( QMouseEvent *e ) 00189 { 00190 if ( !m_dragEnabled ) 00191 { 00192 QPushButton::mouseMoveEvent( e ); 00193 return; 00194 } 00195 00196 if ( (e->state() & LeftButton) && 00197 (e->pos() - startPos).manhattanLength() > 00198 KGlobalSettings::dndEventDelay() ) 00199 { 00200 startDrag(); 00201 setDown( false ); 00202 } 00203 } 00204 00205 QDragObject * KPushButton::dragObject() 00206 { 00207 return 0L; 00208 } 00209 00210 void KPushButton::startDrag() 00211 { 00212 QDragObject *d = dragObject(); 00213 if ( d ) 00214 d->dragCopy(); 00215 } 00216 00217 void KPushButton::virtual_hook( int, void* ) 00218 { /*BASE::virtual_hook( id, data );*/ } 00219 00220 #include "kpushbutton.moc"