0b33be14fcded56d0686724d1d529b061d95aad4
[dorian] / widgets / translucentbutton.cpp
1 #include <QtGui>
2
3 #include "translucentbutton.h"
4 #include "platform.h"
5 #include "trace.h"
6
7 const int TranslucentButton::pixels = 95;
8
9 TranslucentButton::TranslucentButton(const QString &name_, QWidget *parent):
10     QLabel(parent), name(name_), transparent(true)
11 {
12     setGeometry(0, 0, pixels, pixels);
13     timer = new QTimer(this);
14     timer->setSingleShot(true);
15     connect(timer, SIGNAL(timeout()), this, SLOT(stopFlash()));
16     show();
17 }
18
19 void TranslucentButton::paintEvent(QPaintEvent *)
20 {
21     QPainter painter(this);
22     if (!transparent) {
23         painter.setRenderHint(QPainter::Antialiasing, true);
24         painter.drawPixmap(0, 0, QPixmap(Platform::icon(name)).scaled(
25                 QSize(pixels, pixels), Qt::IgnoreAspectRatio,
26                 Qt::SmoothTransformation));
27     } else {
28         painter.fillRect(0, 0, pixels, pixels, Qt::NoBrush);
29     }
30 }
31
32 void TranslucentButton::flash(int duration)
33 {
34     show();
35     raise();
36     transparent = false;
37     update();
38     timer->start(duration);
39 }
40
41 void TranslucentButton::stopFlash()
42 {
43     transparent = true;
44     update();
45 }
46
47 void TranslucentButton::mouseReleaseEvent(QMouseEvent *e)
48 {
49     Q_UNUSED(e);
50     emit triggered();
51     e->accept();
52 }