10 #include "CurrentLocationWidget.h"
13 #include "MarbleDebug.h"
14 #include "MarbleLocale.h"
17 #include "MarbleWidgetPopupMenu.h"
18 #include "GeoDataCoordinates.h"
19 #include "PositionProviderPlugin.h"
20 #include "PluginManager.h"
21 #include "PositionTracking.h"
22 #include "routing/RoutingManager.h"
28 #include "ui_CurrentLocationWidget.h"
31 #include <QFileDialog>
32 #include <QMessageBox>
37 class CurrentLocationWidgetPrivate
40 CurrentLocationWidgetPrivate();
42 Ui::CurrentLocationWidget m_currentLocationUi;
44 AutoNavigation *m_adjustNavigation;
53 void adjustPositionTrackingStatus( PositionProviderStatus status );
54 void changePositionProvider(
const QString &provider );
55 void trackPlacemark();
56 void centerOnCurrentLocation();
57 void updateRecenterComboBox( AutoNavigation::CenterMode centerMode );
58 void updateAutoZoomCheckBox(
bool autoZoom );
60 void updateGuidanceMode();
66 CurrentLocationWidgetPrivate::CurrentLocationWidgetPrivate()
67 : m_widget( nullptr ),
68 m_adjustNavigation( nullptr ),
69 m_positionProviderPlugins(),
78 d( new CurrentLocationWidgetPrivate() )
80 d->m_currentLocationUi.
setupUi(
this );
81 layout()->setMargin( 0 );
83 connect( d->m_currentLocationUi.recenterComboBox, SIGNAL (currentIndexChanged(
int)),
84 this, SLOT(setRecenterMode(
int)) );
86 connect( d->m_currentLocationUi.autoZoomCheckBox, SIGNAL(clicked(
bool)),
87 this, SLOT(setAutoZoom(
bool)) );
89 bool const smallScreen = MarbleGlobal::getInstance()->profiles() & MarbleGlobal::SmallScreen;
90 d->m_currentLocationUi.positionTrackingComboBox->setVisible( !smallScreen );
91 d->m_currentLocationUi.locationLabel->setVisible( !smallScreen );
94 CurrentLocationWidget::~CurrentLocationWidget()
99 void CurrentLocationWidget::setMarbleWidget(
MarbleWidget *widget )
101 d->m_widget = widget;
103 delete d->m_adjustNavigation;
104 d->m_adjustNavigation =
new AutoNavigation( widget->
model(), widget->viewport(),
this );
106 const PluginManager* pluginManager = d->m_widget->model()->pluginManager();
109 d->m_currentLocationUi.positionTrackingComboBox->addItem( plugin->
guiString() );
111 if ( d->m_positionProviderPlugins.isEmpty() ) {
112 d->m_currentLocationUi.positionTrackingComboBox->setEnabled(
false );
113 QString html =
"<p>No Position Tracking Plugin installed.</p>";
114 d->m_currentLocationUi.locationLabel->setText( html );
115 d->m_currentLocationUi.locationLabel->setEnabled (
true );
116 bool const hasTrack = !d->m_widget->model()->positionTracking()->isTrackEmpty();
117 d->m_currentLocationUi.showTrackCheckBox->setEnabled( hasTrack );
118 d->m_currentLocationUi.saveTrackButton->setEnabled( hasTrack );
119 d->m_currentLocationUi.clearTrackButton->setEnabled( hasTrack );
123 disconnect( d->m_widget->model()->positionTracking(),
126 disconnect( d->m_widget->model()->positionTracking(),
129 disconnect( d->m_currentLocationUi.positionTrackingComboBox, SIGNAL(currentIndexChanged(
QString)),
130 this, SLOT(changePositionProvider(
QString)) );
131 disconnect( d->m_currentLocationUi.locationLabel, SIGNAL(linkActivated(
QString)),
132 this, SLOT(centerOnCurrentLocation()) );
133 disconnect( d->m_widget->model()->positionTracking(),
134 SIGNAL(statusChanged(PositionProviderStatus)),
this,
135 SLOT(adjustPositionTrackingStatus(PositionProviderStatus)) );
137 disconnect( d->m_widget->model(), SIGNAL(trackedPlacemarkChanged(
const GeoDataPlacemark*)),
138 this, SLOT(trackPlacemark()) );
141 connect( d->m_widget->model()->positionTracking(),
144 connect( d->m_widget->model()->positionTracking(),
147 d->updateActivePositionProvider( d->m_widget->model()->positionTracking()->positionProviderPlugin() );
148 connect( d->m_currentLocationUi.positionTrackingComboBox, SIGNAL(currentIndexChanged(
QString)),
149 this, SLOT(changePositionProvider(
QString)) );
150 connect( d->m_currentLocationUi.locationLabel, SIGNAL(linkActivated(
QString)),
151 this, SLOT(centerOnCurrentLocation()) );
152 connect( d->m_widget->model()->positionTracking(),
153 SIGNAL(statusChanged(PositionProviderStatus)),
this,
154 SLOT(adjustPositionTrackingStatus(PositionProviderStatus)) );
156 connect( d->m_adjustNavigation, SIGNAL(recenterModeChanged(AutoNavigation::CenterMode)),
157 this, SLOT(updateRecenterComboBox(AutoNavigation::CenterMode)) );
158 connect( d->m_adjustNavigation, SIGNAL(autoZoomToggled(
bool)),
159 this, SLOT(updateAutoZoomCheckBox(
bool)) );
168 d->m_adjustNavigation, SLOT(inhibitAutoAdjustments()) );
169 connect( d->m_widget->model()->routingManager(), SIGNAL(guidanceModeEnabledChanged(
bool)),
170 this, SLOT(updateGuidanceMode()) );
172 connect (d->m_currentLocationUi.showTrackCheckBox, SIGNAL(clicked(
bool)),
173 d->m_widget->model()->positionTracking(), SLOT(setTrackVisible(
bool)));
174 connect (d->m_currentLocationUi.showTrackCheckBox, SIGNAL(clicked(
bool)),
175 d->m_widget, SLOT(
update()));
176 if ( d->m_widget->model()->positionTracking()->trackVisible() ) {
177 d->m_currentLocationUi.showTrackCheckBox->setCheckState(
Qt::Checked);
179 connect ( d->m_currentLocationUi.saveTrackButton, SIGNAL(clicked(
bool)),
180 this, SLOT(saveTrack()));
181 connect ( d->m_currentLocationUi.openTrackButton, SIGNAL(clicked(
bool)),
182 this, SLOT(openTrack()));
183 connect (d->m_currentLocationUi.clearTrackButton, SIGNAL(clicked(
bool)),
184 this, SLOT(clearTrack()));
185 connect( d->m_widget->model(), SIGNAL(trackedPlacemarkChanged(
const GeoDataPlacemark*)),
186 this, SLOT(trackPlacemark()) );
189 void CurrentLocationWidgetPrivate::adjustPositionTrackingStatus( PositionProviderStatus status )
191 if ( status == PositionProviderStatusAvailable ) {
195 QString html =
"<html><body><p>";
198 case PositionProviderStatusUnavailable:
201 case PositionProviderStatusAcquiring:
202 html +=
QObject::tr(
"Waiting for current location information..." );
204 case PositionProviderStatusAvailable:
207 case PositionProviderStatusError:
208 html +=
QObject::tr(
"Error when determining current location: " );
209 html += m_widget->model()->positionTracking()->error();
214 m_currentLocationUi.locationLabel->setEnabled(
true );
215 m_currentLocationUi.locationLabel->setText( html );
220 m_currentLocationUi.positionTrackingComboBox->blockSignals(
true );
222 m_currentLocationUi.positionTrackingComboBox->setCurrentIndex( 0 );
224 for(
int i=0; i<m_currentLocationUi.positionTrackingComboBox->count(); ++i ) {
225 if ( m_currentLocationUi.positionTrackingComboBox->itemText( i ) == plugin->
guiString() ) {
226 m_currentLocationUi.positionTrackingComboBox->setCurrentIndex( i );
231 m_currentLocationUi.positionTrackingComboBox->blockSignals(
false );
232 m_currentLocationUi.recenterLabel->setEnabled( plugin );
233 m_currentLocationUi.recenterComboBox->setEnabled( plugin );
234 m_currentLocationUi.autoZoomCheckBox->setEnabled( plugin );
238 void CurrentLocationWidgetPrivate::updateGuidanceMode()
240 const bool enabled = m_widget->model()->routingManager()->guidanceModeEnabled();
242 m_adjustNavigation->setAutoZoom( enabled );
243 m_adjustNavigation->setRecenter( enabled ? AutoNavigation::RecenterOnBorder : AutoNavigation::DontRecenter );
246 void CurrentLocationWidgetPrivate::receiveGpsCoordinates(
const GeoDataCoordinates &position, qreal speed )
248 m_currentPosition = position;
252 qreal unitSpeed = 0.0;
253 qreal altitude = 0.0;
254 qreal length = m_widget->model()->positionTracking()->
length( m_widget->model()->planetRadius() );
257 "<table cellspacing=\"2\" cellpadding=\"2\">"
258 "<tr><td>Longitude</td><td><a href=\"https://edu.kde.org/marble\">%1</a></td></tr>"
259 "<tr><td>Latitude</td><td><a href=\"https://edu.kde.org/marble\">%2</a></td></tr>"
260 "<tr><td>Altitude</td><td>%3</td></tr>"
261 "<tr><td>Speed</td><td>%4</td></tr>"
262 "<tr><td>Distance</td><td>%5</td></tr>"
266 switch ( MarbleGlobal::getInstance()->
locale()->measurementSystem() ) {
267 case MarbleLocale::MetricSystem:
270 unitSpeed = speed * HOUR2SEC * METER2KM;
273 if ( length > 1000.0 ) {
279 case MarbleLocale::ImperialSystem:
282 unitSpeed = speed * HOUR2SEC * METER2KM * KM2MI;
285 altitude = position.
altitude() * M2FT;
289 case MarbleLocale::NauticalSystem:
292 unitSpeed = speed * HOUR2SEC * METER2KM * KM2NM;
296 length *= METER2KM*KM2NM;
305 html = html.
arg(altitudeString, speedString +
QLatin1Char(
' ') + unitString);
306 html = html.
arg( distanceString );
307 m_currentLocationUi.locationLabel->setText( html );
308 m_currentLocationUi.showTrackCheckBox->setEnabled(
true );
309 m_currentLocationUi.saveTrackButton->setEnabled(
true );
310 m_currentLocationUi.clearTrackButton->setEnabled(
true );
313 void CurrentLocationWidgetPrivate::changePositionProvider(
const QString &provider )
317 m_currentLocationUi.locationLabel->setEnabled(
true );
319 PositionTracking *tracking = m_widget->model()->positionTracking();
320 tracking->setPositionProviderPlugin( instance );
327 m_currentLocationUi.locationLabel->setEnabled(
false );
328 m_widget->model()->positionTracking()->setPositionProviderPlugin(
nullptr );
332 void CurrentLocationWidgetPrivate::trackPlacemark()
334 changePositionProvider(
QObject::tr(
"Placemark" ) );
335 m_adjustNavigation->setRecenter( AutoNavigation::AlwaysRecenter );
338 void CurrentLocationWidget::setRecenterMode(
int mode )
340 if ( mode >= 0 && mode <= AutoNavigation::RecenterOnBorder ) {
341 AutoNavigation::CenterMode centerMode = ( AutoNavigation::CenterMode ) mode;
342 d->m_adjustNavigation->setRecenter( centerMode );
346 void CurrentLocationWidget::setAutoZoom(
bool autoZoom )
348 d->m_adjustNavigation->setAutoZoom( autoZoom );
351 void CurrentLocationWidgetPrivate::updateAutoZoomCheckBox(
bool autoZoom )
353 m_currentLocationUi.autoZoomCheckBox->setChecked( autoZoom );
356 void CurrentLocationWidgetPrivate::updateRecenterComboBox( AutoNavigation::CenterMode centerMode )
358 m_currentLocationUi.recenterComboBox->setCurrentIndex( centerMode );
361 void CurrentLocationWidgetPrivate::centerOnCurrentLocation()
363 m_widget->centerOn(m_currentPosition,
true);
366 void CurrentLocationWidgetPrivate::saveTrack()
368 QString suggested = m_lastSavePath;
379 m_lastSavePath = file.absolutePath();
380 m_widget->model()->positionTracking()->saveTrack( fileName );
383 void CurrentLocationWidgetPrivate::openTrack()
385 QString suggested = m_lastOpenPath;
390 m_lastOpenPath = file.absolutePath();
391 m_widget->model()->addGeoDataFile( fileName );
395 void CurrentLocationWidgetPrivate::clearTrack()
399 QObject::tr(
"Are you sure you want to clear the current track?" ),
404 m_widget->model()->positionTracking()->clearTrack();
406 m_currentLocationUi.saveTrackButton->setEnabled(
false );
407 m_currentLocationUi.clearTrackButton->setEnabled(
false );
411 AutoNavigation::CenterMode CurrentLocationWidget::recenterMode()
const
413 return d->m_adjustNavigation->recenterMode();
416 bool CurrentLocationWidget::autoZoom()
const
418 return d->m_adjustNavigation->autoZoom();
421 bool CurrentLocationWidget::trackVisible()
const
423 return d->m_widget->model()->positionTracking()->trackVisible();
426 QString CurrentLocationWidget::lastOpenPath()
const
428 return d->m_lastOpenPath;
431 QString CurrentLocationWidget::lastSavePath()
const
433 return d->m_lastSavePath;
436 void CurrentLocationWidget::setTrackVisible(
bool visible )
438 d->m_currentLocationUi.showTrackCheckBox->setChecked( visible );
439 d->m_widget->model()->positionTracking()->setTrackVisible( visible );
442 void CurrentLocationWidget::setLastOpenPath(
const QString &path )
444 d->m_lastOpenPath = path;
447 void CurrentLocationWidget::setLastSavePath(
const QString &path )
449 d->m_lastSavePath = path;
454 #include "moc_CurrentLocationWidget.cpp"