From 013478574d2153335aef3bbe8af8f5327d19ec1e Mon Sep 17 00:00:00 2001 From: RickyRister Date: Wed, 20 Nov 2024 00:22:52 -0800 Subject: [PATCH] refactor to use confirmOpen --- .../src/client/tabs/tab_deck_editor.cpp | 63 ++++++++++++++++--- cockatrice/src/client/tabs/tab_deck_editor.h | 12 ++++ 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/cockatrice/src/client/tabs/tab_deck_editor.cpp b/cockatrice/src/client/tabs/tab_deck_editor.cpp index 0c9edbb0b..de024749f 100644 --- a/cockatrice/src/client/tabs/tab_deck_editor.cpp +++ b/cockatrice/src/client/tabs/tab_deck_editor.cpp @@ -767,14 +767,16 @@ void TabDeckEditor::closeRequest() void TabDeckEditor::actNewDeck() { - if (SettingsCache::instance().getOpenDeckInNewTab()) { + auto deckOpenLocation = confirmOpen(false); + + if (deckOpenLocation == CANCELLED) + return; + + if (deckOpenLocation == NEW_TAB) { emit openDeckEditor(nullptr); return; } - if (!confirmClose()) - return; - deckModel->cleanList(); nameEdit->setText(QString()); commentsEdit->setText(QString()); @@ -785,9 +787,9 @@ void TabDeckEditor::actNewDeck() void TabDeckEditor::actLoadDeck() { - bool openInNewTab = SettingsCache::instance().getOpenDeckInNewTab() && !isBlankNewDeck(); + auto deckOpenLocation = confirmOpen(); - if (!openInNewTab && !confirmClose()) + if (deckOpenLocation == CANCELLED) return; QFileDialog dialog(this, tr("Load deck")); @@ -801,7 +803,7 @@ void TabDeckEditor::actLoadDeck() auto *l = new DeckLoader; if (l->loadFromFile(fileName, fmt)) { - if (openInNewTab) { + if (deckOpenLocation == NEW_TAB) { emit openDeckEditor(l); } else { setSaveStatus(false); @@ -878,16 +880,16 @@ bool TabDeckEditor::actSaveDeckAs() void TabDeckEditor::actLoadDeckFromClipboard() { - bool openInNewTab = SettingsCache::instance().getOpenDeckInNewTab() && !isBlankNewDeck(); + auto deckOpenLocation = confirmOpen(); - if (!openInNewTab && !confirmClose()) + if (deckOpenLocation == CANCELLED) return; DlgLoadDeckFromClipboard dlg(this); if (!dlg.exec()) return; - if (openInNewTab) { + if (deckOpenLocation == NEW_TAB) { emit openDeckEditor(dlg.getDeckList()); } else { setDeck(dlg.getDeckList()); @@ -987,6 +989,47 @@ void TabDeckEditor::recursiveExpand(const QModelIndex &index) deckView->expand(index); } +/** + * @brief Displays the save confirmation dialogue that is shown before loading a deck, if required. Takes into + * account the `openDeckInNewTab` settting. + * + * @param openInSameTabIfBlank Open the deck in the same tab instead of a new tab if the current tab is completely + * blank. Only relevant when the `openDeckInNewTab` setting is enabled. + * + * @returns An enum that indicates if and where to load the deck + */ +TabDeckEditor::DeckOpenLocation TabDeckEditor::confirmOpen(const bool openInSameTabIfBlank) +{ + // handle `openDeckInNewTab` setting + if (SettingsCache::instance().getOpenDeckInNewTab()) { + if (openInSameTabIfBlank && isBlankNewDeck()) { + return SAME_TAB; + } else { + return NEW_TAB; + } + } + + // early return if deck is unmodified + if (!modified) { + return SAME_TAB; + } + + // do the save confirmation dialogue + 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); + + switch (ret) { + case QMessageBox::Save: + return actSaveDeck() ? SAME_TAB : CANCELLED; + case QMessageBox::Discard: + return SAME_TAB; + default: + return CANCELLED; + } +} + /** * @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 b8c45c110..efadf9c07 100644 --- a/cockatrice/src/client/tabs/tab_deck_editor.h +++ b/cockatrice/src/client/tabs/tab_deck_editor.h @@ -100,6 +100,18 @@ private slots: void showSearchSyntaxHelp(); private: + /** + * @brief Which tab to open the new deck in + */ + enum DeckOpenLocation + { + CANCELLED, + SAME_TAB, + NEW_TAB + }; + + DeckOpenLocation confirmOpen(const bool openInSameTabIfBlank = true); + bool isBlankNewDeck() const; CardInfoPtr currentCardInfo() const; void addCardHelper(QString zoneName);