2 Situare - A location system for Facebook
3 Copyright (C) 2010 Ixonos Plc. Authors:
5 Kaj Wallin - kaj.wallin@ixonos.com
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.
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.
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,
29 const int TARGET_WIDTH = 140; ///< Target width of the scale in pixels
30 const qreal M_TO_FT = 3.2808399; ///< Meter to feet conversion value
31 const qreal FT_TO_MI = 5280; ///< Feet to mile conversion
32 const qreal M_TO_KM = 1000; ///< Meters to kilometers conversion
33 const int MAPSCALE_HEIGHT = 31; ///< Height of the the map scale
34 const int CENTERLINE_Y = MAPSCALE_HEIGHT / 2; ///< Y position of the centerline
35 const int MAPSCALE_STOP_HEIGHT = 7; ///< Height of each perpendicular stop on the scale
36 const int SCALE_TEXT_X = 3; ///< X coordinate for the scale texts
38 MapScale::MapScale(QWidget *parent) :
40 m_centerLineImperial(0),
41 m_centerLineMetric(0),
45 qDebug() << __PRETTY_FUNCTION__;
47 // updateMapScale(10.0);
48 setAttribute(Qt::WA_TransparentForMouseEvents, true);
51 void MapScale::paintEvent(QPaintEvent *event)
53 qDebug() << __PRETTY_FUNCTION__;
57 resize(ceil(fmax(m_centerLineMetric,m_centerLineImperial)) + 1, MAPSCALE_HEIGHT);
59 QLineF centerLine(0, CENTERLINE_Y, fmax(m_centerLineMetric, m_centerLineImperial), CENTERLINE_Y);
60 QLineF startKm(1, CENTERLINE_Y, 1, CENTERLINE_Y - MAPSCALE_STOP_HEIGHT);
61 QLineF stopKm(m_centerLineMetric, CENTERLINE_Y,
62 m_centerLineMetric, CENTERLINE_Y - MAPSCALE_STOP_HEIGHT);
64 QLineF startMi(1, CENTERLINE_Y, 1, CENTERLINE_Y + MAPSCALE_STOP_HEIGHT);
65 QLineF stopMi(m_centerLineImperial, CENTERLINE_Y,
66 m_centerLineImperial, CENTERLINE_Y + MAPSCALE_STOP_HEIGHT);
68 QPainter painter(this);
69 QPen pen(Qt::black, 2);
71 painter.setFont(QFont("Nokia Sans", 13, QFont::Normal));
73 painter.drawLine(centerLine);
74 painter.drawLine(startKm);
75 painter.drawLine(stopKm);
76 painter.drawText(SCALE_TEXT_X, MAPSCALE_HEIGHT / 2 - 2, m_metricText);
78 painter.drawLine(startMi);
79 painter.drawLine(stopMi);
80 painter.drawText(SCALE_TEXT_X, MAPSCALE_HEIGHT, m_imperialText);
83 qreal MapScale::roundToBaseScale(qreal value)
85 qDebug() << __PRETTY_FUNCTION__;
95 else if (value < 0.35)
97 else if (value < 0.75)
101 baseLine = baseLine * (pow(10,scale-1));
105 void MapScale::updateMapResolution(const qreal &resolution)
107 qDebug() << __PRETTY_FUNCTION__;
109 //Calculate distance scale in metric units
110 qreal genericMetricScale = TARGET_WIDTH * resolution;
111 qreal baseMetricScale = roundToBaseScale(genericMetricScale);
113 m_centerLineMetric = baseMetricScale / resolution;
115 m_centerLineMetric = baseMetricScale / 0.000001;
117 if(baseMetricScale < M_TO_KM)
119 m_metricText.setNum(baseMetricScale);
120 m_metricText.append(" m");
122 m_metricText.setNum(baseMetricScale/M_TO_KM);
123 m_metricText.append(" km");
126 //Calculate distance scale in imperial units
127 qreal imperialScaleResolution = resolution * M_TO_FT;
128 qreal genericImperialScale = TARGET_WIDTH * imperialScaleResolution;
129 qreal baseImperialScale;
131 if(genericImperialScale < FT_TO_MI) {
132 baseImperialScale = roundToBaseScale(genericImperialScale);
133 if(imperialScaleResolution != 0)
134 m_centerLineImperial = baseImperialScale / imperialScaleResolution;
136 m_centerLineImperial = baseImperialScale / 0.000001;
137 m_imperialText.setNum(baseImperialScale);
138 m_imperialText.append(" ft");
140 baseImperialScale = roundToBaseScale(genericImperialScale / FT_TO_MI);
141 if(imperialScaleResolution != 0)
142 m_centerLineImperial = (baseImperialScale*FT_TO_MI) / imperialScaleResolution;
144 m_centerLineImperial = baseImperialScale / 0.000001;
145 m_imperialText.setNum(baseImperialScale);
146 m_imperialText.append(" mi");