From 948d1922047e9ddf47954e6f1f635fdc5ac91103 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Tue, 14 Aug 2007 15:59:01 +0200 Subject: [PATCH] Factored out action code in the Action class. --- src/CMakeLists.txt | 1 + src/actioncollection.cpp | 44 ++++++--------- src/actioncollection.h | 5 -- src/controllers/abstract.cpp | 4 -- src/controllers/abstract.h | 3 +- src/controllers/editgame.cpp | 12 ++--- src/controllers/editgame.h | 1 + src/hlvariant/chess/actions.cpp | 74 +++++++++++++++++++++++++ src/hlvariant/chess/actions.h | 37 +++++++++++++ src/hlvariant/chess/variant.cpp | 98 +++++++++++----------------------- src/hlvariant/chess/variant.h | 13 ++--- src/hlvariant/crazyhouse/movefactory.h | 10 ++++ src/hlvariant/tagua_wrapped.h | 4 +- src/tagua.h | 2 +- 14 files changed, 182 insertions(+), 126 deletions(-) rewrite src/actioncollection.cpp (61%) create mode 100644 src/hlvariant/chess/actions.cpp create mode 100644 src/hlvariant/chess/actions.h rewrite src/hlvariant/chess/variant.cpp (78%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d4db3ef..3f461f2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ set(tagua_SRC hlvariant/chess/move.cpp hlvariant/chess/gamestate.cpp hlvariant/chess/piece.cpp + hlvariant/chess/actions.cpp hlvariant/dummy/variant.cpp diff --git a/src/actioncollection.cpp b/src/actioncollection.cpp dissimilarity index 61% index 9e71ae7..78479dd 100644 --- a/src/actioncollection.cpp +++ b/src/actioncollection.cpp @@ -1,29 +1,15 @@ -#include "actioncollection.h" - -#include - -ActionCollection::ActionCollection() { } - -void ActionCollection::add(QAction* action) { - m_actions.append(action); -} - -void ActionCollection::add(QAction* action, const Callback& callback) { - add(action); - m_callbacks.insert(action, callback); - connect(action, SIGNAL(triggered()), this, SLOT(actionTriggered())); -} - -QList ActionCollection::actions() const { - return m_actions; -} - -void ActionCollection::actionTriggered() { - QAction* action = qobject_cast(sender()); - - if (action && m_callbacks.contains(action)) { - m_callbacks.value(action)(); - } -} - - +#include "actioncollection.h" + +#include + +ActionCollection::ActionCollection() { } + +void ActionCollection::add(QAction* action) { + m_actions.append(action); +} + +QList ActionCollection::actions() const { + return m_actions; +} + + diff --git a/src/actioncollection.h b/src/actioncollection.h index fe1aec6..40f2f64 100644 --- a/src/actioncollection.h +++ b/src/actioncollection.h @@ -10,17 +10,12 @@ class QAction; class ActionCollection : public QObject { Q_OBJECT - typedef boost::function Callback; QList m_actions; - QHash m_callbacks; public: ActionCollection(); void add(QAction* action); - void add(QAction* action, const Callback& callback); QList actions() const; -private Q_SLOTS: - void actionTriggered(); }; #endif // ACTIONCOLLECTION_H diff --git a/src/controllers/abstract.cpp b/src/controllers/abstract.cpp index dabfd90..135cfdf 100644 --- a/src/controllers/abstract.cpp +++ b/src/controllers/abstract.cpp @@ -16,10 +16,6 @@ Controller::Controller(ChessTable* view) Controller::~Controller() { } -ActionCollection* Controller::variantActions() const { - return m_variant_actions; -} - bool Controller::undo() { return entity()->undo(); } diff --git a/src/controllers/abstract.h b/src/controllers/abstract.h index bc97701..1e9d857 100644 --- a/src/controllers/abstract.h +++ b/src/controllers/abstract.h @@ -34,7 +34,6 @@ class ActionCollection; class Controller : public boost::enable_shared_from_this { protected: ChessTable* m_view; - ActionCollection* m_variant_actions; virtual boost::shared_ptr entity() const = 0; public: Controller(ChessTable* view); @@ -43,7 +42,7 @@ public: /** * Variant specific actions. */ - ActionCollection* variantActions() const; + virtual ActionCollection* variantActions() const = 0; /** * Variant associated to the controller. diff --git a/src/controllers/editgame.cpp b/src/controllers/editgame.cpp index 82388ea..010995d 100644 --- a/src/controllers/editgame.cpp +++ b/src/controllers/editgame.cpp @@ -97,9 +97,7 @@ EditGameController::EditGameController(ChessTable* view, init(AbstractPosition::Ptr()); } -EditGameController::~EditGameController() { - delete m_variant_actions; -} +EditGameController::~EditGameController() { } void EditGameController::init(AbstractPosition::Ptr startingPosition) { m_players[0] = m_entity; @@ -119,10 +117,10 @@ void EditGameController::init(AbstractPosition::Ptr startingPosition) { // add clock update agent m_agents.addAgent(m_update_agent); - - // setup variant actions - m_variant_actions = new ActionCollection; - m_variant->setupActions(m_variant_actions); +} + +ActionCollection* EditGameController::variantActions() const { + return m_variant->actions(); } QString EditGameController::variant() const { diff --git a/src/controllers/editgame.h b/src/controllers/editgame.h index 3567665..d361a71 100644 --- a/src/controllers/editgame.h +++ b/src/controllers/editgame.h @@ -55,6 +55,7 @@ public: AbstractPosition::Ptr startingPos = AbstractPosition::Ptr()); ~EditGameController(); + virtual ActionCollection* variantActions() const; virtual QString variant() const; EntityToken addPlayingEngine(int side, const boost::shared_ptr& engine); // EntityToken addAnalysingEngine(const boost::shared_ptr& engine); diff --git a/src/hlvariant/chess/actions.cpp b/src/hlvariant/chess/actions.cpp new file mode 100644 index 0000000..f3125db --- /dev/null +++ b/src/hlvariant/chess/actions.cpp @@ -0,0 +1,74 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include "actions.h" +#include "../variantdata.h" +#include "variant.h" + +#include + +#include +#include + +QAction* createAction(QActionGroup* group, const KIcon& icon, const QString& text) { + QAction* action = new KAction(icon, text, group); + action->setCheckable(true); + group->addAction(action); + return action; +} + +namespace HLVariant { +namespace Chess { + +Actions::Actions() +: m_promotion(VariantData::Piece::QUEEN) { + QActionGroup* group = new QActionGroup(this); + + QAction* queen = createAction(group, KIcon("promoteQueen"), "Promote to queen"); + queen->setChecked(true); + add(queen); + connect(queen, SIGNAL(triggered()), this, SLOT(promote_to_queen())); + + QAction* rook = createAction(group, KIcon("promoteRook"), "Promote to rook"); + add(rook); + connect(rook, SIGNAL(triggered()), this, SLOT(promote_to_rook())); + + QAction* bishop = createAction(group, KIcon("promoteBishop"), "Promote to bishop"); + add(bishop); + connect(bishop, SIGNAL(triggered()), this, SLOT(promote_to_bishop())); + + QAction* knight = createAction(group, KIcon("promoteKnight"), "Promote to knight"); + add(knight); + connect(knight, SIGNAL(triggered()), this, SLOT(promote_to_knight())); +} + +int Actions::promotion() const { + return m_promotion; +} + +void Actions::promote_to_queen() { + m_promotion = VariantData::Piece::QUEEN; +} + +void Actions::promote_to_rook() { + m_promotion = VariantData::Piece::ROOK; +} + +void Actions::promote_to_bishop() { + m_promotion = VariantData::Piece::BISHOP; +} + +void Actions::promote_to_knight() { + m_promotion = VariantData::Piece::KNIGHT; +} + +} +} + diff --git a/src/hlvariant/chess/actions.h b/src/hlvariant/chess/actions.h new file mode 100644 index 0000000..46e608d --- /dev/null +++ b/src/hlvariant/chess/actions.h @@ -0,0 +1,37 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef HLVARIANT__CHESS__ACTIONS_H +#define HLVARIANT__CHESS__ACTIONS_H + +#include "actioncollection.h" + +namespace HLVariant { +namespace Chess { + +class Actions : public ActionCollection { +Q_OBJECT + int m_promotion; +public: + Actions(); + + int promotion() const; +private Q_SLOTS: + virtual void promote_to_queen(); + virtual void promote_to_rook(); + virtual void promote_to_bishop(); + virtual void promote_to_knight(); +}; + +} // namespace Chess +} // namespace HLVariant + +#endif // HLVARIANT__CHESS__ACTIONS_H + diff --git a/src/hlvariant/chess/variant.cpp b/src/hlvariant/chess/variant.cpp dissimilarity index 78% index 083df7f..03edd20 100644 --- a/src/hlvariant/chess/variant.cpp +++ b/src/hlvariant/chess/variant.cpp @@ -1,68 +1,30 @@ -#include "variant.h" - -#include -#include - -#include -#include - -#include "../tagua_wrapped.h" -#include "actioncollection.h" - -namespace HLVariant { -namespace Chess { - -const char* Variant::m_name = "Chess"; -const char* Variant::m_theme_proxy = "Chess"; - -Variant::Variant() -: m_promotion(GameState::Board::Piece::QUEEN) { } - -QAction* createAction(QActionGroup* group, const KIcon& icon, const QString& text) { - QAction* action = new KAction(icon, text, group); - action->setCheckable(true); - group->addAction(action); - return action; -} - -void Variant::setupActions(ActionCollection* actions) { - QActionGroup* group = new QActionGroup(actions); - - QAction* queen = createAction(group, KIcon("promoteQueen"), "Promote to queen"); - queen->setChecked(true); - actions->add(queen, boost::bind(std::mem_fun(&Variant::promote_to_queen), this)); - - QAction* rook = createAction(group, KIcon("promoteRook"), "Promote to rook"); - actions->add(rook, boost::bind(std::mem_fun(&Variant::promote_to_rook), this)); - - QAction* bishop = createAction(group, KIcon("promoteBishop"), "Promote to bishop"); - actions->add(bishop, boost::bind(std::mem_fun(&Variant::promote_to_bishop), this)); - - QAction* knight = createAction(group, KIcon("promoteKnight"), "Promote to knight"); - actions->add(knight, boost::bind(std::mem_fun(&Variant::promote_to_knight), this)); -} - -void Variant::setupMove(NormalUserMove& move) const { - move.promotionType = m_promotion; -} - -void Variant::promote_to_queen() { - m_promotion = GameState::Board::Piece::QUEEN; -} - -void Variant::promote_to_rook() { - m_promotion = GameState::Board::Piece::ROOK; -} - -void Variant::promote_to_bishop() { - m_promotion = GameState::Board::Piece::BISHOP; -} - -void Variant::promote_to_knight() { - m_promotion = GameState::Board::Piece::KNIGHT; -} - -} // namespace Chess -} // namespace HLVariant - - +#include "variant.h" + +#include +#include + +#include +#include + +#include "../tagua_wrapped.h" +#include "actioncollection.h" + +namespace HLVariant { +namespace Chess { + +const char* Variant::m_name = "Chess"; +const char* Variant::m_theme_proxy = "Chess"; + +void Variant::setupMove(NormalUserMove& move) const { + move.promotionType = m_actions.promotion(); +} + +ActionCollection* Variant::actions() { + return &m_actions; +} + + +} // namespace Chess +} // namespace HLVariant + + diff --git a/src/hlvariant/chess/variant.h b/src/hlvariant/chess/variant.h index 058551f..844a1e0 100644 --- a/src/hlvariant/chess/variant.h +++ b/src/hlvariant/chess/variant.h @@ -19,6 +19,9 @@ #include "option.h" #include "../animator.h" #include "../movefactory.h" +#include "actions.h" + +class ActionCollection; namespace HLVariant { namespace Chess { @@ -39,17 +42,11 @@ struct TAGUA_EXPORT Variant { static int moveListLayout() { return 0; } OptList positionOptions() const { return OptList(); } - void setupActions(ActionCollection*); void setupMove(NormalUserMove& m) const; - Variant(); + ActionCollection* actions(); private: - GameState::Board::Piece::Type m_promotion; - - void promote_to_queen(); - void promote_to_rook(); - void promote_to_bishop(); - void promote_to_knight(); + Actions m_actions; }; } // namespace Chess diff --git a/src/hlvariant/crazyhouse/movefactory.h b/src/hlvariant/crazyhouse/movefactory.h index 7297b50..0f2a225 100644 --- a/src/hlvariant/crazyhouse/movefactory.h +++ b/src/hlvariant/crazyhouse/movefactory.h @@ -1,3 +1,13 @@ +/* + Copyright (c) 2006-2007 Paolo Capriotti + (c) 2006-2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #ifndef HLVARIANT__CRAZYHOUSE__MOVEFACTORY_H #define HLVARIANT__CRAZYHOUSE__MOVEFACTORY_H diff --git a/src/hlvariant/tagua_wrapped.h b/src/hlvariant/tagua_wrapped.h index 78b3e7d..9911985 100644 --- a/src/hlvariant/tagua_wrapped.h +++ b/src/hlvariant/tagua_wrapped.h @@ -513,8 +513,8 @@ namespace HLVariant { return ICSAPIPtr(ReturnICSAPI::apply()); } - virtual void setupActions(ActionCollection* c) { - m_variant.setupActions(c); + virtual ActionCollection* actions() { + return m_variant.actions(); } virtual void setupMove(NormalUserMove& m) const { diff --git a/src/tagua.h b/src/tagua.h index c7f9177..ec164b7 100644 --- a/src/tagua.h +++ b/src/tagua.h @@ -391,7 +391,7 @@ public: * Setup a list of variant specific actions to be displayed on a game toolbar * and menu. */ - virtual void setupActions(ActionCollection*) = 0; + virtual ActionCollection* actions() = 0; /** * Add variant specific information (i.e. promotionType) to a move. -- 2.11.4.GIT