2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include <QApplication>
13 #include <KActionCollection>
17 #include "controllers/abstract.h"
18 #include "graphicalgame.h"
20 using namespace boost
;
22 #define SYNC_ACTION(NAME, ENUM) \
23 m_ui->m_actions->action(NAME)->setEnabled(state & GraphicalGame::ENUM)
24 class UIActionStateObserver
: public ActionStateObserver
{
26 Controller
* m_controller
;
28 UIActionStateObserver(const UI
* ui
, const shared_ptr
<Controller
>& controller
)
30 , m_controller(controller
.get()) { }
32 virtual void notifyActionStateChange(GraphicalGame::ActionState state
) {
33 // check that the associated controller is active
34 if (m_ui
->controller().get() == m_controller
) {
35 SYNC_ACTION("begin", BEGIN
);
36 SYNC_ACTION("back", BACK
);
37 SYNC_ACTION("forward", FORWARD
);
38 SYNC_ACTION("end", END
);
39 SYNC_ACTION("edit_undo", UNDO
);
40 SYNC_ACTION("edit_redo", REDO
);
46 UI::UI(KActionCollection
* actions
)
48 , m_actions(actions
) {
51 boost::shared_ptr
<Controller
>& UI::controller() {
52 Q_ASSERT(m_current_tab
);
54 boost::shared_ptr
<Controller
>& res
= m_controller
[m_current_tab
];
60 boost::shared_ptr
<Controller
> UI::controller() const {
61 Q_ASSERT(m_current_tab
);
63 ControllerMap::const_iterator it
= m_controller
.find(m_current_tab
);
64 Q_ASSERT(it
!= m_controller
.end());
68 void UI::addController(QWidget
* w
, const shared_ptr
<Controller
>& controller
) {
70 setController(controller
);
73 void UI::setController(const shared_ptr
<Controller
>& controller
) {
74 Q_ASSERT(m_current_tab
);
75 m_controller
[m_current_tab
] = controller
;
76 controller
->setUI(*this);
79 void UI::removeController(QWidget
* w
) {
81 kDebug() << "removing controller " << w
;
82 m_controller
.erase(w
);
85 void UI::setCurrentTab(QWidget
* w
) {
87 controller()->activate();
100 bool UI::truncate() {
101 controller()->truncate();
105 bool UI::promoteVariation() {
106 controller()->promoteVariation();
111 return controller()->back();
115 return controller()->forward();
118 void UI::gotoFirst() {
119 controller()->gotoFirst();
122 void UI::gotoLast() {
123 controller()->gotoLast();
127 QClipboard
* cb
= QApplication::clipboard();
128 cb
->setText(currentPGN());
131 QString
UI::currentPGN() {
132 return controller()->save();
135 void UI::pgnPaste() {
136 QClipboard
* cb
= QApplication::clipboard();
137 pgnPaste(cb
->text());
140 void UI::pgnPaste(const QString
&) {
141 //controller()->loadPGN(pgn);
144 void UI::clearBoard() {
145 controller()->clearBoard();
148 void UI::setStartingPosition() {
149 controller()->setStartingPosition();
152 void UI::copyPosition() {
153 QClipboard
* cb
= QApplication::clipboard();
154 cb
->setText(controller()->fen());
157 void UI::pastePosition() {
158 QClipboard
* cb
= QApplication::clipboard();
159 controller()->setFEN(cb
->text());
162 void UI::setTurn(int turn
) {
163 controller()->setTurn(turn
);
166 StatePtr
UI::position() const {
167 return controller()->currentPosition();
170 void UI::createCtrlAction() {
171 controller()->createCtrlAction();
174 void UI::destroyCtrlAction() {
175 controller()->destroyCtrlAction();
178 ActionCollection
* UI::variantActions() const {
179 return controller()->variantActions();
182 EntityToken
UI::addPlayingEngine(const IColor
* side
, const shared_ptr
<Engine
>& engine
) {
183 return controller()->addPlayingEngine(side
, engine
);
186 // EntityToken UI::addAnalysingEngine(const shared_ptr<Engine>& engine) {
187 // return controller()->addAnalysingEngine(engine);
190 void UI::removeEntity(const EntityToken
& token
) {
191 controller()->removeEntity(token
);
195 controller() = controller()->end();
199 controller()->detach();
202 QString
UI::currentVariant() const {
203 return controller()->variant();
206 void UI::reloadSettings() {
207 for (ControllerMap::iterator it
= m_controller
.begin(),
208 end
= m_controller
.end();
211 it
->second
->reloadSettings();
215 shared_ptr
<ActionStateObserver
>
216 UI::createActionStateObserver(const shared_ptr
<Controller
>& controller
) const {
217 return shared_ptr
<UIActionStateObserver
>(
218 new UIActionStateObserver(this, controller
));
221 KUrl
UI::url() const { return controller()->url(); }
222 void UI::setUrl(const KUrl
& url
) { controller()->setUrl(url
); }