not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / desktop / toolbutton.cpp
blob9a15aef0f7525a51da0a582ef06bf4ff9a34154c
1 /*
2 * Copyright 2008 Marco Martin <notmart@gmail.com>
3 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2, or
8 * (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 Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "toolbutton.h"
23 //Qt
24 #include <QAction>
25 #include <QPainter>
26 #include <QPaintEvent>
27 #include <QStyle>
28 #include <QStyleOptionToolButton>
29 #include <QGraphicsSceneHoverEvent>
31 //KDE
32 #include <KColorUtils>
34 //Plasma
35 #include <Plasma/PaintUtils>
36 #include <Plasma/Theme>
37 #include <Plasma/FrameSvg>
38 #include <Plasma/Animator>
40 ToolButton::ToolButton(QWidget *parent)
41 : QToolButton(parent),
42 m_action(0),
43 m_animationId(0),
44 m_alpha(0),
45 m_fadeIn(true)
47 m_background = new Plasma::FrameSvg(this);
48 m_background->setImagePath("widgets/button");
49 m_background->setCacheAllRenderedFrames(true);
50 m_background->setElementPrefix("plain");
53 void ToolButton::setAction(QAction *action)
55 if (!action) {
56 return;
59 if (m_action) {
60 disconnect(m_action, SIGNAL(changed()), this, SLOT(syncToAction()));
61 disconnect(this, SIGNAL(clicked()), m_action, SLOT(trigger()));
64 m_action = action;
65 connect(m_action, SIGNAL(changed()), this, SLOT(syncToAction()));
66 connect(this, SIGNAL(clicked()), m_action, SLOT(trigger()));
67 connect(m_action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
68 syncToAction();
71 void ToolButton::syncToAction()
73 if (!m_action) {
74 return;
77 setIcon(m_action->icon());
78 setText(m_action->text());
80 if (toolButtonStyle() == Qt::ToolButtonIconOnly) {
81 setToolTip(m_action->text());
84 setCheckable(m_action->isCheckable());
85 if (m_action->actionGroup()) {
86 setAutoExclusive(m_action->actionGroup()->isExclusive());
89 setEnabled(m_action->isEnabled());
92 void ToolButton::actionDestroyed(QObject *)
94 m_action = 0;
97 void ToolButton::paintEvent(QPaintEvent *event)
99 Q_UNUSED(event)
101 QPainter painter(this);
103 QStyleOptionToolButton buttonOpt;
104 initStyleOption(&buttonOpt);
106 if (m_animationId || (buttonOpt.state & QStyle::State_MouseOver) || (buttonOpt.state & QStyle::State_On)) {
107 if (buttonOpt.state & QStyle::State_Sunken || (buttonOpt.state & QStyle::State_On)) {
108 m_background->setElementPrefix("pressed");
109 } else {
110 m_background->setElementPrefix("normal");
112 m_background->resizeFrame(size());
114 if (m_animationId) {
115 QPixmap buffer = m_background->framePixmap();
117 QPainter bufferPainter(&buffer);
118 bufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
119 QColor alphaColor(Qt::black);
120 alphaColor.setAlphaF(qMin(qreal(0.95), m_alpha));
121 bufferPainter.fillRect(buffer.rect(), alphaColor);
122 bufferPainter.end();
124 painter.drawPixmap(QPoint(0,0), buffer);
126 buttonOpt.palette.setColor(QPalette::ButtonText, KColorUtils::mix(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor), Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor), 1-m_alpha));
127 } else {
128 m_background->paintFrame(&painter);
129 buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
132 } else {
133 buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
136 style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, &painter, this);
139 void ToolButton::enterEvent(QEvent *event)
141 if (isChecked()) {
142 return;
145 const int FadeInDuration = 75;
147 if (m_animationId) {
148 Plasma::Animator::self()->stopElementAnimation(m_animationId);
151 m_fadeIn = true;
152 m_animationId = Plasma::Animator::self()->customAnimation(
153 40 / (1000 / FadeInDuration), FadeInDuration,
154 Plasma::Animator::LinearCurve, this, "animationUpdate");
157 void ToolButton::leaveEvent(QEvent *event)
159 if (isChecked()) {
160 return;
163 const int FadeOutDuration = 150;
165 if (m_animationId) {
166 Plasma::Animator::self()->stopElementAnimation(m_animationId);
169 m_fadeIn = false;
170 m_animationId = Plasma::Animator::self()->customAnimation(
171 40 / (1000 / FadeOutDuration), FadeOutDuration,
172 Plasma::Animator::LinearCurve, this, "animationUpdate");
176 void ToolButton::animationUpdate(qreal progress)
178 if (qFuzzyCompare(progress, 1)) {
179 m_animationId = 0;
180 m_fadeIn = true;
183 m_alpha = m_fadeIn ? progress : 1 - progress;
185 // explicit update
186 update();
189 #include "toolbutton.moc"