2013-05-17 11:14:10 +00:00
|
|
|
#include "globe.h"
|
2013-05-18 00:58:49 +00:00
|
|
|
#include "kmessagewidget.h"
|
2013-06-05 06:41:52 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
2013-05-17 16:28:02 +00:00
|
|
|
#include "../dive.h"
|
2013-06-27 12:33:44 +00:00
|
|
|
#include "../divelist.h"
|
2013-05-30 21:26:08 +00:00
|
|
|
#include "../helpers.h"
|
2013-05-17 16:28:02 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
2013-10-17 21:53:12 +00:00
|
|
|
#include <QTimer>
|
2013-05-17 16:28:02 +00:00
|
|
|
|
2013-05-17 11:14:10 +00:00
|
|
|
#include <marble/AbstractFloatItem.h>
|
2013-05-17 16:28:02 +00:00
|
|
|
#include <marble/GeoDataPlacemark.h>
|
|
|
|
#include <marble/GeoDataDocument.h>
|
|
|
|
#include <marble/MarbleModel.h>
|
2013-05-25 15:28:35 +00:00
|
|
|
#include <marble/MarbleDirs.h>
|
2013-05-29 19:59:38 +00:00
|
|
|
#include <marble/MapThemeManager.h>
|
2013-06-05 02:46:57 +00:00
|
|
|
#include <marble/GeoDataLineString.h>
|
2013-05-18 16:44:54 +00:00
|
|
|
#if INCOMPLETE_MARBLE
|
|
|
|
#include "marble/GeoDataTreeModel.h"
|
|
|
|
#else
|
2013-05-17 16:28:02 +00:00
|
|
|
#include <marble/GeoDataTreeModel.h>
|
2013-05-18 16:44:54 +00:00
|
|
|
#endif
|
2013-05-17 19:12:55 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QMessageBox>
|
2013-05-17 11:14:10 +00:00
|
|
|
|
2013-05-17 16:28:02 +00:00
|
|
|
GlobeGPS::GlobeGPS(QWidget* parent) : MarbleWidget(parent), loadedDives(0)
|
2013-05-17 11:14:10 +00:00
|
|
|
{
|
2013-05-29 19:59:38 +00:00
|
|
|
// check if Google Sat Maps are installed
|
|
|
|
// if not, check if they are in a known location
|
|
|
|
MapThemeManager mtm;
|
|
|
|
QStringList list = mtm.mapThemeIds();
|
2013-05-30 21:26:08 +00:00
|
|
|
QString theme, subsurfaceDataPath;
|
2013-05-29 19:59:38 +00:00
|
|
|
QDir marble;
|
|
|
|
bool foundGoogleMap = false;
|
|
|
|
Q_FOREACH(theme, list)
|
|
|
|
if (theme == "earth/googlesat/googlesat.dgml")
|
|
|
|
foundGoogleMap = true;
|
|
|
|
if (!foundGoogleMap) {
|
2013-05-30 21:26:08 +00:00
|
|
|
subsurfaceDataPath = getSubsurfaceDataPath("marbledata");
|
|
|
|
if (subsurfaceDataPath != "")
|
|
|
|
MarbleDirs::setMarbleDataPath(subsurfaceDataPath);
|
2013-05-30 07:08:11 +00:00
|
|
|
}
|
2013-05-24 19:15:49 +00:00
|
|
|
messageWidget = new KMessageWidget(this);
|
|
|
|
messageWidget->setCloseButtonVisible(false);
|
|
|
|
messageWidget->setHidden(true);
|
2013-05-18 01:52:04 +00:00
|
|
|
|
2013-05-24 14:07:59 +00:00
|
|
|
setMapThemeId("earth/googlesat/googlesat.dgml");
|
2013-05-18 01:52:04 +00:00
|
|
|
//setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
|
2013-05-23 04:25:05 +00:00
|
|
|
setProjection(Marble::Spherical);
|
2013-05-17 11:14:10 +00:00
|
|
|
|
2013-05-17 17:09:10 +00:00
|
|
|
setAnimationsEnabled(true);
|
2013-05-23 04:25:05 +00:00
|
|
|
setShowClouds(false);
|
|
|
|
setShowBorders(false);
|
|
|
|
setShowPlaces(true);
|
|
|
|
setShowCrosshairs(false);
|
|
|
|
setShowGrid(false);
|
2013-05-17 11:14:10 +00:00
|
|
|
setShowOverviewMap(false);
|
2013-05-18 01:52:04 +00:00
|
|
|
setShowScaleBar(true);
|
2013-05-24 19:21:16 +00:00
|
|
|
setShowCompass(false);
|
2013-09-26 21:14:09 +00:00
|
|
|
connect(this, SIGNAL(mouseClickGeoPosition(qreal, qreal, GeoDataCoordinates::Unit)),
|
|
|
|
this, SLOT(mouseClicked(qreal, qreal, GeoDataCoordinates::Unit)));
|
|
|
|
|
|
|
|
setMinimumHeight(0);
|
|
|
|
setMinimumWidth(0);
|
2013-10-13 18:24:12 +00:00
|
|
|
editingDiveCoords = 0;
|
2013-10-17 21:53:12 +00:00
|
|
|
fixZoomTimer = new QTimer();
|
|
|
|
connect(fixZoomTimer, SIGNAL(timeout()), this, SLOT(fixZoom()));
|
|
|
|
fixZoomTimer->setSingleShot(true);
|
2013-06-05 05:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobeGPS::mouseClicked(qreal lon, qreal lat, GeoDataCoordinates::Unit unit)
|
|
|
|
{
|
2013-11-10 21:23:18 +00:00
|
|
|
// don't mess with the selection while the user is editing a dive
|
|
|
|
if (mainWindow()->information()->isEditing())
|
|
|
|
return;
|
|
|
|
|
2013-06-05 05:44:12 +00:00
|
|
|
GeoDataCoordinates here(lon, lat, unit);
|
2013-06-05 06:41:52 +00:00
|
|
|
long lon_udeg = rint(1000000 * here.longitude(GeoDataCoordinates::Degree));
|
|
|
|
long lat_udeg = rint(1000000 * here.latitude(GeoDataCoordinates::Degree));
|
|
|
|
|
|
|
|
// distance() is in km above the map.
|
|
|
|
// We're going to use that to decide how
|
|
|
|
// approximate the dives have to be.
|
|
|
|
//
|
|
|
|
// Totally arbitrarily I say that 1km
|
|
|
|
// distance means that we can resolve
|
|
|
|
// to about 100m. Which in turn is about
|
|
|
|
// 1000 udeg.
|
|
|
|
//
|
|
|
|
// Trigonometry is hard, but sin x == x
|
|
|
|
// for small x, so let's just do this as
|
|
|
|
// a linear thing.
|
|
|
|
long resolve = rint(distance() * 1000);
|
|
|
|
|
|
|
|
int idx;
|
|
|
|
struct dive *dive;
|
2013-06-06 05:07:07 +00:00
|
|
|
bool clear = !(QApplication::keyboardModifiers() && Qt::ControlModifier);
|
2013-06-08 01:25:30 +00:00
|
|
|
bool toggle = !clear;
|
2013-06-05 06:41:52 +00:00
|
|
|
bool first = true;
|
|
|
|
for_each_dive(idx, dive) {
|
|
|
|
long lat_diff, lon_diff;
|
|
|
|
if (!dive_has_gps_location(dive))
|
|
|
|
continue;
|
|
|
|
lat_diff = labs(dive->latitude.udeg - lat_udeg);
|
|
|
|
lon_diff = labs(dive->longitude.udeg - lon_udeg);
|
|
|
|
if (lat_diff > 180000000)
|
|
|
|
lat_diff = 360000000 - lat_diff;
|
|
|
|
if (lon_diff > 180000000)
|
|
|
|
lon_diff = 180000000 - lon_diff;
|
|
|
|
if (lat_diff > resolve || lon_diff > resolve)
|
|
|
|
continue;
|
|
|
|
|
2013-06-06 05:07:07 +00:00
|
|
|
if (clear) {
|
2013-06-05 06:41:52 +00:00
|
|
|
mainWindow()->dive_list()->unselectDives();
|
2013-06-06 05:07:07 +00:00
|
|
|
clear = false;
|
|
|
|
}
|
2013-11-16 20:41:47 +00:00
|
|
|
mainWindow()->dive_list()->selectDive(idx, first, toggle);
|
2013-06-05 06:41:52 +00:00
|
|
|
first = false;
|
|
|
|
}
|
2013-05-17 11:14:10 +00:00
|
|
|
}
|
2013-05-17 16:28:02 +00:00
|
|
|
|
|
|
|
void GlobeGPS::reload()
|
|
|
|
{
|
2013-05-23 04:25:05 +00:00
|
|
|
if (loadedDives) {
|
2013-05-17 16:28:02 +00:00
|
|
|
model()->treeModel()->removeDocument(loadedDives);
|
|
|
|
delete loadedDives;
|
|
|
|
}
|
2013-05-18 00:58:49 +00:00
|
|
|
|
2013-05-23 04:25:05 +00:00
|
|
|
if (editingDiveCoords) {
|
2013-05-17 19:12:55 +00:00
|
|
|
editingDiveCoords = 0;
|
2013-05-27 17:43:21 +00:00
|
|
|
if (messageWidget->isVisible())
|
|
|
|
messageWidget->animatedHide();
|
2013-05-17 19:12:55 +00:00
|
|
|
}
|
2013-05-17 16:28:02 +00:00
|
|
|
|
|
|
|
loadedDives = new GeoDataDocument;
|
2013-06-05 02:46:57 +00:00
|
|
|
QMap<QString, GeoDataPlacemark *> locationMap;
|
2013-05-17 16:28:02 +00:00
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
struct dive *dive;
|
|
|
|
for_each_dive(idx, dive) {
|
|
|
|
if (dive_has_gps_location(dive)) {
|
2013-05-23 04:25:05 +00:00
|
|
|
GeoDataPlacemark *place = new GeoDataPlacemark(dive->location);
|
|
|
|
place->setCoordinate(dive->longitude.udeg / 1000000.0,dive->latitude.udeg / 1000000.0 , 0, GeoDataCoordinates::Degree);
|
2013-06-05 02:46:57 +00:00
|
|
|
// don't add dive locations twice, unless they are at least 50m apart
|
|
|
|
if (locationMap[QString(dive->location)]) {
|
2013-06-08 05:02:00 +00:00
|
|
|
GeoDataCoordinates existingLocation = locationMap[QString(dive->location)]->coordinate();
|
2013-06-05 02:46:57 +00:00
|
|
|
GeoDataLineString segment = GeoDataLineString();
|
|
|
|
segment.append(existingLocation);
|
2013-06-08 05:02:00 +00:00
|
|
|
GeoDataCoordinates newLocation = place->coordinate();
|
2013-06-05 02:46:57 +00:00
|
|
|
segment.append(newLocation);
|
|
|
|
double dist = segment.length(6371);
|
|
|
|
// the dist is scaled to the radius given - so with 6371km as radius
|
|
|
|
// 50m turns into 0.05 as threashold
|
|
|
|
if (dist < 0.05)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
locationMap[QString(dive->location)] = place;
|
2013-05-23 04:25:05 +00:00
|
|
|
loadedDives->append(place);
|
2013-05-17 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-23 04:25:05 +00:00
|
|
|
model()->treeModel()->addDocument(loadedDives);
|
2013-05-17 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobeGPS::centerOn(dive* dive)
|
|
|
|
{
|
2013-05-18 00:58:49 +00:00
|
|
|
// dive has changed, if we had the 'editingDive', hide it.
|
2013-05-23 04:25:05 +00:00
|
|
|
if (messageWidget->isVisible() && (!dive || dive_has_gps_location(dive))) {
|
2013-05-18 00:58:49 +00:00
|
|
|
messageWidget->animatedHide();
|
|
|
|
}
|
2013-05-31 12:05:33 +00:00
|
|
|
if (!dive)
|
|
|
|
return;
|
2013-05-18 00:58:49 +00:00
|
|
|
|
|
|
|
editingDiveCoords = 0;
|
|
|
|
|
2013-05-17 19:12:55 +00:00
|
|
|
qreal longitude = dive->longitude.udeg / 1000000.0;
|
|
|
|
qreal latitude = dive->latitude.udeg / 1000000.0;
|
|
|
|
|
2013-05-23 04:25:05 +00:00
|
|
|
if (!longitude || !latitude) {
|
2013-05-17 19:12:55 +00:00
|
|
|
prepareForGetDiveCoordinates(dive);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-27 17:27:14 +00:00
|
|
|
|
|
|
|
// set the zoom as seen from n kilometer above. 3km / 10,000ft seems pleasant
|
|
|
|
// do not change it it was already modified by user
|
|
|
|
if (!zoom())
|
|
|
|
zoomView(zoomFromDistance(3));
|
|
|
|
|
2013-10-17 21:53:12 +00:00
|
|
|
if (!fixZoomTimer->isActive())
|
|
|
|
currentZoomLevel = zoom();
|
|
|
|
// From the marble source code, the maximum time of
|
|
|
|
// 'spin and fit' is 2 seconds, so wait a bit them zoom again.
|
|
|
|
fixZoomTimer->start(2100);
|
|
|
|
|
2013-05-17 19:12:55 +00:00
|
|
|
centerOn(longitude,latitude, true);
|
|
|
|
}
|
|
|
|
|
2013-10-17 21:53:12 +00:00
|
|
|
void GlobeGPS::fixZoom()
|
|
|
|
{
|
|
|
|
zoomView(currentZoomLevel, Marble::Linear);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-17 19:12:55 +00:00
|
|
|
void GlobeGPS::prepareForGetDiveCoordinates(dive* dive)
|
|
|
|
{
|
2013-05-22 14:28:25 +00:00
|
|
|
if (!messageWidget->isVisible()) {
|
|
|
|
messageWidget->setMessageType(KMessageWidget::Warning);
|
2013-05-22 16:00:07 +00:00
|
|
|
messageWidget->setText(QObject::tr("No location data - move the map and double-click to set the dive location"));
|
2013-05-22 14:28:25 +00:00
|
|
|
messageWidget->setWordWrap(true);
|
|
|
|
messageWidget->animatedShow();
|
|
|
|
}
|
2013-05-17 19:12:55 +00:00
|
|
|
editingDiveCoords = dive;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobeGPS::changeDiveGeoPosition(qreal lon, qreal lat, GeoDataCoordinates::Unit unit)
|
|
|
|
{
|
|
|
|
// convert to degrees if in radian.
|
2013-05-23 04:25:05 +00:00
|
|
|
if (unit == GeoDataCoordinates::Radian) {
|
2013-05-17 19:12:55 +00:00
|
|
|
lon = lon * 180 / M_PI;
|
|
|
|
lat = lat * 180 / M_PI;
|
|
|
|
}
|
2013-10-11 13:21:04 +00:00
|
|
|
if (!editingDiveCoords)
|
2013-05-17 19:12:55 +00:00
|
|
|
return;
|
|
|
|
|
2013-11-17 20:17:41 +00:00
|
|
|
/* change everything on the selection. */
|
|
|
|
int i;
|
|
|
|
struct dive* dive;
|
|
|
|
for_each_dive(i, dive){
|
|
|
|
if(!dive->selected)
|
|
|
|
continue;
|
|
|
|
dive->latitude.udeg = lrint(lat * 1000000.0);
|
|
|
|
dive->longitude.udeg = lrint(lon * 1000000.0);
|
|
|
|
}
|
2013-05-17 19:12:55 +00:00
|
|
|
centerOn(lon, lat, true);
|
|
|
|
editingDiveCoords = 0;
|
2013-06-27 12:33:44 +00:00
|
|
|
mark_divelist_changed(TRUE);
|
2013-11-17 20:17:41 +00:00
|
|
|
messageWidget->animatedHide();
|
|
|
|
mainWindow()->refreshDisplay();
|
2013-05-17 16:28:02 +00:00
|
|
|
}
|
2013-05-17 19:12:55 +00:00
|
|
|
|
|
|
|
void GlobeGPS::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
qreal lat, lon;
|
2013-05-24 23:06:51 +00:00
|
|
|
if (editingDiveCoords && geoCoordinates(event->pos().x(), event->pos().y(), lon, lat, GeoDataCoordinates::Degree)) {
|
|
|
|
changeDiveGeoPosition(lon, lat, GeoDataCoordinates::Degree);
|
2013-05-17 19:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-24 19:15:49 +00:00
|
|
|
void GlobeGPS::resizeEvent(QResizeEvent* event)
|
2013-05-18 00:58:49 +00:00
|
|
|
{
|
2013-05-24 19:15:49 +00:00
|
|
|
int size = event->size().width();
|
|
|
|
MarbleWidget::resizeEvent(event);
|
|
|
|
if (size > 600)
|
|
|
|
messageWidget->setGeometry((size - 600) / 2, 5, 600, 0);
|
|
|
|
else
|
|
|
|
messageWidget->setGeometry(5, 5, size - 10, 0);
|
|
|
|
messageWidget->setMaximumHeight(500);
|
|
|
|
}
|