From 5b3dab719e44f5c20304ed48c5df35f8e115c28b Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Tue, 11 Mar 2014 18:31:00 +0200 Subject: MainWindow: maintain one window instance of the yearly statistics 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 Signed-off-by: Dirk Hohndel --- 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 426e979f7..4d4d0baa2 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!"); @@ -110,6 +112,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) @@ -386,15 +395,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() @@ -672,6 +689,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 94655e516..8244d0a0f 100644 --- a/qt-ui/mainwindow.h +++ b/qt-ui/mainwindow.h @@ -156,6 +156,8 @@ private: QAction *actionNextDive; QAction *actionPreviousDive; UserManual *helpView; + QTreeView *yearlyStats; + QAbstractItemModel *yearlyStatsModel; CurrentState state; QString filter(); static MainWindow *m_Instance; -- cgit v1.2.3-70-g09d2