compile
[kdegraphics.git] / gwenview / lib / expandbutton.cpp
blob56e6040c4f139dc1b45c2bc7afc909d0eb4896e6
1 /*
2 Gwenview: an image viewer
3 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "expandbutton.h"
22 // Qt
23 #include <QStyle>
24 #include <QStyleOption>
25 #include <QStylePainter>
27 namespace Gwenview {
29 static const int INDICATOR_SIZE = 9; // ### hardcoded in qcommonstyle.cpp
31 ExpandButton::ExpandButton(QWidget* parent)
32 : QToolButton(parent) {
33 setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
34 setAutoRaise(true);
35 setCheckable(true);
38 QSize ExpandButton::sizeHint() const {
39 QSize size = QToolButton::sizeHint();
40 size.setWidth(size.width() + 2*INDICATOR_SIZE);
41 size.setHeight(fontMetrics().height());
42 return size;
45 void ExpandButton::paintEvent(QPaintEvent*) {
46 QStylePainter painter(this);
48 QStyleOption option;
49 option.initFrom(this);
51 // Paint branch indicator
52 // Code comes from Designer, sheet_delegate.cpp
53 QStyleOption branchOption;
54 QRect r = option.rect;
55 branchOption.rect = QRect(r.left() + INDICATOR_SIZE/2, r.top() + (r.height() - INDICATOR_SIZE)/2, INDICATOR_SIZE, INDICATOR_SIZE);
56 branchOption.palette = option.palette;
57 branchOption.state = QStyle::State_Children;
59 if (isChecked()) {
60 branchOption.state |= QStyle::State_Open;
63 style()->drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, &painter, this);
65 // Paint text
66 QRect textRect = rect();
67 textRect.setLeft(INDICATOR_SIZE * 2);
69 painter.drawItemText(textRect, Qt::TextShowMnemonic | Qt::AlignVCenter, option.palette, isEnabled(), text());
73 } // namespace