Replaced (almost) all occurrences of 'kboard' with 'tagua'.
[tagua/yd.git] / src / chesstable.cpp
blobfc91332a5ebeeafc4fe03b966795642964fd2d87
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 <QLayout>
13 #include <QSplitter>
14 #include <QMouseEvent>
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(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, 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_board->settingsChanged();
76 for(int i=0;i<2;i++)
77 m_clocks[i]->settingsChanged();
78 m_info->settingsChanged();
79 for(int i=0;i<2;i++)
80 m_pools[i]->settingsChanged();
82 if(m_wallpaper)
83 delete m_wallpaper;
85 QPixmap bg = m_board->controlsLoader()->getStaticValue<QPixmap>("wallpaper");
86 if(!bg.isNull()) {
87 m_wallpaper = new KGameCanvasTiledPixmap(bg, QSize(), QPoint(), false, this);
88 m_wallpaper->lower();
89 m_wallpaper->show();
91 else
92 m_wallpaper = 0;
94 /* redo the layout, forcing reload */
95 if(isVisible())
96 layout(true);
97 else
98 m_need_reload = true;
101 ClickableCanvas* ChessTable::eventItemAt(QPoint pos) {
102 if (m_board->boardRect().contains(pos))
103 return m_board;
105 for (int i=0; i<2; i++)
106 if (m_pools[i]->boardRect().contains(pos))
107 return m_pools[i];
109 for (int i=0; i<2; i++)
110 if (m_clocks[i]->rect().contains(pos))
111 return m_clocks[i];
113 return NULL;
116 void ChessTable::setEntity(const boost::shared_ptr<UserEntity>& entity) {
117 m_board->setEntity(entity);
120 void ChessTable::layout(bool force_reload) {
121 force_reload |= m_need_reload;
122 m_need_reload = false;
124 if (m_wallpaper) {
125 m_wallpaper->setSize(size());
126 QSize delta = (m_wallpaper->pixmap().size()-size())/2;
127 m_wallpaper->setOrigin(QPoint(delta.width(), delta.height()));
130 ::LuaApi::LuaValueMap params;
131 params["width"] = width();
132 params["height"] = height();
133 params["grid_size"] = QPointF(m_board->gridSize());
135 ::LuaApi::LuaValueMap lvals = m_board->controlsLoader()->getStaticValue< ::LuaApi::LuaValueMap>("layout", &params);
137 #if 0
138 for(::LuaApi::LuaValueMap::iterator it = lvals.begin(); it != lvals.end(); ++it)
139 if(double* val = boost::get<double>(&it.value()))
140 std::cout << "lvals[" << it.key() << "] = " << *val << std::endl;
141 else if(QPointF* val = boost::get<QPointF>(&it.value()))
142 std::cout << "lvals[" << it.key() << "] = Point(" << val->x() << "," << val->y() << ")" << std::endl;
143 else if(QRectF* val = boost::get<QRectF>(&it.value()))
144 std::cout << "lvals[" << it.key() << "] = Rect(" << val->x() << "," << val->y()
145 << "," << val->width() << "," << val->height() << ")" << std::endl;
146 #endif
148 #define GET_INT(name) \
149 int name = 0; \
150 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
151 if(double* val = (it==lvals.end()) ? 0 : boost::get<double>(&lvals[#name]) ) \
152 name = (int)*val; \
153 else \
154 ERROR("Hey Jack, please set "<<#name<<" to a number in the layout!");}
156 #define GET_POINT(name) \
157 QPoint name; \
158 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
159 if(QPointF* val = (it==lvals.end()) ? 0 : boost::get<QPointF>(&lvals[#name]) ) \
160 name = val->toPoint(); \
161 else \
162 ERROR("Hey Jack, please set "<<#name<<" to a point in the layout!");}
164 GET_POINT(board_position);
165 GET_INT(square_size);
166 GET_INT(border_size);
167 GET_INT(border_text_near);
168 GET_INT(border_text_far);
169 GET_POINT(clock0_position);
170 GET_POINT(clock1_position);
171 GET_INT(clock_size);
172 GET_POINT(pool0_position);
173 GET_POINT(pool1_position);
174 GET_INT(pool_piece_size);
175 GET_INT(pool_width);
177 m_board->moveTo(board_position.x(), board_position.y());
178 m_board->onResize( square_size, border_size, border_text_near, border_text_far, force_reload);
180 int x = !m_board->flipped();
182 m_clocks[x]->resize(clock_size);
183 m_clocks[x]->moveTo(clock0_position.x(), clock0_position.y());
185 m_clocks[!x]->resize(clock_size);
186 m_clocks[!x]->moveTo(clock1_position.x(), clock1_position.y());
188 m_pools[x]->m_flipped = false;
189 m_pools[x]->onResize(pool_piece_size);
190 m_pools[x]->moveTo(pool0_position.x(), pool0_position.y());
191 m_pools[x]->setGridWidth(pool_width);
193 m_pools[!x]->m_flipped = true;
194 m_pools[!x]->onResize(pool_piece_size);
195 m_pools[!x]->moveTo(pool1_position.x(), pool1_position.y());
196 m_pools[!x]->setGridWidth(pool_width);
199 void ChessTable::resizeEvent(QResizeEvent* /*e*/) {
200 layout();
203 void ChessTable::mouseReleaseEvent(QMouseEvent* e) {
205 if(m_mousegrab) {
206 m_mousegrab->onMouseRelease(e->pos() - m_mousegrab->pos(), e->button() );
207 if(!e->buttons()) {
208 m_mousegrab = NULL;
210 ClickableCanvas* cb = eventItemAt(e->pos());
211 if(cb != m_current) {
212 if(m_current)
213 m_current->onMouseLeave();
214 if(cb) {
215 cb->onMouseEnter();
216 cb->onMouseMove(e->pos() - cb->pos(), 0);
218 m_current = cb;
221 return;
225 void ChessTable::mousePressEvent(QMouseEvent* e) {
226 if(m_mousegrab) {
227 m_mousegrab->onMousePress(e->pos() - m_mousegrab->pos(), e->button() );
228 return;
231 ClickableCanvas* cb = eventItemAt(e->pos());
232 if(cb != m_current) {
233 if(m_current)
234 m_current->onMouseLeave();
235 if(cb)
236 cb->onMouseEnter();
237 m_current = cb;
239 if(cb) {
240 cb->onMousePress(e->pos() - cb->pos(), e->button() );
241 m_mousegrab = cb;
245 void ChessTable::mouseMoveEvent(QMouseEvent* e) {
246 if(m_mousegrab) {
247 m_mousegrab->onMouseMove(e->pos() - m_mousegrab->pos(), e->button() );
248 return;
251 ClickableCanvas* cb = eventItemAt(e->pos());
252 if(cb != m_current) {
253 if(m_current)
254 m_current->onMouseLeave();
255 if(cb)
256 cb->onMouseEnter();
257 m_current = cb;
259 if(cb)
260 cb->onMouseMove(e->pos() - cb->pos(), e->button() );
263 void ChessTable::enterEvent(QEvent*) { }
265 void ChessTable::leaveEvent(QEvent*) {
266 if(m_current)
267 m_current->onMouseLeave();
268 m_current = NULL;
271 void ChessTable::flip() {
272 m_board->flip();
274 int delta = qAbs(m_pools[0]->pos().y() - m_pools[1]->pos().y());
275 for(int i=0;i<2;i++)
276 m_pools[i]->flipAndMoveBy( QPoint(0, delta) );
279 void ChessTable::flip(bool flipped) {
280 if(m_board->flipped() != flipped)
281 flip();
284 void ChessTable::changeClock(int color) {
285 std::cout << "change " << color << std::endl;
286 if(m_clocks[0]->running() || m_clocks[1]->running())
287 for(int i=0;i<2;i++) {
288 if ( (i == color) != m_clocks[i]->running() )
289 if( i==color )
290 m_clocks[i]->start();
291 else
292 m_clocks[i]->stop();
296 void ChessTable::updateTurn(int color) {
297 for(int i=0; i<2; i++)
298 m_clocks[i]->activate(color == i);
301 void ChessTable::stopClocks() {
302 for(int i=0; i<2; i++)
303 m_clocks[i]->stop();
306 void ChessTable::updateTime(int white, int black) {
307 m_clocks[0]->setTime(white);
308 m_clocks[1]->setTime(black);
311 void ChessTable::resetClock() {
312 stopClocks();
313 updateTime(0, 0);
314 for(int i=0; i<2; i++)
315 m_clocks[i]->setPlayer(Player());
318 void ChessTable::setPlayers(const Player& white, const Player& black) {
319 std::cout << "set players " << std::endl;
320 m_clocks[0]->setPlayer(white);
321 m_clocks[1]->setPlayer(black);
324 void ChessTable::run() {
325 std::cout << "run" << std::endl;
326 for(int i=0;i<2;i++)
327 if(m_clocks[i]->active() && !m_clocks[i]->running())
328 m_clocks[i]->start();
331 void ChessTable::displayMessage(const QString& msg) {
332 std::cout << msg << std::endl;
333 emit message(msg);