| 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 | #include "mark.h" |
| 24 | #include "imagehandler.h" |
| 25 | |
| 26 | Mark::Mark(int x, int y) |
| 27 | : myX(x), myY(y) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | Mark::~Mark() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | * MarkSquare |
| 37 | */ |
| 38 | |
| 39 | MarkSquare::MarkSquare(int x, int y, double size, QGraphicsScene *canvas, QColor col) |
| 40 | : QGraphicsRectItem(0), |
| 41 | Mark(x, y) |
| 42 | { |
| 43 | canvas->addItem(this); |
| 44 | // if (setting->readBoolEntry("SMALL_MARKS")) |
| 45 | // size_d *= 0.85; |
| 46 | |
| 47 | // if (setting->readBoolEntry("BOLD_MARKS")) |
| 48 | setPen(QPen(col, 2)); |
| 49 | // else |
| 50 | // setPen(QPen(col, 1)); |
| 51 | setSize(size, size); |
| 52 | setZValue(10); |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * MarkCircle |
| 57 | */ |
| 58 | |
| 59 | MarkCircle::MarkCircle(int x, int y, double size,QGraphicsScene *canvas, QColor col, bool s) |
| 60 | : QGraphicsEllipseItem(0), |
| 61 | Mark(x, y), |
| 62 | small(s) |
| 63 | { |
| 64 | canvas->addItem(this); |
| 65 | // if (setting->readBoolEntry("BOLD_MARKS")) |
| 66 | setPen(QPen(col, 2)); |
| 67 | // else |
| 68 | // setPen(QPen(col, 1)); |
| 69 | setSize(size, size); |
| 70 | setZValue(10); |
| 71 | } |
| 72 | /* |
| 73 | void MarkCircle::drawShape(QPainter & p) |
| 74 | { |
| 75 | p.setPen(pen()); |
| 76 | p.drawEllipse((int)(x()-rect().width()/2.0+0.5),(int)( y()-rect().height()/2.0+0.5), (int)rect().width(), (int)rect().height()); |
| 77 | } |
| 78 | */ |
| 79 | void MarkCircle::setSize(double x, double y) |
| 80 | { |
| 81 | double m; |
| 82 | // if (setting->readBoolEntry("SMALL_MARKS")) |
| 83 | // m = 0.4; |
| 84 | // else |
| 85 | m = 0.5; |
| 86 | |
| 87 | if (!small) |
| 88 | { |
| 89 | x *= 1.25*m; |
| 90 | y *= 1.25*m; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | x *= m; |
| 95 | y *= m; |
| 96 | } |
| 97 | |
| 98 | QGraphicsEllipseItem::setRect(rect().x()/*myX*/, rect().y()/*myY*/, x, y); |
| 99 | |
| 100 | |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * MarkTriangle |
| 105 | */ |
| 106 | MarkTriangle::MarkTriangle(int x, int y, double s, QGraphicsScene *canvas, QColor col) |
| 107 | : QGraphicsPolygonItem(0), |
| 108 | Mark(x, y) |
| 109 | { |
| 110 | canvas->addItem(this); |
| 111 | // if (setting->readBoolEntry("BOLD_MARKS")) |
| 112 | setPen(QPen(col, 2)); |
| 113 | // else |
| 114 | |
| 115 | pa = QPolygon(3); |
| 116 | |
| 117 | // setPen(QPen(col, 1)); |
| 118 | setSize(s, s); |
| 119 | // setPolygon(pa); |
| 120 | setZValue(10); |
| 121 | } |
| 122 | /* |
| 123 | void MarkTriangle::drawShape(QPainter &p) |
| 124 | { |
| 125 | p.setPen(pen()); |
| 126 | p.drawPolygon(pa);//poly); |
| 127 | } |
| 128 | */ |
| 129 | void MarkTriangle::setSize(double w, double) |
| 130 | { |
| 131 | // if (setting->readBoolEntry("SMALL_MARKS")) |
| 132 | // size = (int)(w*0.45); |
| 133 | // else |
| 134 | size = (w*0.55); |
| 135 | |
| 136 | // QPolygon pa(3); |
| 137 | // pa[0] = QPoint(0, 0); |
| 138 | // pa[1] = QPoint(size/2, -size); |
| 139 | // pa[2] = QPoint(size, 0); |
| 140 | pa.setPoint(0,(int)(size/2), 0); |
| 141 | pa.setPoint(1,0, (int)size); |
| 142 | pa.setPoint(2,(int)size, (int)size); |
| 143 | setPolygon(pa); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * MarkCross |
| 148 | */ |
| 149 | |
| 150 | MarkCross::MarkCross(int x, int y, double s, QGraphicsScene *canvas, QColor col, bool plus) |
| 151 | : QGraphicsLineItem(0), |
| 152 | Mark(x, y), size(s) |
| 153 | { |
| 154 | canvas->addItem(this); |
| 155 | |
| 156 | plussign = plus; |
| 157 | ol = NULL; |
| 158 | |
| 159 | int penWidth; |
| 160 | // if (setting->readBoolEntry("BOLD_MARKS")) |
| 161 | penWidth = 2; |
| 162 | // else |
| 163 | // penWidth = 1; |
| 164 | |
| 165 | setPen(QPen(col, penWidth)); |
| 166 | setSize(s, s); |
| 167 | setZValue(10); |
| 168 | |
| 169 | ol = new MarkOtherLine(canvas); |
| 170 | if (plussign) |
| 171 | ol->setLine(0, size/2, size, size/2); |
| 172 | else |
| 173 | ol->setLine(0, size, size, 0); |
| 174 | ol->setPen(QPen(col, penWidth)); |
| 175 | ol->setZValue(10); |
| 176 | ol->show(); |
| 177 | } |
| 178 | |
| 179 | MarkCross::~MarkCross() |
| 180 | { |
| 181 | if (ol != NULL) |
| 182 | ol->hide(); |
| 183 | /* There's something funny here. A double free with |
| 184 | * something later. FIXME. Here's a start on it. */ |
| 185 | //delete ol; |
| 186 | //ol = NULL; |
| 187 | hide(); |
| 188 | } |
| 189 | |
| 190 | void MarkCross::setSize(double w, double) |
| 191 | { |
| 192 | // if (setting->readBoolEntry("SMALL_MARKS")) |
| 193 | size = (w*0.45); |
| 194 | // else |
| 195 | // size = (int)(w*0.55); |
| 196 | |
| 197 | if (plussign) |
| 198 | { |
| 199 | setLine(size/2, 0, size/2, size); |
| 200 | if (ol != NULL) |
| 201 | ol->setLine(0, size/2, size, size/2); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | setLine(0, 0, size, size); |
| 206 | if (ol != NULL) |
| 207 | ol->setLine(0, size, size, 0); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void MarkCross::setColor(const QColor &col) |
| 212 | { |
| 213 | QGraphicsLineItem::setPen(col); |
| 214 | if (ol != NULL) |
| 215 | ol->setPen(col); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * MarkText |
| 220 | */ |
| 221 | |
| 222 | bool MarkText::useBold = false; |
| 223 | int MarkText::maxLength = 1; |
| 224 | |
| 225 | MarkText::MarkText( int x, int y, double size, const QString &txt, |
| 226 | QGraphicsScene *canvas, QColor col, short c, bool bold, bool /*overlay*/) |
| 227 | : QGraphicsSimpleTextItem(txt,0), |
| 228 | Mark(x, y), curSize(size), counter(c) |
| 229 | { |
| 230 | canvas->addItem(this); |
| 231 | |
| 232 | // rect = NULL; |
| 233 | useBold = bold; |
| 234 | // maxLength = 1; |
| 235 | |
| 236 | if (text().length() > maxLength) |
| 237 | maxLength = text().length(); |
| 238 | |
| 239 | setSize(size, size); |
| 240 | setColor(col); |
| 241 | |
| 242 | setZValue(10); |
| 243 | } |
| 244 | |
| 245 | MarkText::~MarkText() |
| 246 | { |
| 247 | // if (rect != NULL) |
| 248 | // rect->hide(); |
| 249 | // delete rect; |
| 250 | // hide(); |
| 251 | } |
| 252 | |
| 253 | /* FIXME This changes the pen whenever any mark is "setSize"d for all |
| 254 | * marks. It looks really bad for high numbers since they alter the |
| 255 | * low numbers and the low numbers keep that small size, even when |
| 256 | * one starts a new board. Which is actually weird and likely because |
| 257 | * the old marks were not deleted and are still being drawn !!!! |
| 258 | * But I see the logic in having a common font size in general which |
| 259 | * means either changing it for text versus numbers or having it adjust. |
| 260 | * The deletion of marks though is a serious issue !!! */ |
| 261 | void MarkText::setSize(double x, double) |
| 262 | { |
| 263 | curSize = x; |
| 264 | |
| 265 | // if (setting->readBoolEntry("SMALL_MARKS")) |
| 266 | x *= 0.5; |
| 267 | // else |
| 268 | // x *= 0.6; |
| 269 | |
| 270 | // adjust font size, so all labels have the same size. |
| 271 | // if (setting->readBoolEntry("ADJ_FONT") && maxLength > 1) |
| 272 | x /= (double)(maxLength);// * 0.6; |
| 273 | |
| 274 | // QFont f = setting->fontMarks; |
| 275 | QFont f("",(int) x);// = QFont::QFont(); |
| 276 | // if (setting->readBoolEntry("VAR_FONT")) |
| 277 | |
| 278 | // font().setPointSize((int)x); |
| 279 | |
| 280 | // f.setPointSize((int)x); |
| 281 | f.setBold(false); |
| 282 | f.setStyleStrategy(QFont::NoAntialias); |
| 283 | // f.setWeight(10); |
| 284 | setFont(f); |
| 285 | |
| 286 | width = (int)boundingRect().width(); |
| 287 | height = (int)boundingRect().height(); |
| 288 | |
| 289 | |
| 290 | } |
| 291 | |
| 292 | MarkSmallStoneTerr::MarkSmallStoneTerr(int x, int y, double s, StoneColor c, QList<QPixmap> * p, QGraphicsScene *canvas) |
| 293 | : Mark(x, y), QGraphicsPixmapItem(0), _x(x), _y(y), col(c), size(s) |
| 294 | { |
| 295 | canvas->addItem(this); |
| 296 | |
| 297 | //FIXME the ZValue keeps the gatter and hoshi marks from messing with |
| 298 | //the territory marks, but I get the feeling that ZValues are inconsistently |
| 299 | //used and that the gatter drawing could be significantly sped up |
| 300 | setZValue(1); |
| 301 | setPixmap(p); |
| 302 | } |
| 303 | |
| 304 | void MarkSmallStoneTerr::setPixmap(QList <QPixmap> * p) |
| 305 | { |
| 306 | if (p->count() <= 2) |
| 307 | QGraphicsPixmapItem::setPixmap(p->at( col == stoneBlack ? 0 : 1)); |
| 308 | else |
| 309 | QGraphicsPixmapItem::setPixmap(p->at( col == stoneBlack ? 0 : (rand() % (p->count() -2) ) + 1)); |
| 310 | } |