Root/markerlayer.cpp

1/*
2 * Copyright 2010-2011 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 "markerlayer.h"
21
22#include "mapwidget.h"
23
24#include <QtCore/QSettings>
25
26MarkerLayer::MarkerLayer(MapWidget *map) :
27    AbstractLayer(map),
28    m_markerPos(),
29    m_markerName(),
30    m_filename()
31{
32}
33
34MarkerLayer::~MarkerLayer()
35{
36    QSettings set(m_filename, QSettings::NativeFormat);
37
38    set.beginGroup("marker");
39    set.remove("");
40    for (int i = 0; i < m_markerPos.count(); ++i) {
41        set.setValue(m_markerName.at(i), m_markerPos.at(i));
42    }
43    set.endGroup();
44}
45
46void MarkerLayer::load(const QString &filename)
47{
48    m_filename = filename;
49
50    QSettings set(filename, QSettings::NativeFormat);
51
52    set.beginGroup("marker");
53    QStringList m = set.childKeys();
54    foreach (const QString &marker, m) {
55        QPointF pos = set.value(marker).toPointF();
56        m_markerPos << pos;
57        m_markerName << marker;
58        emit markerAdded(marker);
59    }
60    set.endGroup();
61}
62
63void MarkerLayer::keyPressed(QKeyEvent *event)
64{
65    if (event->key() == Qt::Key_M) {
66        if (event->modifiers() == Qt::NoModifier) {
67            int n = 0;
68            if (!m_markerName.isEmpty()) {
69                n = m_markerName.last().toInt();
70            }
71            QString newName = QString::number(n+1);
72
73            m_markerPos << map()->geoPos();
74            m_markerName << newName;
75            emit markerAdded(newName);
76        } else if (event->modifiers() == Qt::AltModifier) {
77            toggleVisibility();
78        }
79    }
80}
81
82void MarkerLayer::paint(QPainter *painter)
83{
84    int i = 0;
85    painter->setBrush(QBrush(QColor(255, 237, 60)));
86    QFontMetrics fm(painter->font());
87    int h = fm.height() / 2;
88    foreach (const QPointF &m, m_markerPos) {
89        QPoint pos = map()->geo2screen(m.x(), m.y());
90        int w = fm.width(m_markerName.at(i)) / 2;
91        QRect rect(pos.x() - w - 2, pos.y() - h - 11, 2*w + 4, 2*h);
92        QPolygon polygon;
93        polygon << pos;
94        polygon << pos + QPoint(-2, -5);
95        polygon << rect.bottomLeft();
96        polygon << rect.topLeft();
97        polygon << rect.topRight();
98        polygon << rect.bottomRight();
99        polygon << pos + QPoint(2, -5);
100        polygon << pos;
101        painter->drawPolygon(polygon);
102        painter->drawText(rect, Qt::AlignCenter, m_markerName.at(i));
103        ++i;
104    }
105}
106
107void MarkerLayer::removeMarker(int index)
108{
109    if (index >= 0 && m_markerPos.count() > index) {
110        m_markerPos.removeAt(index);
111        m_markerName.removeAt(index);
112        map()->update();
113    }
114}
115
116void MarkerLayer::renameMarker(int index, const QString &name)
117{
118    if (index >= 0 && m_markerName.count() > index) {
119        m_markerName.replace(index, name);
120    }
121}
122
123void MarkerLayer::centerOnMarker(int index)
124{
125    if (index >= 0 && m_markerPos.count() > index) {
126        qreal lon = m_markerPos.at(index).x();
127        qreal lat = m_markerPos.at(index).y();
128
129        map()->centerOnGeoPos(lon, lat);
130    }
131}
132
133

Archive Download this file

Branches:
master



interactive