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