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 <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Lubomir I. Ivanov 2014-03-11 18:31:00 +02:00 committed by Dirk Hohndel
parent 4abe9d5c8b
commit 5b3dab719e
2 changed files with 34 additions and 9 deletions

View file

@ -44,6 +44,8 @@ MainWindow::MainWindow() : QMainWindow(),
actionNextDive(0), actionNextDive(0),
actionPreviousDive(0), actionPreviousDive(0),
helpView(0), helpView(0),
yearlyStats(0),
yearlyStatsModel(0),
state(VIEWALL) state(VIEWALL)
{ {
Q_ASSERT_X(m_Instance == NULL, "MainWindow", "MainWindow recreated!"); 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->reload(DiveTripModel::CURRENT);
ui.ListWidget->setFocus(); ui.ListWidget->setFocus();
WSInfoModel::instance()->updateInfo(); 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) void MainWindow::current_dive_changed(int divenr)
@ -386,15 +395,23 @@ void MainWindow::on_actionAutoGroup_triggered()
void MainWindow::on_actionYearlyStatistics_triggered() void MainWindow::on_actionYearlyStatistics_triggered()
{ {
QTreeView *view = new QTreeView(); // create the widget only once
QAbstractItemModel *model = new YearlyStatisticsModel(); if (!yearlyStats) {
view->setModel(model); yearlyStats = new QTreeView();
view->setWindowModality(Qt::NonModal); yearlyStats->setWindowModality(Qt::NonModal);
view->setMinimumWidth(600); yearlyStats->setMinimumWidth(600);
view->setAttribute(Qt::WA_QuitOnClose, false); yearlyStats->setWindowTitle(tr("Yearly Statistics"));
view->setWindowTitle(tr("Yearly Statistics")); yearlyStats->setWindowIcon(QIcon(":subsurface-icon"));
view->setWindowIcon(QIcon(":subsurface-icon")); }
view->show(); /* 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>() #define BEHAVIOR QList<int>()
@ -672,6 +689,12 @@ void MainWindow::closeEvent(QCloseEvent *event)
helpView->deleteLater(); helpView->deleteLater();
} }
if (yearlyStats && yearlyStats->isVisible()) {
yearlyStats->close();
yearlyStats->deleteLater();
yearlyStatsModel->deleteLater();
}
if (unsaved_changes() && (askSaveChanges() == false)) { if (unsaved_changes() && (askSaveChanges() == false)) {
event->ignore(); event->ignore();
return; return;

View file

@ -156,6 +156,8 @@ private:
QAction *actionNextDive; QAction *actionNextDive;
QAction *actionPreviousDive; QAction *actionPreviousDive;
UserManual *helpView; UserManual *helpView;
QTreeView *yearlyStats;
QAbstractItemModel *yearlyStatsModel;
CurrentState state; CurrentState state;
QString filter(); QString filter();
static MainWindow *m_Instance; static MainWindow *m_Instance;