From ca7a1b7c81570fef68b2731e59d081a20fb694a1 Mon Sep 17 00:00:00 2001 From: RickyRister Date: Wed, 20 Nov 2024 19:36:59 -0800 Subject: [PATCH] refactor save confirmation window into function --- .../src/client/tabs/tab_deck_editor.cpp | 31 ++++++++++++------- cockatrice/src/client/tabs/tab_deck_editor.h | 2 ++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cockatrice/src/client/tabs/tab_deck_editor.cpp b/cockatrice/src/client/tabs/tab_deck_editor.cpp index ef9678d9f..1e2284186 100644 --- a/cockatrice/src/client/tabs/tab_deck_editor.cpp +++ b/cockatrice/src/client/tabs/tab_deck_editor.cpp @@ -748,9 +748,7 @@ bool TabDeckEditor::confirmClose() { if (modified) { tabSupervisor->setCurrentWidget(this); - QMessageBox::StandardButton ret = QMessageBox::warning( - this, tr("Are you sure?"), tr("The decklist has been modified.\nDo you want to save the changes?"), - QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); + int ret = createSaveConfirmationWindow()->exec(); if (ret == QMessageBox::Save) return actSaveDeck(); else if (ret == QMessageBox::Cancel) @@ -1020,18 +1018,14 @@ TabDeckEditor::DeckOpenLocation TabDeckEditor::confirmOpen(const bool openInSame // do the save confirmation dialogue tabSupervisor->setCurrentWidget(this); - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Warning); - msgBox.setWindowTitle(tr("Are you sure?")); - msgBox.setText(tr("The decklist has been modified.\nDo you want to save the changes?")); - msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); - QPushButton *newTabButton = msgBox.addButton(tr("Open in new tab"), QMessageBox::ApplyRole); + QMessageBox *msgBox = createSaveConfirmationWindow(); + QPushButton *newTabButton = msgBox->addButton(tr("Open in new tab"), QMessageBox::ApplyRole); - int ret = msgBox.exec(); + int ret = msgBox->exec(); // `exec()` returns an opaque value if a non-standard button was clicked. // Directly check if newTabButton was clicked before switching over the standard buttons. - if (msgBox.clickedButton() == newTabButton) { + if (msgBox->clickedButton() == newTabButton) { return NEW_TAB; } @@ -1045,6 +1039,21 @@ TabDeckEditor::DeckOpenLocation TabDeckEditor::confirmOpen(const bool openInSame } } +/** + * @brief Creates the base save confirmation dialogue box. + * + * @returns A QMessageBox that can be further modified + */ +QMessageBox *TabDeckEditor::createSaveConfirmationWindow() +{ + QMessageBox *msgBox = new QMessageBox(this); + msgBox->setIcon(QMessageBox::Warning); + msgBox->setWindowTitle(tr("Are you sure?")); + msgBox->setText(tr("The decklist has been modified.\nDo you want to save the changes?")); + msgBox->setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); + return msgBox; +} + /** * @brief Returns true if this tab is a blank newly opened tab, as if it was just created with the `New Deck` action. */ diff --git a/cockatrice/src/client/tabs/tab_deck_editor.h b/cockatrice/src/client/tabs/tab_deck_editor.h index efadf9c07..6aff1cc31 100644 --- a/cockatrice/src/client/tabs/tab_deck_editor.h +++ b/cockatrice/src/client/tabs/tab_deck_editor.h @@ -22,6 +22,7 @@ class Response; class FilterTreeModel; class FilterBuilder; class QGroupBox; +class QMessageBox; class QHBoxLayout; class QVBoxLayout; class QPushButton; @@ -111,6 +112,7 @@ private: }; DeckOpenLocation confirmOpen(const bool openInSameTabIfBlank = true); + QMessageBox *createSaveConfirmationWindow(); bool isBlankNewDeck() const; CardInfoPtr currentCardInfo() const;