subsurface/qt-ui/globe.cpp
Dirk Hohndel 92ee374494 Find Google maps in Subsurface's source tree
This way the user doesn't need to move the two folders googlemaps and
googlesat around in the filesystem.

This only works if Subsurface is started from the build directory - it
doesn't work when Subsurface is installed (and it doesn't at all address
the need to install these files and bundle them as well).

I'd consider this a hack to show how the real solution should work.

There is one more part of this that is a hack: Marble no longer searches
its default data directory; the path we set replaces the Marble system
search path. Sadly, Marble doesn't support paths the way Unix thinks of
them with multiple directories, separated by ':'. So this means that
Marble no longer finds any of its default icons. For most of them that
seems fine as I don't think lacking the icons for "manned_landing",
"robotic_rover", "unmanned_hard_landing" or the various types of places of
worship that Marble supports is necessarily a big issues for Subsurface,
but at least the default_location icon seemed important. And since we now
need to carry our own, I replaced the boring circle with a tiny dive flag.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2013-05-25 08:58:37 -07:00

146 lines
3.8 KiB
C++

#include "globe.h"
#include "kmessagewidget.h"
#include "../dive.h"
#include <QDebug>
#include <marble/AbstractFloatItem.h>
#include <marble/GeoDataPlacemark.h>
#include <marble/GeoDataDocument.h>
#include <marble/MarbleModel.h>
#include <marble/MarbleDirs.h>
#if INCOMPLETE_MARBLE
#include "marble/GeoDataTreeModel.h"
#else
#include <marble/GeoDataTreeModel.h>
#endif
#include <QMouseEvent>
#include <QMessageBox>
GlobeGPS::GlobeGPS(QWidget* parent) : MarbleWidget(parent), loadedDives(0)
{
// this will find the Google maps when running from your build directory
// TODO: all the magic to find the install path (and actually install/bundle these files)
MarbleDirs::setMarbleDataPath(QDir("./marbledata").absolutePath());
messageWidget = new KMessageWidget(this);
messageWidget->setCloseButtonVisible(false);
messageWidget->setHidden(true);
setMapThemeId("earth/googlesat/googlesat.dgml");
//setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
setProjection(Marble::Spherical);
setAnimationsEnabled(true);
setShowClouds(false);
setShowBorders(false);
setShowPlaces(true);
setShowCrosshairs(false);
setShowGrid(false);
setShowOverviewMap(false);
setShowScaleBar(true);
setShowCompass(false);
}
void GlobeGPS::reload()
{
if (loadedDives) {
model()->treeModel()->removeDocument(loadedDives);
delete loadedDives;
}
if (editingDiveCoords) {
editingDiveCoords = 0;
}
loadedDives = new GeoDataDocument;
diveLocations.clear();
int idx = 0;
struct dive *dive;
for_each_dive(idx, dive) {
if (dive_has_gps_location(dive)) {
// don't add dive locations twice.
if (diveLocations.contains(QString(dive->location)))
continue;
diveLocations.append(QString(dive->location));
GeoDataPlacemark *place = new GeoDataPlacemark(dive->location);
place->setCoordinate(dive->longitude.udeg / 1000000.0,dive->latitude.udeg / 1000000.0 , 0, GeoDataCoordinates::Degree);
loadedDives->append(place);
}
}
model()->treeModel()->addDocument(loadedDives);
}
void GlobeGPS::centerOn(dive* dive)
{
// dive has changed, if we had the 'editingDive', hide it.
if (messageWidget->isVisible() && (!dive || dive_has_gps_location(dive))) {
messageWidget->animatedHide();
}
editingDiveCoords = 0;
qreal longitude = dive->longitude.udeg / 1000000.0;
qreal latitude = dive->latitude.udeg / 1000000.0;
if (!longitude || !latitude) {
prepareForGetDiveCoordinates(dive);
return;
}
centerOn(longitude,latitude, true);
}
void GlobeGPS::prepareForGetDiveCoordinates(dive* dive)
{
if (!messageWidget->isVisible()) {
messageWidget->setMessageType(KMessageWidget::Warning);
messageWidget->setText(QObject::tr("No location data - move the map and double-click to set the dive location"));
messageWidget->setWordWrap(true);
messageWidget->animatedShow();
}
editingDiveCoords = dive;
}
void GlobeGPS::changeDiveGeoPosition(qreal lon, qreal lat, GeoDataCoordinates::Unit unit)
{
// convert to degrees if in radian.
if (unit == GeoDataCoordinates::Radian) {
lon = lon * 180 / M_PI;
lat = lat * 180 / M_PI;
}
if (!editingDiveCoords) {
return;
}
editingDiveCoords->latitude.udeg = lrint(lat * 1000000.0);
editingDiveCoords->longitude.udeg = lrint(lon * 1000000.0);
centerOn(lon, lat, true);
reload();
editingDiveCoords = 0;
messageWidget->animatedHide();
}
void GlobeGPS::mousePressEvent(QMouseEvent* event)
{
qreal lat, lon;
if (editingDiveCoords && geoCoordinates(event->pos().x(), event->pos().y(), lon, lat, GeoDataCoordinates::Degree)) {
changeDiveGeoPosition(lon, lat, GeoDataCoordinates::Degree);
}
}
void GlobeGPS::resizeEvent(QResizeEvent* event)
{
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);
}