knotes Library API Documentation

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 *******************************************************************/
00020 
00021 #include <qstyle.h>
00022 #include <qpainter.h>
00023 #include <qiconset.h>
00024 #include <qsizepolicy.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 QString& icon, QWidget *parent, const char *name )
00034     : QPushButton( parent, name )
00035 {
00036     setFocusPolicy( NoFocus );
00037     setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::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( QEvent * )
00050 {
00051     m_flat = false;
00052     repaint( false );
00053 }
00054 
00055 void KNoteButton::leaveEvent( QEvent * )
00056 {
00057     m_flat = true;
00058     repaint();
00059 }
00060 
00061 QSize KNoteButton::sizeHint() const
00062 {
00063     return QSize( QPushButton::sizeHint().height(), QPushButton::sizeHint().height() );
00064 }
00065 
00066 void KNoteButton::drawButton( QPainter* p )
00067 {
00068     QStyle::SFlags flags = QStyle::Style_Default;
00069 
00070     if ( isEnabled() )
00071         flags |= QStyle::Style_Enabled;
00072     if ( isDown() )
00073         flags |= QStyle::Style_Down;
00074     if ( isOn() )
00075         flags |= QStyle::Style_On;
00076     if ( !isFlat() && !isDown() )
00077         flags |= QStyle::Style_Raised;
00078     if ( !m_flat )
00079         flags |= QStyle::Style_MouseOver;
00080 
00081     style().drawPrimitive( QStyle::PE_ButtonTool, p, rect(), colorGroup(), flags );
00082     drawButtonLabel( p );
00083 }
00084 
00085 void KNoteButton::drawButtonLabel( QPainter* p )
00086 {
00087     if ( iconSet() && !iconSet()->isNull() )
00088     {
00089         QIconSet::Mode  mode  = QIconSet::Disabled;
00090         QIconSet::State state = QIconSet::Off;
00091 
00092         if ( isEnabled() )
00093             mode = hasFocus() ? QIconSet::Active : QIconSet::Normal;
00094         if ( isToggleButton() && isOn() )
00095             state = QIconSet::On;
00096 
00097         QPixmap pix = iconSet()->pixmap( QIconSet::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( QStyle::PM_ButtonShiftHorizontal, this );
00106             dy += style().pixelMetric( QStyle::PM_ButtonShiftVertical, this );
00107         }
00108 
00109         p->drawPixmap( dx, dy, pix );
00110     }
00111 }
KDE Logo
This file is part of the documentation for knotes Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:55 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003