Plasma
toolbutton.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "toolbutton.h"
00022
00023
00024 #include <QAction>
00025 #include <QPainter>
00026 #include <QPaintEvent>
00027 #include <QStyle>
00028 #include <QStyleOptionToolButton>
00029 #include <QGraphicsSceneHoverEvent>
00030
00031
00032 #include <KColorUtils>
00033
00034
00035 #include <Plasma/PaintUtils>
00036 #include <Plasma/Theme>
00037 #include <Plasma/FrameSvg>
00038 #include <Plasma/Animator>
00039
00040 ToolButton::ToolButton(QWidget *parent)
00041 : QToolButton(parent),
00042 m_action(0),
00043 m_animationId(0),
00044 m_alpha(0),
00045 m_fadeIn(true)
00046 {
00047 m_background = new Plasma::FrameSvg(this);
00048 m_background->setImagePath("widgets/button");
00049 m_background->setCacheAllRenderedFrames(true);
00050 m_background->setElementPrefix("plain");
00051 }
00052
00053 void ToolButton::setAction(QAction *action)
00054 {
00055 if (!action) {
00056 return;
00057 }
00058
00059 if (m_action) {
00060 disconnect(m_action, SIGNAL(changed()), this, SLOT(syncToAction()));
00061 disconnect(this, SIGNAL(clicked()), m_action, SLOT(trigger()));
00062 }
00063
00064 m_action = action;
00065 connect(m_action, SIGNAL(changed()), this, SLOT(syncToAction()));
00066 connect(this, SIGNAL(clicked()), m_action, SLOT(trigger()));
00067 connect(m_action, SIGNAL(destroyed(QObject*)), this, SLOT(actionDestroyed(QObject*)));
00068 syncToAction();
00069 }
00070
00071 void ToolButton::syncToAction()
00072 {
00073 if (!m_action) {
00074 return;
00075 }
00076
00077 setIcon(m_action->icon());
00078 setText(m_action->text());
00079
00080 if (toolButtonStyle() == Qt::ToolButtonIconOnly) {
00081 setToolTip(m_action->text());
00082 }
00083
00084 setCheckable(m_action->isCheckable());
00085 if (m_action->actionGroup()) {
00086 setAutoExclusive(m_action->actionGroup()->isExclusive());
00087 }
00088
00089 setEnabled(m_action->isEnabled());
00090 }
00091
00092 void ToolButton::actionDestroyed(QObject *)
00093 {
00094 m_action = 0;
00095 }
00096
00097 void ToolButton::paintEvent(QPaintEvent *event)
00098 {
00099 Q_UNUSED(event)
00100
00101 QPainter painter(this);
00102
00103 QStyleOptionToolButton buttonOpt;
00104 initStyleOption(&buttonOpt);
00105
00106 if (m_animationId || (buttonOpt.state & QStyle::State_MouseOver) || (buttonOpt.state & QStyle::State_On)) {
00107 if (buttonOpt.state & QStyle::State_Sunken || (buttonOpt.state & QStyle::State_On)) {
00108 m_background->setElementPrefix("pressed");
00109 } else {
00110 m_background->setElementPrefix("normal");
00111 }
00112 m_background->resizeFrame(size());
00113
00114 if (m_animationId) {
00115 QPixmap buffer = m_background->framePixmap();
00116
00117 QPainter bufferPainter(&buffer);
00118 bufferPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
00119 QColor alphaColor(Qt::black);
00120 alphaColor.setAlphaF(qMin(qreal(0.95), m_alpha));
00121 bufferPainter.fillRect(buffer.rect(), alphaColor);
00122 bufferPainter.end();
00123
00124 painter.drawPixmap(QPoint(0,0), buffer);
00125
00126 buttonOpt.palette.setColor(QPalette::ButtonText, KColorUtils::mix(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor), Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor), 1-m_alpha));
00127 } else {
00128 m_background->paintFrame(&painter);
00129 buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
00130 }
00131
00132 } else {
00133 buttonOpt.palette.setColor(QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
00134 }
00135
00136 style()->drawControl(QStyle::CE_ToolButtonLabel, &buttonOpt, &painter, this);
00137 }
00138
00139 void ToolButton::enterEvent(QEvent *event)
00140 {
00141 if (isChecked()) {
00142 return;
00143 }
00144
00145 const int FadeInDuration = 75;
00146
00147 if (m_animationId) {
00148 Plasma::Animator::self()->stopElementAnimation(m_animationId);
00149 }
00150
00151 m_fadeIn = true;
00152 m_animationId = Plasma::Animator::self()->customAnimation(
00153 40 / (1000 / FadeInDuration), FadeInDuration,
00154 Plasma::Animator::LinearCurve, this, "animationUpdate");
00155 }
00156
00157 void ToolButton::leaveEvent(QEvent *event)
00158 {
00159 if (isChecked()) {
00160 return;
00161 }
00162
00163 const int FadeOutDuration = 150;
00164
00165 if (m_animationId) {
00166 Plasma::Animator::self()->stopElementAnimation(m_animationId);
00167 }
00168
00169 m_fadeIn = false;
00170 m_animationId = Plasma::Animator::self()->customAnimation(
00171 40 / (1000 / FadeOutDuration), FadeOutDuration,
00172 Plasma::Animator::LinearCurve, this, "animationUpdate");
00173 }
00174
00175
00176 void ToolButton::animationUpdate(qreal progress)
00177 {
00178 if (qFuzzyCompare(progress, 1)) {
00179 m_animationId = 0;
00180 m_fadeIn = true;
00181 }
00182
00183 m_alpha = m_fadeIn ? progress : 1 - progress;
00184
00185
00186 update();
00187 }
00188
00189 #include "toolbutton.moc"
00190