diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp index ad3c6ad4f..21f628f9d 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.cpp @@ -209,18 +209,29 @@ InnerDecklistNode *DecklistNodeTree::getZoneObjFromName(const QString &zoneName) return new InnerDecklistNode(zoneName, root); } +InnerDecklistNode *DecklistNodeTree::findBoardZone(const QString &boardZoneName) const +{ + return dynamic_cast(root->findChild(boardZoneName)); +} + +InnerDecklistNode *DecklistNodeTree::findOrCreateBoardZone(const QString &boardZoneName) +{ + auto *boardZone = findBoardZone(boardZoneName); + if (!boardZone && + (boardZoneName == DECK_ZONE_MAYBEBOARD || boardZoneName == DECK_ZONE_MAIN || boardZoneName == DECK_ZONE_SIDE)) { + // The boards are lazy zones: they only exist once cards or custom zones need them. + boardZone = new InnerDecklistNode(boardZoneName, root); + } + return boardZone; +} + InnerDecklistNode *DecklistNodeTree::addCustomZone(const QString &boardZoneName, const QString &zoneName) { if (hasZoneName(zoneName)) { return nullptr; } - auto *boardZone = dynamic_cast(root->findChild(boardZoneName)); - if (!boardZone && - (boardZoneName == DECK_ZONE_MAYBEBOARD || boardZoneName == DECK_ZONE_MAIN || boardZoneName == DECK_ZONE_SIDE)) { - // The boards are lazy zones: they only exist once cards or custom zones need them. - boardZone = new InnerDecklistNode(boardZoneName, root); - } + auto *boardZone = findOrCreateBoardZone(boardZoneName); if (!boardZone) { return nullptr; @@ -256,12 +267,7 @@ bool DecklistNodeTree::moveCustomZone(const QString &zoneName, const QString &ne return true; } - auto *newBoardZone = dynamic_cast(root->findChild(newBoardZoneName)); - if (!newBoardZone && (newBoardZoneName == DECK_ZONE_MAYBEBOARD || newBoardZoneName == DECK_ZONE_MAIN || - newBoardZoneName == DECK_ZONE_SIDE)) { - // The boards are lazy zones: they only exist once cards or custom zones need them. - newBoardZone = new InnerDecklistNode(newBoardZoneName, root); - } + auto *newBoardZone = findOrCreateBoardZone(newBoardZoneName); if (!newBoardZone) { return false; } @@ -290,7 +296,7 @@ QList DecklistNodeTree::getCustomZones(const QString { QList result; - auto *boardZone = dynamic_cast(root->findChild(boardZoneName)); + auto *boardZone = findBoardZone(boardZoneName); if (!boardZone) { return result; } diff --git a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.h b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.h index 75325e1b0..1012d5919 100644 --- a/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.h +++ b/libcockatrice_deck_list/libcockatrice/deck_list/deck_list_node_tree.h @@ -125,6 +125,8 @@ public: private: // Helpers for traversing the tree InnerDecklistNode *getZoneObjFromName(const QString &zoneName) const; + InnerDecklistNode *findBoardZone(const QString &boardZoneName) const; + InnerDecklistNode *findOrCreateBoardZone(const QString &boardZoneName); InnerDecklistNode *findCustomZoneByName(const QString &zoneName) const; bool hasZoneName(const QString &zoneName) const; };