[PATCH 3/4] MainWindow: maintain one window instance of the yearly statistics

Lubomir I. Ivanov neolit123 at gmail.com
Tue Mar 11 09:31:00 PDT 2014


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

This is a bit tricky because we are using a plain widget for
a window and don't have a class for it (req. more source files).
Also for the table model to update we need to create a new
YearlyStatisticsModel instance each time. At least, in that regard
we can re-create the model each time refreshDisplay() is called.

This patch adds a couple of private variables that are used
to manage the memory of the yearly statistics model and window
and also close that same window on MainWindow::closeEvent().

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
 qt-ui/mainwindow.cpp | 41 ++++++++++++++++++++++++++++++++---------
 qt-ui/mainwindow.h   |  2 ++
 2 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index b43f8a8..171161a 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -44,6 +44,8 @@ MainWindow::MainWindow() : QMainWindow(),
 	actionNextDive(0),
 	actionPreviousDive(0),
 	helpView(0),
+	yearlyStats(0),
+	yearlyStatsModel(0),
 	state(VIEWALL)
 {
 	Q_ASSERT_X(m_Instance == NULL, "MainWindow", "MainWindow recreated!");
@@ -100,6 +102,13 @@ void MainWindow::refreshDisplay(bool recreateDiveList)
 		ui.ListWidget->reload(DiveTripModel::CURRENT);
 	ui.ListWidget->setFocus();
 	WSInfoModel::instance()->updateInfo();
+	// refresh the yearly stats if the window has an instance
+	if (yearlyStats) {
+		if (yearlyStatsModel)
+			delete yearlyStatsModel;
+		yearlyStatsModel = new YearlyStatisticsModel();
+		yearlyStats->setModel(yearlyStatsModel);
+	}
 }
 
 void MainWindow::current_dive_changed(int divenr)
@@ -376,15 +385,23 @@ void MainWindow::on_actionAutoGroup_triggered()
 
 void MainWindow::on_actionYearlyStatistics_triggered()
 {
-	QTreeView *view = new QTreeView();
-	QAbstractItemModel *model = new YearlyStatisticsModel();
-	view->setModel(model);
-	view->setWindowModality(Qt::NonModal);
-	view->setMinimumWidth(600);
-	view->setAttribute(Qt::WA_QuitOnClose, false);
-	view->setWindowTitle(tr("Yearly Statistics"));
-	view->setWindowIcon(QIcon(":subsurface-icon"));
-	view->show();
+	// create the widget only once
+	if (!yearlyStats) {
+		yearlyStats = new QTreeView();
+		yearlyStats->setWindowModality(Qt::NonModal);
+		yearlyStats->setMinimumWidth(600);
+		yearlyStats->setWindowTitle(tr("Yearly Statistics"));
+		yearlyStats->setWindowIcon(QIcon(":subsurface-icon"));
+	}
+	/* problem here is that without more MainWindow variables or a separate YearlyStatistics
+	 * class the user needs to close the window/widget and re-open it for it to update.
+	 */
+	if (yearlyStatsModel)
+		delete yearlyStatsModel;
+	yearlyStatsModel = new YearlyStatisticsModel();
+	yearlyStats->setModel(yearlyStatsModel);
+	yearlyStats->raise();
+	yearlyStats->show();
 }
 
 #define BEHAVIOR QList<int>()
@@ -662,6 +679,12 @@ void MainWindow::closeEvent(QCloseEvent *event)
 		helpView->deleteLater();
 	}
 
+	if (yearlyStats && yearlyStats->isVisible()) {
+		yearlyStats->close();
+		yearlyStats->deleteLater();
+		yearlyStatsModel->deleteLater();
+	}
+
 	if (unsaved_changes() && (askSaveChanges() == false)) {
 		event->ignore();
 		return;
diff --git a/qt-ui/mainwindow.h b/qt-ui/mainwindow.h
index 70b9793..eccdbfb 100644
--- a/qt-ui/mainwindow.h
+++ b/qt-ui/mainwindow.h
@@ -154,6 +154,8 @@ private:
 	QAction *actionNextDive;
 	QAction *actionPreviousDive;
 	UserManual *helpView;
+	QTreeView *yearlyStats;
+	QAbstractItemModel *yearlyStatsModel;
 	CurrentState state;
 	QString filter();
 	static MainWindow *m_Instance;
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list