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.
14 #include <QMouseEvent>
15 #include "chesstable.h"
18 #include "connection.h"
19 #include "piecepool.h"
22 #include "movelist_table.h"
23 #include "infodisplay.h"
25 using namespace boost
;
27 ChessTable::ChessTable(QWidget
* parent
)
28 : KGameCanvasWidget(parent
)
32 , m_need_reload(false) {
34 setMouseTracking(true);
37 m_board
= new Board(this);
41 m_movelist
= new MoveList::Table
;
44 for(int i
=0;i
<2;i
++) {
45 m_clocks
[i
] = new Clock(i
, this);
48 m_clocks
[0]->activate(0);
50 // create info display
51 m_info
= new InfoDisplay(this);
55 for(int i
=0;i
<2;i
++) {
56 m_pools
[i
] = new PiecePool(i
, m_board
, this);
64 ChessTable::~ChessTable() {
74 void ChessTable::settingsChanged() {
75 m_board
->settingsChanged();
77 m_clocks
[i
]->settingsChanged();
78 m_info
->settingsChanged();
80 m_pools
[i
]->settingsChanged();
85 QPixmap bg
= m_board
->controlsLoader()->getStaticValue
<QPixmap
>("wallpaper");
87 m_wallpaper
= new KGameCanvasTiledPixmap(bg
, QSize(), QPoint(), false, this);
94 /* redo the layout, forcing reload */
101 ClickableCanvas
* ChessTable::eventItemAt(QPoint pos
) {
102 if (m_board
->boardRect().contains(pos
))
105 for (int i
=0; i
<2; i
++)
106 if (m_pools
[i
]->boardRect().contains(pos
))
109 for (int i
=0; i
<2; i
++)
110 if (m_clocks
[i
]->rect().contains(pos
))
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;
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", ¶ms
);
137 for(::LuaApi::LuaValueMap::iterator it
= lvals
.begin(); it
!= lvals
.end(); ++it
)
138 if(double* val
= boost::get
<double>(&it
.value()))
139 std::cout
<< "lvals[" << it
.key() << "] = " << *val
<< std::endl
;
140 else if(QPointF
* val
= boost::get
<QPointF
>(&it
.value()))
141 std::cout
<< "lvals[" << it
.key() << "] = Point(" << val
->x() << "," << val
->y() << ")" << std::endl
;
142 else if(QRectF
* val
= boost::get
<QRectF
>(&it
.value()))
143 std::cout
<< "lvals[" << it
.key() << "] = Rect(" << val
->x() << "," << val
->y()
144 << "," << val
->width() << "," << val
->height() << ")" << std::endl
;
146 #define GET_INT(name) \
148 if(double* val = boost::get<double>(&lvals[#name])) \
151 ERROR("Hey Jack, please set "<<#name<<" to a number in the layout!");
153 #define GET_POINT(name) \
155 if(QPointF* val = boost::get<QPointF>(&lvals[#name])) \
156 name = val->toPoint(); \
158 ERROR("Hey Jack, please set "<<#name<<" to a point in the layout!");
160 GET_POINT(board_position
);
161 GET_INT(square_size
);
162 GET_INT(border_size
);
163 GET_POINT(clock0_position
);
164 GET_POINT(clock1_position
);
166 GET_POINT(pool0_position
);
167 GET_POINT(pool1_position
);
168 GET_INT(pool_piece_size
);
171 m_board
->moveTo(board_position
.x(), board_position
.y());
172 m_board
->onResize( square_size
, border_size
, force_reload
);
174 int x
= !m_board
->flipped();
176 m_clocks
[x
]->resize(clock_size
);
177 m_clocks
[x
]->moveTo(clock0_position
.x(), clock0_position
.y());
179 m_clocks
[!x
]->resize(clock_size
);
180 m_clocks
[!x
]->moveTo(clock1_position
.x(), clock1_position
.y());
182 m_pools
[x
]->m_flipped
= false;
183 m_pools
[x
]->onResize(pool_piece_size
);
184 m_pools
[x
]->moveTo(pool0_position
.x(), pool0_position
.y());
185 m_pools
[x
]->setGridWidth(pool_width
);
187 m_pools
[!x
]->m_flipped
= true;
188 m_pools
[!x
]->onResize(pool_piece_size
);
189 m_pools
[!x
]->moveTo(pool1_position
.x(), pool1_position
.y());
190 m_pools
[!x
]->setGridWidth(pool_width
);
193 void ChessTable::resizeEvent(QResizeEvent
* /*e*/) {
197 void ChessTable::mouseReleaseEvent(QMouseEvent
* e
) {
200 m_mousegrab
->onMouseRelease(e
->pos() - m_mousegrab
->pos(), e
->button() );
204 ClickableCanvas
* cb
= eventItemAt(e
->pos());
205 if(cb
!= m_current
) {
207 m_current
->onMouseLeave();
210 cb
->onMouseMove(e
->pos() - cb
->pos(), 0);
219 void ChessTable::mousePressEvent(QMouseEvent
* e
) {
221 m_mousegrab
->onMousePress(e
->pos() - m_mousegrab
->pos(), e
->button() );
225 ClickableCanvas
* cb
= eventItemAt(e
->pos());
226 if(cb
!= m_current
) {
228 m_current
->onMouseLeave();
234 cb
->onMousePress(e
->pos() - cb
->pos(), e
->button() );
239 void ChessTable::mouseMoveEvent(QMouseEvent
* e
) {
241 m_mousegrab
->onMouseMove(e
->pos() - m_mousegrab
->pos(), e
->button() );
245 ClickableCanvas
* cb
= eventItemAt(e
->pos());
246 if(cb
!= m_current
) {
248 m_current
->onMouseLeave();
254 cb
->onMouseMove(e
->pos() - cb
->pos(), e
->button() );
257 void ChessTable::enterEvent(QEvent
*) { }
259 void ChessTable::leaveEvent(QEvent
*) {
261 m_current
->onMouseLeave();
265 void ChessTable::flip() {
268 int delta
= qAbs(m_pools
[0]->pos().y() - m_pools
[1]->pos().y());
270 m_pools
[i
]->flipAndMoveBy( QPoint(0, delta
) );
273 void ChessTable::flip(bool flipped
) {
274 if(m_board
->flipped() != flipped
)
278 void ChessTable::changeClock(int color
) {
279 std::cout
<< "change " << color
<< std::endl
;
280 if(m_clocks
[0]->running() || m_clocks
[1]->running())
281 for(int i
=0;i
<2;i
++) {
282 if ( (i
== color
) != m_clocks
[i
]->running() )
284 m_clocks
[i
]->start();
290 void ChessTable::updateTurn(int color
) {
291 std::cout
<< "updatet" << std::endl
;
293 m_clocks
[i
]->activate(color
== i
);
296 void ChessTable::stopClocks() {
301 void ChessTable::updateTime(int white
, int black
) {
302 std::cout
<< "update" << std::endl
;
303 m_clocks
[0]->setTime(white
);
304 m_clocks
[1]->setTime(black
);
307 void ChessTable::resetClock() {
308 std::cout
<< "reset" << std::endl
;
312 m_clocks
[i
]->setPlayer(Player());
315 void ChessTable::setPlayers(const Player
& white
, const Player
& black
) {
316 std::cout
<< "set players " << std::endl
;
317 m_clocks
[0]->setPlayer(white
);
318 m_clocks
[1]->setPlayer(black
);
321 void ChessTable::run() {
322 std::cout
<< "run" << std::endl
;
324 if(m_clocks
[i
]->active() && !m_clocks
[i
]->running())
325 m_clocks
[i
]->start();
328 void ChessTable::displayMessage(const QString
& msg
) {
329 std::cout
<< msg
<< std::endl
;