First version of the scaling map scale
authorKaj Wallin <kaj.wallin@ixonos.com>
Mon, 7 Jun 2010 13:01:51 +0000 (16:01 +0300)
committerKaj Wallin <kaj.wallin@ixonos.com>
Mon, 7 Jun 2010 13:01:51 +0000 (16:01 +0300)
Reviewed by:

src/ui/mainwindow.cpp
src/ui/mapscale.cpp
src/ui/mapscale.h

index c2e4fcb..ec3cd6b 100644 (file)
@@ -85,7 +85,7 @@ MainWindow::MainWindow(QWidget *parent)
     setWindowTitle(tr("Situare"));
 
     MapScale *mapScale = new MapScale(this);
-    mapScale->move(250,250);
+    mapScale->move(50, 400);
 
     // set stacking order of widgets
     m_zoomButtonPanel->stackUnder(m_userPanel);
index c6d3110..40c8ead 100644 (file)
  */
 
 #include <QPainter>
+#include <QPen>
 #include <QLine>
+#include <QDebug>
 #include "mapscale.h"
+#include <math.h>
+
+const int METRIC_STEP = 90;                 ///< TEST value in pixels
+const qreal KM_TO_MILE = 1.609344;          ///< Kilometer to mile conversion value
+const int MAPSCALE_HEIGHT = 14;             ///< Height of the the map scale
+const int BASELINE_Y = MAPSCALE_HEIGHT / 2; ///< Y position of the baseline
+const int MAPSCALE_STOP_HEIGHT = 7;         ///< Height of each perpendicular stop on the scale
+
+const qreal INPUTVALUE = 450.35;          ///< SIMULATED input value: meters/pixel
 
 MapScale::MapScale(QWidget *parent) :
     QWidget(parent)
 {
     qDebug() << __PRETTY_FUNCTION__;
+//    setAutoFillBackground(true);
+    setAttribute(Qt::WA_TransparentForMouseEvents, true);
 }
 
-MapScale::paintEvent(QPaintEvent *event)
+void MapScale::paintEvent(QPaintEvent *event)
 {
     qDebug() << __PRETTY_FUNCTION__;
 
     Q_UNUSED(event);
+    qreal gen_scale = METRIC_STEP * INPUTVALUE;
+    qreal baseScale;
+
+
+    baseScale = roundToBaseScale(gen_scale);
+    qreal baseLineLength = baseScale / INPUTVALUE;
+
+    qWarning() << gen_scale << ">" << baseScale << ">" << baseLineLength;
+
+    QLineF BaseLine(0, BASELINE_Y, baseLineLength * KM_TO_MILE, BASELINE_Y);
+    QLineF StartKm(1, BASELINE_Y, 1, (BASELINE_Y) - MAPSCALE_STOP_HEIGHT);
+    QLineF StartMi(1, BASELINE_Y, 1, (BASELINE_Y) + MAPSCALE_STOP_HEIGHT);
+    QLineF StopKm(baseLineLength, BASELINE_Y,
+                  baseLineLength, (BASELINE_Y) - MAPSCALE_STOP_HEIGHT);
+    QLineF StopMi(baseLineLength * KM_TO_MILE, BASELINE_Y,
+                  baseLineLength * KM_TO_MILE, (BASELINE_Y) + MAPSCALE_STOP_HEIGHT);
+
+    resize(ceil(baseLineLength * KM_TO_MILE)+1, MAPSCALE_HEIGHT);
 
-    QLineF mapScaleBaseLine(2.0, 5.0, 20.0, 5.0);
     QPainter painter(this);
-    painter.drawLine(mapScaleBaseLine);
+    QPen pen(Qt::black, 2);
+
+    painter.setPen(pen);
+    painter.drawLine(BaseLine);
+    painter.drawLine(StartKm);
+    painter.drawLine(StartMi);
+    painter.drawLine(StopKm);
+    painter.drawLine(StopMi);
+}
+
+qreal MapScale::roundToBaseScale(qreal value)
+{
+    qDebug() << __PRETTY_FUNCTION__;
 
+    int scale = 0;
+    qreal baseLine;
+    while(value > 1){
+        value = value/10;
+        scale++;
+    }
+    if(value < 0.15)
+        baseLine = 1;
+    else if (value < 0.375)
+        baseLine = 2;
+    else if (value < 0.75)
+        baseLine = 5;
+    else
+        baseLine = 10;
+    baseLine = baseLine * (pow(10,scale-1));
+    qWarning() << baseLine;
+    return baseLine;
 }
index 2642350..46aff56 100644 (file)
@@ -46,6 +46,8 @@ public:
 /******************************************************************************
  * MEMBER FUNCTIONS AND SLOTS
  ******************************************************************************/
+private:
+    qreal roundToBaseScale(qreal value);
 public slots:
 
 /*******************************************************************************