Added signals, distance counter, text label to panel and distance text.
[situare] / src / ui / indicatorbuttonpanel.cpp
index 3be6109..28e14b6 100644 (file)
@@ -4,6 +4,7 @@
 
        Pekka Nissinen - pekka.nissinen@ixonos.com
        Kaj Wallin - kaj.wallin@ixonos.com
+       Katri Kaikkonen - katri.kaikkonen@ixonos.com
 
    Situare is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    USA.
 */
 
-#include <QDebug>
-#include <QPainter>
 #include <QSettings>
+#include <QPainter>
+#include <QDebug>
 
 #include "indicatorbuttonpanel.h"
-#include "common.h"
-#include "panelcommon.h"
 #include "indicatorbutton.h"
+#include "panelcommon.h"
+#include "common.h"
 
-const int ROUNDING_RADIUS = 9;          ///< Roundness of the background edges
-const qreal OPACITY = 0.50;             ///< Opacity of the background in percents
+
+const int ROUNDING_RADIUS = 9;              ///< Roundness of the background edges
+const qreal OPACITY = 0.50;                 ///< Opacity of the background in percents
 
 IndicatorButtonPanel::IndicatorButtonPanel(QWidget *parent)
     : QWidget(parent),
-      m_isDraggable(false),
       m_indicatorButton(0),
+      m_isDraggable(false),
       m_distanceText("")
 {
+    /**
+     * @brief Horizontal position of direction indicator button
+     *
+     * @var DIRECTION_INDICATOR_POSITION_X
+     */
+    const int DIRECTION_INDICATOR_POSITION_X = 10 + PANEL_PEEK_AMOUNT;
+    const int DIRECTION_INDICATOR_POSITION_Y = 290;     ///< Vertical position of indicator button
+
+    const QString DIRECTION_INDICATOR_BUTTON_POSITION = "Direction_Indicator_Position";
+
+    const int MARGIN_LEFT = 0;                  ///< Inner margin (left)
+    const int MARGIN_TOP = 3;                   ///< Inner margin (top)
+    const int MARGIN_RIGHT = 0;                 ///< Inner margin (right)
+    const int MARGIN_BOTTOM = 0;                ///< Inner margin (bottom)
+    const int LABEL_MARGIN_TOP = 0;             ///< Distance text label inner margin (top)
+
+    const int INDICATOR_BUTTON_PANEL_SPACING = 5;   ///< Size of a indicator button spacing
+
+    const int PANEL_WIDTH = 90;                 ///< Width of the draggable panel
+    const int PANEL_HEIGHT = 100;               ///< Height of the draggable panel
+
     qDebug() << __PRETTY_FUNCTION__;
 
     QVBoxLayout *verticalLayout = new QVBoxLayout;
     setLayout(verticalLayout);
-    verticalLayout->setContentsMargins(0, 0, 0, 0);
-    verticalLayout->setSpacing(5);
+    verticalLayout->setContentsMargins(MARGIN_LEFT, MARGIN_TOP, MARGIN_RIGHT, MARGIN_BOTTOM);
+    verticalLayout->setSpacing(INDICATOR_BUTTON_PANEL_SPACING);
 
     m_indicatorButton = new IndicatorButton(this);
 
     m_distanceTextLabel = new QLabel();
     m_distanceTextLabel->setFont(QFont(NOKIA_FONT_SMALL));
-    m_distanceTextLabel->setText(m_distanceText);
     m_distanceTextLabel->setAlignment(Qt::AlignHCenter);
-    m_distanceTextLabel->setContentsMargins(0, 0, 0, 0);
+    m_distanceTextLabel->setContentsMargins(MARGIN_LEFT, LABEL_MARGIN_TOP, MARGIN_RIGHT
+                                            , MARGIN_BOTTOM);
 
     m_normalColor = new QColor(Qt::black);
     m_normalColor->setAlpha(OPACITY * 255);
@@ -59,7 +82,8 @@ IndicatorButtonPanel::IndicatorButtonPanel(QWidget *parent)
     verticalLayout->addWidget(m_indicatorButton, 0, Qt::AlignHCenter);
     verticalLayout->addWidget(m_distanceTextLabel, 0, Qt::AlignHCenter);
     verticalLayout->addStretch();
-    setFixedSize(90, 100);
+
+    setFixedSize(PANEL_WIDTH, PANEL_HEIGHT);
 
     QSettings settings(DIRECTORY_NAME, FILE_NAME);
     QPoint savedLocation = settings.value(DIRECTION_INDICATOR_BUTTON_POSITION,
@@ -81,24 +105,16 @@ IndicatorButtonPanel::IndicatorButtonPanel(QWidget *parent)
     m_forceReleaseTimer->setSingleShot(true);
     m_forceReleaseTimer->setInterval(FORCE_RELEASE_TIME);
 
-    connect(m_indicatorButton, SIGNAL(pressed()),
-            m_dragStartTimer, SLOT(start()));
-    connect(m_indicatorButton, SIGNAL(released()),
-            m_dragStartTimer, SLOT(stop()));
-
     connect(m_dragStartTimer, SIGNAL(timeout()),
             this, SLOT(timerExpired()));
     connect(m_forceReleaseTimer, SIGNAL(timeout()),
             this, SLOT(forceMouseRelease()));
 
     connect(this, SIGNAL(directionIndicatorValuesUpdate(qreal, qreal, bool)),
-            m_indicatorButton, SLOT(updateValues(qreal, qreal, bool)));
+            this, SLOT(updateValues(qreal, qreal, bool)));
 
     connect(m_indicatorButton, SIGNAL(autoCenteringTriggered(bool)),
             this, SIGNAL(autoCenteringTriggered(bool)));
-
-    connect(this, SIGNAL(draggingModeTriggered()),
-            this, SIGNAL(draggingModeTriggered()));
 }
 
 void IndicatorButtonPanel::mouseMoveEvent(QMouseEvent *event)
@@ -148,11 +164,9 @@ void IndicatorButtonPanel::mouseReleaseEvent(QMouseEvent *event)
         setDraggable(false);
 
         QSettings settings(DIRECTORY_NAME, FILE_NAME);
-        settings.setValue(ZOOMPANEL_POSITION, pos());
+        settings.setValue(DIRECTION_INDICATOR_BUTTON_POSITION, pos());
 
         releaseMouse();
-
-//        m_indicatorButton->setDown();
     }
 }
 
@@ -165,13 +179,25 @@ void IndicatorButtonPanel::paintEvent(QPaintEvent *event)
     QPainter painter(this);
     painter.setRenderHint(QPainter::Antialiasing);
 
-    QPainterPath backgroundPath;
-    QRect distanceLabelRect = m_distanceTextLabel->rect();
+    if (!m_indicatorButton->isChecked()) {
+        const int ADJUST_LEFT = -5;              ///< Pixels added to the rectangle (left)
+        const int ADJUST_TOP = 0;                ///< Pixels added to the rectangle (top)
+        const int ADJUST_RIGHT = +5;             ///< Pixels added to the rectangle (right)
+        const int ADJUST_BOTTOM = 0;             ///< Pixels added to the rectangle (bottom)
 
-    distanceLabelRect.translate(m_distanceTextLabel->pos());
-    distanceLabelRect.adjust(-5, 0, +5, 0);
-    backgroundPath.addRoundedRect(distanceLabelRect, 5, 5);
-    painter.fillPath(backgroundPath, QBrush(*m_normalColor));
+        const int RADIUS_WIDTH = 5;              ///< Roundness width
+        const int RADIUS_HEIGHT = 5;             ///< Roundness height
+
+        QPainterPath backgroundPath;
+        QRect distanceLabelRect = m_distanceTextLabel->rect();
+
+        distanceLabelRect.translate(m_distanceTextLabel->pos());
+        distanceLabelRect.adjust(ADJUST_LEFT, ADJUST_TOP, ADJUST_RIGHT, ADJUST_BOTTOM);
+        backgroundPath.addRoundedRect(distanceLabelRect, RADIUS_WIDTH, RADIUS_HEIGHT);
+        painter.fillPath(backgroundPath, QBrush(*m_normalColor));
+
+        update();
+    }
 
     if(m_isDraggable) {
         QPainterPath backgroundPath;
@@ -187,6 +213,7 @@ void IndicatorButtonPanel::setDraggable(bool mode, QPoint eventPosition)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_isDraggable = mode;
+    emit draggingModeTriggered();
 
     if(mode) {
         grabMouse();
@@ -200,6 +227,11 @@ void IndicatorButtonPanel::setDraggable(bool mode, QPoint eventPosition)
     update();
 }
 
+void IndicatorButtonPanel::setIndicatorButtonEnabled(bool enabled)
+{
+        m_indicatorButton->setChecked(enabled);
+}
+
 void IndicatorButtonPanel::screenResized(const QSize &newSize)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -250,41 +282,48 @@ void IndicatorButtonPanel::timerExpired()
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if(m_indicatorButton->isDown())
-        m_dragPosition = m_indicatorButton->eventPosition();
-
     setDraggable(true, m_dragPosition);
 }
 
-QString IndicatorButtonPanel::countDistance(qreal m_distance)
+void IndicatorButtonPanel::updateValues(qreal direction, qreal distance, bool draw)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-//    QString m_distanceText;
-
-//    if(m_distance < 0.01)
-//    {
-//        m_distanceText.setNum(10);
-//        m_distanceText.prepend("< ");
-//        m_distanceText.append(" m");
-//    }
-//    else if((m_distance >= 0.01) && (m_distance <= 0.999999))
-//    {
-//        m_distanceText.setNum(m_distance * 1000,1,1);
-//        m_distanceText.append(" m");
-//    }
-//    else if((m_distance >= 1) && (m_distance <= 100))
-//    {
-//        m_distanceText.setNum(m_distance,1,1);
-//        m_distanceText.append(" km");
-//    }
-//    else {
-//        m_distanceText.setNum(m_distance,0,0);
-//        m_distanceText.append(" km");
-//    }
-
-//    return m_distanceText;
-//    qWarning() << m_distanceText;
+    const int SMALL_REDUCE = 0.5;                   ///< 0.5 meter reduce
+    const int BIG_REDUCE = 100;                     ///< 100 meter reduce
+    const qreal M_TO_KM = 1000.0;                   ///< Meters to kilometers conversion
 
-}
+    m_indicatorButton->setDirection(direction, draw);
+
+    m_distance = distance;
+
+        if(m_distance < 10.0)
+        {
+            m_distanceText.setNum(10);
+            m_distanceText.prepend("< ");
+            m_distanceText.append(" m");
+        }
+        else if(m_distance < 1000 - SMALL_REDUCE)
+        {
+            m_distanceText.setNum(m_distance, 0 , 0);
+            m_distanceText.append(" m");
+        }
+        else if(m_distance < 100000 - BIG_REDUCE)
+        {
+            m_distanceText.setNum(m_distance / M_TO_KM, 1, 1);
+            m_distanceText.append(" km");
+        }
+        else {
+            m_distanceText.setNum(m_distance / M_TO_KM, 0, 0);
+            m_distanceText.append(" km");
+        }
+
+        if (!m_indicatorButton->isChecked()) {
+            m_distanceTextLabel->setText(m_distanceText);
+        }
+        else {
+            m_distanceTextLabel->setText("");
+        }
+        update();
+    }