From 19fd03b66e73da53fe61a6809659b2abe22d2a23 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sun, 10 May 2020 18:38:06 +0300 Subject: [PATCH] Read/write window state. Will help with setting the carousel size. --- src/app/puzzle/puzzlemainwindow.cpp | 118 ++++++++++++++++++++++++++++ src/app/puzzle/puzzlemainwindow.h | 7 ++ 2 files changed, 125 insertions(+) diff --git a/src/app/puzzle/puzzlemainwindow.cpp b/src/app/puzzle/puzzlemainwindow.cpp index df72ac4f4..34fba421e 100644 --- a/src/app/puzzle/puzzlemainwindow.cpp +++ b/src/app/puzzle/puzzlemainwindow.cpp @@ -36,6 +36,7 @@ #include "puzzleapplication.h" #include "../vlayout/vrawlayout.h" #include "../vmisc/vsysexits.h" +#include "../vmisc/projectversion.h" #include "../ifc/xml/vlayoutconverter.h" #include "../ifc/exception/vexception.h" @@ -75,6 +76,8 @@ PuzzleMainWindow::PuzzleMainWindow(const VPuzzleCommandLinePtr &cmd, QWidget *pa SetPropertiesData(); + + ReadSettings(); } //--------------------------------------------------------------------------------------------------------------------- @@ -469,7 +472,98 @@ void PuzzleMainWindow::SetCheckBoxValue(QCheckBox *checkbox, bool value) checkbox->blockSignals(false); } +//--------------------------------------------------------------------------------------------------------------------- +void PuzzleMainWindow::ReadSettings() +{ + qCDebug(pWindow, "Reading settings."); + const VPuzzleSettings *settings = qApp->PuzzleSettings(); + if (settings->status() == QSettings::NoError) + { + restoreGeometry(settings->GetGeometry()); + restoreState(settings->GetWindowState()); + restoreState(settings->GetToolbarsState(), APP_VERSION); + + ui->dockWidgetProperties->setVisible(settings->IsDockWidgetPropertiesActive()); + ui->dockWidgetPropertiesContents->setVisible(settings->IsDockWidgetPropertiesContentsActive()); + + // Scene antialiasing + m_graphicsView->SetAntialiasing(settings->GetGraphicalOutput()); + + // Stack limit +// qApp->getUndoStack()->setUndoLimit(settings->GetUndoCount()); + } + else + { + qWarning() << tr("Cannot read settings from a malformed .INI file."); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void PuzzleMainWindow::WriteSettings() +{ + VPuzzleSettings *settings = qApp->PuzzleSettings(); + settings->SetGeometry(saveGeometry()); + settings->SetWindowState(saveState()); + settings->SetToolbarsState(saveState(APP_VERSION)); + + settings->SetDockWidgetPropertiesActive(ui->dockWidgetProperties->isEnabled()); + settings->SetDockWidgetPropertiesContentsActive(ui->dockWidgetPropertiesContents->isEnabled()); + + settings->sync(); + if (settings->status() == QSettings::AccessError) + { + qWarning() << tr("Cannot save settings. Access denied."); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +bool PuzzleMainWindow::MaybeSave() +{ + // TODO: Implement maybe save check +// if (this->isWindowModified()) +// { +// if (curFile.isEmpty() && ui->tableWidget->rowCount() == 0) +// { +// return true;// Don't ask if file was created without modifications. +// } + +// QScopedPointer messageBox(new QMessageBox(tr("Unsaved changes"), +// tr("Measurements have been modified.\n" +// "Do you want to save your changes?"), +// QMessageBox::Warning, QMessageBox::Yes, QMessageBox::No, +// QMessageBox::Cancel, this, Qt::Sheet)); + +// messageBox->setDefaultButton(QMessageBox::Yes); +// messageBox->setEscapeButton(QMessageBox::Cancel); + +// messageBox->setButtonText(QMessageBox::Yes, curFile.isEmpty() || mIsReadOnly ? tr("Saveā€¦") : tr("Save")); +// messageBox->setButtonText(QMessageBox::No, tr("Don't Save")); + +// messageBox->setWindowModality(Qt::ApplicationModal); +// const auto ret = static_cast(messageBox->exec()); + +// switch (ret) +// { +// case QMessageBox::Yes: +// if (mIsReadOnly) +// { +// return FileSaveAs(); +// } +// else +// { +// return FileSave(); +// } +// case QMessageBox::No: +// return true; +// case QMessageBox::Cancel: +// return false; +// default: +// break; +// } +// } + return true; +} //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::on_actionNew_triggered() @@ -486,6 +580,30 @@ void PuzzleMainWindow::on_actionNew_triggered() } +//--------------------------------------------------------------------------------------------------------------------- +void PuzzleMainWindow::closeEvent(QCloseEvent *event) +{ +#if defined(Q_OS_MAC) && QT_VERSION < QT_VERSION_CHECK(5, 11, 1) + // Workaround for Qt bug https://bugreports.qt.io/browse/QTBUG-43344 + static int numCalled = 0; + if (numCalled++ >= 1) + { + return; + } +#endif + + if (MaybeSave()) + { + WriteSettings(); + event->accept(); + deleteLater(); + } + else + { + event->ignore(); + } +} + //--------------------------------------------------------------------------------------------------------------------- void PuzzleMainWindow::on_actionOpen_triggered() { diff --git a/src/app/puzzle/puzzlemainwindow.h b/src/app/puzzle/puzzlemainwindow.h index 4ffa3f29c..00abf3c7f 100644 --- a/src/app/puzzle/puzzlemainwindow.h +++ b/src/app/puzzle/puzzlemainwindow.h @@ -84,6 +84,8 @@ public slots: protected: enum { MaxRecentFiles = 5 }; + virtual void closeEvent(QCloseEvent *event) override; + private: Q_DISABLE_COPY(PuzzleMainWindow) Ui::PuzzleMainWindow *ui; @@ -188,6 +190,11 @@ private: */ void SetCheckBoxValue(QCheckBox *checkbox, bool value); + void ReadSettings(); + void WriteSettings(); + + bool MaybeSave(); + private slots: /** * @brief on_actionOpen_triggered When the menu action File > Open is