refactor to use confirmOpen

This commit is contained in:
RickyRister 2024-11-20 00:22:52 -08:00
parent f73196841a
commit 013478574d
2 changed files with 65 additions and 10 deletions

View file

@ -767,14 +767,16 @@ void TabDeckEditor::closeRequest()
void TabDeckEditor::actNewDeck() void TabDeckEditor::actNewDeck()
{ {
if (SettingsCache::instance().getOpenDeckInNewTab()) { auto deckOpenLocation = confirmOpen(false);
if (deckOpenLocation == CANCELLED)
return;
if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(nullptr); emit openDeckEditor(nullptr);
return; return;
} }
if (!confirmClose())
return;
deckModel->cleanList(); deckModel->cleanList();
nameEdit->setText(QString()); nameEdit->setText(QString());
commentsEdit->setText(QString()); commentsEdit->setText(QString());
@ -785,9 +787,9 @@ void TabDeckEditor::actNewDeck()
void TabDeckEditor::actLoadDeck() void TabDeckEditor::actLoadDeck()
{ {
bool openInNewTab = SettingsCache::instance().getOpenDeckInNewTab() && !isBlankNewDeck(); auto deckOpenLocation = confirmOpen();
if (!openInNewTab && !confirmClose()) if (deckOpenLocation == CANCELLED)
return; return;
QFileDialog dialog(this, tr("Load deck")); QFileDialog dialog(this, tr("Load deck"));
@ -801,7 +803,7 @@ void TabDeckEditor::actLoadDeck()
auto *l = new DeckLoader; auto *l = new DeckLoader;
if (l->loadFromFile(fileName, fmt)) { if (l->loadFromFile(fileName, fmt)) {
if (openInNewTab) { if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(l); emit openDeckEditor(l);
} else { } else {
setSaveStatus(false); setSaveStatus(false);
@ -878,16 +880,16 @@ bool TabDeckEditor::actSaveDeckAs()
void TabDeckEditor::actLoadDeckFromClipboard() void TabDeckEditor::actLoadDeckFromClipboard()
{ {
bool openInNewTab = SettingsCache::instance().getOpenDeckInNewTab() && !isBlankNewDeck(); auto deckOpenLocation = confirmOpen();
if (!openInNewTab && !confirmClose()) if (deckOpenLocation == CANCELLED)
return; return;
DlgLoadDeckFromClipboard dlg(this); DlgLoadDeckFromClipboard dlg(this);
if (!dlg.exec()) if (!dlg.exec())
return; return;
if (openInNewTab) { if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(dlg.getDeckList()); emit openDeckEditor(dlg.getDeckList());
} else { } else {
setDeck(dlg.getDeckList()); setDeck(dlg.getDeckList());
@ -987,6 +989,47 @@ void TabDeckEditor::recursiveExpand(const QModelIndex &index)
deckView->expand(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. * @brief Returns true if this tab is a blank newly opened tab, as if it was just created with the `New Deck` action.
*/ */

View file

@ -100,6 +100,18 @@ private slots:
void showSearchSyntaxHelp(); void showSearchSyntaxHelp();
private: 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; bool isBlankNewDeck() const;
CardInfoPtr currentCardInfo() const; CardInfoPtr currentCardInfo() const;
void addCardHelper(QString zoneName); void addCardHelper(QString zoneName);