2
// C++ Implementation: GotoDialog
7
// Author: Chris Browet <cbro@semperpax.com>, (C) 2008
9
// Copyright: See COPYING file that comes with this distribution
12
#include "GotoDialog.h"
14
#include <QMessageBox>
16
#include "Preferences/MerkaartorPreferences.h"
18
#include "NameFinder/namefinderwidget.h"
20
GotoDialog::GotoDialog(const Projection& aProj, QWidget *parent)
21
:QDialog(parent), theProjection(aProj)
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);
32
double dist = 6372.795;
34
QMap<QString, CoordBox> Bookmarks = M_PREFS->getBookmarks();
35
QMapIterator<QString, CoordBox> i(Bookmarks);
39
coordBookmark->addItem(i.key());
40
CoordBox C = i.value();
41
if ((d = C.center().distanceFrom(B.center())) < dist) {
47
coordBookmark->setCurrentIndex(selIdx);
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);
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))
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))
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))
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))
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))
88
void GotoDialog::on_searchButton_clicked()
90
QString searchString = NameFinderEdit->text();
91
searchWidget->search(searchString);
93
void GotoDialog::on_buttonBox_clicked(QAbstractButton * button)
95
if (buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) {
96
if (rbBookmark->isChecked()) {
97
theNewViewport = M_PREFS->getBookmarks()[coordBookmark->currentText()];
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!"));
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!"));
113
int zoom = url.queryItemValue("zoom").toInt();
115
if (zoom < 1 || zoom > 18) // use default when not in bounds
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));
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)));
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>'"));
134
theNewViewport = CoordBox(Coord(angToInt(tokens[1].toDouble()), angToInt(tokens[0].toDouble())), Coord(angToInt(tokens[3].toDouble()), angToInt(tokens[2].toDouble())));
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>'"));
143
theNewViewport = CoordBox(
145
angToInt(tokens[0].toDouble() - tokens[2].toDouble() / 2),
146
angToInt(tokens[1].toDouble() - tokens[3].toDouble() / 2)),
148
angToInt(tokens[0].toDouble() + tokens[2].toDouble() / 2),
149
angToInt(tokens[1].toDouble() + tokens[3].toDouble() / 2))
156
void GotoDialog::searchWidget_selectionChanged()
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
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))
168
rbOSM->setChecked(true);
172
void GotoDialog::on_NameFinderEdit_textChanged(const QString & text)
174
if (!text.isEmpty()) {
175
searchButton->setDefault(true);
177
searchButton->setDefault(false);
178
buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
182
void GotoDialog::searchWidget_doubleClicked()
184
buttonBox->button(QDialogButtonBox::Ok)->click();
187
void GotoDialog::changeEvent(QEvent * event)
189
if (event->type() == QEvent::LanguageChange)