From 01cafed303ec26c1903295eededd3f178bdb145e Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <tomaz.canabrava@gmail.com>
Date: Thu, 30 Jul 2015 21:51:38 -0300
Subject: [PATCH 04/10] Transform GlobeGPS in a static instance() class

This is needed to start easing the transition from the completely
wrong and bogus MainWindow::instance()->globe() calls. this is still
wrong, but with it I removed one level of indirection.
I did that now because I wanted to not taint the location management
when I use it to deal with the globe.
---
 qt-ui/globe.cpp                 | 10 ++++++++--
 qt-ui/globe.h                   |  3 ++-
 qt-ui/maintab.cpp               |  5 ++---
 qt-ui/mainwindow.cpp            | 17 ++++++-----------
 qt-ui/mainwindow.h              |  2 --
 qt-ui/subsurfacewebservices.cpp |  4 ++--
 6 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/qt-ui/globe.cpp b/qt-ui/globe.cpp
index 066fdc2..4f40e22 100644
--- a/qt-ui/globe.cpp
+++ b/qt-ui/globe.cpp
@@ -24,6 +24,12 @@
 #include <marble/MarbleDebug.h>
 #endif
 
+GlobeGPS *GlobeGPS::instance()
+{
+	static GlobeGPS *self = new GlobeGPS();
+	return self;
+}
+
 GlobeGPS::GlobeGPS(QWidget *parent) : MarbleWidget(parent),
 	loadedDives(0),
 	messageWidget(new KMessageWidget(this)),
@@ -378,9 +384,9 @@ void GlobeGPS::centerOnIndex(const QModelIndex& idx)
 {
 	struct dive_site *ds = get_dive_site_by_uuid(idx.model()->index(idx.row(), 0).data().toInt());
 	if (!ds || !dive_site_has_gps_location(ds))
-		MainWindow::instance()->globe()->centerOnDiveSite(&displayed_dive_site);
+		centerOnDiveSite(&displayed_dive_site);
 	else
-		MainWindow::instance()->globe()->centerOnDiveSite(ds);
+		centerOnDiveSite(ds);
 }
 #else
 
diff --git a/qt-ui/globe.h b/qt-ui/globe.h
index e8be53b..a8cb894 100644
--- a/qt-ui/globe.h
+++ b/qt-ui/globe.h
@@ -21,7 +21,7 @@ class GlobeGPS : public MarbleWidget {
 	Q_OBJECT
 public:
 	using MarbleWidget::centerOn;
-	GlobeGPS(QWidget *parent = 0);
+	static GlobeGPS *instance();
 	void reload();
 	bool eventFilter(QObject *, QEvent *);
 
@@ -38,6 +38,7 @@ private:
 	bool needResetZoom;
 	bool editingDiveLocation;
 	bool doubleClick;
+	GlobeGPS(QWidget *parent = 0);
 
 signals:
 	void coordinatesChanged();
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index 4b6f961..369a754 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -463,7 +463,7 @@ void MainTab::updateDiveInfo(bool clear)
 	// I don't like this code here - but globe() wasn't initialized on the constructor.
 	{
 		QListView *completerListview = qobject_cast<QListView*>(ui.location->completer()->popup());
-		connect(completerListview, SIGNAL(entered(QModelIndex)), MainWindow::instance()->globe(), SLOT(centerOnIndex(QModelIndex)), Qt::UniqueConnection);
+		connect(completerListview, SIGNAL(entered(QModelIndex)), GlobeGPS::instance(), SLOT(centerOnIndex(QModelIndex)), Qt::UniqueConnection);
 	}
 
 	EditMode rememberEM = editMode;
@@ -1208,8 +1208,7 @@ void MainTab::rejectChanges()
 	// the user could have edited the location and then canceled the edit
 	// let's get the correct location back in view
 #ifndef NO_MARBLE
-	MainWindow::instance()->globe()->centerOnDiveSite(get_dive_site_by_uuid(displayed_dive.dive_site_uuid));
-	MainWindow::instance()->globe()->reload();
+	GlobeGPS::instance()->centerOnDiveSite(get_dive_site_by_uuid(displayed_dive.dive_site_uuid));
 #endif
 	// show the profile and dive info
 	MainWindow::instance()->graphics()->replot();
diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index e7d53e5..4f82415 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -73,7 +73,7 @@ MainWindow::MainWindow() : QMainWindow(),
 	ProfileWidget2 *profileWidget = new ProfileWidget2();
 
 #ifndef NO_MARBLE
-	GlobeGPS *globeGps = new GlobeGPS();
+	GlobeGPS *globeGps = GlobeGPS::instance();
 #else
 	QWidget *globeGps = NULL;
 #endif
@@ -174,7 +174,7 @@ MainWindow::MainWindow() : QMainWindow(),
 	diveListView->reload(DiveTripModel::TREE);
 	diveListView->reloadHeaderActions();
 	diveListView->setFocus();
-	globe()->reload();
+	GlobeGPS::instance()->reload();
 	diveListView->expand(dive_list()->model()->index(0, 0));
 	diveListView->scrollTo(dive_list()->model()->index(0, 0), QAbstractItemView::PositionAtCenter);
 	divePlannerWidget()->settingsChanged();
@@ -258,7 +258,7 @@ void MainWindow::refreshDisplay(bool doRecreateDiveList)
 	getNotificationWidget()->showNotification(get_error_string(), KMessageWidget::Error);
 	information()->reload();
 	TankInfoModel::instance()->update();
-	globe()->reload();
+	GlobeGPS::instance()->reload();
 	if (doRecreateDiveList)
 		recreateDiveList();
 
@@ -286,7 +286,7 @@ void MainWindow::current_dive_changed(int divenr)
 	}
 	graphics()->plotDive();
 	information()->updateDiveInfo();
-	globe()->reload();
+	GlobeGPS::instance()->reload();
 }
 
 void MainWindow::on_actionNew_triggered()
@@ -429,7 +429,7 @@ void MainWindow::cleanUpEmpty()
 	information()->updateDiveInfo(true);
 	graphics()->setEmptyState();
 	dive_list()->reload(DiveTripModel::TREE);
-	globe()->reload();
+	GlobeGPS::instance()->reload();
 	if (!existing_filename)
 		setTitle(MWTF_DEFAULT);
 	disableShortcuts();
@@ -741,7 +741,7 @@ void MainWindow::on_actionEditDive_triggered()
 	disableShortcuts();
 	DivePlannerPointsModel::instance()->setPlanMode(DivePlannerPointsModel::ADD);
 	graphics()->setAddState();
-	globe()->endGetDiveCoordinates();
+	GlobeGPS::instance()->endGetDiveCoordinates();
 	setApplicationState("EditDive");
 	DivePlannerPointsModel::instance()->loadFromDive(current_dive);
 	information()->enableEdition(MainTab::MANUALLY_ADDED_DIVE);
@@ -1179,11 +1179,6 @@ DiveListView *MainWindow::dive_list()
 	return qobject_cast<DiveListView*>(applicationState["Default"].bottomLeft);
 }
 
-GlobeGPS *MainWindow::globe()
-{
-	return qobject_cast<GlobeGPS*>(applicationState["Default"].bottomRight);
-}
-
 MainTab *MainWindow::information()
 {
 	return qobject_cast<MainTab*>(applicationState["Default"].topLeft);
diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h
index f3a0f22..583dc59 100644
--- a/qt-ui/mainwindow.h
+++ b/qt-ui/mainwindow.h
@@ -26,7 +26,6 @@ class Stats;
 class Equipment;
 class QItemSelection;
 class DiveListView;
-class GlobeGPS;
 class MainTab;
 class ProfileGraphicsView;
 class QWebView;
@@ -69,7 +68,6 @@ public:
 	void addRecentFile(const QStringList &newFiles);
 	void removeRecentFile(QStringList failedFiles);
 	DiveListView *dive_list();
-	GlobeGPS *globe();
 	DivePlannerWidget *divePlannerWidget();
 	PlannerSettingsWidget *divePlannerSettingsWidget();
 	LocationInformationWidget *locationInformationWidget();
diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp
index 8154ce5..128e199 100644
--- a/qt-ui/subsurfacewebservices.cpp
+++ b/qt-ui/subsurfacewebservices.cpp
@@ -418,8 +418,8 @@ void SubsurfaceWebServices::buttonClicked(QAbstractButton *button)
 		// finally now that all the extra GPS fixes that weren't used have been deleted
 		// we can update the globe
 		if (changed) {
-			MainWindow::instance()->globe()->repopulateLabels();
-			MainWindow::instance()->globe()->centerOnDiveSite(get_dive_site_by_uuid(current_dive->dive_site_uuid));
+			GlobeGPS::instance()->repopulateLabels();
+			GlobeGPS::instance()->centerOnDiveSite(get_dive_site_by_uuid(current_dive->dive_site_uuid));
 		}
 #endif
 
-- 
2.3.2 (Apple Git-55)

