add "open in new tab" button to decklist confirmation dialogue (#5183)

* refactor to use confirmOpen

* implement extra button in confirmation

* use brackets in one-liner if statements

* refactor save confirmation window into function
This commit is contained in:
RickyRister 2024-11-21 14:24:50 -08:00 committed by GitHub
parent f73196841a
commit 1bc92623dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 12 deletions

View file

@ -748,9 +748,7 @@ bool TabDeckEditor::confirmClose()
{ {
if (modified) { if (modified) {
tabSupervisor->setCurrentWidget(this); tabSupervisor->setCurrentWidget(this);
QMessageBox::StandardButton ret = QMessageBox::warning( int ret = createSaveConfirmationWindow()->exec();
this, tr("Are you sure?"), tr("The decklist has been modified.\nDo you want to save the changes?"),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save) if (ret == QMessageBox::Save)
return actSaveDeck(); return actSaveDeck();
else if (ret == QMessageBox::Cancel) else if (ret == QMessageBox::Cancel)
@ -767,13 +765,16 @@ void TabDeckEditor::closeRequest()
void TabDeckEditor::actNewDeck() void TabDeckEditor::actNewDeck()
{ {
if (SettingsCache::instance().getOpenDeckInNewTab()) { auto deckOpenLocation = confirmOpen(false);
emit openDeckEditor(nullptr);
if (deckOpenLocation == CANCELLED) {
return; return;
} }
if (!confirmClose()) if (deckOpenLocation == NEW_TAB) {
emit openDeckEditor(nullptr);
return; return;
}
deckModel->cleanList(); deckModel->cleanList();
nameEdit->setText(QString()); nameEdit->setText(QString());
@ -785,10 +786,11 @@ 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"));
dialog.setDirectory(SettingsCache::instance().getDeckPath()); dialog.setDirectory(SettingsCache::instance().getDeckPath());
@ -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,17 @@ 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 +990,70 @@ 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 *msgBox = createSaveConfirmationWindow();
QPushButton *newTabButton = msgBox->addButton(tr("Open in new tab"), QMessageBox::ApplyRole);
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) {
return NEW_TAB;
}
switch (ret) {
case QMessageBox::Save:
return actSaveDeck() ? SAME_TAB : CANCELLED;
case QMessageBox::Discard:
return SAME_TAB;
default:
return CANCELLED;
}
}
/**
* @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. * @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

@ -22,6 +22,7 @@ class Response;
class FilterTreeModel; class FilterTreeModel;
class FilterBuilder; class FilterBuilder;
class QGroupBox; class QGroupBox;
class QMessageBox;
class QHBoxLayout; class QHBoxLayout;
class QVBoxLayout; class QVBoxLayout;
class QPushButton; class QPushButton;
@ -100,6 +101,19 @@ 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);
QMessageBox *createSaveConfirmationWindow();
bool isBlankNewDeck() const; bool isBlankNewDeck() const;
CardInfoPtr currentCardInfo() const; CardInfoPtr currentCardInfo() const;
void addCardHelper(QString zoneName); void addCardHelper(QString zoneName);