Applets
monitorbutton.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 #include "monitorbutton.h"
00021
00022 #include <QIcon>
00023 #include <QPainter>
00024 #include <QTimeLine>
00025
00026 #include <KDebug>
00027 #include <KIcon>
00028 #include <KPushButton>
00029
00030 #include <Plasma/PaintUtils>
00031
00032 #define MARGIN 2
00033
00034 class MonitorButton::Private
00035 {
00036 public:
00037 Private() : imageSize(32, 32)
00038 {
00039 }
00040
00041 QSize imageSize;
00042 QString image;
00043 KIcon icon;
00044 QTimeLine highlighter;
00045 };
00046
00047 MonitorButton::MonitorButton(QGraphicsWidget *parent) :
00048 Plasma::PushButton(parent),
00049 d(new Private)
00050 {
00051 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
00052 setPreferredSize(d->imageSize.width() + 2 * MARGIN, d->imageSize.height() + 2 * MARGIN);
00053
00054 d->highlighter.setDuration(100);
00055 d->highlighter.setFrameRange(0, 10);
00056 d->highlighter.setCurveShape(QTimeLine::EaseInCurve);
00057 connect(&d->highlighter, SIGNAL(valueChanged(qreal)), this, SLOT(highlight()));
00058 }
00059
00060 MonitorButton::~MonitorButton()
00061 {
00062 delete d;
00063 }
00064
00065 QString MonitorButton::image() const
00066 {
00067 return d->image;
00068 }
00069
00070 void MonitorButton::setImage(const QString &image)
00071 {
00072 d->image = image;
00073 d->icon = KIcon(image);
00074 update();
00075 }
00076
00077 void MonitorButton::highlight()
00078 {
00079 update();
00080 }
00081
00082 void MonitorButton::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
00083 {
00084 if (nativeWidget()->isChecked()) {
00085 return;
00086 }
00087
00088 d->highlighter.setDirection(QTimeLine::Forward);
00089 d->highlighter.start();
00090 }
00091
00092 void MonitorButton::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
00093 {
00094 if (nativeWidget()->isChecked()) {
00095 return;
00096 }
00097
00098 d->highlighter.setDirection(QTimeLine::Backward);
00099 d->highlighter.start();
00100 }
00101
00102 void MonitorButton::paint(QPainter *p,
00103 const QStyleOptionGraphicsItem *option,
00104 QWidget *widget)
00105 {
00106 Q_UNUSED(option)
00107 Q_UNUSED(widget)
00108
00109 QIcon::Mode mode = QIcon::Disabled;
00110 if (nativeWidget()->isChecked()) {
00111 mode = QIcon::Normal;
00112 }
00113
00114 QPixmap icon = Plasma::PaintUtils::transition(d->icon.pixmap(d->imageSize, QIcon::Disabled),
00115 d->icon.pixmap(d->imageSize, QIcon::Normal),
00116 nativeWidget()->isChecked() ? 1 : d->highlighter.currentValue());
00117 p->drawPixmap(QPointF((size().width() - d->imageSize.width()) / 2,
00118 (size().height() - d->imageSize.height()) / 2),
00119 icon);
00120 }
00121
00122 #include "monitorbutton.moc"