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