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.
13 #include <QMouseEvent>
15 #include <core/color.h>
16 #include "chesstable.h"
19 #include "connection.h"
20 #include "piecepool.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
)
33 , m_need_reload(false)
34 , m_components(variant
) {
36 setMouseTracking(true);
39 m_board
= new ChessBoard(&m_components
, &m_anim_manager
, this);
43 m_movelist
= new MoveList::Table
;
46 for(int i
=0;i
<2;i
++) {
47 m_clocks
[i
] = new Clock(i
, this);
50 m_clocks
[0]->activate(0);
52 // create info display
53 m_info
= new InfoDisplay(this);
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
);
61 m_pools
[i
] = new PiecePool(color
, m_board
, &m_anim_manager
, this);
70 ChessTable::~ChessTable() {
80 void ChessTable::settingsChanged() {
81 m_anim_manager
.reload();
83 m_board
->settingsChanged();
85 m_clocks
[i
]->settingsChanged();
86 m_info
->settingsChanged();
88 m_pools
[i
]->settingsChanged();
93 QPixmap bg
= m_board
->controlsLoader()->getStaticValue
<QPixmap
>("wallpaper", 0, true);
95 m_wallpaper
= new KGameCanvasTiledPixmap(bg
, QSize(), QPoint(), false, this);
102 /* redo the layout, forcing reload */
106 m_need_reload
= true;
109 ClickableCanvas
* ChessTable::eventItemAt(QPoint pos
) {
110 if (m_board
->boardRect().contains(pos
))
113 for (int i
=0; i
<2; i
++)
114 if (m_pools
[i
]->boardRect().contains(pos
))
117 for (int i
=0; i
<2; i
++)
118 if (m_clocks
[i
]->rect().contains(pos
))
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;
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", ¶ms
);
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() << ")";
156 #define GET_INT(name) \
158 {::LuaApi::LuaValueMap::iterator it = lvals.find(#name);\
159 if(double* val = (it==lvals.end()) ? 0 : boost::get<double>(&lvals[#name]) ) \
162 kError() << "Theme error:" << #name << "should be set to a number in the layout";}
164 #define GET_POINT(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(); \
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
);
180 GET_POINT(pool0_position
);
181 GET_POINT(pool1_position
);
182 GET_INT(pool_piece_size
);
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*/) {
213 void ChessTable::mouseReleaseEvent(QMouseEvent
* e
) {
216 m_mousegrab
->onMouseRelease(e
->pos() - m_mousegrab
->pos(), e
->button() );
220 ClickableCanvas
* cb
= eventItemAt(e
->pos());
221 if(cb
!= m_current
) {
223 m_current
->onMouseLeave();
226 cb
->onMouseMove(e
->pos() - cb
->pos(), 0);
235 void ChessTable::mousePressEvent(QMouseEvent
* e
) {
237 m_mousegrab
->onMousePress(e
->pos() - m_mousegrab
->pos(), e
->button() );
241 ClickableCanvas
* cb
= eventItemAt(e
->pos());
242 if(cb
!= m_current
) {
244 m_current
->onMouseLeave();
250 cb
->onMousePress(e
->pos() - cb
->pos(), e
->button() );
255 void ChessTable::mouseMoveEvent(QMouseEvent
* e
) {
257 m_mousegrab
->onMouseMove(e
->pos() - m_mousegrab
->pos(), e
->button() );
261 ClickableCanvas
* cb
= eventItemAt(e
->pos());
262 if(cb
!= m_current
) {
264 m_current
->onMouseLeave();
270 cb
->onMouseMove(e
->pos() - cb
->pos(), e
->button() );
273 void ChessTable::enterEvent(QEvent
*) { }
275 void ChessTable::leaveEvent(QEvent
*) {
277 m_current
->onMouseLeave();
281 void ChessTable::flip() {
284 int delta
= qAbs(m_pools
[0]->pos().y() - m_pools
[1]->pos().y());
286 m_pools
[i
]->flipAndMoveBy( QPoint(0, delta
) );
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
)
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() )
304 m_clocks
[i
]->start();
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
++)
320 void ChessTable::updateTime(int white
, int black
) {
321 m_clocks
[0]->setTime(white
);
322 m_clocks
[1]->setTime(black
);
325 void ChessTable::resetClock() {
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() {
339 if(m_clocks
[i
]->active() && !m_clocks
[i
]->running())
340 m_clocks
[i
]->start();
343 void ChessTable::displayMessage(const QString
& 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
; }