~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to GotoDialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// C++ Implementation: GotoDialog
3
 
//
4
 
// Description:
5
 
//
6
 
//
7
 
// Author: Chris Browet <cbro@semperpax.com>, (C) 2008
8
 
//
9
 
// Copyright: See COPYING file that comes with this distribution
10
 
//
11
 
//
12
 
#include "GotoDialog.h"
13
 
 
14
 
#include <QMessageBox>
15
 
 
16
 
#include "Preferences/MerkaartorPreferences.h"
17
 
 
18
 
#include "NameFinder/namefinderwidget.h"
19
 
 
20
 
GotoDialog::GotoDialog(const Projection& aProj, QWidget *parent)
21
 
        :QDialog(parent), theProjection(aProj)
22
 
{
23
 
        setupUi(this);
24
 
 
25
 
        CoordBox B = theProjection.viewport();
26
 
        int OsmZoom = int((log((360.0 / intToAng(B.latDiff()))) / log(2.0)) + 1);
27
 
        OsmZoom = qMin(OsmZoom, 18);
28
 
        OsmZoom = qMax(OsmZoom, 1);
29
 
 
30
 
        int idx = 0;
31
 
        int selIdx = -1;
32
 
        double dist = 6372.795;
33
 
        double d;
34
 
        QMap<QString, CoordBox> Bookmarks = M_PREFS->getBookmarks();
35
 
        QMapIterator<QString, CoordBox> i(Bookmarks);
36
 
        while (i.hasNext()) {
37
 
                i.next();
38
 
 
39
 
                coordBookmark->addItem(i.key());
40
 
                CoordBox C = i.value();
41
 
                if ((d = C.center().distanceFrom(B.center())) < dist) {
42
 
                        dist = d;
43
 
                        selIdx = idx;
44
 
                }
45
 
                ++idx;
46
 
        }
47
 
        coordBookmark->setCurrentIndex(selIdx);
48
 
        
49
 
        searchWidget = new NameFinder::NameFinderWidget(this);
50
 
        connect(searchWidget, SIGNAL(selectionChanged()), this, SLOT(searchWidget_selectionChanged()));
51
 
        connect(searchWidget, SIGNAL(doubleClicked()), this, SLOT(searchWidget_doubleClicked()));
52
 
        verticalLayout_4->addWidget(searchWidget);
53
 
 
54
 
        coordOSM->setText( QString("http://www.openstreetmap.org/?lat=%1&lon=%2&zoom=%3")
55
 
                .arg(QString::number(intToAng(B.center().lat()), 'f', 4))
56
 
                .arg(QString::number(intToAng(B.center().lon()), 'f', 4))
57
 
                .arg(QString::number(OsmZoom))
58
 
                );
59
 
        coordOsmApi->setText( QString("http://www.openstreetmap.org/api/%1/map?bbox=%2,%3,%4,%5")
60
 
                .arg(M_PREFS->apiVersion())
61
 
                .arg(QString::number(intToAng(B.bottomLeft().lon()), 'f', 4))
62
 
                .arg(QString::number(intToAng(B.bottomLeft().lat()), 'f', 4))
63
 
                .arg(QString::number(intToAng(B.topRight().lon()), 'f', 4))
64
 
                .arg(QString::number(intToAng(B.topRight().lat()), 'f', 4))
65
 
                );
66
 
        coordOsmXApi->setText( QString("http://xapi.openstreetmap.org/api/0.5/*[bbox=%1,%2,%3,%4]")
67
 
                .arg(QString::number(intToAng(B.bottomLeft().lon()), 'f', 4))
68
 
                .arg(QString::number(intToAng(B.bottomLeft().lat()), 'f', 4))
69
 
                .arg(QString::number(intToAng(B.topRight().lon()), 'f', 4))
70
 
                .arg(QString::number(intToAng(B.topRight().lat()), 'f', 4))
71
 
                );
72
 
        coordCoord->setText( QString("%1, %2, %3, %4")
73
 
                .arg(QString::number(intToAng(B.bottomLeft().lon()), 'f', 4))
74
 
                .arg(QString::number(intToAng(B.bottomLeft().lat()), 'f', 4))
75
 
                .arg(QString::number(intToAng(B.topRight().lon()), 'f', 4))
76
 
                .arg(QString::number(intToAng(B.topRight().lat()), 'f', 4))
77
 
                );
78
 
        coordSpan->setText( QString("%1, %2, %3, %4")
79
 
                .arg(QString::number(intToAng(B.center().lat()), 'f', 4))
80
 
                .arg(QString::number(intToAng(B.center().lon()), 'f', 4))
81
 
                .arg(QString::number(intToAng(B.latDiff()), 'f', 4))
82
 
                .arg(QString::number(intToAng(B.lonDiff()), 'f', 4))
83
 
                );
84
 
 
85
 
        resize(1,1);
86
 
}
87
 
 
88
 
void GotoDialog::on_searchButton_clicked()
89
 
{
90
 
                QString searchString = NameFinderEdit->text();
91
 
                searchWidget->search(searchString);
92
 
}
93
 
void GotoDialog::on_buttonBox_clicked(QAbstractButton * button)
94
 
{
95
 
        if (buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) {
96
 
                if (rbBookmark->isChecked()) {
97
 
                        theNewViewport = M_PREFS->getBookmarks()[coordBookmark->currentText()];
98
 
                } else
99
 
                if (rbOSM->isChecked()) {
100
 
                        QUrl url = QUrl(coordOSM->text()); 
101
 
                        if (!url.isValid()) {
102
 
                                QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid OSM url"),
103
 
                                        QApplication::translate("GotoDialog", "The specified url is invalid!"));
104
 
                                return;
105
 
                        }
106
 
                        double lat = url.queryItemValue("lat").toDouble(); 
107
 
                        double lon = url.queryItemValue("lon").toDouble(); 
108
 
                        if (lat == 0.0 || lon == 0.0) {
109
 
                                QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid OSM url"),
110
 
                                        QApplication::translate("GotoDialog", "The specified url is invalid!"));
111
 
                                return;
112
 
                        }
113
 
                        int zoom = url.queryItemValue("zoom").toInt();
114
 
 
115
 
                        if (zoom < 1 || zoom > 18) // use default when not in bounds
116
 
                                zoom = 15;
117
 
 
118
 
                        /* term to calculate the angle from the zoom-value */
119
 
                        double zoomLat = 360.0 / (double)(1 << zoom);
120
 
                        double zoomLon = zoomLat / fabs(cos(angToRad(lat)));
121
 
                        /* the following line is equal to the line above. (just for explanation) */
122
 
                        //double zoomLon = zoomLat / aParent->view()->projection().latAnglePerM() * aParent->view()->projection().lonAnglePerM(angToRad(lat));
123
 
 
124
 
                        /* the OSM link contains the coordinates from the middle of the visible map so we have to add and sub zoomLon/zoomLat */
125
 
                        theNewViewport = CoordBox(Coord(angToInt(lat-zoomLat), angToInt(lon-zoomLon)), Coord(angToInt(lat+zoomLat), angToInt(lon+zoomLon)));
126
 
                } else
127
 
                if (rbCoord->isChecked()) {
128
 
                        QStringList tokens = coordCoord->text().split(",");
129
 
                        if (tokens.size() < 4) {
130
 
                                QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid Coordinates format"),
131
 
                                        QApplication::translate("GotoDialog", "Coordinates must be: '<left lon>, <bottom lat>, <right lon>, <top lat>'"));
132
 
                                return;
133
 
                        }
134
 
                        theNewViewport = CoordBox(Coord(angToInt(tokens[1].toDouble()), angToInt(tokens[0].toDouble())), Coord(angToInt(tokens[3].toDouble()), angToInt(tokens[2].toDouble())));
135
 
                } else
136
 
                if (rbSpan->isChecked()) {
137
 
                        QStringList tokens = coordSpan->text().split(",");
138
 
                        if (tokens.size() < 4) {
139
 
                                QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid Coordinates format"),
140
 
                                        QApplication::translate("GotoDialog", "Coordinates must be: '<center lat>, <center lon>, <span lat>, <span lon>'"));
141
 
                                return;
142
 
                        }
143
 
                        theNewViewport = CoordBox(
144
 
                                                                Coord(
145
 
                                                                        angToInt(tokens[0].toDouble() - tokens[2].toDouble() / 2),
146
 
                                                                        angToInt(tokens[1].toDouble() - tokens[3].toDouble() / 2)),
147
 
                                                                Coord(
148
 
                                                                        angToInt(tokens[0].toDouble() + tokens[2].toDouble() / 2),
149
 
                                                                        angToInt(tokens[1].toDouble() + tokens[3].toDouble() / 2))
150
 
                                                                );
151
 
                }
152
 
                accept();
153
 
        }
154
 
}
155
 
 
156
 
void GotoDialog::searchWidget_selectionChanged()
157
 
{
158
 
        QPointF centerPoint = searchWidget->selectedCoords();
159
 
        int zoom = searchWidget->selectedZoom();
160
 
        // The API doesn't like request for too large bounding boxes so reset to a default
161
 
        if (zoom < 15)
162
 
                zoom = 15;
163
 
        coordOSM->setText( QString("http://www.openstreetmap.org/?lat=%1&lon=%2&zoom=%3")
164
 
                .arg(QString::number(centerPoint.x(), 'f', 4))
165
 
                .arg(QString::number(centerPoint.y(), 'f', 4))
166
 
                .arg(QString::number(zoom))
167
 
                );
168
 
        rbOSM->setChecked(true);
169
 
        
170
 
}
171
 
 
172
 
void GotoDialog::on_NameFinderEdit_textChanged(const QString & text)
173
 
{
174
 
    if (!text.isEmpty()) {
175
 
                searchButton->setDefault(true);
176
 
    } else {
177
 
                searchButton->setDefault(false);
178
 
                buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
179
 
    }
180
 
}
181
 
 
182
 
void GotoDialog::searchWidget_doubleClicked()
183
 
{
184
 
    buttonBox->button(QDialogButtonBox::Ok)->click();
185
 
}
186
 
 
187
 
void GotoDialog::changeEvent(QEvent * event)
188
 
{
189
 
        if (event->type() == QEvent::LanguageChange)
190
 
                retranslateUi(this);
191
 
}
192