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"
26 #include <QPaintEvent>
28 #include <QStyleOptionToolButton>
29 #include <QGraphicsSceneHoverEvent>
32 #include <KColorUtils>
35 #include <Plasma/PaintUtils>
36 #include <Plasma/Theme>
37 #include <Plasma/FrameSvg>
38 #include <Plasma/Animator>
40 ToolButton::ToolButton(QWidget
*parent
)
41 : QToolButton(parent
),
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
)
60 disconnect(m_action
, SIGNAL(changed()), this, SLOT(syncToAction()));
61 disconnect(this, SIGNAL(clicked()), m_action
, SLOT(trigger()));
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
*)));
71 void ToolButton::syncToAction()
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
*)
97 void ToolButton::paintEvent(QPaintEvent
*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");
110 m_background
->setElementPrefix("normal");
112 m_background
->resizeFrame(size());
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
);
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
));
128 m_background
->paintFrame(&painter
);
129 buttonOpt
.palette
.setColor(QPalette::ButtonText
, Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor
));
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
)
145 const int FadeInDuration
= 75;
148 Plasma::Animator::self()->stopElementAnimation(m_animationId
);
152 m_animationId
= Plasma::Animator::self()->customAnimation(
153 40 / (1000 / FadeInDuration
), FadeInDuration
,
154 Plasma::Animator::LinearCurve
, this, "animationUpdate");
157 void ToolButton::leaveEvent(QEvent
*event
)
163 const int FadeOutDuration
= 150;
166 Plasma::Animator::self()->stopElementAnimation(m_animationId
);
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)) {
183 m_alpha
= m_fadeIn
? progress
: 1 - progress
;
189 #include "toolbutton.moc"