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 "MerkaartorPreferences.h"
17 #include "namefinderwidget.h"
18 #include "OsmLink.h"
19 
GotoDialog(MapView * aView,QWidget * parent)20 GotoDialog::GotoDialog(MapView* aView, QWidget *parent)
21     :QDialog(parent)
22 {
23     setupUi(this);
24 
25     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
26     setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
27 
28     theViewport = aView->viewport();
29     theProjection = &aView->projection();
30     theRect = aView->rect();
31 
32     theCenter = theViewport.center();
33     int OsmZoom = int((log((360.0 / theViewport.latDiff())) / log(2.0)) + 1);
34     OsmZoom = qMin(OsmZoom, 18);
35     OsmZoom = qMax(OsmZoom, 1);
36 
37     int idx = 0;
38     int selIdx = -1;
39     qreal dist = 6372.795;
40     qreal d;
41     BookmarkList* Bookmarks = M_PREFS->getBookmarks();
42     BookmarkListIterator i(*Bookmarks);
43     while (i.hasNext()) {
44         i.next();
45 
46         if (i.value().deleted == false) {
47             coordBookmark->addItem(i.key());
48             CoordBox C = i.value().Coordinates;
49             if ((d = C.center().distanceFrom(theViewport.center())) < dist) {
50                 dist = d;
51                 selIdx = idx;
52             }
53         }
54         ++idx;
55     }
56     coordBookmark->setCurrentIndex(selIdx);
57 
58     connect(cbCoordShowProjected, SIGNAL(toggled(bool)), SLOT(fillCoordinates()));
59 
60     searchWidget = new NameFinder::NameFinderWidget(this);
61     connect(searchWidget, SIGNAL(selectionChanged()), this, SLOT(searchWidget_selectionChanged()));
62     connect(searchWidget, SIGNAL(doubleClicked()), this, SLOT(searchWidget_doubleClicked()));
63     connect(searchWidget, SIGNAL(done()), this, SLOT(searchWidget_done()));
64     verticalLayout_4->addWidget(searchWidget);
65 
66     coordLink->setText( QString("https://www.openstreetmap.org/#map=%3/%1/%2")
67         .arg(COORD2STRING(theViewport.center().y()))
68         .arg(COORD2STRING(theViewport.center().x()))
69         .arg(QString::number(OsmZoom))
70         );
71     coordOsmApi->setText( QString("%1/map?bbox=%2,%3,%4,%5")
72         .arg(M_PREFS->getOsmApiUrl())
73         .arg(COORD2STRING(theViewport.bottomLeft().x()))
74         .arg(COORD2STRING(theViewport.bottomLeft().y()))
75         .arg(COORD2STRING(theViewport.topRight().x()))
76         .arg(COORD2STRING(theViewport.topRight().y()))
77         );
78     coordOsmXApi->setText( QString("%1*[bbox=%2,%3,%4,%5]")
79         .arg(M_PREFS->getXapiUrl().endsWith("/") ? M_PREFS->getXapiUrl() : M_PREFS->getXapiUrl() + "/")
80         .arg(COORD2STRING(theViewport.bottomLeft().x()))
81         .arg(COORD2STRING(theViewport.bottomLeft().y()))
82         .arg(COORD2STRING(theViewport.topRight().x()))
83         .arg(COORD2STRING(theViewport.topRight().y()))
84         );
85     fillCoordinates();
86 }
87 
on_buttonBox_clicked(QAbstractButton * button)88 void GotoDialog::on_buttonBox_clicked(QAbstractButton * button)
89 {
90     if (buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) {
91         if (rbBookmark->isChecked()) {
92             theNewViewport = M_PREFS->getBookmarks()->value(coordBookmark->currentText()).Coordinates;
93         } else
94         if (rbLink->isChecked()) {
95             OsmLink ol(coordLink->text());
96             if (!ol.isValid()) {
97                 QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid OSM url"),
98                     QApplication::translate("GotoDialog", "The specified url is invalid!"));
99                 return;
100             }
101             theNewViewport = ol.getCoordBox();
102         } else
103         if (rbCoord->isChecked()) {
104             QStringList tokens = coordCoord->text().split(",");
105             if (tokens.size() < 4) {
106                 QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid Coordinates format"),
107                     QApplication::translate("GotoDialog", "Coordinates must be: '<left lon>, <bottom lat>, <right lon>, <top lat>'"));
108                 return;
109             }
110             theNewViewport = CoordBox(Coord(tokens[0].toDouble(), tokens[1].toDouble()), Coord(tokens[2].toDouble(), tokens[3].toDouble()));
111             if (cbCoordShowProjected->isChecked())
112                 theNewViewport = theProjection->fromProjectedRectF(theNewViewport);
113         } else
114         if (rbSpan->isChecked()) {
115             QStringList tokens = coordSpan->text().split(",");
116             if (tokens.size() < 4) {
117                 QMessageBox::warning(this, QApplication::translate("GotoDialog", "Invalid Coordinates format"),
118                     QApplication::translate("GotoDialog", "Coordinates must be: '<center lat>, <center lon>, <span lat>, <span lon>'"));
119                 return;
120             }
121             theNewViewport = CoordBox(
122                                 Coord(
123                                     tokens[1].toDouble() - tokens[3].toDouble() / 2,
124                                     tokens[0].toDouble() - tokens[2].toDouble() / 2),
125                                 Coord(
126                                     tokens[1].toDouble() + tokens[3].toDouble() / 2,
127                                     tokens[0].toDouble() + tokens[2].toDouble() / 2)
128                                 );
129             if (cbCoordShowProjected->isChecked())
130                 theNewViewport = theProjection->fromProjectedRectF(theNewViewport);
131         }
132         accept();
133     }
134 }
135 
searchWidget_selectionChanged()136 void GotoDialog::searchWidget_selectionChanged()
137 {
138 //    QPointF centerPoint = searchWidget->selectedCoords();
139 //    int zoom = searchWidget->selectedZoom();
140 //    // The API doesn't like request for too large bounding boxes so reset to a default
141 //    if (zoom < 15)
142 //        zoom = 15;
143 //    coordLink->setText( QString("http://www.openstreetmap.org/?lat=%1&lon=%2&zoom=%3")
144 //        .arg(QString::number(centerPoint.x(), 'f', 4))
145 //        .arg(QString::number(centerPoint.y(), 'f', 4))
146 //                .arg(QString::number(zoom))
147 //        );
148 //    rbLink->setChecked(true);
149 
150     rbCoord->setChecked(true);
151     coordCoord->setText(QString("%1, %2, %3, %4")
152                         .arg(searchWidget->selectedBbox().left())
153                         .arg(searchWidget->selectedBbox().top())
154                         .arg(searchWidget->selectedBbox().right())
155                         .arg(searchWidget->selectedBbox().bottom())
156                         );
157 
158 }
159 
on_NameFinderEdit_textChanged(const QString & text)160 void GotoDialog::on_NameFinderEdit_textChanged(const QString & text)
161 {
162     if (!text.isEmpty()) {
163         searchButton->setDefault(true);
164     } else {
165         searchButton->setDefault(false);
166         buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
167     }
168 }
169 
on_searchButton_clicked()170 void GotoDialog::on_searchButton_clicked()
171 {
172         QString searchString = NameFinderEdit->text();
173         QPointF C = theCenter;
174 
175         searchWidget->search(searchString, C);
176         searchButton->setEnabled(false);
177 }
178 
searchWidget_doubleClicked()179 void GotoDialog::searchWidget_doubleClicked()
180 {
181     buttonBox->button(QDialogButtonBox::Ok)->click();
182 }
183 
searchWidget_done()184 void GotoDialog::searchWidget_done()
185 {
186     searchButton->setEnabled(true);
187 }
188 
changeEvent(QEvent * event)189 void GotoDialog::changeEvent(QEvent * event)
190 {
191         if (event->type() == QEvent::LanguageChange)
192             retranslateUi(this);
193 }
194 
fillCoordinates()195 void GotoDialog::fillCoordinates()
196 {
197     CoordBox bbox = theViewport;
198     if (cbCoordShowProjected->isChecked())
199         bbox = theProjection->toProjectedRectF(theViewport, theRect);
200 
201     coordCoord->setText( QString("%1, %2, %3, %4")
202         .arg(COORD2STRING(bbox.bottomLeft().x()))
203         .arg(COORD2STRING(bbox.bottomLeft().y()))
204         .arg(COORD2STRING(bbox.topRight().x()))
205         .arg(COORD2STRING(bbox.topRight().y()))
206         );
207     coordSpan->setText( QString("%1, %2, %3, %4")
208         .arg(COORD2STRING(bbox.center().y()))
209         .arg(COORD2STRING(bbox.center().x()))
210         .arg(COORD2STRING(bbox.latDiff()))
211         .arg(COORD2STRING(bbox.lonDiff()))
212         );
213 }
214 
215