From dcc6461253a4b1f730fa218ffa55aee68cefb263 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Tue, 14 Aug 2007 12:18:31 +0200 Subject: [PATCH] Added action callbacks. --- src/actioncollection.cpp | 17 +++++++++++++++ src/actioncollection.h | 9 +++++++- src/hlvariant/chess/variant.cpp | 47 ++++++++++++++++++++++++++++++++--------- src/hlvariant/chess/variant.h | 14 ++++++++++-- src/hlvariant/tagua_wrapped.h | 6 +++--- src/tagua.h | 2 +- 6 files changed, 78 insertions(+), 17 deletions(-) diff --git a/src/actioncollection.cpp b/src/actioncollection.cpp index dc35b84..9e71ae7 100644 --- a/src/actioncollection.cpp +++ b/src/actioncollection.cpp @@ -1,12 +1,29 @@ #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)(); + } +} + + diff --git a/src/actioncollection.h b/src/actioncollection.h index 80cfd30..fe1aec6 100644 --- a/src/actioncollection.h +++ b/src/actioncollection.h @@ -1,19 +1,26 @@ #ifndef ACTIONCOLLECTION_H #define ACTIONCOLLECTION_H +#include #include +#include #include class QAction; class ActionCollection : public QObject { Q_OBJECT + typedef boost::function Callback; QList m_actions; + QHash m_callbacks; public: ActionCollection(); void add(QAction* action); - QList actions() const; + void add(QAction* action, const Callback& callback); + QList actions() const; +private Q_SLOTS: + void actionTriggered(); }; #endif // ACTIONCOLLECTION_H diff --git a/src/hlvariant/chess/variant.cpp b/src/hlvariant/chess/variant.cpp index 8e1a149..1f0047f 100644 --- a/src/hlvariant/chess/variant.cpp +++ b/src/hlvariant/chess/variant.cpp @@ -1,34 +1,61 @@ #include "variant.h" -#include "../tagua_wrapped.h" -#include "actioncollection.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"; -QAction* createAction(QActionGroup* group, ActionCollection* actions, - const KIcon& icon, const QString& text) { +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); - actions->add(action); return action; } void Variant::setupActions(ActionCollection* actions) { QActionGroup* group = new QActionGroup(actions); - QAction* queen = createAction(group, actions, KIcon("promoteQueen"), "Promote to queen"); - createAction(group, actions, KIcon("promoteRook"), "Promote to rook"); - createAction(group, actions, KIcon("promoteBishop"), "Promote to bishop"); - createAction(group, actions, KIcon("promoteKnight"), "Promote to knight"); - + 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::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 diff --git a/src/hlvariant/chess/variant.h b/src/hlvariant/chess/variant.h index bec0a35..b10b2ea 100644 --- a/src/hlvariant/chess/variant.h +++ b/src/hlvariant/chess/variant.h @@ -37,8 +37,18 @@ struct TAGUA_EXPORT Variant { static const char *m_theme_proxy; static int moveListLayout() { return 0; } - static OptList positionOptions() { return OptList(); } - static void setupActions(ActionCollection*); + + OptList positionOptions() const { return OptList(); } + void setupActions(ActionCollection*); + + Variant(); +private: + GameState::Board::Piece::Type m_promotion; + + void promote_to_queen(); + void promote_to_rook(); + void promote_to_bishop(); + void promote_to_knight(); }; } // namespace Chess diff --git a/src/hlvariant/tagua_wrapped.h b/src/hlvariant/tagua_wrapped.h index 5c8d7d6..180948d 100644 --- a/src/hlvariant/tagua_wrapped.h +++ b/src/hlvariant/tagua_wrapped.h @@ -506,15 +506,15 @@ namespace HLVariant { } virtual OptList positionOptions() const { - return Variant::positionOptions(); + return m_variant.positionOptions(); } virtual ICSAPIPtr icsAPI() const { return ICSAPIPtr(ReturnICSAPI::apply()); } - virtual void setupActions(ActionCollection* c) const { - Variant::setupActions(c); + virtual void setupActions(ActionCollection* c) { + m_variant.setupActions(c); } }; diff --git a/src/tagua.h b/src/tagua.h index a64de41..4c5ec87 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*) const = 0; + virtual void setupActions(ActionCollection*) = 0; }; -- 2.11.4.GIT