Factor canBeCaptured() out of Shogi legal() check for east reuse.
[tagua/yd.git] / src / ui.cpp
blobf4d61bb7629b1555923f127307059d0cdbbde9b3
1 /*
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.
9 */
11 #include <iostream>
12 #include <qapplication.h>
13 #include <qclipboard.h>
14 #include "ui.h"
15 #include "controllers/abstract.h"
17 using namespace boost;
19 UI::UI()
20 : m_current_tab(NULL) {
23 boost::shared_ptr<Controller>& UI::controller() {
24 Q_ASSERT(m_current_tab);
26 boost::shared_ptr<Controller>& res = m_controller[m_current_tab];
27 Q_ASSERT(res);
29 return res;
32 boost::shared_ptr<Controller> UI::controller() const {
33 Q_ASSERT(m_current_tab);
35 ControllerMap::const_iterator it = m_controller.find(m_current_tab);
36 Q_ASSERT(it != m_controller.end());
37 return it->second;
40 void UI::addController(QWidget* w, const shared_ptr<Controller>& controller) {
41 m_controller[w] = controller;
44 void UI::setController(const shared_ptr<Controller>& controller) {
45 Q_ASSERT(m_current_tab);
47 m_controller[m_current_tab] = controller;
50 void UI::removeController(QWidget* w) {
51 controller()->end();
52 std::cout << "removing controller " << w << std::endl;
53 m_controller.erase(w);
56 void UI::setCurrentTab(QWidget* w) {
57 m_current_tab = w;
60 bool UI::undo() {
61 controller()->undo();
62 return true;
65 bool UI::redo() {
66 controller()->redo();
67 return true;
70 bool UI::truncate() {
71 controller()->truncate();
72 return true;
75 bool UI::promoteVariation() {
76 controller()->promoteVariation();
77 return true;
80 bool UI::back() {
81 return controller()->back();
84 bool UI::forward() {
85 return controller()->forward();
88 void UI::gotoFirst() {
89 controller()->gotoFirst();
92 void UI::gotoLast() {
93 controller()->gotoLast();
96 void UI::pgnCopy() {
97 QClipboard* cb = QApplication::clipboard();
98 cb->setText(controller()->save());
101 void UI::pgnPaste() {
102 QClipboard* cb = QApplication::clipboard();
103 pgnPaste(cb->text());
106 void UI::pgnPaste(const QString&) {
107 //controller()->loadPGN(pgn);
110 void UI::clearBoard() {
111 controller()->clearBoard();
114 void UI::setStartingPosition() {
115 controller()->setStartingPosition();
118 void UI::copyPosition() {
119 QClipboard* cb = QApplication::clipboard();
120 cb->setText(controller()->fen());
123 void UI::pastePosition() {
124 QClipboard* cb = QApplication::clipboard();
125 controller()->setFEN(cb->text());
128 void UI::setTurn(int turn) {
129 controller()->setTurn(turn);
132 AbstractPosition::Ptr UI::position() const {
133 return controller()->currentPosition();
136 void UI::createCtrlAction() {
137 controller()->createCtrlAction();
140 void UI::destroyCtrlAction() {
141 controller()->destroyCtrlAction();
144 ActionCollection* UI::variantActions() const {
145 return controller()->variantActions();
148 EntityToken UI::addPlayingEngine(int side, const shared_ptr<Engine>& engine) {
149 return controller()->addPlayingEngine(side, engine);
152 // EntityToken UI::addAnalysingEngine(const shared_ptr<Engine>& engine) {
153 // return controller()->addAnalysingEngine(engine);
154 // }
156 void UI::removeEntity(const EntityToken& token) {
157 controller()->removeEntity(token);
160 void UI::end() {
161 controller() = controller()->end();
164 void UI::detach() {
165 controller()->detach();
168 QString UI::currentVariant() const {
169 return controller()->variant();
172 void UI::reloadSettings() {
173 for (ControllerMap::iterator it = m_controller.begin(),
174 end = m_controller.end();
175 it != end;
176 ++it) {
177 it->second->reloadSettings();