.
[dorian] / widgets / translucentbutton.cpp
index bdb103b..3b4121e 100644 (file)
@@ -1,40 +1,62 @@
+#include <QtGui>
+
 #include "translucentbutton.h"
+#include "platform.h"
 
-#ifdef Q_WS_MAC
-#   define ICON_PREFIX ":/icons/mac/"
-#else
-#   define ICON_PREFIX ":/icons/"
-#endif
+const int TranslucentButton::pixels = 95;
+const int TranslucentButton::elevatorInterval = 750;
 
 TranslucentButton::TranslucentButton(const QString &name_, QWidget *parent):
-    QWidget(parent), name(name_), mOpacity(1.)
+    QLabel(parent), name(name_), transparent(true)
 {
-    setGeometry(0, 0, 50, 50);
-    show();
+    setFixedSize(pixels, pixels);
+    elevatorTimer = startTimer(elevatorInterval);
+}
+
+TranslucentButton::~TranslucentButton()
+{
+    killTimer(elevatorTimer);
 }
 
 void TranslucentButton::paintEvent(QPaintEvent *)
 {
-    if (mOpacity < 1) {
-        QPainter painter(this);
+    QPainter painter(this);
+    if (!transparent) {
         painter.setRenderHint(QPainter::Antialiasing, true);
-        painter.drawPixmap(0, 0, QPixmap(ICON_PREFIX + name + ".png").scaled(
-                QSize(95, 95), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
+        painter.drawPixmap(0, 0, QPixmap(Platform::instance()->icon(name)).scaled(
+                QSize(pixels, pixels), Qt::IgnoreAspectRatio,
+                Qt::SmoothTransformation));
+    } else {
+        painter.fillRect(0, 0, pixels, pixels, Qt::NoBrush);
     }
 }
 
 void TranslucentButton::flash(int duration)
 {
-    QPropertyAnimation *ani = new QPropertyAnimation(this, "opacity", 0);
-    ani->setDuration(duration);
-    ani->setStartValue(0.);
-    ani->setEndValue(1.);
-    ani->setEasingCurve(QEasingCurve::OutQuart);
-    ani->start(QPropertyAnimation::DeleteWhenStopped);
+    raise();
+    show();
+    transparent = false;
+    update();
+    QTimer::singleShot(duration, this, SLOT(stopFlash()));
 }
 
-void TranslucentButton::setOpacity(qreal opacity)
+void TranslucentButton::stopFlash()
 {
-    mOpacity = opacity;
+    transparent = true;
     update();
 }
+
+void TranslucentButton::mouseReleaseEvent(QMouseEvent *e)
+{
+    Q_UNUSED(e);
+    emit triggered();
+    e->accept();
+}
+
+void TranslucentButton::timerEvent(QTimerEvent *e)
+{
+    if (e->timerId() == elevatorTimer) {
+        raise();
+    }
+    QLabel::timerEvent(e);
+}