knotebutton.cpp
00001 /******************************************************************* 00002 KNotes -- Notes for the KDE project 00003 00004 Copyright (c) 2002-2004, The KNotes Developers 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 *******************************************************************/ 00020 00021 #include <tqstyle.h> 00022 #include <tqpainter.h> 00023 #include <tqiconset.h> 00024 #include <tqsizepolicy.h> 00025 00026 #include <kglobal.h> 00027 #include <kicontheme.h> 00028 #include <kiconloader.h> 00029 00030 #include "knotebutton.h" 00031 00032 00033 KNoteButton::KNoteButton( const TQString& icon, TQWidget *parent, const char *name ) 00034 : TQPushButton( parent, name ) 00035 { 00036 setFocusPolicy( TQ_NoFocus ); 00037 setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) ); 00038 00039 m_flat = true; 00040 00041 if ( !icon.isEmpty() ) 00042 setIconSet( KGlobal::iconLoader()->loadIconSet( icon, KIcon::Small, 10 ) ); 00043 } 00044 00045 KNoteButton::~KNoteButton() 00046 { 00047 } 00048 00049 void KNoteButton::enterEvent( TQEvent * ) 00050 { 00051 m_flat = false; 00052 repaint( false ); 00053 } 00054 00055 void KNoteButton::leaveEvent( TQEvent * ) 00056 { 00057 m_flat = true; 00058 repaint(); 00059 } 00060 00061 TQSize KNoteButton::sizeHint() const 00062 { 00063 return TQSize( TQPushButton::sizeHint().height(), TQPushButton::sizeHint().height() ); 00064 } 00065 00066 void KNoteButton::drawButton( TQPainter* p ) 00067 { 00068 TQStyle::SFlags flags = TQStyle::Style_Default; 00069 00070 if ( isEnabled() ) 00071 flags |= TQStyle::Style_Enabled; 00072 if ( isDown() ) 00073 flags |= TQStyle::Style_Down; 00074 if ( isOn() ) 00075 flags |= TQStyle::Style_On; 00076 if ( !isFlat() && !isDown() ) 00077 flags |= TQStyle::Style_Raised; 00078 if ( !m_flat ) 00079 flags |= TQStyle::Style_MouseOver; 00080 00081 style().tqdrawPrimitive( TQStyle::PE_ButtonTool, p, rect(), colorGroup(), flags ); 00082 drawButtonLabel( p ); 00083 } 00084 00085 void KNoteButton::drawButtonLabel( TQPainter* p ) 00086 { 00087 if ( iconSet() && !iconSet()->isNull() ) 00088 { 00089 TQIconSet::Mode mode = TQIconSet::Disabled; 00090 TQIconSet::State state = TQIconSet::Off; 00091 00092 if ( isEnabled() ) 00093 mode = hasFocus() ? TQIconSet::Active : TQIconSet::Normal; 00094 if ( isToggleButton() && isOn() ) 00095 state = TQIconSet::On; 00096 00097 TQPixmap pix = iconSet()->pixmap( TQIconSet::Small, mode, state ); 00098 00099 int dx = ( width() - pix.width() ) / 2; 00100 int dy = ( height() - pix.height() ) / 2; 00101 00102 // Shift button contents if pushed. 00103 if ( isOn() || isDown() ) 00104 { 00105 dx += style().pixelMetric( TQStyle::PM_ButtonShiftHorizontal, this ); 00106 dy += style().pixelMetric( TQStyle::PM_ButtonShiftVertical, this ); 00107 } 00108 00109 p->drawPixmap( dx, dy, pix ); 00110 } 00111 }