knotes

knotebutton.cpp
1 /*******************************************************************
2  KNotes -- Notes for the KDE project
3 
4  Copyright (c) 2002-2004, The KNotes Developers
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************/
20 
21 #include <tqstyle.h>
22 #include <tqpainter.h>
23 #include <tqiconset.h>
24 #include <tqsizepolicy.h>
25 
26 #include <tdeglobal.h>
27 #include <kicontheme.h>
28 #include <kiconloader.h>
29 
30 #include "knotebutton.h"
31 
32 
33 KNoteButton::KNoteButton( const TQString& icon, TQWidget *parent, const char *name )
34  : TQPushButton( parent, name )
35 {
36  setFocusPolicy( TQWidget::NoFocus );
37  setSizePolicy( TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed ) );
38 
39  m_flat = true;
40 
41  if ( !icon.isEmpty() )
42  setIconSet( TDEGlobal::iconLoader()->loadIconSet( icon, TDEIcon::Small, 10 ) );
43 }
44 
45 KNoteButton::~KNoteButton()
46 {
47 }
48 
49 void KNoteButton::enterEvent( TQEvent * )
50 {
51  m_flat = false;
52  repaint( false );
53 }
54 
55 void KNoteButton::leaveEvent( TQEvent * )
56 {
57  m_flat = true;
58  repaint();
59 }
60 
61 TQSize KNoteButton::sizeHint() const
62 {
63  return TQSize( TQPushButton::sizeHint().height(), TQPushButton::sizeHint().height() );
64 }
65 
66 void KNoteButton::drawButton( TQPainter* p )
67 {
68  TQStyle::SFlags flags = TQStyle::Style_Default;
69 
70  if ( isEnabled() )
71  flags |= TQStyle::Style_Enabled;
72  if ( isDown() )
73  flags |= TQStyle::Style_Down;
74  if ( isOn() )
75  flags |= TQStyle::Style_On;
76  if ( !isFlat() && !isDown() )
77  flags |= TQStyle::Style_Raised;
78  if ( !m_flat )
79  flags |= TQStyle::Style_MouseOver;
80 
81  style().drawPrimitive( TQStyle::PE_ButtonTool, p, rect(), colorGroup(), flags );
82  drawButtonLabel( p );
83 }
84 
85 void KNoteButton::drawButtonLabel( TQPainter* p )
86 {
87  if ( iconSet() && !iconSet()->isNull() )
88  {
89  TQIconSet::Mode mode = TQIconSet::Disabled;
90  TQIconSet::State state = TQIconSet::Off;
91 
92  if ( isEnabled() )
93  mode = hasFocus() ? TQIconSet::Active : TQIconSet::Normal;
94  if ( isToggleButton() && isOn() )
95  state = TQIconSet::On;
96 
97  TQPixmap pix = iconSet()->pixmap( TQIconSet::Small, mode, state );
98 
99  int dx = ( width() - pix.width() ) / 2;
100  int dy = ( height() - pix.height() ) / 2;
101 
102  // Shift button contents if pushed.
103  if ( isOn() || isDown() )
104  {
105  dx += style().pixelMetric( TQStyle::PM_ButtonShiftHorizontal, this );
106  dy += style().pixelMetric( TQStyle::PM_ButtonShiftVertical, this );
107  }
108 
109  p->drawPixmap( dx, dy, pix );
110  }
111 }