not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / system-monitor / monitorbutton.cpp
blob4377206646de17a52117ad1a797578968b283370
1 /*
2 * Copyright (C) 2007 Petri Damsten <damu@iki.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "monitorbutton.h"
22 #include <QIcon>
23 #include <QPainter>
24 #include <QTimeLine>
26 #include <KDebug>
27 #include <KIcon>
28 #include <KPushButton>
30 #include <Plasma/PaintUtils>
32 #define MARGIN 2
34 class MonitorButton::Private
36 public:
37 Private() : imageSize(32, 32)
41 QSize imageSize;
42 QString image;
43 KIcon icon;
44 QTimeLine highlighter;
47 MonitorButton::MonitorButton(QGraphicsWidget *parent) :
48 Plasma::PushButton(parent),
49 d(new Private)
51 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
52 setPreferredSize(d->imageSize.width() + 2 * MARGIN, d->imageSize.height() + 2 * MARGIN);
54 d->highlighter.setDuration(100);
55 d->highlighter.setFrameRange(0, 10);
56 d->highlighter.setCurveShape(QTimeLine::EaseInCurve);
57 connect(&d->highlighter, SIGNAL(valueChanged(qreal)), this, SLOT(highlight()));
60 MonitorButton::~MonitorButton()
62 delete d;
65 QString MonitorButton::image() const
67 return d->image;
70 void MonitorButton::setImage(const QString &image)
72 d->image = image;
73 d->icon = KIcon(image);
74 update();
77 void MonitorButton::highlight()
79 update();
82 void MonitorButton::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
84 if (nativeWidget()->isChecked()) {
85 return;
88 d->highlighter.setDirection(QTimeLine::Forward);
89 d->highlighter.start();
92 void MonitorButton::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
94 if (nativeWidget()->isChecked()) {
95 return;
98 d->highlighter.setDirection(QTimeLine::Backward);
99 d->highlighter.start();
102 void MonitorButton::paint(QPainter *p,
103 const QStyleOptionGraphicsItem *option,
104 QWidget *widget)
106 Q_UNUSED(option)
107 Q_UNUSED(widget)
109 QIcon::Mode mode = QIcon::Disabled;
110 if (nativeWidget()->isChecked()) {
111 mode = QIcon::Normal;
114 QPixmap icon = Plasma::PaintUtils::transition(d->icon.pixmap(d->imageSize, QIcon::Disabled),
115 d->icon.pixmap(d->imageSize, QIcon::Normal),
116 nativeWidget()->isChecked() ? 1 : d->highlighter.currentValue());
117 p->drawPixmap(QPointF((size().width() - d->imageSize.width()) / 2,
118 (size().height() - d->imageSize.height()) / 2),
119 icon);
122 #include "monitorbutton.moc"