Fixing defects found in the review
[situare] / src / ui / indicatorbuttonpanel.cpp
index 28e14b6..2d2ada4 100644 (file)
    USA.
 */
 
-#include <QSettings>
-#include <QPainter>
 #include <QDebug>
+#include <QPainter>
+#include <QSettings>
 
-#include "indicatorbuttonpanel.h"
+#include "common.h"
 #include "indicatorbutton.h"
 #include "panelcommon.h"
-#include "common.h"
 
+#include "indicatorbuttonpanel.h"
 
-const int ROUNDING_RADIUS = 9;              ///< Roundness of the background edges
-const qreal OPACITY = 0.50;                 ///< Opacity of the background in percents
+const QString DIRECTION_INDICATOR_BUTTON_POSITION = "DIRECTION_INDICATOR_POSITION";
 
 IndicatorButtonPanel::IndicatorButtonPanel(QWidget *parent)
     : QWidget(parent),
-      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";
+    qDebug() << __PRETTY_FUNCTION__;
 
-    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 DIRECTION_INDICATOR_POSITION_X = 10 + PANEL_PEEK_AMOUNT;
+    const int DIRECTION_INDICATOR_POSITION_Y = 290;
 
-    const int INDICATOR_BUTTON_PANEL_SPACING = 5;   ///< Size of a indicator button spacing
+    const int INDICATOR_BUTTON_PANEL_SPACING = 5;
 
-    const int PANEL_WIDTH = 90;                 ///< Width of the draggable panel
-    const int PANEL_HEIGHT = 100;               ///< Height of the draggable panel
+    const qreal OPACITY = 0.50;
+    const int MARGIN_LEFT = 0;
+    const int MARGIN_TOP = 3;
+    const int MARGIN_RIGHT = 0;
+    const int MARGIN_BOTTOM = 0;
+    const int LABEL_MARGIN_TOP = 0;
 
-    qDebug() << __PRETTY_FUNCTION__;
+    const int PANEL_WIDTH = 90;
+    const int PANEL_HEIGHT = 100;
 
     QVBoxLayout *verticalLayout = new QVBoxLayout;
     setLayout(verticalLayout);
@@ -107,6 +99,7 @@ IndicatorButtonPanel::IndicatorButtonPanel(QWidget *parent)
 
     connect(m_dragStartTimer, SIGNAL(timeout()),
             this, SLOT(timerExpired()));
+
     connect(m_forceReleaseTimer, SIGNAL(timeout()),
             this, SLOT(forceMouseRelease()));
 
@@ -117,6 +110,14 @@ IndicatorButtonPanel::IndicatorButtonPanel(QWidget *parent)
             this, SIGNAL(autoCenteringTriggered(bool)));
 }
 
+void IndicatorButtonPanel::forceMouseRelease()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    releaseMouse();
+    setDraggable(false);
+}
+
 void IndicatorButtonPanel::mouseMoveEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -147,9 +148,7 @@ void IndicatorButtonPanel::mousePressEvent(QMouseEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    if (event->button() == Qt::LeftButton)
-        m_dragPosition = event->pos();
-
+    m_dragPosition = event->pos();
     m_dragStartTimer->start();
 }
 
@@ -165,8 +164,6 @@ void IndicatorButtonPanel::mouseReleaseEvent(QMouseEvent *event)
 
         QSettings settings(DIRECTORY_NAME, FILE_NAME);
         settings.setValue(DIRECTION_INDICATOR_BUTTON_POSITION, pos());
-
-        releaseMouse();
     }
 }
 
@@ -180,19 +177,20 @@ void IndicatorButtonPanel::paintEvent(QPaintEvent *event)
     painter.setRenderHint(QPainter::Antialiasing);
 
     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)
+        const int EXTRA_SPACE_LEFT = -5;
+        const int EXTRA_SPACE_TOP = 0;
+        const int EXTRA_SPACE_RIGHT = +5;
+        const int EXTRA_SPACE_BOTTOM = 0;
 
-        const int RADIUS_WIDTH = 5;              ///< Roundness width
-        const int RADIUS_HEIGHT = 5;             ///< Roundness height
+        const int RADIUS_WIDTH = 5;
+        const int RADIUS_HEIGHT = 5;
 
         QPainterPath backgroundPath;
         QRect distanceLabelRect = m_distanceTextLabel->rect();
 
         distanceLabelRect.translate(m_distanceTextLabel->pos());
-        distanceLabelRect.adjust(ADJUST_LEFT, ADJUST_TOP, ADJUST_RIGHT, ADJUST_BOTTOM);
+        distanceLabelRect.adjust(EXTRA_SPACE_LEFT, EXTRA_SPACE_TOP, EXTRA_SPACE_RIGHT
+                                 , EXTRA_SPACE_BOTTOM);
         backgroundPath.addRoundedRect(distanceLabelRect, RADIUS_WIDTH, RADIUS_HEIGHT);
         painter.fillPath(backgroundPath, QBrush(*m_normalColor));
 
@@ -200,9 +198,10 @@ void IndicatorButtonPanel::paintEvent(QPaintEvent *event)
     }
 
     if(m_isDraggable) {
+        const int ROUNDING_RADIUS = 9;
+
         QPainterPath backgroundPath;
         backgroundPath.addRoundedRect(this->rect(), ROUNDING_RADIUS, ROUNDING_RADIUS);
-        painter.setRenderHint(QPainter::Antialiasing);
         painter.fillPath(backgroundPath, QBrush(Qt::Dense4Pattern));
         painter.setPen(Qt::black);
     }
@@ -213,9 +212,9 @@ void IndicatorButtonPanel::setDraggable(bool mode, QPoint eventPosition)
     qDebug() << __PRETTY_FUNCTION__;
 
     m_isDraggable = mode;
-    emit draggingModeTriggered();
 
     if(mode) {
+        emit draggingModeTriggered();
         grabMouse();
         m_forceReleaseTimer->start();
         m_dragPosition = eventPosition;
@@ -227,11 +226,6 @@ 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__;
@@ -270,12 +264,10 @@ void IndicatorButtonPanel::screenResized(const QSize &newSize)
     move(resizedPosition);
 }
 
-void IndicatorButtonPanel::forceMouseRelease()
+void IndicatorButtonPanel::setIndicatorButtonEnabled(bool enabled)
 {
-    qDebug() << __PRETTY_FUNCTION__;
-
-    releaseMouse();
-    setDraggable(false);
+        m_indicatorButton->setChecked(enabled);
+        m_distanceTextLabel->setVisible(!enabled);
 }
 
 void IndicatorButtonPanel::timerExpired()
@@ -289,41 +281,41 @@ void IndicatorButtonPanel::updateValues(qreal direction, qreal distance, bool dr
 {
     qDebug() << __PRETTY_FUNCTION__;
 
-    const int SMALL_REDUCE = 0.5;                   ///< 0.5 meter reduce
-    const int BIG_REDUCE = 100;                     ///< 100 meter reduce
+    const int MAX_TO_METERS = 1000 - 0.5;
+    const int MAX_FOR_KM_WITH_DESIMAL = 100000 - 50;
+    const int MIN_TO_METERS = 10.0;
     const qreal M_TO_KM = 1000.0;                   ///< Meters to kilometers conversion
 
+    QString UNIT_KILOMETER = " km";
+    QString UNIT_METER = " m";
+
     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();
+    if(m_distance < MIN_TO_METERS)
+    {
+        m_distanceText.setNum(10);
+        m_distanceText.prepend("< ");
+        m_distanceText.append(UNIT_METER);
+    }
+    else if(m_distance < MAX_TO_METERS)
+    {
+        m_distanceText.setNum(m_distance, 0 , 0);
+        m_distanceText.append(UNIT_METER);
     }
+    else if(m_distance < MAX_FOR_KM_WITH_DESIMAL)
+    {
+        m_distanceText.setNum(m_distance / M_TO_KM, 1, 1);
+        m_distanceText.append(UNIT_KILOMETER);
+    }
+    else {
+        m_distanceText.setNum(m_distance / M_TO_KM, 0, 0);
+        m_distanceText.append(UNIT_KILOMETER);
+    }
+
+    m_distanceTextLabel->setText(m_distanceText);
+
+    update();
+}