Fix doxygen comments that were breaking parsing.
[tagua/yd.git] / src / chesstable.cpp
blobdc6edc5da51e45ef469b148660f92f6ba39c9858
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 <QLayout>
12 #include <QSplitter>
13 #include <QMouseEvent>
14 #include <KDebug>
15 #include <core/color.h>
16 #include "chesstable.h"
17 #include "game.h"
18 #include "gameinfo.h"
19 #include "connection.h"
20 #include "piecepool.h"
21 #include "clock.h"
22 #include "mastersettings.h"
23 #include "movelist_table.h"
24 #include "infodisplay.h"
26 using namespace boost;
28 ChessTable::ChessTable(Variant* variant, QWidget* parent)
29 : KGameCanvasWidget(parent)
30 , m_wallpaper(NULL)
31 , m_current(NULL)
32 , m_mousegrab(NULL)
33 , m_need_reload(false)
34 , m_components(variant) {
36 setMouseTracking(true);
38 // create m_board
39 m_board = new ChessBoard(&m_components, &m_anim_manager, this);
40 m_board->show();
42 // create move list
43 m_movelist = new MoveList::Table;
45 // create clocks
46 for(int i=0;i<2;i++) {
47 m_clocks[i] = new Clock(i, this);
48 m_clocks[i]->show();
50 m_clocks[0]->activate(0);
52 // create info display
53 m_info = new InfoDisplay(this);
54 // m_info->show();
56 // create pools
57 // FIXME what to do for more than 2 players?
58 for (int i = 0; i < 2; i++) {
59 const IColor* color = m_components.player(i);
60 if (color) {
61 m_pools[i] = new PiecePool(color, m_board, &m_anim_manager, this);
62 m_pools[i]->show();
66 m_board->raise();
67 settingsChanged();
70 ChessTable::~ChessTable() {
71 for(int i=0;i<2;i++)
72 delete m_clocks[i];
73 delete m_movelist;
74 delete m_board;
75 for(int i=0;i<2;i++)
76 delete m_pools[i];
77 delete m_info;
80 void ChessTable::settingsChanged() {
81 m_anim_manager.reload();
83 m_board->settingsChanged();
84 for(int i=0;i<2;i++)
85 m_clocks[i]->settingsChanged();
86 m_info->settingsChanged();
87 for(int i=0;i<2;i++)
88 m_pools[i]->settingsChanged();
90 if(m_wallpaper)
91 delete m_wallpaper;
93 QPixmap bg = m_board->controlsLoader()->getStaticValue<QPixmap>("wallpaper", 0, true);
94 if (!bg.isNull()) {
95 m_wallpaper = new KGameCanvasTiledPixmap(bg, QSize(), QPoint(), false, this);
96 m_wallpaper->lower();
97 m_wallpaper->show();
99 else
100 m_wallpaper = 0;
102 /* redo the layout, forcing reload */
103 if(isVisible())
104 layout(true);
105 else
106 m_need_reload = true;
109 ClickableCanvas* ChessTable::eventItemAt(QPoint pos) {
110 if (m_board->boardRect().contains(pos))
111 return m_board;
113 for (int i=0; i<2; i++)
114 if (m_pools[i]->boardRect().contains(pos))
115 return m_pools[i];
117 for (int i=0; i<2; i++)
118 if (m_clocks[i]->rect().contains(pos))
119 return m_clocks[i];
121 return NULL;
124 void ChessTable::setEntity(const boost::shared_ptr<UserEntity>& entity) {
125 m_board->setEntity(entity);
128 void ChessTable::layout(bool force_reload) {
129 force_reload |= m_need_reload;
130 m_need_reload = false;
132 if (m_wallpaper) {
133 m_wallpaper->setSize(size());
134 QSize delta = (m_wallpaper->pixmap().size()-size())/2;
135 m_wallpaper->setOrigin(QPoint(delta.width(), delta.height()));
138 ::LuaApi::LuaValueMap params;
139 params["width"] = width();
140 params["height"] = height();
141 params["grid_size"] = QPointF(m_board->gridSize());
143 ::LuaApi::LuaValueMap lvals = m_board->controlsLoader()->getStaticValue< ::LuaApi::LuaValueMap>("layout", &params);
145 #if 0
146 for(::LuaApi::LuaValueMap::iterator it = lvals.begin(); it != lvals.end(); ++it)
147 if(double* val = boost::get<double>(&it.value()))
148 kDebug() << "lvals[" << it.key() << "] = " << *val;
149 else if(QPointF* val = boost::get<QPointF>(&it.value()))
150 kDebug() << "lvals[" << it.key() << "] = Point(" << val->x() << "," << val->y() << ")";
151 else if(QRectF* val = boost::get<QRectF>(&it.value()))
152 kDebug() << "lvals[" << it.key() << "] = Rect(" << val->x() << "," << val->y()
153 << "," << val->width() << "," << val->height() << ")";
154 #endif
156 #define GET_INT(name) \
157 int name = 0; \
158 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
159 if(double* val = (it==lvals.end()) ? 0 : boost::get<double>(&lvals[#name]) ) \
160 name = (int)*val; \
161 else \
162 kError() << "Theme error:" << #name << "should be set to a number in the layout";}
164 #define GET_POINT(name) \
165 QPoint name; \
166 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
167 if(QPointF* val = (it==lvals.end()) ? 0 : boost::get<QPointF>(&lvals[#name]) ) \
168 name = val->toPoint(); \
169 else \
170 kError() << "Theme error:" << #name << "should be set to a point in the layout";}
172 GET_POINT(board_position);
173 GET_INT(square_size);
174 GET_INT(border_size);
175 GET_INT(border_text_near);
176 GET_INT(border_text_far);
177 GET_POINT(clock0_position);
178 GET_POINT(clock1_position);
179 GET_INT(clock_size);
180 GET_POINT(pool0_position);
181 GET_POINT(pool1_position);
182 GET_INT(pool_piece_size);
183 GET_INT(pool_width);
185 m_board->moveTo(board_position.x(), board_position.y());
186 m_board->onResize( square_size, border_size, border_text_near, border_text_far, force_reload);
188 int x = !m_board->flipped();
190 m_clocks[x]->resize(clock_size);
191 m_clocks[x]->moveTo(clock0_position.x(), clock0_position.y());
192 // kDebug() << "moving clock " << x << " to " << clock0_position.y();
194 m_clocks[!x]->resize(clock_size);
195 m_clocks[!x]->moveTo(clock1_position.x(), clock1_position.y());
196 // kDebug() << "moving clock " << !x << " to " << clock1_position.y();
198 m_pools[x]->m_flipped = false;
199 m_pools[x]->onResize(pool_piece_size, force_reload);
200 m_pools[x]->moveTo(pool0_position.x(), pool0_position.y());
201 m_pools[x]->setGridWidth(pool_width);
203 m_pools[!x]->m_flipped = true;
204 m_pools[!x]->onResize(pool_piece_size, force_reload);
205 m_pools[!x]->moveTo(pool1_position.x(), pool1_position.y());
206 m_pools[!x]->setGridWidth(pool_width);
209 void ChessTable::resizeEvent(QResizeEvent* /*e*/) {
210 layout();
213 void ChessTable::mouseReleaseEvent(QMouseEvent* e) {
215 if(m_mousegrab) {
216 m_mousegrab->onMouseRelease(e->pos() - m_mousegrab->pos(), e->button() );
217 if(!e->buttons()) {
218 m_mousegrab = NULL;
220 ClickableCanvas* cb = eventItemAt(e->pos());
221 if(cb != m_current) {
222 if(m_current)
223 m_current->onMouseLeave();
224 if(cb) {
225 cb->onMouseEnter();
226 cb->onMouseMove(e->pos() - cb->pos(), 0);
228 m_current = cb;
231 return;
235 void ChessTable::mousePressEvent(QMouseEvent* e) {
236 if(m_mousegrab) {
237 m_mousegrab->onMousePress(e->pos() - m_mousegrab->pos(), e->button() );
238 return;
241 ClickableCanvas* cb = eventItemAt(e->pos());
242 if(cb != m_current) {
243 if(m_current)
244 m_current->onMouseLeave();
245 if(cb)
246 cb->onMouseEnter();
247 m_current = cb;
249 if(cb) {
250 cb->onMousePress(e->pos() - cb->pos(), e->button() );
251 m_mousegrab = cb;
255 void ChessTable::mouseMoveEvent(QMouseEvent* e) {
256 if(m_mousegrab) {
257 m_mousegrab->onMouseMove(e->pos() - m_mousegrab->pos(), e->button() );
258 return;
261 ClickableCanvas* cb = eventItemAt(e->pos());
262 if(cb != m_current) {
263 if(m_current)
264 m_current->onMouseLeave();
265 if(cb)
266 cb->onMouseEnter();
267 m_current = cb;
269 if(cb)
270 cb->onMouseMove(e->pos() - cb->pos(), e->button() );
273 void ChessTable::enterEvent(QEvent*) { }
275 void ChessTable::leaveEvent(QEvent*) {
276 if(m_current)
277 m_current->onMouseLeave();
278 m_current = NULL;
281 void ChessTable::flip() {
282 m_board->flip();
284 int delta = qAbs(m_pools[0]->pos().y() - m_pools[1]->pos().y());
285 for(int i=0;i<2;i++)
286 m_pools[i]->flipAndMoveBy( QPoint(0, delta) );
288 // flip clocks
289 QPoint pos = m_clocks[0]->pos();
290 m_clocks[0]->moveTo(m_clocks[1]->pos());
291 m_clocks[1]->moveTo(pos);
294 void ChessTable::flip(bool flipped) {
295 if(m_board->flipped() != flipped)
296 flip();
299 void ChessTable::changeClock(int color) {
300 if(m_clocks[0]->running() || m_clocks[1]->running())
301 for(int i=0;i<2;i++) {
302 if ( (i == color) != m_clocks[i]->running() )
303 if( i==color )
304 m_clocks[i]->start();
305 else
306 m_clocks[i]->stop();
310 void ChessTable::updateTurn(int color) {
311 for(int i=0; i<2; i++)
312 m_clocks[i]->activate(color == i);
315 void ChessTable::stopClocks() {
316 for(int i=0; i<2; i++)
317 m_clocks[i]->stop();
320 void ChessTable::updateTime(int white, int black) {
321 m_clocks[0]->setTime(white);
322 m_clocks[1]->setTime(black);
325 void ChessTable::resetClock() {
326 stopClocks();
327 updateTime(0, 0);
328 for(int i=0; i<2; i++)
329 m_clocks[i]->setPlayer(Player());
332 void ChessTable::setPlayers(const Player& white, const Player& black) {
333 m_clocks[0]->setPlayer(white);
334 m_clocks[1]->setPlayer(black);
337 void ChessTable::run() {
338 for(int i=0;i<2;i++)
339 if(m_clocks[i]->active() && !m_clocks[i]->running())
340 m_clocks[i]->start();
343 void ChessTable::displayMessage(const QString& msg) {
344 kDebug() << msg;
345 message(msg);
348 AnimationManager* ChessTable::animationManager() { return &m_anim_manager; }
349 const AnimationManager* ChessTable::animationManager() const { return &m_anim_manager; }
351 void ChessTable::setVariant(Variant* variant) {
352 m_components.setVariant(variant);
355 Components* ChessTable::components() { return &m_components; }