2013-05-17 11:14:10 +00:00
# include "globe.h"
2013-05-18 00:58:49 +00:00
# include "kmessagewidget.h"
2013-05-17 16:28:02 +00:00
# include "../dive.h"
# include <QDebug>
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-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-18 01:52:04 +00:00
2013-05-17 11:14:10 +00:00
setMapThemeId ( " earth/bluemarble/bluemarble.dgml " ) ;
2013-05-18 01:52:04 +00:00
//setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
2013-05-17 11:14:10 +00:00
setProjection ( Marble : : Spherical ) ;
2013-05-17 17:09:10 +00:00
setAnimationsEnabled ( true ) ;
2013-05-17 16:28:02 +00:00
setShowClouds ( false ) ;
setShowBorders ( false ) ;
2013-05-18 01:52:04 +00:00
setShowPlaces ( true ) ;
2013-05-17 16:28:02 +00:00
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-17 11:14:10 +00:00
Q_FOREACH ( AbstractFloatItem * floatItem , floatItems ( ) ) {
if ( floatItem & & floatItem - > nameId ( ) = = " compass " ) {
floatItem - > setPosition ( QPoint ( 10 , 10 ) ) ;
floatItem - > setContentSize ( QSize ( 50 , 50 ) ) ;
}
}
}
2013-05-17 16:28:02 +00:00
void GlobeGPS : : reload ( )
{
if ( loadedDives ) {
model ( ) - > treeModel ( ) - > removeDocument ( loadedDives ) ;
delete loadedDives ;
}
2013-05-18 00:58:49 +00:00
2013-05-17 19:12:55 +00:00
if ( editingDiveCoords ) {
editingDiveCoords = 0 ;
}
2013-05-17 16:28:02 +00:00
2013-05-18 00:58:49 +00:00
messageWidget - > animatedHide ( ) ;
2013-05-17 16:28:02 +00:00
loadedDives = new GeoDataDocument ;
2013-05-18 01:56:21 +00:00
diveLocations . clear ( ) ;
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-17 17:09:10 +00:00
// don't add dive locations twice.
if ( diveLocations . contains ( QString ( dive - > location ) ) )
continue ;
2013-05-18 01:56:21 +00:00
diveLocations . append ( QString ( dive - > location ) ) ;
2013-05-17 16:28:02 +00:00
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 )
{
2013-05-18 00:58:49 +00:00
// dive has changed, if we had the 'editingDive', hide it.
if ( messageWidget - > isVisible ( ) ) {
messageWidget - > animatedHide ( ) ;
}
editingDiveCoords = 0 ;
2013-05-17 19:12:55 +00:00
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 )
{
2013-05-18 00:58:49 +00:00
messageWidget - > setMessageType ( KMessageWidget : : Warning ) ;
messageWidget - > setText ( QObject : : tr ( " This dive has no location! Please move the planet to the desired position, then double-click to set the new location for this dive. " ) ) ;
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.
if ( unit = = GeoDataCoordinates : : Radian ) {
lon = lon * 180 / M_PI ;
lat = lat * 180 / M_PI ;
}
if ( ! editingDiveCoords ) {
return ;
}
editingDiveCoords - > latitude . udeg = ( int ) lat * 1000000.0 ;
editingDiveCoords - > longitude . udeg = ( int ) lon * 1000000.0 ;
centerOn ( lon , lat , true ) ;
reload ( ) ;
editingDiveCoords = 0 ;
2013-05-18 00:58:49 +00:00
messageWidget - > animatedHide ( ) ;
2013-05-17 16:28:02 +00:00
}
2013-05-17 19:12:55 +00:00
void GlobeGPS : : mousePressEvent ( QMouseEvent * event )
{
qreal lat , lon ;
if ( editingDiveCoords & & geoCoordinates ( event - > pos ( ) . x ( ) , event - > pos ( ) . y ( ) , lon , lat , GeoDataCoordinates : : Radian ) ) {
changeDiveGeoPosition ( lon , lat , GeoDataCoordinates : : Radian ) ;
}
}
2013-05-18 00:58:49 +00:00
void GlobeGPS : : setMessageWidget ( KMessageWidget * globeMessage )
{
messageWidget = globeMessage ;
}