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>
29 #include <QSizePolicy>
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
) );
42 if ( !icon
.isEmpty() ) {
43 setIcon( KIconLoader::global()->loadIcon( icon
, KIconLoader::Small
, 10 ) );
47 KNoteButton::~KNoteButton()
51 void KNoteButton::enterEvent( QEvent
* )
57 void KNoteButton::leaveEvent( QEvent
* )
63 int KNoteButton::heightForWidth( int w
) const
68 QSize
KNoteButton::sizeHint() const
70 return QSize( QPushButton::sizeHint().height(),
71 QPushButton::sizeHint().height() );
74 void KNoteButton::paintEvent( QPaintEvent
* )
83 opt
.state
|= QStyle::State_Enabled
;
86 opt
.state
|= QStyle::State_DownArrow
;
88 if ( isCheckable() && isChecked() ) {
89 opt
.state
|= QStyle::State_On
;
93 opt
.state
|= QStyle::State_Raised
;
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 );
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
) {
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"