knotes Library API Documentation

knotebutton.h

00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 1997-2001, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 *******************************************************************/
00020 
00021 #ifndef KNOTEBUTTON_H
00022 #define KNOTEBUTTON_H
00023 
00024 #include <qpushbutton.h>
00025 #include <qdrawutil.h>
00026 #include <qstyle.h>
00027 #include <qpainter.h>
00028 
00029 
00030 class KNoteButton: public QPushButton
00031 {
00032     Q_OBJECT
00033 public:
00034     KNoteButton( QWidget* parent=0, const char* name=0 )
00035         : QPushButton( parent, name )
00036     {
00037         setFocusPolicy( NoFocus );
00038         flat = true;
00039         last_button = 0;
00040     }
00041     ~KNoteButton() {}
00042 
00043 private:
00044     bool flat;
00045     int  last_button;
00046 
00047 protected:
00048     void enterEvent( QEvent * )
00049     {
00050         flat = false;
00051         repaint( false );
00052     }
00053 
00054     void leaveEvent( QEvent * )
00055     {
00056         flat = true;
00057         repaint();
00058     }
00059 
00060     void mousePressEvent( QMouseEvent *e )
00061     {
00062         if ( isDown() )
00063             return;
00064 
00065         bool hit = hitButton( e->pos() );
00066         if ( hit )
00067         {
00068             last_button = e->button();
00069             setDown( TRUE );
00070             repaint( FALSE );
00071             emit pressed();
00072         }
00073     }
00074 
00075     void mouseReleaseEvent( QMouseEvent *e )
00076     {
00077         if ( !isDown() )
00078         {
00079             last_button = 0;
00080             return;
00081         }
00082 
00083         bool hit = hitButton( e->pos() );
00084         setDown( FALSE );
00085         if ( hit )
00086         {
00087             if ( isToggleButton() )
00088                 setOn( !isOn() );
00089             repaint( FALSE );
00090 
00091             if ( isToggleButton() )
00092                 emit toggled( isOn() );
00093             emit released();
00094             emit clicked();
00095         }
00096         else
00097         {
00098             repaint();
00099             emit released();
00100         }
00101         last_button = 0;
00102     }
00103     
00104     void mouseMoveEvent( QMouseEvent *e )
00105     {
00106         if ( !last_button )
00107             return;
00108 
00109         if ( !(e->state() & LeftButton) &&
00110              !(e->state() & MidButton) &&
00111              !(e->state() & RightButton))
00112             return;
00113 
00114         bool hit = hitButton( e->pos() );
00115         if ( hit )
00116         {
00117             if ( !isDown() )
00118             {
00119                 setDown( true );
00120                 repaint( false );
00121                 emit pressed();
00122             }
00123         }
00124         else
00125         {
00126             if ( isDown() )
00127             {
00128                 setDown( false );
00129                 repaint();
00130                 emit released();
00131             }
00132         }
00133     }
00134 
00135     void paint( QPainter* painter )
00136     {
00137         if ( !painter )
00138             return;
00139 
00140         if ( isDown() || (isOn() && !flat) )
00141         {
00142             if ( style().styleHint(QStyle::SH_GUIStyle) == Qt::WindowsStyle )
00143                 qDrawWinButton( painter, 0, 0, width(),
00144                                 height(), colorGroup(), TRUE );
00145             else
00146                 qDrawShadePanel( painter, 0, 0, width(),
00147                                  height(), colorGroup(), TRUE, 2, 0L );
00148         }
00149         else if ( !flat )
00150         {
00151             if ( style().styleHint(QStyle::SH_GUIStyle) == Qt::WindowsStyle )
00152                 qDrawWinButton( painter, 0, 0, width(), height(),
00153                                 colorGroup(), FALSE );
00154             else
00155                 qDrawShadePanel( painter, 0, 0, width(), height(),
00156                                  colorGroup(), FALSE, 2, 0L );
00157         }
00158 
00159         int dx = ( width() - pixmap()->width() ) / 2;
00160         int dy = ( height() - pixmap()->height() ) / 2;
00161         if ( isDown() && style().styleHint(QStyle::SH_GUIStyle) == Qt::WindowsStyle )
00162         {
00163             dx++;
00164             dy++;
00165         }
00166         painter->drawPixmap( dx, dy, *pixmap() );
00167     }
00168 
00169     void drawButton( QPainter* p )
00170     {
00171         paint( p );
00172     }
00173 
00174     void drawButtonLabel( QPainter* p )
00175     {
00176         paint( p );
00177     }
00178 };
00179 
00180 #endif
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:46:58 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001