Fix typo
[kdepim.git] / knotes / knotebutton.cpp
blobe42627b2fbfcd80ffd93a42120b05ee0d98138ed
1 /*******************************************************************
2 KNotes -- Notes for the KDE project
4 Copyright (c) 2002-2007, The KNotes Developers
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.
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.
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 *******************************************************************/
21 #include "knotebutton.h"
23 #include <kiconloader.h>
24 #include <kicontheme.h>
26 #include <QIcon>
27 #include <QPainter>
28 #include <QPixmap>
29 #include <QSizePolicy>
30 #include <QStyle>
31 #include <QStyleOption>
34 KNoteButton::KNoteButton( const QString &icon, QWidget *parent )
35 : QPushButton( parent )
37 setFocusPolicy( Qt::NoFocus );
38 setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
40 setFlat( true );
42 if ( !icon.isEmpty() ) {
43 setIcon( KIconLoader::global()->loadIcon( icon, KIconLoader::Small, 10 ) );
47 KNoteButton::~KNoteButton()
51 void KNoteButton::enterEvent( QEvent * )
53 setFlat( false );
54 repaint();
57 void KNoteButton::leaveEvent( QEvent * )
59 setFlat( true );
60 repaint();
63 int KNoteButton::heightForWidth( int w ) const
65 return w;
68 QSize KNoteButton::sizeHint() const
70 return QSize( QPushButton::sizeHint().height(),
71 QPushButton::sizeHint().height() );
74 void KNoteButton::paintEvent( QPaintEvent * )
76 QPainter p( this );
78 // the button
79 QStyleOption opt;
80 opt.initFrom( this );
82 if ( isEnabled() ) {
83 opt.state |= QStyle::State_Enabled;
85 if ( isDown() ) {
86 opt.state |= QStyle::State_DownArrow;
88 if ( isCheckable() && isChecked() ) {
89 opt.state |= QStyle::State_On;
91 if ( !isFlat() ) {
92 if ( !isDown() ) {
93 opt.state |= QStyle::State_Raised;
94 } else {
95 opt.state |= QStyle::State_MouseOver;
99 if ( ( opt.state & QStyle::State_MouseOver ) && ( ! ( opt.state & QStyle::State_DownArrow ) ) ) {
100 style()->drawPrimitive( QStyle::PE_PanelButtonTool, &opt, &p, this );
103 // the button label
104 if ( !icon().isNull() ) {
105 QIcon::Mode mode = QIcon::Disabled;
106 QIcon::State state = QIcon::Off;
108 if ( opt.state & QStyle::State_Enabled ) {
109 mode = hasFocus() ? QIcon::Active : QIcon::Normal;
111 if ( opt.state & QStyle::State_On ) {
112 state = QIcon::On;
115 QPixmap pix = icon().pixmap( style()->pixelMetric(
116 QStyle::PM_SmallIconSize ), mode, state );
118 int dx = ( width() - pix.width() ) / 2;
119 int dy = ( height() - pix.height() ) / 2;
121 // Shift button contents if pushed.
122 if ( ( opt.state & QStyle::State_On ) || ( opt.state & QStyle::State_DownArrow ) ) {
123 dx += style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal );
124 dy += style()->pixelMetric( QStyle::PM_ButtonShiftVertical );
127 p.drawPixmap( dx, dy, pix );
131 #include "knotebutton.moc"