diff --git a/cockatrice/src/client/tabs/abstract_tab_deck_editor.cpp b/cockatrice/src/client/tabs/abstract_tab_deck_editor.cpp index 1cae30975..8d8926931 100644 --- a/cockatrice/src/client/tabs/abstract_tab_deck_editor.cpp +++ b/cockatrice/src/client/tabs/abstract_tab_deck_editor.cpp @@ -374,7 +374,7 @@ void AbstractTabDeckEditor::saveDeckRemoteFinished(const Response &response) void AbstractTabDeckEditor::actEditDeckInClipboard() { - DlgLoadDeckFromClipboard dlg(*getDeckList(), this); + DlgEditDeckInClipboard dlg(*getDeckList(), this); if (!dlg.exec()) return; diff --git a/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.cpp b/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.cpp index 609461604..23680dec5 100644 --- a/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.cpp +++ b/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.cpp @@ -2,6 +2,7 @@ #include "../deck/deck_loader.h" #include "../settings/cache_settings.h" +#include "dlg_settings.h" #include #include @@ -16,17 +17,17 @@ /** * Creates the main layout and connects the signals that are common to all versions of this window */ -void DlgLoadDeckFromClipboard::createMainLayout() +AbstractDlgDeckTextEdit::AbstractDlgDeckTextEdit(QWidget *parent) : QDialog(parent) { contentsEdit = new QPlainTextEdit; refreshButton = new QPushButton(tr("&Refresh")); - connect(refreshButton, SIGNAL(clicked()), this, SLOT(actRefresh())); + connect(refreshButton, &QPushButton::clicked, this, &AbstractDlgDeckTextEdit::actRefresh); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); buttonBox->addButton(refreshButton, QDialogButtonBox::ActionRole); - connect(buttonBox, SIGNAL(accepted()), this, SLOT(actOK())); - connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + connect(buttonBox, &QDialogButtonBox::accepted, this, &AbstractDlgDeckTextEdit::actOK); + connect(buttonBox, &QDialogButtonBox::rejected, this, &AbstractDlgDeckTextEdit::reject); loadSetNameAndNumberCheckBox = new QCheckBox(tr("Parse Set Name and Number (if available)")); loadSetNameAndNumberCheckBox->setChecked(true); @@ -43,21 +44,108 @@ void DlgLoadDeckFromClipboard::createMainLayout() resize(500, 500); - actRefresh(); - connect(&SettingsCache::instance().shortcuts(), SIGNAL(shortCutChanged()), this, SLOT(refreshShortcuts())); + connect(&SettingsCache::instance().shortcuts(), &ShortcutsSettings::shortCutChanged, this, + &AbstractDlgDeckTextEdit::refreshShortcuts); refreshShortcuts(); } +void AbstractDlgDeckTextEdit::refreshShortcuts() +{ + refreshButton->setShortcut( + SettingsCache::instance().shortcuts().getSingleShortcut("DlgLoadDeckFromClipboard/refreshButton")); +} + +/** + * Replaces the contents of the contentsEdit with the given text. + * @param text The text + */ +void AbstractDlgDeckTextEdit::setText(const QString &text) +{ + contentsEdit->setPlainText(text); +} + +/** + * Tries to load the current contents of the contentsEdit into the DeckLoader + * + * @param deckLoader The DeckLoader to load the deck into + * @return Whether the loading was successful + */ +bool AbstractDlgDeckTextEdit::loadIntoDeck(DeckLoader *deckLoader) const +{ + QString buffer = contentsEdit->toPlainText(); + + if (buffer.contains("")) { + return deckLoader->loadFromString_Native(buffer); + } + + QTextStream stream(&buffer); + + if (deckLoader->loadFromStream_Plain(stream)) { + if (loadSetNameAndNumberCheckBox->isChecked()) { + deckLoader->resolveSetNameAndNumberToProviderID(); + } else { + deckLoader->clearSetNamesAndNumbers(); + } + return true; + } + + return false; +} + +void AbstractDlgDeckTextEdit::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Return && event->modifiers() & Qt::ControlModifier) { + event->accept(); + actOK(); + return; + } + QDialog::keyPressEvent(event); +} + /** * Creates the dialog window for the "Load deck from clipboard" action * * @param parent The parent widget */ -DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(QWidget *parent) : QDialog(parent), deckList(nullptr) +DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(QWidget *parent) : AbstractDlgDeckTextEdit(parent), deckList(nullptr) { - createMainLayout(); - setWindowTitle(tr("Load deck from clipboard")); + + DlgLoadDeckFromClipboard::actRefresh(); +} + +void DlgLoadDeckFromClipboard::actRefresh() +{ + setText(QApplication::clipboard()->text()); +} + +void DlgLoadDeckFromClipboard::actOK() +{ + deckList = new DeckLoader; + deckList->setParent(this); + + if (loadIntoDeck(deckList)) { + accept(); + } else { + QMessageBox::critical(this, tr("Error"), tr("Invalid deck list.")); + } +} + +/** + * Creates the dialog window for the "Edit deck in clipboard" action + * + * @param deckList The existing deck in the deck editor. + * @param parent The parent widget + */ +DlgEditDeckInClipboard::DlgEditDeckInClipboard(const DeckLoader &deckList, QWidget *parent) + : AbstractDlgDeckTextEdit(parent) +{ + setWindowTitle(tr("Edit deck in clipboard")); + + deckLoader = new DeckLoader(deckList); + deckLoader->setParent(this); + + DlgEditDeckInClipboard::actRefresh(); } /** @@ -73,74 +161,16 @@ static QString deckListToString(const DeckLoader *deckList) return buffer; } -/** - * Creates the dialog window for the "Edit deck in clipboard" action - * - * @param deck The existing deck in the deck editor - * @param parent The parent widget - */ -DlgLoadDeckFromClipboard::DlgLoadDeckFromClipboard(const DeckLoader &deck, QWidget *parent) - : DlgLoadDeckFromClipboard(parent) +void DlgEditDeckInClipboard::actRefresh() { - createMainLayout(); - - setWindowTitle(tr("Edit deck in clipboard")); - - deckList = new DeckLoader(deck); - deckList->setParent(this); - - contentsEdit->setPlainText(deckListToString(deckList)); + setText(deckListToString(deckLoader)); } -void DlgLoadDeckFromClipboard::actRefresh() +void DlgEditDeckInClipboard::actOK() { - if (deckList) { - contentsEdit->setPlainText(deckListToString(deckList)); - } else { - contentsEdit->setPlainText(QApplication::clipboard()->text()); - } -} - -void DlgLoadDeckFromClipboard::refreshShortcuts() -{ - refreshButton->setShortcut( - SettingsCache::instance().shortcuts().getSingleShortcut("DlgLoadDeckFromClipboard/refreshButton")); -} - -void DlgLoadDeckFromClipboard::actOK() -{ - QString buffer = contentsEdit->toPlainText(); - QTextStream stream(&buffer); - - if (!deckList) { - deckList = new DeckLoader; - deckList->setParent(this); - } - - if (buffer.contains("")) { - if (deckList->loadFromString_Native(buffer)) { - accept(); - } else { - QMessageBox::critical(this, tr("Error"), tr("Invalid deck list.")); - } - } else if (deckList->loadFromStream_Plain(stream)) { - if (loadSetNameAndNumberCheckBox->isChecked()) { - deckList->resolveSetNameAndNumberToProviderID(); - } else { - deckList->clearSetNamesAndNumbers(); - } + if (loadIntoDeck(deckLoader)) { accept(); } else { QMessageBox::critical(this, tr("Error"), tr("Invalid deck list.")); } -} - -void DlgLoadDeckFromClipboard::keyPressEvent(QKeyEvent *event) -{ - if (event->key() == Qt::Key_Return && event->modifiers() & Qt::ControlModifier) { - event->accept(); - actOK(); - return; - } - QDialog::keyPressEvent(event); } \ No newline at end of file diff --git a/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.h b/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.h index ee447f4f1..3fdb1ac59 100644 --- a/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.h +++ b/cockatrice/src/dialogs/dlg_load_deck_from_clipboard.h @@ -9,31 +9,21 @@ class QPlainTextEdit; class QPushButton; /** - * Dialog window for actions that involve loading decks from text input. + * Base class for dialog windows for actions that involve loading decks from text input. */ -class DlgLoadDeckFromClipboard : public QDialog +class AbstractDlgDeckTextEdit : public QDialog { Q_OBJECT -private slots: - void actOK(); - void actRefresh(); - void refreshShortcuts(); - private: - /** - * before actOK has been called, null deckList indicates "load deck from clipboard" action, and nonnull deckList - * indicates "edit deck in clipboard" action - */ - DeckLoader *deckList; QPlainTextEdit *contentsEdit; QPushButton *refreshButton; QCheckBox *loadSetNameAndNumberCheckBox; - void createMainLayout(); +private slots: + void refreshShortcuts(); public: - explicit DlgLoadDeckFromClipboard(QWidget *parent = nullptr); - explicit DlgLoadDeckFromClipboard(const DeckLoader &deck, QWidget *parent = nullptr); + explicit AbstractDlgDeckTextEdit(QWidget *parent = nullptr); /** * Gets the loaded deck. Only call this method after this dialog window has been successfully exec'd. @@ -42,13 +32,60 @@ public: * to use it, since otherwise it will get destroyed once this dlg is destroyed * @return The DeckLoader */ - DeckLoader *getDeckList() const + virtual DeckLoader *getDeckList() const = 0; + +protected: + void setText(const QString &text); + bool loadIntoDeck(DeckLoader *deckLoader) const; + void keyPressEvent(QKeyEvent *event) override; + +protected slots: + virtual void actOK() = 0; + virtual void actRefresh() = 0; +}; + +/** + * Dialog window for the "Load deck from clipboard" action + */ +class DlgLoadDeckFromClipboard : public AbstractDlgDeckTextEdit +{ + Q_OBJECT +protected slots: + void actOK() override; + void actRefresh() override; + +private: + DeckLoader *deckList; + +public: + explicit DlgLoadDeckFromClipboard(QWidget *parent = nullptr); + + DeckLoader *getDeckList() const override { return deckList; } +}; -protected: - void keyPressEvent(QKeyEvent *event) override; +/** + * Dialog window for the "Edit deck in clipboard" action + */ +class DlgEditDeckInClipboard : public AbstractDlgDeckTextEdit +{ + Q_OBJECT +protected slots: + void actOK() override; + void actRefresh() override; + +private: + DeckLoader *deckLoader; + +public: + explicit DlgEditDeckInClipboard(const DeckLoader &deckList, QWidget *parent = nullptr); + + DeckLoader *getDeckList() const override + { + return deckLoader; + } }; #endif