Merge branch 'master' into indicator
[situare] / src / ui / indicatorbutton.cpp
index d6b1a9d..ab43659 100644 (file)
@@ -4,6 +4,7 @@
 
        Kaj Wallin - kaj.wallin@ixonos.com
        Katri Kaikkonen - katri.kaikkonen@ixonos.com
+       Sami Rämö - sami.ramo@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -41,7 +42,9 @@ const qreal OPACITY = 0.50;     ///< Opacity of the background in percents
 
 IndicatorButton::IndicatorButton(QWidget *parent)
     : QToolButton(parent),
-      m_isDraggable(false)
+      m_drawTriangle(false),
+      m_isDraggable(false),
+      m_direction(0)
 {
     m_indicatorLeds[OFF].load(":res/images/led_red.png");
     m_indicatorLeds[ON].load(":res/images/led_red_s.png");
@@ -253,14 +256,51 @@ void IndicatorButton::paintEvent(QPaintEvent *event)
     else
         painter.fillPath(backgroundPath, QBrush(*m_normalColor));
 
-    if(isChecked())
-        painter.drawPixmap((this->width() / 2) - (m_indicatorLeds[ON].width() / 2),
-                           (this->height() / 2) - (m_indicatorLeds[ON].height() / 2),
-                           m_indicatorLeds[ON]);
-    else
-        painter.drawPixmap((this->width() / 2) - (m_indicatorLeds[OFF].width() / 2),
-                           (this->height() / 2) - (m_indicatorLeds[OFF].height() / 2),
-                           m_indicatorLeds[OFF]);
+    const QPointF CENTER = QPointF(this->width(), this->height()) / 2;
+
+    if (isChecked()) {
+        const QPointF offset = QPointF(m_indicatorLeds[ON].width(),
+                                       m_indicatorLeds[ON].height()) / 2;
+
+        painter.drawPixmap(CENTER - offset, m_indicatorLeds[ON]);
+    } else {
+        const QPointF offset = QPointF(m_indicatorLeds[OFF].width(),
+                                       m_indicatorLeds[OFF].height()) / 2;
+
+        painter.drawPixmap(CENTER - offset, m_indicatorLeds[OFF]);
+    }
+
+    // draw the direction indicator triangle only when autocentering is disabled and MapEngine
+    // doesn't deny drawing (because GPS location item is visible)
+    if (!isChecked() && m_drawTriangle) {
+        const int TRIANGLE_WIDTH = 10;
+        const int TRIANGLE_HEIGHT = 10;
+        const int TRIANGLE_DISTANCE_FROM_CENTER = 15;
+
+        const int POINTS = 3;
+        const QPointF points[POINTS] = {
+            QPointF(-TRIANGLE_WIDTH / 2, -TRIANGLE_DISTANCE_FROM_CENTER),
+            QPointF(0, -(TRIANGLE_DISTANCE_FROM_CENTER + TRIANGLE_HEIGHT)),
+            QPointF(TRIANGLE_WIDTH / 2, -TRIANGLE_DISTANCE_FROM_CENTER)
+        };
+
+        // base triangle is facing up, and needs to be rotated to the required direction
+        QTransform rotationTransform;
+        rotationTransform.rotate(m_direction);
+
+        // origin is in the top left corner of the button, and needs to be translated to the
+        // center of the button
+        QTransform translateTransform;
+        translateTransform.translate(CENTER.x(), CENTER.y());
+
+        painter.setTransform(rotationTransform * translateTransform);
+
+        // setting the look of the triangle
+        painter.setBrush(Qt::red);
+        painter.setPen(Qt::red);
+
+        painter.drawPolygon(points, POINTS);
+    }
 }
 
 void IndicatorButton::timerExpired()
@@ -269,3 +309,13 @@ void IndicatorButton::timerExpired()
 
     setDraggable(true, m_dragPosition);
 }
+
+void IndicatorButton::updateValues(qreal direction, qreal distance, bool draw)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    m_direction = direction;
+    m_drawTriangle = draw;
+
+    update();
+}