First version of the scaling map scale
[situare] / src / ui / mapscale.cpp
1  /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Kaj Wallin - kaj.wallin@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20  */
21
22 #include <QPainter>
23 #include <QPen>
24 #include <QLine>
25 #include <QDebug>
26 #include "mapscale.h"
27 #include <math.h>
28
29 const int METRIC_STEP = 90;                 ///< TEST value in pixels
30 const qreal KM_TO_MILE = 1.609344;          ///< Kilometer to mile conversion value
31 const int MAPSCALE_HEIGHT = 14;             ///< Height of the the map scale
32 const int BASELINE_Y = MAPSCALE_HEIGHT / 2; ///< Y position of the baseline
33 const int MAPSCALE_STOP_HEIGHT = 7;         ///< Height of each perpendicular stop on the scale
34
35 const qreal INPUTVALUE = 450.35;          ///< SIMULATED input value: meters/pixel
36
37 MapScale::MapScale(QWidget *parent) :
38     QWidget(parent)
39 {
40     qDebug() << __PRETTY_FUNCTION__;
41 //    setAutoFillBackground(true);
42     setAttribute(Qt::WA_TransparentForMouseEvents, true);
43 }
44
45 void MapScale::paintEvent(QPaintEvent *event)
46 {
47     qDebug() << __PRETTY_FUNCTION__;
48
49     Q_UNUSED(event);
50     qreal gen_scale = METRIC_STEP * INPUTVALUE;
51     qreal baseScale;
52
53
54     baseScale = roundToBaseScale(gen_scale);
55     qreal baseLineLength = baseScale / INPUTVALUE;
56
57     qWarning() << gen_scale << ">" << baseScale << ">" << baseLineLength;
58
59     QLineF BaseLine(0, BASELINE_Y, baseLineLength * KM_TO_MILE, BASELINE_Y);
60     QLineF StartKm(1, BASELINE_Y, 1, (BASELINE_Y) - MAPSCALE_STOP_HEIGHT);
61     QLineF StartMi(1, BASELINE_Y, 1, (BASELINE_Y) + MAPSCALE_STOP_HEIGHT);
62     QLineF StopKm(baseLineLength, BASELINE_Y,
63                   baseLineLength, (BASELINE_Y) - MAPSCALE_STOP_HEIGHT);
64     QLineF StopMi(baseLineLength * KM_TO_MILE, BASELINE_Y,
65                   baseLineLength * KM_TO_MILE, (BASELINE_Y) + MAPSCALE_STOP_HEIGHT);
66
67     resize(ceil(baseLineLength * KM_TO_MILE)+1, MAPSCALE_HEIGHT);
68
69     QPainter painter(this);
70     QPen pen(Qt::black, 2);
71
72     painter.setPen(pen);
73     painter.drawLine(BaseLine);
74     painter.drawLine(StartKm);
75     painter.drawLine(StartMi);
76     painter.drawLine(StopKm);
77     painter.drawLine(StopMi);
78 }
79
80 qreal MapScale::roundToBaseScale(qreal value)
81 {
82     qDebug() << __PRETTY_FUNCTION__;
83
84     int scale = 0;
85     qreal baseLine;
86     while(value > 1){
87         value = value/10;
88         scale++;
89     }
90     if(value < 0.15)
91         baseLine = 1;
92     else if (value < 0.375)
93         baseLine = 2;
94     else if (value < 0.75)
95         baseLine = 5;
96     else
97         baseLine = 10;
98     baseLine = baseLine * (pow(10,scale-1));
99     qWarning() << baseLine;
100     return baseLine;
101 }