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
, m_board
, 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();
84 bool was0
= m_board
->squareSize() == 0;
85 if(was0
) m_board
->tagsLoader()->setSize(1);
86 QPixmap bg
= m_board
->tagsLoader()->operator()("wallpaper");
87 if(was0
) m_board
->tagsLoader()->setSize(0);
89 std::cout
<< "Size is " << bg
.size().width() << " "<< bg
.size().height() << std::endl
;
90 m_wallpaper
= new KGameCanvasTiledPixmap(bg
, QSize(), QPoint(), false, this);
97 /* redo the layout, forcing reload */
101 m_need_reload
= true;
104 ClickableCanvas
* ChessTable::eventItemAt(QPoint pos
) {
105 if (m_board
->boardRect().contains(pos
))
108 for (int i
=0; i
<2; i
++)
109 if (m_pools
[i
]->boardRect().contains(pos
))
112 for (int i
=0; i
<2; i
++)
113 if (m_clocks
[i
]->rect().contains(pos
))
119 void ChessTable::setEntity(const boost::shared_ptr
<UserEntity
>& entity
) {
120 m_board
->setEntity(entity
);
123 void ChessTable::layout(bool force_reload
) {
124 force_reload
|= m_need_reload
;
125 m_need_reload
= false;
128 m_wallpaper
->setSize(size());
129 QSize delta
= (m_wallpaper
->pixmap().size()-size())/2;
130 m_wallpaper
->setOrigin(QPoint(delta
.width(), delta
.height()));
133 Point gs
= m_board
->gridSize();
134 int sq_size
= std::max(0, std::min(int(width()/(gs
.x
+2.6+(2.0/3.0)*3)),
135 int(gs
.y
== 0 ? 100000 : height()/(gs
.y
+4.0/3.0))) );
137 m_board
->moveTo(b
,b
);
138 m_board
->onResize( sq_size
, force_reload
);
140 m_clocks
[0]->resize();
141 m_clocks
[1]->resize();
142 m_clocks
[0]->moveTo(sq_size
*gs
.x
+2*b
+b
/2, b
/2);
143 m_clocks
[1]->moveTo(sq_size
*gs
.x
+2*b
+b
/2, sq_size
*gs
.y
+b
+b
/2-m_clocks
[1]->height());
145 int x
= !!m_board
->flipped();
146 m_pools
[x
]->m_flipped
= false;
147 m_pools
[x
]->setGridWidth(3);
148 m_pools
[x
]->moveTo(sq_size
*gs
.x
+2*b
+b
/2, b
+m_clocks
[0]->height());
149 m_pools
[x
]->onResize(static_cast<int>(sq_size
*2.2/3), force_reload
);
152 m_pools
[x
]->m_flipped
= true;
153 m_pools
[x
]->setGridWidth(3);
154 m_pools
[x
]->moveTo(sq_size
*gs
.x
+2*b
+b
/2, sq_size
*gs
.y
+b
-m_clocks
[1]->height());
155 m_pools
[x
]->onResize(static_cast<int>(sq_size
*2.2/3), force_reload
);
157 m_info
->moveTo(sq_size
*gs
.x
+4*b
, 80+sq_size
*gs
.y
/2+b
+b
/2);
160 void ChessTable::resizeEvent(QResizeEvent
* /*e*/) {
164 void ChessTable::mouseReleaseEvent(QMouseEvent
* e
) {
167 m_mousegrab
->onMouseRelease(e
->pos() - m_mousegrab
->pos(), e
->button() );
171 ClickableCanvas
* cb
= eventItemAt(e
->pos());
172 if(cb
!= m_current
) {
174 m_current
->onMouseLeave();
177 cb
->onMouseMove(e
->pos() - cb
->pos(), 0);
186 void ChessTable::mousePressEvent(QMouseEvent
* e
) {
188 m_mousegrab
->onMousePress(e
->pos() - m_mousegrab
->pos(), e
->button() );
192 ClickableCanvas
* cb
= eventItemAt(e
->pos());
193 if(cb
!= m_current
) {
195 m_current
->onMouseLeave();
201 cb
->onMousePress(e
->pos() - cb
->pos(), e
->button() );
206 void ChessTable::mouseMoveEvent(QMouseEvent
* e
) {
208 m_mousegrab
->onMouseMove(e
->pos() - m_mousegrab
->pos(), e
->button() );
212 ClickableCanvas
* cb
= eventItemAt(e
->pos());
213 if(cb
!= m_current
) {
215 m_current
->onMouseLeave();
221 cb
->onMouseMove(e
->pos() - cb
->pos(), e
->button() );
224 void ChessTable::enterEvent(QEvent
*) { }
226 void ChessTable::leaveEvent(QEvent
*) {
228 m_current
->onMouseLeave();
232 void ChessTable::flip() {
236 m_pools
[i
]->flipAndMoveBy( QPoint(0, m_board
->squareSize()*8+m_board
->squareSize()*2/3) );
239 void ChessTable::flip(bool flipped
) {
240 if(m_board
->flipped() != flipped
)
244 void ChessTable::changeClock(int color
) {
245 std::cout
<< "change " << color
<< std::endl
;
246 if(m_clocks
[0]->running() || m_clocks
[1]->running())
247 for(int i
=0;i
<2;i
++) {
248 if ( (i
== color
) != m_clocks
[i
]->running() )
250 m_clocks
[i
]->start();
256 void ChessTable::updateTurn(int color
) {
257 std::cout
<< "updatet" << std::endl
;
259 m_clocks
[i
]->activate(color
== i
);
262 void ChessTable::stopClocks() {
267 void ChessTable::updateTime(int white
, int black
) {
268 std::cout
<< "update" << std::endl
;
269 m_clocks
[0]->setTime(white
);
270 m_clocks
[1]->setTime(black
);
273 void ChessTable::resetClock() {
274 std::cout
<< "reset" << std::endl
;
278 m_clocks
[i
]->setPlayer(Player());
281 void ChessTable::setPlayers(const Player
& white
, const Player
& black
) {
282 std::cout
<< "set players " << std::endl
;
283 m_clocks
[0]->setPlayer(white
);
284 m_clocks
[1]->setPlayer(black
);
287 void ChessTable::run() {
288 std::cout
<< "run" << std::endl
;
290 if(m_clocks
[i
]->active() && !m_clocks
[i
]->running())
291 m_clocks
[i
]->start();
294 void ChessTable::displayMessage(const QString
& msg
) {
295 std::cout
<< msg
<< std::endl
;