From 38f7df72fb7f395504dd84cf05021ddf9a241f2b Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Sun, 22 Mar 2020 17:28:36 +0200 Subject: [PATCH] get userfeedback for saving and switching tabs --- app/settings/settingsdialog.cpp | 77 +++++++++++++++++++++++++++++---- app/settings/settingsdialog.h | 9 +++- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/app/settings/settingsdialog.cpp b/app/settings/settingsdialog.cpp index 9db89ea7b..239e4a62d 100644 --- a/app/settings/settingsdialog.cpp +++ b/app/settings/settingsdialog.cpp @@ -185,8 +185,12 @@ void SettingsDialog::initFileMenu() //! triggers connect(quitAction, &QAction::triggered, this, [&]() { - close(); - m_corona->quitApplication(); + bool accepted = saveChanges(); + + if (accepted) { + close(); + m_corona->quitApplication(); + } }); } @@ -443,6 +447,15 @@ void SettingsDialog::accept() qDebug() << Q_FUNC_INFO; } +void SettingsDialog::reject() +{ + bool accepted = saveChanges(); + + if (accepted) { + QDialog::reject(); + } +} + void SettingsDialog::apply() { qDebug() << Q_FUNC_INFO; @@ -514,19 +527,65 @@ void SettingsDialog::updateApplyButtonsState() } } +int SettingsDialog::saveChangesConfirmation() +{ + auto msg = new QMessageBox(this); + msg->setIcon(QMessageBox::Warning); + msg->setWindowTitle(i18n("Apply Settings")); + QString tabName = m_ui->tabWidget->tabBar()->tabText(m_acceptedPage).remove("&"); + msg->setText(i18n("The settings of %0 tab have changed. Do you want to apply the changes or discard them?").arg(tabName)); + msg->setStandardButtons(QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Cancel); + msg->setDefaultButton(QMessageBox::Apply); + + connect(msg, &QFileDialog::finished, msg, &QFileDialog::deleteLater); + return msg->exec(); +} + +bool SettingsDialog::saveChanges() +{ + if ((m_acceptedPage == Latte::Types::LayoutPage && m_tabLayoutsHandler->dataAreChanged()) + || (m_acceptedPage == Latte::Types::PreferencesPage && m_tabPreferencesHandler->dataAreChanged())) { + int result = saveChangesConfirmation(); + + if (result == QMessageBox::Apply) { + save(); + } else if (result == QMessageBox::Discard) { + reset(); + } else { + return false; + } + } + + return true; +} + void SettingsDialog::on_currentTabChanged(int index) { - if ((m_currentAcceptedPage == Latte::Types::LayoutPage && m_tabLayoutsHandler->dataAreChanged()) - || (m_currentAcceptedPage == Latte::Types::PreferencesPage && m_tabPreferencesHandler->dataAreChanged())) { + //! Before switching into a new tab the user must confirm first if the data should be saved or not + + if ((m_acceptedPage == Latte::Types::LayoutPage && m_tabLayoutsHandler->dataAreChanged()) + || (m_acceptedPage == Latte::Types::PreferencesPage && m_tabPreferencesHandler->dataAreChanged())) { - if (index != m_currentAcceptedPage) { - setCurrentPage(m_currentAcceptedPage); + if (index != m_acceptedPage) { + m_nextPage = index; + setCurrentPage(m_acceptedPage); + return; } - return; + bool approvedNext = saveChanges(); + + if (!approvedNext) { + m_nextPage = m_acceptedPage; + return; + } + } else { + m_nextPage = index; } - m_currentAcceptedPage = index; + m_acceptedPage = m_nextPage >= 0 ? m_nextPage : index/*initialize*/; + m_nextPage = m_acceptedPage; + + setCurrentPage(m_acceptedPage); updateApplyButtonsState(); } @@ -639,6 +698,8 @@ void SettingsDialog::updateWindowActivities() void SettingsDialog::save() { + qDebug() << Q_FUNC_INFO; + if (currentPage() == Latte::Types::LayoutPage) { m_tabLayoutsHandler->save(); } else if (currentPage() == Latte::Types::PreferencesPage) { diff --git a/app/settings/settingsdialog.h b/app/settings/settingsdialog.h index 38c8e0e2f..2057525e0 100644 --- a/app/settings/settingsdialog.h +++ b/app/settings/settingsdialog.h @@ -100,6 +100,7 @@ private slots: void on_export_fullconfiguration(); void accept() override; + void reject() override; void apply(); void reset(); @@ -125,6 +126,9 @@ private: void save(); void setCurrentFreeActivitiesLayout(const int &row); + int saveChangesConfirmation(); + bool saveChanges(); + QSize storedWindowSize() const; private: @@ -142,7 +146,10 @@ private: //! Global menu QMenuBar *m_globalMenuBar{nullptr}; - int m_currentAcceptedPage{-1}; + //! are used for confirmation purposes, the user can choose to save or discard settings and + //! change its current tab also + int m_acceptedPage{-1}; + int m_nextPage{-1}; //! File menu actions QMenu *m_fileMenu{nullptr};