Root/monavlayer.cpp

1/*
2 * Copyright 2010 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20#include "monavlayer.h"
21
22#include "mapwidget.h"
23#include "projection.h"
24
25#include <QtCore/QDebug>
26#include <QtCore/QDir>
27#include <QtCore/QFile>
28#include <QtCore/QPluginLoader>
29#include <QtCore/QSettings>
30
31MonavLayer::MonavLayer(MapWidget *map) :
32    AbstractLayer(map),
33    m_gpsLookup(0),
34    m_router(0),
35    m_loaded(true),
36    m_routeStart(),
37    m_routeEnd(),
38    m_track(),
39    m_trackOnScreen(),
40    m_trackOffset(),
41    m_zoomLevel(0),
42    m_currentDirection(0),
43    m_names(),
44    m_types()
45{
46    QSettings set(QDir::homePath()+"/Maps/nanomap.conf", QSettings::NativeFormat);
47    set.beginGroup("monav");
48    QString dataDir = set.value("datadir").toString();
49    QString routerLib = set.value("router",
50            "/usr/lib/monav/libcontractionhierarchiesclient.so").toString();
51    QString gpsLookupLib = set.value("gpslookup",
52            "/usr/lib/monav/libgpsgridclient.so").toString();
53    set.endGroup();
54
55    QSettings pluginSettings(dataDir+"/plugins.ini", QSettings::IniFormat);
56    QString routerName = pluginSettings.value("router").toString();
57    QString gpsLookupName = pluginSettings.value("gpsLookup").toString();
58
59    QPluginLoader rLoader(routerLib);
60    QObject *plugin = rLoader.instance();
61    if (plugin) {
62        m_router = qobject_cast<IRouter*>(plugin);
63        if (m_router) {
64            m_router->SetInputDirectory(dataDir);
65            m_loaded = m_loaded && m_router->LoadData();
66        } else {
67            m_loaded = false;
68        }
69    } else {
70        m_loaded = false;
71    }
72    QPluginLoader gLoader(gpsLookupLib);
73    plugin = gLoader.instance();
74    if (plugin) {
75        m_gpsLookup = qobject_cast<IGPSLookup*>(plugin);
76        if (m_gpsLookup) {
77            m_gpsLookup->SetInputDirectory(dataDir);
78            m_loaded = m_loaded && m_gpsLookup->LoadData();
79        } else {
80            m_loaded = false;
81        }
82    } else {
83        m_loaded = false;
84    }
85}
86
87void MonavLayer::zoom(int level)
88{
89    m_zoomLevel = level;
90    if (m_track.count() > 1) {
91        int scale = 1 << level;
92        m_trackOnScreen.clear();
93        m_trackOffset = map()->raw2screen(m_track.first().x(), m_track.first().y(), scale);
94        m_trackOnScreen << QPoint(0, 0);
95        for (int i = 1; i < m_track.count(); ++i) {
96            QPointF p = m_track.at(i);
97            m_trackOnScreen << map()->raw2screen(p.x(), p.y(), scale) - m_trackOffset;
98        }
99    }
100}
101
102void MonavLayer::pan(const QPoint &move)
103{
104    m_trackOffset += move;
105}
106
107void MonavLayer::paint(QPainter *painter)
108{
109    if (!m_loaded) {
110        return;
111    }
112    if (m_trackOnScreen.count() > 1) {
113        QPoint p1, p2 = m_trackOnScreen.first();
114        for (int i = 1; i < m_trackOnScreen.count(); ++i) {
115            p1 = m_trackOnScreen.at(i);
116            painter->drawLine(p1 + m_trackOffset, p2 + m_trackOffset);
117            p2 = p1;
118        }
119    }
120
121    QPoint p;
122    QPolygon tri;
123
124    if (!m_routeStart.isNull()) {
125        p = map()->geo2screen(m_routeStart.x(), m_routeStart.y());
126        tri << p << p+QPoint(-5, -9) << p+QPoint(5, -9) << p;
127        painter->setBrush(Qt::red);
128        painter->drawPolygon(tri);
129    }
130
131    if (!m_routeEnd.isNull()) {
132        p = map()->geo2screen(m_routeEnd.x(), m_routeEnd.y());
133        tri.clear();
134        tri << p << p+QPoint(-5, -9) << p+QPoint(5, -9) << p;
135        painter->setBrush(Qt::blue);
136        painter->drawPolygon(tri);
137    }
138    //if (m_currentDirection < m_names.count()) {
139    // painter->setBrush(QBrush(QColor(255, 255, 255, 210)));
140    // painter->drawRoundedRect(25, 1, 200, 16, 5, 5);
141    // painter->drawText(30, 3, 190, 14, Qt::AlignCenter,
142    // m_names.at(m_currentDirection));
143    //}
144}
145
146void MonavLayer::keyPressed(QKeyEvent *event)
147{
148    switch (event->key()) {
149        case Qt::Key_R:
150        {
151            if (event->modifiers() == Qt::NoModifier) {
152                findRoute();
153            }
154            break;
155        }
156        case Qt::Key_S:
157        {
158            if (event->modifiers() == Qt::NoModifier) {
159                QPointF p = map()->geoPos();
160                UnsignedCoordinate coord(GPSCoordinate(p.y(), p.x()));
161                IGPSLookup::Result pos;
162                if (m_loaded && m_gpsLookup->GetNearestEdge(&pos, coord, 1000.0)) {
163                    m_routeStart = p;
164                }
165            }
166            break;
167        }
168        case Qt::Key_E:
169        {
170            if (event->modifiers() == Qt::NoModifier) {
171                QPointF p = map()->geoPos();
172                UnsignedCoordinate coord(GPSCoordinate(p.y(), p.x()));
173                IGPSLookup::Result pos;
174                if (m_loaded && m_gpsLookup->GetNearestEdge(&pos, coord, 1000.0)) {
175                    m_routeEnd = p;
176                }
177            }
178            break;
179        }
180        case Qt::Key_N:
181        {
182            if (m_currentDirection < m_names.count()-1) {
183                ++m_currentDirection;
184            }
185            break;
186        }
187        case Qt::Key_P:
188        {
189            if (m_currentDirection > 0) {
190                --m_currentDirection;
191            }
192            break;
193        }
194    }
195}
196
197void MonavLayer::findRoute()
198{
199    if (!m_loaded) {
200        return;
201    }
202
203    QVector<IRouter::Node> nodes;
204    QVector<IRouter::Edge> edges;
205    double lookupRadius = 1000.0;
206    double dist;
207
208    UnsignedCoordinate startCoord(GPSCoordinate(m_routeStart.y(), m_routeStart.x()));
209    IGPSLookup::Result startPos;
210    if (!m_gpsLookup->GetNearestEdge(&startPos, startCoord, lookupRadius)) {
211        qDebug() << "source not found";
212        return;
213    }
214    UnsignedCoordinate endCoord(GPSCoordinate(m_routeEnd.y(), m_routeEnd.x()));
215    IGPSLookup::Result endPos;
216    if (!m_gpsLookup->GetNearestEdge(&endPos, endCoord, lookupRadius)) {
217        qDebug() << "target not found";
218        return;
219    }
220    if (m_router->GetRoute(&dist, &nodes, &edges, startPos, endPos)) {
221        qDebug() << "route found";
222        m_track.clear();
223        for (int i = 0; i < nodes.size(); ++i) {
224            GPSCoordinate c = nodes[i].coordinate.ToGPSCoordinate();
225            m_track << QPointF(Projection::lon2rawx(c.longitude), Projection::lat2rawy(c.latitude));
226        }
227        for (int i = 0; i < edges.size(); ++i) {
228            QString name, type;
229            m_router->GetName(&name, edges[i].name);
230            m_router->GetType(&type, edges[i].type);
231            if (m_names.isEmpty() || (!m_names.isEmpty() && name != m_names.last())) {
232                m_names << name;
233                m_types << type;
234            }
235            //qDebug() << name << type << edges[i].seconds;
236        }
237        m_currentDirection = 0;
238        zoom(m_zoomLevel);
239    }
240}
241
242

Archive Download this file

Branches:
master



interactive