Debian Codegraph @

src/board/board.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 /*
24 * board.cpp
25 * This 'board' class handles graphics (wooden and table background,
26 * gatter, coordinates,
27 * specific cursor, stones and variaton marks.
28 * It does not know about the game tree or the matrix,
29 * and takes its orders only from boardhandler
30 */
31 #include "board.h"
32 #include "stone.h"
33 #include "gatter.h"
34 #include "mark.h"
35 #include "imagehandler.h"
36 #include "move.h" //for updateLastMove, cleaner and yet not FIXME
37 #include "graphicsitemstypes.h"
38 #include "matrix.h"
39
40 #include <QMouseEvent>
41 #include <QApplication>
42 #include <QMessageBox>
43 #include <QClipboard>
44
45 class ImageHandler;
46
47
48 /*
49 * This initialises everything on the board : background, gatter, cursor, etc ....
50 */
51 Board::Board(QWidget *parent, QGraphicsScene *c)
52 : QGraphicsView(c,parent), oneColorGo(false)
53 {
54 QSettings settings;
55
56 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58 isDisplayBoard = false;
59 lockResize = false;
60 showCoords = true;//TODO setting->readBoolEntry("BOARD_COORDS");
61 showSGFCoords = false;//TODO setting->readBoolEntry("SGF_BOARD_COORDS");
62 //antiClicko = setting->readBoolEntry("ANTICLICKO");
63
64 curX = curY = -1;
65 downX = downY = -1;
66
67 coordType = uninit;
68 marks = new QList<Mark*>;
69 stones = new QHash<int,Stone *>();
70 ghosts = new QList<Stone*>;
71 lastMoveMark = NULL;
72 gatter = NULL;
73
74 vCoords1 = new QList<QGraphicsSimpleTextItem*>;
75 hCoords1 = new QList<QGraphicsSimpleTextItem*>;
76 vCoords2 = new QList<QGraphicsSimpleTextItem*>;
77 hCoords2 = new QList<QGraphicsSimpleTextItem*>;
78
79 imageHandler = new ImageHandler();
80 Q_CHECK_PTR(imageHandler);
81
82 // Init the canvas
83 canvas = new QGraphicsScene(0,0, BOARD_X, BOARD_Y,this);
84 Q_CHECK_PTR(canvas);
85 // Set background texture
86 canvas->setBackgroundBrush(QBrush(*(ImageHandler::getTablePixmap( settings.value("SKIN_TABLE").toString()))));
87 table = canvas->addRect(QRectF(),
88 QPen(Qt::NoPen),
89 QBrush(* (ImageHandler::getBoardPixmap(settings.value("SKIN").toString()))));
90
91 //setRenderHints(QPainter::SmoothPixmapTransform);
92 setScene(canvas);
93 viewport()->setMouseTracking(true);
94
95 init(DEFAULT_BOARD_SIZE);
96
97 // Init the ghost cursor stone
98 cursor = cursorIdle;
99 curStone = new Stone(imageHandler->getGhostPixmaps(), canvas, stoneBlack, 0, 0);
100 curStone->setZValue(4);
101 curStone->hide();
102 showCursor = false;
103
104 setUpdatesEnabled(true);
105 }
106
107 Board::~Board()
108 {
109 setUpdatesEnabled(false);
110
111 delete stones;
112 delete ghosts;
113 delete marks;
114
115 delete hCoords1;
116 delete hCoords2;
117 delete vCoords1;
118 delete vCoords2;
119 delete gatter;
120 delete imageHandler;
121
122 delete canvas; // Also deletes all remaining QGraphicsItem's
123 }
124
125 void Board::init(int size)
126 {
127 board_size = size;
128 if (gatter != NULL)
129 delete gatter;
130 gatter = new Gatter(canvas, board_size);
131 setupCoords();
132 resizeBoard();
133 }
134
135 void Board::setupCoords(void)
136 {
137 qDeleteAll(*hCoords1);
138 qDeleteAll(*hCoords2);
139 qDeleteAll(*vCoords1);
140 qDeleteAll(*vCoords2);
141 hCoords1->clear();
142 hCoords2->clear();
143 vCoords1->clear();
144 vCoords2->clear();
145 QString hTxt,vTxt;
146
147 // Init the coordinates
148 QGraphicsSimpleTextItem *tmp;
149 for (int i=0; i<board_size; i++)
150 {
151 switch(coordType)
152 {
153 case sgf:
154 vTxt = QString(QChar(static_cast<const char>('a' + i)));
155 hTxt = QString(QChar(static_cast<const char>('a' + i)));
156 break;
157 case numberbottomi:
158 vTxt = QString::number(i + 1);
159 hTxt = QString(QChar(static_cast<const char>('A' + i)));
160 break;
161 case numbertopnoi:
162 default:
163 vTxt = QString::number(board_size - i);
164 hTxt = QString(QChar(static_cast<const char>('A' + (i<8?i:i+1))));
165 break;
166 }
167
168 tmp = new QGraphicsSimpleTextItem(vTxt, 0);
169 vCoords1->append(tmp);
170 canvas->addItem(tmp);
171 tmp = new QGraphicsSimpleTextItem(hTxt, 0);
172 hCoords1->append(tmp);
173 canvas->addItem(tmp);
174 tmp = new QGraphicsSimpleTextItem(vTxt, 0);
175 vCoords2->append(tmp);
176 canvas->addItem(tmp);
177 tmp = new QGraphicsSimpleTextItem(hTxt, 0);
178 hCoords2->append(tmp);
179 canvas->addItem(tmp);
180 }
181 }
182
183 /*
184 * cleans up the board
185 */
186 void Board::clearData()
187 {
188 qDeleteAll(*stones);
189 stones->clear();
190 qDeleteAll(*ghosts);
191 ghosts->clear();
192 qDeleteAll(*marks);
193 marks->clear();
194
195 update();
196 }
197
198 /*
199 * Calculates the size of the goban
200 */
201 void Board::calculateSize()
202 {
203 // Calculate the size values
204
205 // distance from table edge to wooden board edge
206 const int margin = 1,
207 w = (int)canvas->width() - margin * 2,
208 h = (int)canvas->height() - margin * 2;
209
210 int table_size = (w < h ? w : h );
211
212 // distance from edge of wooden board to playing area (grids + space for stones on 1st & last line)
213 offset = table_size * 2/100 ;
214
215
216 QGraphicsSimpleTextItem *coordV = new QGraphicsSimpleTextItem(QString::number(board_size),0);
217 QGraphicsSimpleTextItem *coordH = new QGraphicsSimpleTextItem("A",0);
218 canvas->addItem(coordV);
219 canvas->addItem(coordH);
220 int coord_width = (int)coordV->boundingRect().width();
221 int coord_height = (int)coordH->boundingRect().height();
222
223 // space for coodinates if shown
224 int coord_offset = (coord_width < coord_height ? coord_height : coord_width);
225
226 if (showCoords)
227 offset = coord_offset + 2 ;
228
229 //we need 1 more virtual 'square' for the stones on 1st and last line getting off the grid
230 square_size = (table_size - 2*offset) / (board_size);
231
232 // Should not happen, but safe is safe.
233 if (square_size == 0)
234 square_size = 1;
235
236 // grid size
237 board_pixel_size = square_size * (board_size-1) + 1 ;
238 offset = (table_size - board_pixel_size)/2;
239
240 // Center the board in canvas
241
242 offsetX = margin + (w - board_pixel_size) / 2;
243 offsetY = margin + (h - board_pixel_size) / 2;
244
245 //deletes the samples
246 delete coordV;
247 delete coordH;
248 }
249
250 /*
251 * actually draws the gatter lines
252 */
253 void Board::drawGatter()
254 {
255 gatter->resize(offsetX,offsetY,square_size);
256 }
257
258 /*
259 * draws the goban wood texture
260 */
261 void Board::drawBack()
262 {
263 table->setRect(offsetX - offset,
264 offsetY - offset,
265 board_pixel_size + offset*2,
266 board_pixel_size + offset*2);
267 }
268
269 /*
270 * Draws the coordinates around the goban grid
271 */
272 void Board::drawCoordinates()
273 {
274 QGraphicsSimpleTextItem *coord;
275
276 // centres the coordinates text within the remaining space at table edge
277 const int coord_centre = (offset - square_size/2 )/2;
278
279 for (int i=0; i<board_size; i++)
280 {
281 // Left side
282 coord = vCoords1->at(i);
283 coord->setPos(offsetX - offset + coord_centre - coord->boundingRect().width()/2 , offsetY + square_size * i - coord->boundingRect().height()/2);
284 coord->setVisible(showCoords);
285
286 // Right side
287 coord = vCoords2->at(i);
288 coord->setPos(offsetX + board_pixel_size + offset - coord_centre - coord->boundingRect().width()/2 , offsetY + square_size * i - coord->boundingRect().height()/2);
289 coord->setVisible(showCoords);
290
291 // Top
292 coord = hCoords1->at(i);
293 coord->setPos(offsetX + square_size * i - coord->boundingRect().width()/2 , offsetY - offset + coord_centre - coord->boundingRect().height()/2 );
294 coord->setVisible(showCoords);
295
296 // Bottom
297 coord = hCoords2->at(i);
298 coord->setPos(offsetX + square_size * i - coord->boundingRect().width()/2 ,offsetY + offset + board_pixel_size - coord_centre - coord->boundingRect().height()/2 );
299 coord->setVisible(showCoords);
300 }
301 }
302
303 /*
304 * called by 'boardwindow' when the toolbar button is toggled
305 */
306 void Board::setShowCoords(bool b)
307 {
308 if (b != showCoords)
309 {
310 showCoords = b;
311 resizeBoard(); // Redraw the board if the value changed.
312 }
313 }
314
315 /*
316 * the viewport is resized
317 */
318 void Board::resizeEvent(QResizeEvent*)
319 {
320 #ifdef _WS_WIN_x
321 if (!resizeDelayFlag)
322 {
323 resizeDelayFlag = true;
324 // not necessary?
325 QTimer::singleShot(50, this, resizeBoard()));
326 }
327 #else
328 if (!lockResize)
329 resizeBoard();
330 #endif
331 }
332
333 /*
334 * does the resizing work
335 */
336 void Board::resizeBoard()
337 {
338 if (width() < 30 || height() < 30)
339 return;
340
341 // Resize canvas
342 canvas->setSceneRect(0,0,width(),height());
343
344 // Recalculate the size values
345 calculateSize();
346
347 // Rescale the pixmaps in the ImageHandler
348 imageHandler->setSquareSize(square_size);
349
350 // Delete gatter lines and update stones positions
351 QList<QGraphicsItem *> list = canvas->items();
352 QGraphicsItem *item;
353
354 QListIterator<QGraphicsItem *> it( list );
355
356
357 for (; it.hasNext();)
358 {
359 item = it.next();
360 /*
361 * Coordinates : type = 9
362 */
363 // if (item->type() == 9)// || item->type() == 3)// || item->rtti() == 7)
364 // {
365 // item->hide();
366 // delete item;
367 // }
368 /*else*/ if (item->type() == RTTI_STONE)
369 {
370 Stone *s = (Stone*)item;
371 s->setColor(s->getColor());
372 s->setPos(offsetX + square_size * (s->posX() - 1) - s->pixmap().width()/2,
373 offsetY + square_size * (s->posY() - 1) - s->pixmap().height()/2 );
374
375 //TODO introduce a ghost list in the stone class so that this becomes redundant code
376 if (s->isDead())
377 s->togglePixmap(imageHandler->getGhostPixmaps(), false);
378
379
380
381 }
382 else if (item->type() >= RTTI_MARK_SQUARE &&
383 item->type() <= RTTI_MARK_TERR)
384 {
385 Mark *m;
386 switch(item->type())
387 {
388 case RTTI_MARK_SQUARE: m = (MarkSquare*)item; break;
389 case RTTI_MARK_CIRCLE: m = (MarkCircle*)item;/* m->setSmall(setting->readBoolEntry("SMALL_MARKS")); */break;
390 case RTTI_MARK_TRIANGLE: m = (MarkTriangle*)item; break;
391 case RTTI_MARK_CROSS: m = (MarkCross*)item; break;
392 case RTTI_MARK_TEXT: m = (MarkText*)item; break;
393 case RTTI_MARK_NUMBER: m = (MarkNumber*)item; break;
394 case RTTI_MARK_TERR: m = (MarkTerr*)item; break;
395 default: continue;
396 }
397 if(item->type() == RTTI_MARK_TERR && preferences.terr_stone_mark)
398 {
399 m = (MarkSmallStoneTerr *)item;
400 static_cast<MarkSmallStoneTerr *>(item)->setSize(square_size / SMALL_STONE_TERR_DIVISOR, square_size / SMALL_STONE_TERR_DIVISOR);
401 static_cast<MarkSmallStoneTerr *>(item)->setPixmap(imageHandler->getSmallStonePixmaps());
402 }
403 else
404 m->setSize(square_size, square_size);
405 m->setPos(offsetX + square_size * (m->posX() - 1) - m->getSizeX()/2.0,
406 offsetY + square_size * (m->posY() - 1) - m->getSizeY()/2.0);
407 }
408 }
409
410 // boardHandler->gotoMove(m_save);
411
412 /* FIXME sometimes this draws the lines after/on top of the marks.
413 * moving it earlier doesn't fix anything */
414
415 // Redraw the board
416 drawBack();
417 drawGatter();
418 // if (showCoords && !isDisplayBoard)
419 drawCoordinates();
420
421 // Redraw the mark on the last played stone
422 // updateLastMove(m_save->getColor(), m_save->getX(), m_save->getY());
423 }
424
425 /*
426 * sets the cursor shape over the goban,
427 * this is called by boardhandler and qgoboard proxy depending on the game mode and phase
428 */
429 void Board::setCursorType(CursorType cur)
430 {
431 if (cursor == cur)
432 return;
433
434 switch (cur)
435 {
436 case cursorIdle :
437 {
438 if (cursor == cursorNavTo || cursor == cursorWait)
439 setCursor(Qt::ArrowCursor);
440
441 showCursor = false;
442 curStone->hide();
443 break;
444 }
445
446 case cursorGhostBlack :
447 {
448 if (cursor == cursorNavTo || cursor == cursorWait)
449 setCursor(Qt::ArrowCursor);
450
451 curStone->setColor(stoneBlack);
452 showCursor = true;
453 break;
454 }
455
456 case cursorGhostWhite :
457 {
458 if (cursor == cursorNavTo || cursor == cursorWait)
459 setCursor(Qt::ArrowCursor);
460
461 curStone->setColor(stoneWhite);
462 showCursor = true;
463 break;
464 }
465
466 case cursorNavTo :
467 {
468 setCursor(Qt::PointingHandCursor);
469 showCursor = false;
470 curStone->hide();
471 break;
472 }
473
474 case cursorWait :
475 {
476 setCursor(Qt::WaitCursor);
477 showCursor = false;
478 curStone->hide();
479 break;
480 }
481 }
482
483 cursor = cur;
484 }
485
486 /*
487 * 0 : no stone
488 * -1 : hidden
489 * 1 : shown
490 */
491 int Board::hasStone(int x, int y)
492 {
493 if (!stones->contains(coordsToKey(x, y)))
494 return 0;
495
496 if (stones->find(coordsToKey(x, y)).value()->isVisible())
497 return 1;
498
499 return -1;
500 }
501
502 /*
503 * Used for setting the mark color in case a mark is set over the var mark
504 */
505 bool Board::hasVarGhost(StoneColor c, int x, int y)
506 {
507 Stone *s;
508
509 for (int i=0; i<ghosts->count(); i++)
510 {
511 s=ghosts->at(i);
512 if (s->posX() == x && s->posY() == y && s->getColor() == c)
513 return true;
514 }
515
516 return false;
517 }
518
519 /*
520 * Synchronize the board with the given stone color and coordinates
521 * This is usually called by boardhandler when scanning a matrix.
522 */
523 void Board::updateStone(StoneColor c, int x, int y, bool dead)
524 {
525 Stone *stone = stones->value(coordsToKey(x, y),NULL);
526 //if ((stone == NULL) & ((c == stoneBlack) || (c == stoneWhite)))
527
528 switch (c)
529 {
530 case stoneBlack:
531 case stoneWhite:
532 if (stone == NULL)
533 {
534 stone = new Stone(imageHandler->getStonePixmaps(), canvas, c, x, y,true);
535 stone->setPos(offsetX + square_size * (x - 1) - stone->pixmap().width()/2,
536 offsetY + square_size * (y - 1) - stone->pixmap().height()/2 );
537 stones->insert(coordsToKey(x,y) , stone);
538 break;
539 }
540
541 if (stone->getColor() != c)
542 stone->setColor(c);
543
544 // We need to check wether the stones have been toggled dead or seki before (scoring mode)
545 if ((stone->isDead() || stone->isSeki()) && !dead)
546 {
547 stone->togglePixmap(imageHandler->getStonePixmaps(), true);
548 stone->setDead(false);
549 }
550
551 if ((!stone->isDead()) && dead)
552 {
553 stone->togglePixmap(imageHandler->getGhostPixmaps(), false);
554 stone->setDead();
555 }
556
557 if (!stone->isVisible())
558 stone->show();
559
560 break;
561
562
563 case stoneNone:
564 case stoneErase:
565 if (stone && stone->isVisible())
566 {
567 stone->hide();
568 }
569 break;
570
571 default:
572 qWarning("Bad data <%d> at %d/%d in board::updateStone !",
573 c, x, y);
574 }
575 }
576
577 /*
578 * Sets a variation mark of color 'c' at positon 'x,y'
579 */
580 void Board::setVarGhost(StoneColor c, int x, int y)
581 {
582 Stone *s = NULL;
583
584 // if (setting->readIntEntry("VAR_GHOSTS") == vardisplayGhost) TODO
585 // s = new Stone(imageHandler->getGhostPixmaps(), canvas, c, x, y);
586 // else if (setting->readIntEntry("VAR_GHOSTS") == vardisplaySmallStone)
587 s = new Stone(imageHandler->getAlternateGhostPixmaps(), canvas, c, x, y, false);
588 // else
589 // return;
590
591 s->setZValue(3);//FIXME : this should not be here, but in the 'stone' code
592 /* FIXME Variation ghosts on handicap stones for some weird reason */
593 ghosts->append(s);
594
595 if (x == PASS_XY && y == PASS_XY) // Pass
596 {
597 s->setPos( offsetX + square_size * (board_size+1),
598 offsetY + square_size * board_size);
599 // setMark(board_size+2, board_size+1, markText, false, tr("Pass"), false);
600 }
601 else
602 {
603 s->setPos(offsetX + square_size * (x-1) - s->boundingRect().width()/2,
604 offsetY + square_size * (y-1) - s->boundingRect().height()/2);
605 }
606 }
607
608 /*
609 * Removes all the variation marks (when navigating into the tree)
610 */
611 void Board::removeGhosts()
612 {
613 qDeleteAll(*ghosts);
614 ghosts->clear();
615 }
616
617 /*
618 * Sets a mark of type 't' at positon 'x,y'. If a text mark : 'txt'
619 */
620 void Board::setMark(int x, int y, MarkType t, bool /*update*/, QString txt, bool overlay)
621 {
622 if (x == -1 || y == -1)
623 return;
624
625 Mark *m;
626
627 // We already have a mark on this spot? If it is of the same type,
628 // do nothing, else overwrite with the new mark.
629 if ((m = hasMark(x, y)) != NULL)
630 {
631 if (m->getType() == t && m->getType() != markText) // Text labels are overwritten
632 return;
633 removeMark(x, y);
634 }
635
636 if (lastMoveMark != NULL &&
637 lastMoveMark->posX() == x &&
638 lastMoveMark->posY() == y)
639 removeLastMoveMark();
640
641 QColor col = Qt::black;
642
643 // Black stone or black ghost underlying? Then we need a white mark.
644 if (( hasStone(x, y) == 1 && getStoneAt(x, y)->getColor() == stoneBlack) || hasVarGhost(stoneBlack, x, y))//||
645 //TODO (settings->readIntEntry("VAR_GHOSTS") && hasVarGhost(stoneBlack, x, y)))
646 col = Qt::white;
647
648 short n = -1;
649
650 switch(t)
651 {
652 case markSquare:
653 m = new MarkSquare(x, y, square_size, canvas, col);
654 break;
655
656 case markCircle:
657 m = new MarkCircle(x, y, square_size, canvas, col, true);//setting->readBoolEntry("SMALL_STONES"));
658 break;
659
660 case markTriangle:
661 m = new MarkTriangle(x, y, square_size, canvas, col);
662 break;
663
664 case markCross:
665 m = new MarkCross(x, y, square_size, canvas, col);
666 break;
667
668 case markText:
669 // if (txt == NULL)
670 // {
671 // n = 0;
672 // while (letterPool[n] && n < 51)
673 // n++;
674 // letterPool[n] = true;
675
676 // txt = QString(QChar(static_cast<const char>('A' + (n>=26 ? n+6 : n))));
677
678 // Update matrix with this letter
679 // boardHandler->getTree()->getCurrent()->getMatrix()->setMarkText(x, y, txt);
680 // }
681 /*else*/ if (txt.length() == 1)
682 {
683 // Text was given as argument, check if it can converted to a single letter
684 n = -1;
685 if (txt[0] >= 'A' && txt[0] <= 'Z')
686 n = txt[0].toLatin1() - 'A';
687 else if (txt[0] >= 'a' && txt[0] <= 'a')
688 n = txt[0].toLatin1() - 'a' + 26;
689
690 if (n > -1)
691 letterPool[n] = true;
692 }
693 m = new MarkText(x, y, square_size, txt, canvas, col, n, false, overlay);
694 gatter->hide(x,y);//setMarkText(x, y, txt);
695 break;
696
697 case markNumber:
698 // if (txt == NULL)
699 // {
700 // n = 0;
701 // while (numberPool[n] && n < 399)
702 // n++;
703 //
704 // txt = QString::number(n+1);
705 //
706 // // Update matrix with this letter
707 // boardHandler->getTree()->getCurrent()->getMatrix()->setMarkText(x, y, txt);
708 // }
709 // else
710 n = txt.toInt() - 1;
711 numberPool[n] = true;
712 m = new MarkNumber(x, y, square_size, n, canvas, col, false);
713 //FIXME why do we call "setMarkText" here and not in markText case above??
714 //I'm commenting it out, doesn't seem to change anything
715 //setMarkText(x, y, txt);
716 gatter->hide(x,y);
717 break;
718
719 case markTerrBlack:
720 if(!preferences.terr_stone_mark)
721 m = new MarkTerr(x, y, square_size, stoneBlack, canvas);
722 else
723 m = new MarkSmallStoneTerr(x, y, square_size / SMALL_STONE_TERR_DIVISOR, stoneBlack, imageHandler->getSmallStonePixmaps(), canvas);
724 if (hasStone(x, y) == 1)
725 {
726 //getStoneAt(x, y)->setDead(true);
727 //getStoneAt(x, y)->togglePixmap(imageHandler->getGhostPixmaps(), false);
728 // getStoneAt(x, y)->shadow->hide();
729 // boardHandler->markedDead = true;
730 updateStone(stoneWhite, x, y, true);
731 }
732 // boardHandler->getTree()->getCurrent()->setScored(true);
733 break;
734
735 case markTerrWhite:
736 if(!preferences.terr_stone_mark)
737 m = new MarkTerr(x, y, square_size, stoneWhite, canvas);
738 else
739 m = new MarkSmallStoneTerr(x, y, square_size / SMALL_STONE_TERR_DIVISOR, stoneWhite, imageHandler->getSmallStonePixmaps(), canvas);
740 if (hasStone(x, y) == 1)
741 {
742 //getStoneAt(x, y)->setDead(true);
743 //getStoneAt(x, y)->togglePixmap(imageHandler->getGhostPixmaps(), false);
744 // boardHandler->getStoneHandler()->getStoneAt(x, y)->shadow->hide();
745 // boardHandler->markedDead = true;
746 updateStone(stoneBlack, x, y, true);
747 }
748 // boardHandler->getTree()->getCurrent()->setScored(true);
749 break;
750
751 default:
752 qWarning(" *** Board::setMark() - Bad mark type! ***");
753 return;
754 }
755
756 Q_CHECK_PTR(m);
757 m->setPos(offsetX + square_size * (x-1) - m->getSizeX()/2 , offsetY + square_size * (y-1) - m->getSizeY()/2);
758 m->show();
759
760 marks->append(m);
761 // if (update)
762 // boardHandler->editMark(x, y, t, txt);
763 }
764
765 /*
766 * Sets a text in 'text' mark at positon 'x,y'.
767 */
768 void Board::setMarkText(int x, int y, const QString &txt)
769 {
770 Mark *m;
771
772 // Oops, no mark here, or no text mark
773 if (txt.isNull() || txt.isEmpty() ||
774 (m = hasMark(x, y)) == NULL || m->getType() != markText)
775 return;
776
777 m->setText(txt);
778 // Adjust the position on the board, if the text size has changed.
779 m->setSize((double)square_size, (double)square_size);
780 m->setPos(offsetX + square_size * (x-1) - m->getSizeX()/2 , offsetY + square_size * (y-1) - m->getSizeY()/2);
781 }
782
783 /*
784 * Queries wether there is a mark at position 'x,y'
785 */
786 Mark* Board::hasMark(int x, int y)
787 {
788 Mark *m = NULL;
789
790 // for (m=marks->first(); m != NULL; m=marks->next())
791 for (int i=0; i<marks->count(); i++)
792 {
793 m=marks->at(i);
794 if (m->posX() == x && m->posY() == y)
795 return m;
796 }
797
798 return NULL;
799 }
800
801 /*
802 * Removes the mark at position 'x,y'
803 */
804 void Board::removeMark(int x, int y, bool /*update*/)
805 {
806 Mark *m = NULL;
807
808 if (lastMoveMark != NULL &&
809 lastMoveMark->posX() == x &&
810 lastMoveMark->posY() == y)
811 removeLastMoveMark();
812
813 // for (m=marks->first(); m != NULL; m=marks->next())
814 for (int i=0; i<marks->count(); i++)
815 {
816 m=marks->at(i);
817 if (m->posX() == x && m->posY() == y)
818 {
819 if (m->getCounter() != -1)
820 {
821 if (m->getType() == markText)
822 letterPool[m->getCounter()] = false;
823 else if (m->getType() == markNumber)
824 numberPool[m->getCounter()] = false;
825 }
826 delete marks->takeAt(i);
827 gatter->show(x,y);
828 // if (update)
829 // boardHandler->editMark(x, y, markNone);
830 return;
831 }
832 }
833 }
834
835 /*
836 * Removes the mark on the last stone played
837 */
838 void Board::removeLastMoveMark()
839 {
840 if (lastMoveMark != NULL)
841 {
842 // lastMoveMark->hide();
843 delete lastMoveMark;
844 lastMoveMark = NULL;
845 }
846 }
847
848 /*
849 * Updates the mark on the last stone played
850 */
851 void Board::updateLastMove(Move * move)
852 {
853 StoneColor c = move->getColor();
854 int x = move->getX();
855 int y = move->getY();
856
857 delete lastMoveMark;
858 lastMoveMark = NULL;
859
860 if (x == PASS_XY && y == PASS_XY) // Passing (FIXME don't think we use this anymore)
861 removeLastMoveMark();
862 else if(move->isHandicapMove()) {} // no last move marks on handicaps
863 else if (c != stoneNone && x != -1 && y != -1 && x <= board_size && y <= board_size)
864 {
865
866 if(preferences.number_current_move)
867 {
868 //possible FIXME, MarkNumber adds 1 in mark.h to the move number ?!
869 lastMoveMark = new MarkNumber(x, y, square_size, move->getMoveNumber() - 1, canvas,
870 c == stoneBlack ? Qt::white : Qt::black, false);
871 }
872 else
873 {
874 lastMoveMark = new MarkCross(x, y, square_size, canvas,
875 c == stoneBlack ? Qt::white : Qt::black, true);
876 }
877 Q_ASSERT(lastMoveMark);
878 // lastMoveMark = new MarkCircle(x, y, square_size, canvas,
879 // c == stoneBlack ? white : black, setting->readBoolEntry("SMALL_STONES"));
880 lastMoveMark->setPos(offsetX + square_size * (x-1) - lastMoveMark->getSizeX()/2,
881 offsetY + square_size * (y-1) - lastMoveMark->getSizeY()/2);
882 lastMoveMark->show();
883 }
884
885 // setCurStoneColor();
886 }
887
888 /*
889 * Used to know where the mouse is over the goban
890 */
891 int Board::convertCoordsToPoint(int c, int o)
892 {
893 int p = c - o + square_size/2;
894 if (p >= 0)
895 return p / square_size + 1;
896 else
897 return -1;
898 }
899
900 /*
901 * Called when the mouse pointer leaves the goban
902 */
903 void Board::leaveEvent(QEvent*)
904 {
905 curStone->hide();
906 }
907
908 /*
909 * Sets the ghost cursor depending on the arrow position
910 */
911 void Board::mouseMoveEvent ( QMouseEvent * e )
912 {
913
914 if (!showCursor)
915 return;
916
917 int x = convertCoordsToPoint(e->x(), offsetX),
918 y = convertCoordsToPoint(e->y(), offsetY);
919
920 /* FIXME, maybe don't draw cursor if x/y changes from downX downY?? */
921 if(downX > 0 && (downX != x || downY != y))
922 {
923 curStone->hide();
924 curX = curY = -1;
925 return;
926 }
927 // Outside the valid board?
928 if ((x < 1) || x > board_size || y < 1 || y > board_size)
929 {
930 curStone->hide();
931 curX = curY = -1;
932 return;
933 }
934
935 // Nothing changed
936 if (curX == (short)x && curY == (short)y)
937 return;
938
939 // Update the statusbar coords tip
940 // emit coordsChanged(x, y, board_size,showSGFCoords);
941
942 // Remember if the cursor was hidden meanwhile.
943 // If yes, we need to repaint it at the old position.
944 // bool flag = curX == -1;
945
946 curX = (short)x;
947 curY = (short)y;
948
949 // if (// !showCursor || setting->readBoolEntry("CURSOR") ||
950 // (gamePhase == phaseEdit /*&& boardHandler->getMarkType() != markNone*/) ||
951 // gamePhase == phaseScore ||
952 // (curStone->posX() == x && curStone->posY() == y && !flag))
953 // return;
954
955 curStone->setPos(offsetX + square_size * (x - 1) - curStone->pixmap().width()/2,
956 offsetY + square_size * (y - 1) - curStone->pixmap().height()/2 );
957 // curStone->setX(offsetX + square_size * (x-1));
958 // curStone->setY(offsetY + square_size * (y-1));
959 curStone->setCoord(x, y);
960 /*
961 bool notMyTurn = (curStone->getColor() == stoneBlack && !myColorIsBlack ||
962 curStone->getColor() == stoneWhite && myColorIsBlack);
963
964 if (navIntersectionStatus ||
965 boardHandler->getGameMode() == modeObserve ||
966 ( boardHandler->getGameMode() == modeMatch && notMyTurn) ||
967 ( boardHandler->getGameMode() == modeComputer && notMyTurn))
968
969 curStone->hide();
970 else
971 */
972 curStone->show();
973 }
974
975 /*
976 * mouse wheel has been turned
977 */
978 void Board::wheelEvent(QWheelEvent *e)
979 {
980 emit signalWheelEvent( e);
981 }
982
983 /*
984 * starts the counter for anticlivko moves
985 */
986 void Board::mousePressEvent(QMouseEvent * e)
987 {
988 downX = convertCoordsToPoint(e->x(), offsetX),
989 downY = convertCoordsToPoint(e->y(), offsetY);
990
991 // Button gesture outside the board?
992 if ((downX < 1) || downX > board_size || downY < 1 || downY > board_size)
993 {
994 downX = downY = -1;
995 //return //??
996 }
997 clickTime = QTime::currentTime();
998 clickTime = clickTime.addMSecs(250);
999 }
1000
1001 void Board::mouseReleaseEvent(QMouseEvent* e)
1002 {
1003 mouseState = e->button();
1004
1005 int x = convertCoordsToPoint(e->x(), offsetX),
1006 y = convertCoordsToPoint(e->y(), offsetY);
1007
1008 /* FIXME click protection... if we don't like, we should change it */
1009 if(downX != x || downY != y)
1010 {
1011 downX = downY = -1;
1012 return;
1013 }
1014 downX = downY = -1;
1015 // Button gesture outside the board?
1016 if ((x < 1) || x > board_size || y < 1 || y > board_size)
1017 /* {
1018 if (e->button() == LeftButton &&
1019 e->state() == RightButton)
1020 previousMove();
1021 else if (e->button() == RightButton &&
1022 e->state() == LeftButton)
1023 nextMove();
1024 else if (e->button() == LeftButton &&
1025 e->state() == MidButton)
1026 gotoVarStart();
1027 else if (e->button() == RightButton &&
1028 e->state() == MidButton)
1029 gotoNextBranch();
1030 */
1031 return;
1032 // }
1033 /*
1034 // Lock accidental gesture over board
1035 if ((e->button() == Qt::LeftButton && e->state() == Qt::RightButton) ||
1036 (e->button() == Qt::RightButton && e->state() == Qt::LeftButton) ||
1037 (e->button() == Qt::LeftButton && e->state() == Qt::MidButton) ||
1038 (e->button() == Qt::RightButton && e->state() == Qt::MidButton))
1039 return;
1040 */
1041 // Check delay
1042 bool delay = (QTime::currentTime() > clickTime);
1043 //note that apparently nothing uses delay at the moment
1044 emit signalClicked(delay, x , y , mouseState);
1045 }
1046
1047 /*
1048 * called when using a button menu 'export'
1049 * saves the board image and puts it to clipboard or file
1050 */
1051 void Board::exportPicture(const QString &fileName, QString *filter, bool toClipboard)
1052 {
1053
1054 QPixmap pix = this->grab(table->boundingRect().toAlignedRect());
1055
1056 if (toClipboard)
1057 {
1058 QApplication::clipboard()->setPixmap(pix);
1059 return;
1060 }
1061
1062
1063 if (!pix.save(fileName, filter->toLatin1()))
1064 QMessageBox::warning(this, PACKAGE, tr("Failed to save image!"));
1065 }
1066
1067 bool Board::updateAll(Move * move)
1068 {
1069 Matrix * m = move->getMatrix();
1070 Q_CHECK_PTR(m);
1071
1072 bool modified = false;//, fake = false;
1073 StoneColor color;
1074 bool dead;
1075 /*
1076 * Synchronize the matrix with the stonehandler data and
1077 * update the canvas.
1078 * This is usually called when navigating through the tree.
1079 */
1080 for (int y=1; y<=board_size; y++)
1081 {
1082 for (int x=1; x<=board_size; x++)
1083 {
1084 /* FIXME apparently matrix uses negative values for
1085 * both dead and edited stones. I think the
1086 * assumption is that we won't be editing during
1087 * a score phase which is the only time things will
1088 * be marked dead whereas other ghosts are used
1089 * for variations.
1090 * I'm not sure what the consequences on editing
1091 * will be for this, but for now, I just want to
1092 * make sure the handicap stones can't be ghosted.
1093 * We could say that the handicap stones aren't
1094 * edits, but this is what they've been set up
1095 * as so that's more tricky. */
1096 dead = (m->isStoneDead(x, y)) & (move->getMoveNumber() != 0);
1097 color = m->getStoneAt(x, y);
1098
1099 if (oneColorGo && color == stoneBlack)
1100 color = stoneWhite;
1101 updateStone(color,x,y, dead);
1102
1103 switch (m->getMarkAt(x, y))
1104 {
1105 case markKoMarker:
1106 case markSquare:
1107 modified = true;
1108 setMark(x, y, markSquare, false);
1109 break;
1110
1111 case markCircle:
1112 modified = true;
1113 setMark(x, y, markCircle, false);
1114 break;
1115
1116 case markTriangle:
1117 modified = true;
1118 setMark(x, y, markTriangle, false);
1119 break;
1120
1121 case markCross:
1122 modified = true;
1123 setMark(x, y, markCross, false);
1124 break;
1125
1126 case markText:
1127 modified = true;
1128 setMark(x, y, markText, false, m->getMarkText(x, y));
1129 break;
1130
1131 case markNumber:
1132 modified = true;
1133 setMark(x, y, markNumber, false, m->getMarkText(x, y));
1134 break;
1135
1136 case markTerrBlack:
1137 modified = true;
1138 setMark(x, y, markTerrBlack, false);
1139 break;
1140
1141 case markTerrWhite:
1142 modified = true;
1143 setMark(x, y, markTerrWhite, false);
1144 break;
1145
1146 case markNone:
1147 case markTerrDame:
1148 if (hasMark(x, y))
1149 {
1150 modified = true;
1151 removeMark(x, y, false);
1152 }
1153 default:
1154 break;
1155 }
1156 }
1157 }
1158 return modified;
1159 }
1160
1161 /*
1162 * Update the variation marks on the board if any
1163 */
1164 void Board::updateVariationGhosts(Move *move)
1165 {
1166 // qDebug("BoardHandler::updateVariationGhosts()");
1167 Move *m = move->parent->son;
1168 Q_CHECK_PTR(m);
1169
1170 do
1171 {
1172 setVarGhost(m->getColor(), m->getX(), m->getY());
1173 } while ((m = m->brother) != NULL);
1174 }