*/
#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;
}