From ea6d9366a39102a200ed7a2664fe960e5efe4a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 13 Apr 2026 20:58:59 +0200 Subject: [PATCH] Prototype intents Took 53 minutes Took 6 seconds --- cockatrice/CMakeLists.txt | 3 ++ cockatrice/src/interface/intents/intent.cpp | 1 + cockatrice/src/interface/intents/intent.h | 49 +++++++++++++++++++ .../intents/intent_open_local_deck.cpp | 1 + .../intents/intent_open_local_deck.h | 44 +++++++++++++++++ .../intents/intent_wait_for_database_load.cpp | 1 + .../intents/intent_wait_for_database_load.h | 29 +++++++++++ cockatrice/src/main.cpp | 15 ++---- 8 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 cockatrice/src/interface/intents/intent.cpp create mode 100644 cockatrice/src/interface/intents/intent.h create mode 100644 cockatrice/src/interface/intents/intent_open_local_deck.cpp create mode 100644 cockatrice/src/interface/intents/intent_open_local_deck.h create mode 100644 cockatrice/src/interface/intents/intent_wait_for_database_load.cpp create mode 100644 cockatrice/src/interface/intents/intent_wait_for_database_load.h diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 8e5fc5722..93f1fe1c5 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -125,6 +125,9 @@ set(cockatrice_SOURCES src/interface/card_picture_loader/card_picture_loader_worker.cpp src/interface/card_picture_loader/card_picture_loader_worker_work.cpp src/interface/card_picture_loader/card_picture_to_load.cpp + src/interface/intents/intent.cpp + src/interface/intents/intent_open_local_deck.cpp + src/interface/intents/intent_wait_for_database_load.cpp src/interface/layouts/flow_layout.cpp src/interface/layouts/overlap_layout.cpp src/interface/widgets/utility/line_edit_completer.cpp diff --git a/cockatrice/src/interface/intents/intent.cpp b/cockatrice/src/interface/intents/intent.cpp new file mode 100644 index 000000000..916af1649 --- /dev/null +++ b/cockatrice/src/interface/intents/intent.cpp @@ -0,0 +1 @@ +#include "intent.h" diff --git a/cockatrice/src/interface/intents/intent.h b/cockatrice/src/interface/intents/intent.h new file mode 100644 index 000000000..651946a14 --- /dev/null +++ b/cockatrice/src/interface/intents/intent.h @@ -0,0 +1,49 @@ +#ifndef COCKATRICE_INTENT_H +#define COCKATRICE_INTENT_H + +#include + +class Intent : public QObject +{ + Q_OBJECT + +public: + explicit Intent(QObject *parent = nullptr) : QObject(parent) + { + } + virtual ~Intent() = default; + + void execute() + { + if (checkPrecondition()) { + onPreconditionSatisfied(); + } else { + onPreconditionNotSatisfied(); + } + } + +signals: + void finished(); + void failed(QString reason); + +protected: + // --- Subclasses must implement these --- + virtual bool checkPrecondition() const = 0; + virtual void onPreconditionSatisfied() = 0; + virtual void onPreconditionNotSatisfied() = 0; + + // Helper to chain another intent + void runDependency(Intent *dependency) + { + connect(dependency, &Intent::finished, this, [this]() { + // Re-check after dependency finishes + this->execute(); + }); + + connect(dependency, &Intent::failed, this, &Intent::failed); + + dependency->execute(); + } +}; + +#endif // COCKATRICE_INTENT_H diff --git a/cockatrice/src/interface/intents/intent_open_local_deck.cpp b/cockatrice/src/interface/intents/intent_open_local_deck.cpp new file mode 100644 index 000000000..c4a0c81b8 --- /dev/null +++ b/cockatrice/src/interface/intents/intent_open_local_deck.cpp @@ -0,0 +1 @@ +#include "intent_open_local_deck.h" diff --git a/cockatrice/src/interface/intents/intent_open_local_deck.h b/cockatrice/src/interface/intents/intent_open_local_deck.h new file mode 100644 index 000000000..0c06f7078 --- /dev/null +++ b/cockatrice/src/interface/intents/intent_open_local_deck.h @@ -0,0 +1,44 @@ +#ifndef COCKATRICE_INTENT_OPEN_LOCAL_DECK_H +#define COCKATRICE_INTENT_OPEN_LOCAL_DECK_H +#include "../widgets/tabs/tab_supervisor.h" +#include "intent.h" +#include "intent_wait_for_database_load.h" +#include "libcockatrice/card/database/card_database_manager.h" + +class IntentOpenLocalDeck : public Intent +{ + Q_OBJECT + +public: + IntentOpenLocalDeck(TabSupervisor *_tabSupervisor, const QString &_file) + : Intent(), tabSupervisor(_tabSupervisor), file(_file) + { + } + +protected: + bool checkPrecondition() const override + { + return CardDatabaseManager::getInstance()->getLoadStatus() == LoadStatus::Ok; + } + + void onPreconditionSatisfied() override + { + std::optional deckOpt = + DeckLoader::loadFromFile(file, DeckFileFormat::getFormatFromName(file), true); + if (deckOpt) { + tabSupervisor->openDeckInNewTab(deckOpt.value()); + } + emit finished(); + } + + void onPreconditionNotSatisfied() override + { + runDependency(new IntentWaitForDatabaseLoad); + } + +private: + TabSupervisor *tabSupervisor; + const QString &file; +}; + +#endif // COCKATRICE_INTENT_OPEN_LOCAL_DECK_H diff --git a/cockatrice/src/interface/intents/intent_wait_for_database_load.cpp b/cockatrice/src/interface/intents/intent_wait_for_database_load.cpp new file mode 100644 index 000000000..dfdeab8b1 --- /dev/null +++ b/cockatrice/src/interface/intents/intent_wait_for_database_load.cpp @@ -0,0 +1 @@ +#include "intent_wait_for_database_load.h" diff --git a/cockatrice/src/interface/intents/intent_wait_for_database_load.h b/cockatrice/src/interface/intents/intent_wait_for_database_load.h new file mode 100644 index 000000000..51f4712a8 --- /dev/null +++ b/cockatrice/src/interface/intents/intent_wait_for_database_load.h @@ -0,0 +1,29 @@ +#ifndef COCKATRICE_INTENT_WAIT_FOR_DATABASE_LOAD_H +#define COCKATRICE_INTENT_WAIT_FOR_DATABASE_LOAD_H + +#include "intent.h" +#include "libcockatrice/card/database/card_database_manager.h" + +class IntentWaitForDatabaseLoad : public Intent +{ + Q_OBJECT + +protected: + bool checkPrecondition() const override + { + return CardDatabaseManager::getInstance()->getLoadStatus() == LoadStatus::Ok; + } + + void onPreconditionSatisfied() override + { + emit finished(); + } + + void onPreconditionNotSatisfied() override + { + connect(CardDatabaseManager::getInstance(), &CardDatabase::cardDatabaseLoadingFinished, this, + [this]() { emit finished(); }); + } +}; + +#endif // COCKATRICE_INTENT_WAIT_FOR_DATABASE_LOAD_H diff --git a/cockatrice/src/main.cpp b/cockatrice/src/main.cpp index 5df4bcf19..c385828fc 100644 --- a/cockatrice/src/main.cpp +++ b/cockatrice/src/main.cpp @@ -24,6 +24,7 @@ #include "client/settings/cache_settings.h" #include "client/sound_engine.h" #include "database/interface/settings_card_preference_provider.h" +#include "interface/intents/intent_open_local_deck.h" #include "interface/logger.h" #include "interface/pixel_map_generator.h" #include "interface/theme_manager.h" @@ -318,11 +319,8 @@ int main(int argc, char *argv[]) if (file.startsWith("cockatrice://")) { // ui.openUrl(QUrl(file)); } else if (QFileInfo(file).exists()) { - std::optional deckOpt = - DeckLoader::loadFromFile(file, DeckFileFormat::getFormatFromName(file), true); - if (deckOpt) { - ui.getTabSupervisor()->openDeckInNewTab(deckOpt.value()); - } + auto openDeckIntent = new IntentOpenLocalDeck(ui.getTabSupervisor(), file); + openDeckIntent->execute(); } } @@ -332,11 +330,8 @@ int main(int argc, char *argv[]) if (file.startsWith("cockatrice://")) { // ui.openUrl(QUrl(file)); } else if (QFileInfo(file).exists()) { - std::optional deckOpt = - DeckLoader::loadFromFile(file, DeckFileFormat::getFormatFromName(file), true); - if (deckOpt) { - ui.getTabSupervisor()->openDeckInNewTab(deckOpt.value()); - } + auto openDeckIntent = new IntentOpenLocalDeck(ui.getTabSupervisor(), file); + openDeckIntent->execute(); } } });