Replaced all std::cout with kDebug.
[tagua/yd.git] / src / chesstable.cpp
blobb0a06412ddf836aa96fdc4d37e7dcc658bd7e7c6
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 "chesstable.h"
16 #include "game.h"
17 #include "gameinfo.h"
18 #include "connection.h"
19 #include "piecepool.h"
20 #include "clock.h"
21 #include "mastersettings.h"
22 #include "movelist_table.h"
23 #include "infodisplay.h"
25 using namespace boost;
27 ChessTable::ChessTable(QWidget* parent)
28 : KGameCanvasWidget(parent)
29 , m_wallpaper(NULL)
30 , m_current(NULL)
31 , m_mousegrab(NULL)
32 , m_need_reload(false) {
34 setMouseTracking(true);
36 // create m_board
37 m_board = new Board(m_anim_settings, this);
38 m_board->show();
40 // create move list
41 m_movelist = new MoveList::Table;
43 // create clocks
44 for(int i=0;i<2;i++) {
45 m_clocks[i] = new Clock(i, this);
46 m_clocks[i]->show();
48 m_clocks[0]->activate(0);
50 // create info display
51 m_info = new InfoDisplay(this);
52 // m_info->show();
54 // create pools
55 for(int i=0;i<2;i++) {
56 m_pools[i] = new PiecePool(i, m_board, m_anim_settings, this);
57 m_pools[i]->show();
60 m_board->raise();
61 settingsChanged();
64 ChessTable::~ChessTable() {
65 for(int i=0;i<2;i++)
66 delete m_clocks[i];
67 delete m_movelist;
68 delete m_board;
69 for(int i=0;i<2;i++)
70 delete m_pools[i];
71 delete m_info;
74 void ChessTable::settingsChanged() {
75 m_anim_settings.reload();
77 m_board->settingsChanged();
78 for(int i=0;i<2;i++)
79 m_clocks[i]->settingsChanged();
80 m_info->settingsChanged();
81 for(int i=0;i<2;i++)
82 m_pools[i]->settingsChanged();
84 if(m_wallpaper)
85 delete m_wallpaper;
87 QPixmap bg = m_board->controlsLoader()->getStaticValue<QPixmap>("wallpaper", 0, true);
88 if (!bg.isNull()) {
89 m_wallpaper = new KGameCanvasTiledPixmap(bg, QSize(), QPoint(), false, this);
90 m_wallpaper->lower();
91 m_wallpaper->show();
93 else
94 m_wallpaper = 0;
96 /* redo the layout, forcing reload */
97 if(isVisible())
98 layout(true);
99 else
100 m_need_reload = true;
103 ClickableCanvas* ChessTable::eventItemAt(QPoint pos) {
104 if (m_board->boardRect().contains(pos))
105 return m_board;
107 for (int i=0; i<2; i++)
108 if (m_pools[i]->boardRect().contains(pos))
109 return m_pools[i];
111 for (int i=0; i<2; i++)
112 if (m_clocks[i]->rect().contains(pos))
113 return m_clocks[i];
115 return NULL;
118 void ChessTable::setEntity(const boost::shared_ptr<UserEntity>& entity) {
119 m_board->setEntity(entity);
122 void ChessTable::layout(bool force_reload) {
123 force_reload |= m_need_reload;
124 m_need_reload = false;
126 if (m_wallpaper) {
127 m_wallpaper->setSize(size());
128 QSize delta = (m_wallpaper->pixmap().size()-size())/2;
129 m_wallpaper->setOrigin(QPoint(delta.width(), delta.height()));
132 ::LuaApi::LuaValueMap params;
133 params["width"] = width();
134 params["height"] = height();
135 params["grid_size"] = QPointF(m_board->gridSize());
137 ::LuaApi::LuaValueMap lvals = m_board->controlsLoader()->getStaticValue< ::LuaApi::LuaValueMap>("layout", &params);
139 #if 0
140 for(::LuaApi::LuaValueMap::iterator it = lvals.begin(); it != lvals.end(); ++it)
141 if(double* val = boost::get<double>(&it.value()))
142 kDebug() << "lvals[" << it.key() << "] = " << *val;
143 else if(QPointF* val = boost::get<QPointF>(&it.value()))
144 kDebug() << "lvals[" << it.key() << "] = Point(" << val->x() << "," << val->y() << ")";
145 else if(QRectF* val = boost::get<QRectF>(&it.value()))
146 kDebug() << "lvals[" << it.key() << "] = Rect(" << val->x() << "," << val->y()
147 << "," << val->width() << "," << val->height() << ")";
148 #endif
150 #define GET_INT(name) \
151 int name = 0; \
152 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
153 if(double* val = (it==lvals.end()) ? 0 : boost::get<double>(&lvals[#name]) ) \
154 name = (int)*val; \
155 else \
156 kError() << "Theme error:" << #name << "should be set to a number in the layout";}
158 #define GET_POINT(name) \
159 QPoint name; \
160 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
161 if(QPointF* val = (it==lvals.end()) ? 0 : boost::get<QPointF>(&lvals[#name]) ) \
162 name = val->toPoint(); \
163 else \
164 kError() << "Theme error:" << #name << "should be set to a point in the layout";}
166 GET_POINT(board_position);
167 GET_INT(square_size);
168 GET_INT(border_size);
169 GET_INT(border_text_near);
170 GET_INT(border_text_far);
171 GET_POINT(clock0_position);
172 GET_POINT(clock1_position);
173 GET_INT(clock_size);
174 GET_POINT(pool0_position);
175 GET_POINT(pool1_position);
176 GET_INT(pool_piece_size);
177 GET_INT(pool_width);
179 m_board->moveTo(board_position.x(), board_position.y());
180 m_board->onResize( square_size, border_size, border_text_near, border_text_far, force_reload);
182 int x = !m_board->flipped();
184 m_clocks[x]->resize(clock_size);
185 m_clocks[x]->moveTo(clock0_position.x(), clock0_position.y());
186 // kDebug() << "moving clock " << x << " to " << clock0_position.y();
188 m_clocks[!x]->resize(clock_size);
189 m_clocks[!x]->moveTo(clock1_position.x(), clock1_position.y());
190 // kDebug() << "moving clock " << !x << " to " << clock1_position.y();
192 m_pools[x]->m_flipped = false;
193 m_pools[x]->onResize(pool_piece_size, force_reload);
194 m_pools[x]->moveTo(pool0_position.x(), pool0_position.y());
195 m_pools[x]->setGridWidth(pool_width);
197 m_pools[!x]->m_flipped = true;
198 m_pools[!x]->onResize(pool_piece_size, force_reload);
199 m_pools[!x]->moveTo(pool1_position.x(), pool1_position.y());
200 m_pools[!x]->setGridWidth(pool_width);
203 void ChessTable::resizeEvent(QResizeEvent* /*e*/) {
204 layout();
207 void ChessTable::mouseReleaseEvent(QMouseEvent* e) {
209 if(m_mousegrab) {
210 m_mousegrab->onMouseRelease(e->pos() - m_mousegrab->pos(), e->button() );
211 if(!e->buttons()) {
212 m_mousegrab = NULL;
214 ClickableCanvas* cb = eventItemAt(e->pos());
215 if(cb != m_current) {
216 if(m_current)
217 m_current->onMouseLeave();
218 if(cb) {
219 cb->onMouseEnter();
220 cb->onMouseMove(e->pos() - cb->pos(), 0);
222 m_current = cb;
225 return;
229 void ChessTable::mousePressEvent(QMouseEvent* e) {
230 if(m_mousegrab) {
231 m_mousegrab->onMousePress(e->pos() - m_mousegrab->pos(), e->button() );
232 return;
235 ClickableCanvas* cb = eventItemAt(e->pos());
236 if(cb != m_current) {
237 if(m_current)
238 m_current->onMouseLeave();
239 if(cb)
240 cb->onMouseEnter();
241 m_current = cb;
243 if(cb) {
244 cb->onMousePress(e->pos() - cb->pos(), e->button() );
245 m_mousegrab = cb;
249 void ChessTable::mouseMoveEvent(QMouseEvent* e) {
250 if(m_mousegrab) {
251 m_mousegrab->onMouseMove(e->pos() - m_mousegrab->pos(), e->button() );
252 return;
255 ClickableCanvas* cb = eventItemAt(e->pos());
256 if(cb != m_current) {
257 if(m_current)
258 m_current->onMouseLeave();
259 if(cb)
260 cb->onMouseEnter();
261 m_current = cb;
263 if(cb)
264 cb->onMouseMove(e->pos() - cb->pos(), e->button() );
267 void ChessTable::enterEvent(QEvent*) { }
269 void ChessTable::leaveEvent(QEvent*) {
270 if(m_current)
271 m_current->onMouseLeave();
272 m_current = NULL;
275 void ChessTable::flip() {
276 m_board->flip();
278 int delta = qAbs(m_pools[0]->pos().y() - m_pools[1]->pos().y());
279 for(int i=0;i<2;i++)
280 m_pools[i]->flipAndMoveBy( QPoint(0, delta) );
282 // flip clocks
283 QPoint pos = m_clocks[0]->pos();
284 m_clocks[0]->moveTo(m_clocks[1]->pos());
285 m_clocks[1]->moveTo(pos);
288 void ChessTable::flip(bool flipped) {
289 if(m_board->flipped() != flipped)
290 flip();
293 void ChessTable::changeClock(int color) {
294 if(m_clocks[0]->running() || m_clocks[1]->running())
295 for(int i=0;i<2;i++) {
296 if ( (i == color) != m_clocks[i]->running() )
297 if( i==color )
298 m_clocks[i]->start();
299 else
300 m_clocks[i]->stop();
304 void ChessTable::updateTurn(int color) {
305 for(int i=0; i<2; i++)
306 m_clocks[i]->activate(color == i);
309 void ChessTable::stopClocks() {
310 for(int i=0; i<2; i++)
311 m_clocks[i]->stop();
314 void ChessTable::updateTime(int white, int black) {
315 m_clocks[0]->setTime(white);
316 m_clocks[1]->setTime(black);
319 void ChessTable::resetClock() {
320 stopClocks();
321 updateTime(0, 0);
322 for(int i=0; i<2; i++)
323 m_clocks[i]->setPlayer(Player());
326 void ChessTable::setPlayers(const Player& white, const Player& black) {
327 m_clocks[0]->setPlayer(white);
328 m_clocks[1]->setPlayer(black);
331 void ChessTable::run() {
332 for(int i=0;i<2;i++)
333 if(m_clocks[i]->active() && !m_clocks[i]->running())
334 m_clocks[i]->start();
337 void ChessTable::displayMessage(const QString& msg) {
338 kDebug() << msg;
339 message(msg);
342 const AnimationSettings& ChessTable::animationSettings() const {
343 return m_anim_settings;