| 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 | #include "gatter.h" |
| 23 | |
| 24 | #include <QGraphicsItem> |
| 25 | |
| 26 | /************************************************************* |
| 27 | * |
| 28 | * Initialises the gatter intersections and hoshis points |
| 29 | * |
| 30 | *************************************************************/ |
| 31 | Gatter::Gatter(QGraphicsScene *canvas, int size) |
| 32 | { |
| 33 | board_size = size; |
| 34 | |
| 35 | VGatter.reserve(board_size*board_size); |
| 36 | HGatter.reserve(board_size*board_size); |
| 37 | QGraphicsLineItem *tmp1; |
| 38 | for (int i=0; i<board_size*board_size; i++) |
| 39 | { |
| 40 | tmp1 = new QGraphicsLineItem(0); |
| 41 | canvas->addItem(tmp1); |
| 42 | VGatter.append(tmp1); |
| 43 | tmp1 = new QGraphicsLineItem(0); |
| 44 | canvas->addItem(tmp1); |
| 45 | HGatter.append(tmp1); |
| 46 | } |
| 47 | |
| 48 | int edge_dist = (board_size > 12 ? 4 : 3); |
| 49 | int low = edge_dist - 1; |
| 50 | int middle = (board_size + 1) / 2 - 1; |
| 51 | int high = board_size - edge_dist; |
| 52 | if (board_size % 2 && board_size >= 9) |
| 53 | { |
| 54 | hoshisList.insert(middle*board_size + middle , new QGraphicsEllipseItem(0)); |
| 55 | } |
| 56 | if (board_size % 2 && board_size > 13) |
| 57 | { |
| 58 | hoshisList.insert(middle*board_size + low , new QGraphicsEllipseItem(0)); |
| 59 | hoshisList.insert(middle*board_size + high , new QGraphicsEllipseItem(0)); |
| 60 | hoshisList.insert(low*board_size + middle , new QGraphicsEllipseItem(0)); |
| 61 | hoshisList.insert(high*board_size + middle , new QGraphicsEllipseItem(0)); |
| 62 | } |
| 63 | hoshisList.insert(low*board_size + low , new QGraphicsEllipseItem(0)); |
| 64 | hoshisList.insert(high*board_size + low , new QGraphicsEllipseItem(0)); |
| 65 | hoshisList.insert(high*board_size + high , new QGraphicsEllipseItem(0)); |
| 66 | hoshisList.insert(low*board_size + high , new QGraphicsEllipseItem(0)); |
| 67 | |
| 68 | |
| 69 | QMapIterator<int,QGraphicsEllipseItem*> it( hoshisList ); |
| 70 | |
| 71 | while ( it.hasNext() ) |
| 72 | { |
| 73 | it.next(); |
| 74 | it.value()->setBrush(Qt::SolidPattern); |
| 75 | it.value()->setPen(Qt::NoPen); |
| 76 | canvas->addItem(it.value()); |
| 77 | } |
| 78 | |
| 79 | showAll(); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Destroys the gatter |
| 84 | */ |
| 85 | Gatter::~Gatter() |
| 86 | { |
| 87 | qDeleteAll(HGatter); |
| 88 | qDeleteAll(VGatter); |
| 89 | qDeleteAll(hoshisList); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | * Calculates the gatter intersections and hoshis position |
| 95 | */ |
| 96 | void Gatter::resize(int offsetX, int offsetY, int square_size) |
| 97 | { |
| 98 | int size = square_size / 5; |
| 99 | |
| 100 | // Round size top be odd (hoshis) |
| 101 | if (size % 2 == 0) |
| 102 | size--; |
| 103 | if ((size < 7) && (size>2)) |
| 104 | size = 7; |
| 105 | else if (size <= 2) |
| 106 | size = 3; |
| 107 | |
| 108 | |
| 109 | // indexOf accepts coordinated between 1 and board_size, FIXME iterator |
| 110 | for (int i=0; i<board_size; i++) |
| 111 | for (int j=0; j<board_size; j++) |
| 112 | { |
| 113 | HGatter[indexOf(i+1,j+1)]->setLine(int(offsetX + square_size * ( i - 0.5*(i!=0))), |
| 114 | offsetY + square_size * j, |
| 115 | int(offsetX + square_size * ( i + 0.5 * (i+1 != board_size))), |
| 116 | offsetY + square_size * j ); |
| 117 | |
| 118 | VGatter[indexOf(i+1,j+1)]->setLine(offsetX + square_size * i, |
| 119 | int(offsetY + square_size * ( j - 0.5*(j!=0))), |
| 120 | offsetX + square_size * i, |
| 121 | int(offsetY + square_size * ( j + 0.5 * (j+1 != board_size)))); |
| 122 | } |
| 123 | |
| 124 | QMapIterator<int,QGraphicsEllipseItem*> it( hoshisList ); |
| 125 | |
| 126 | while ( it.hasNext() ) |
| 127 | { |
| 128 | it.next(); |
| 129 | int x = it.key() / board_size; |
| 130 | int y = it.key() % board_size; |
| 131 | it.value()->setRect(offsetX + square_size * x - size/2, |
| 132 | offsetY + square_size * y - size/2, |
| 133 | size , |
| 134 | size ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Resets all intersections and hoshis to be shown |
| 140 | */ |
| 141 | void Gatter::showAll() |
| 142 | { |
| 143 | for (int i=0; i<board_size*board_size; i++) |
| 144 | { |
| 145 | VGatter[i]->show(); |
| 146 | HGatter[i]->show(); |
| 147 | } |
| 148 | |
| 149 | QMapIterator<int,QGraphicsEllipseItem*> it( hoshisList ); |
| 150 | |
| 151 | while ( it.hasNext() ) |
| 152 | { |
| 153 | it.next(); |
| 154 | it.value()->show(); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Hides an intersection (when placing a letter mark) |
| 161 | */ |
| 162 | void Gatter::hide(int x, int y) |
| 163 | { |
| 164 | if (( x<1) || (x > board_size) || ( y<1) || (y > board_size)) |
| 165 | return; |
| 166 | |
| 167 | VGatter[indexOf(x,y)]->hide(); |
| 168 | HGatter[indexOf(x,y)]->hide(); |
| 169 | |
| 170 | if (hoshisList.contains(indexOf(x,y))) |
| 171 | hoshisList.value(indexOf(x,y))->hide(); |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * shows an intersection (when removing a letter mark) |
| 176 | */ |
| 177 | void Gatter::show(int x, int y) |
| 178 | { |
| 179 | if (( x<1) || (x > board_size) || ( y<1) || (y > board_size)) |
| 180 | return; |
| 181 | |
| 182 | VGatter[indexOf(x,y)]->show(); |
| 183 | HGatter[indexOf(x,y)]->show(); |
| 184 | |
| 185 | if (hoshisList.contains(indexOf(x,y))) |
| 186 | hoshisList.value(indexOf(x,y))->show(); |
| 187 | } |