Update from old repository.
[dorian] / dorian / translucentbutton.cpp
1 #include "translucentbutton.h" 
2
3 #ifdef Q_WS_MAC
4 #   define ICON_PREFIX ":/icons/mac/"
5 #else
6 #   define ICON_PREFIX ":/icons/"
7 #endif
8
9 TranslucentButton::TranslucentButton(const QString &name_, QWidget *parent):
10     QWidget(parent), name(name_), mOpacity(1.)
11 {
12     setGeometry(0, 0, 50, 50);
13     hide();
14 }
15
16 void TranslucentButton::paintEvent(QPaintEvent *e)
17 {
18     (void)e;
19     QPainter painter(this);
20     painter.setRenderHint(QPainter::Antialiasing, true);
21     painter.drawPixmap(0, 0, QPixmap(ICON_PREFIX + name + ".png").
22         scaled(QSize(95, 95), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
23 }
24
25 void TranslucentButton::flash()
26 {
27     QPropertyAnimation *ani = new QPropertyAnimation(this, "opacity", 0);
28     ani->setDuration(2000);
29     ani->setStartValue(0.);
30     ani->setEndValue(1.);
31     ani->setEasingCurve(QEasingCurve::OutQuart);
32     show();
33     ani->start(QPropertyAnimation::DeleteWhenStopped);
34     connect(ani, SIGNAL(destroyed()), this, SLOT(onAnimationEnd()));
35 }
36
37 void TranslucentButton::setOpacity(qreal opacity)
38 {
39     mOpacity = opacity;
40     update();
41 }
42
43 void TranslucentButton::onAnimationEnd()
44 {
45     hide();
46 }