| 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 | #ifndef MARK_H |
| 24 | #define MARK_H |
| 25 | |
| 26 | #include "graphicsitemstypes.h" |
| 27 | #include "defines.h" |
| 28 | |
| 29 | #include <QGraphicsItem> |
| 30 | #include <QGraphicsView> |
| 31 | |
| 32 | class Mark |
| 33 | { |
| 34 | public: |
| 35 | Mark(int x, int y); |
| 36 | virtual ~Mark(); |
| 37 | virtual int type() const = 0; |
| 38 | virtual MarkType getType() const = 0; |
| 39 | virtual void setPos(double,double) = 0; |
| 40 | // virtual void setY(double) = 0; |
| 41 | virtual void setSize(double, double) = 0; |
| 42 | virtual double getSizeX() const = 0; |
| 43 | virtual double getSizeY() const = 0; |
| 44 | virtual void show() = 0; |
| 45 | virtual void hide() = 0; |
| 46 | virtual void setColor(const QColor &col) = 0; |
| 47 | virtual const QColor getColor() = 0; |
| 48 | virtual void setText(const QString&) { } |
| 49 | virtual short getCounter() { return -1; } |
| 50 | virtual void setCounter(short) { } |
| 51 | int posX() const { return myX; } |
| 52 | int posY() const { return myY; } |
| 53 | virtual void setSmall(bool) { } |
| 54 | // QCanvasRectangle *rect; |
| 55 | protected: |
| 56 | int myX, myY; |
| 57 | |
| 58 | |
| 59 | }; |
| 60 | |
| 61 | class MarkSquare : public QGraphicsRectItem, public Mark |
| 62 | { |
| 63 | public: |
| 64 | MarkSquare(int x, int y, double size, QGraphicsScene *canvas, QColor col=Qt::black); |
| 65 | virtual ~MarkSquare() {/* hide(); */} |
| 66 | virtual int type() const { return RTTI_MARK_SQUARE; } |
| 67 | virtual MarkType getType() const { return markSquare; } |
| 68 | virtual void setPos(double x, double y) { QGraphicsRectItem::setPos((qreal)x, (qreal)y); } |
| 69 | // virtual void setY(double y) { QGraphicsRectItem::setY(y); } |
| 70 | virtual void setSize(double x, double y) { setRect(rect().x()/*myX*/,rect().y()/* myY*/, x*0.5, y*0.5); } |
| 71 | virtual double getSizeX() const { return (int) rect().width(); } |
| 72 | virtual double getSizeY() const { return (int) rect().height(); } |
| 73 | virtual void show() { QGraphicsRectItem::show(); } |
| 74 | virtual void hide() { QGraphicsRectItem::hide(); } |
| 75 | virtual void setColor(const QColor &col) { setPen(QPen(col, 1)); } |
| 76 | virtual const QColor getColor() { return pen().color(); } |
| 77 | }; |
| 78 | |
| 79 | class MarkCircle : public QGraphicsEllipseItem, public Mark |
| 80 | { |
| 81 | public: |
| 82 | MarkCircle(int x, int y, double size, QGraphicsScene *canvas, QColor col=Qt::black, bool s=false); |
| 83 | virtual ~MarkCircle() { /*hide();*/ } |
| 84 | virtual int type() const { return RTTI_MARK_CIRCLE; } |
| 85 | virtual MarkType getType() const { return markCircle; } |
| 86 | virtual void setPos(double x, double y) { QGraphicsEllipseItem::setPos((qreal)x /*- rect().width()/2.0*/, (qreal)y /*- rect().height()*/); } |
| 87 | // virtual void setX(double x) { QGraphicsEllipseItem::setX(x + width()/2.0); } |
| 88 | // virtual void setY(double y) { QGraphicsEllipseItem::setY(y + width()/2.0); } |
| 89 | virtual void setSize(double x, double y); |
| 90 | virtual double getSizeX() const { return (int)rect().width(); } |
| 91 | virtual double getSizeY() const { return (int)rect().height(); } |
| 92 | virtual void show() { QGraphicsEllipseItem::show(); } |
| 93 | virtual void hide() { QGraphicsEllipseItem::hide(); } |
| 94 | virtual void setColor(const QColor &col) { setPen(QPen(col, 1)); } |
| 95 | virtual const QColor getColor() { return pen().color(); } |
| 96 | virtual void setSmall(bool s) { small = s; } |
| 97 | |
| 98 | protected: |
| 99 | // virtual void drawShape(QPainter & p); |
| 100 | void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) { painter->setRenderHints(QPainter::Antialiasing); QGraphicsEllipseItem::paint(painter, option, widget);} |
| 101 | bool small; |
| 102 | }; |
| 103 | |
| 104 | class MarkTriangle : public QGraphicsPolygonItem, public Mark |
| 105 | { |
| 106 | public: |
| 107 | MarkTriangle(int x, int y, double size, QGraphicsScene *canvas, QColor col=Qt::black); |
| 108 | virtual ~MarkTriangle() { /*hide();*/ } |
| 109 | virtual int type() const { return RTTI_MARK_TRIANGLE; } |
| 110 | virtual MarkType getType() const { return markTriangle; } |
| 111 | virtual void setPos(double x, double y) { QGraphicsPolygonItem::setPos((qreal)x , (qreal)y); } |
| 112 | // virtual void setX(double x) { QGraphicsPolygonItem::setX(x); } |
| 113 | // virtual void setY(double y) { QGraphicsPolygonItem::setY(y + size-1); } |
| 114 | virtual void setSize(double, double); |
| 115 | virtual double getSizeX() const { return size; } |
| 116 | virtual double getSizeY() const { return size; } |
| 117 | virtual void show() { QGraphicsPolygonItem::show(); } |
| 118 | virtual void hide() { QGraphicsPolygonItem::hide(); } |
| 119 | virtual void setColor(const QColor &col) { setPen(QPen(col, 1)); } |
| 120 | virtual const QColor getColor() { return pen().color(); } |
| 121 | |
| 122 | protected: |
| 123 | // virtual void drawShape(QPainter & p); |
| 124 | void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) { painter->setRenderHints(QPainter::Antialiasing); QGraphicsPolygonItem::paint(painter, option, widget);} |
| 125 | private: |
| 126 | double size; |
| 127 | QPolygon pa; |
| 128 | }; |
| 129 | |
| 130 | // Need to have a line with another rtti to differ this from the gatter lines |
| 131 | class MarkOtherLine : public QGraphicsLineItem |
| 132 | { |
| 133 | public: |
| 134 | MarkOtherLine(QGraphicsScene *canvas) |
| 135 | : QGraphicsLineItem(0) |
| 136 | { canvas->addItem(this); } |
| 137 | int type() const { return RTTI_MARK_OTHERLINE; } |
| 138 | }; |
| 139 | |
| 140 | class MarkCross : public QGraphicsLineItem, public Mark |
| 141 | { |
| 142 | public: |
| 143 | MarkCross(int x, int y, double s, QGraphicsScene *canvas, QColor col=Qt::black, bool plus=false); |
| 144 | virtual ~MarkCross(); |
| 145 | virtual int type() const { return RTTI_MARK_CROSS; } |
| 146 | virtual MarkType getType() const { return markSquare; } |
| 147 | virtual void setPos(double x, double y) { QGraphicsLineItem::setPos((qreal)x , (qreal)y); if (ol != NULL) ol->setPos(x,y); } |
| 148 | // virtual void setX(double x) { QGraphicsLineItem::setX(x); if (ol != NULL) ol->setX(x); } |
| 149 | // virtual void setY(double y) { QGraphicsLineItem:setY(y); if (ol != NULL) ol->setY(y); } |
| 150 | virtual void setSize(double, double); |
| 151 | virtual double getSizeX() const { return size; } |
| 152 | virtual double getSizeY() const { return size; } |
| 153 | virtual void show() { QGraphicsLineItem::show(); } |
| 154 | virtual void hide() { QGraphicsLineItem::hide(); } |
| 155 | virtual void setColor(const QColor &col); |
| 156 | virtual const QColor getColor() { return pen().color(); } |
| 157 | |
| 158 | private: |
| 159 | MarkOtherLine *ol; |
| 160 | double size; |
| 161 | bool plussign; |
| 162 | }; |
| 163 | |
| 164 | class MarkText : public QGraphicsSimpleTextItem, public Mark |
| 165 | { |
| 166 | public: |
| 167 | MarkText( int x, int y, double size, const QString &txt, QGraphicsScene *canvas, QColor col=Qt::black, |
| 168 | short c=-1, bool bold=false, bool overlay=true); |
| 169 | virtual ~MarkText(); |
| 170 | virtual int type() const { return RTTI_MARK_TEXT; } |
| 171 | virtual MarkType getType() const { return markText; } |
| 172 | virtual void setPos(double x, double y) { QGraphicsSimpleTextItem::setPos((qreal)x , (qreal)y);/* if (rect != NULL) rect->setPos(x,y);*/ } |
| 173 | // virtual void setX(double x) { QGraphicsRectItem::setX(x); if (rect != NULL) rect->setX(x); } |
| 174 | // virtual void setY(double y) { QGraphicsRectItem::setY(y); if (rect != NULL) rect->setY(y); } |
| 175 | virtual void setSize(double, double); |
| 176 | virtual double getSizeX() const { return width; } |
| 177 | virtual double getSizeY() const { return height; } |
| 178 | virtual void show() { QGraphicsSimpleTextItem::show(); } |
| 179 | virtual void hide() { QGraphicsSimpleTextItem::hide(); } |
| 180 | virtual void setColor(const QColor &col) { setPen(QPen(col, 1));}//setColor(col); } |
| 181 | virtual const QColor getColor() { return pen().color();}//QCanvasText::color(); } |
| 182 | // virtual void setText(const QString &txt) { QCanvasText::setText(txt);} |
| 183 | virtual short getCounter() { return counter; } |
| 184 | virtual void setCounter(short n) { counter = n; } |
| 185 | |
| 186 | static int maxLength; |
| 187 | |
| 188 | protected: |
| 189 | void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) { painter->setRenderHints(QPainter::SmoothPixmapTransform); QGraphicsSimpleTextItem::paint(painter, option, widget);} |
| 190 | |
| 191 | private: |
| 192 | // QGraphicsRectItem *rect; |
| 193 | int width, height; |
| 194 | double curSize; |
| 195 | short counter; |
| 196 | static bool useBold; |
| 197 | }; |
| 198 | |
| 199 | class MarkNumber : public MarkText |
| 200 | { |
| 201 | public: |
| 202 | MarkNumber( int x, int y, double size, short num, QGraphicsScene *canvas, QColor col=Qt::black, bool bold=false) |
| 203 | : MarkText( x, y, size, QString::number(num+1), canvas, col, num, bold) |
| 204 | { } |
| 205 | virtual int type() const { return RTTI_MARK_NUMBER; } |
| 206 | virtual MarkType getType() const { return markNumber; } |
| 207 | }; |
| 208 | |
| 209 | class MarkTerr : public MarkCross |
| 210 | { |
| 211 | public: |
| 212 | MarkTerr(int x, int y, double s, StoneColor c, QGraphicsScene *canvas) |
| 213 | : MarkCross(x, y, s, canvas, (c == stoneBlack ? Qt::black : Qt::white)), |
| 214 | col(c) |
| 215 | { } |
| 216 | virtual ~MarkTerr() { } |
| 217 | virtual int type() const { return RTTI_MARK_TERR; } |
| 218 | virtual MarkType getType() const { if (col == stoneBlack) return markTerrBlack; else return markTerrWhite; } |
| 219 | private: |
| 220 | StoneColor col; |
| 221 | }; |
| 222 | |
| 223 | class MarkSmallStoneTerr : public Mark, public QGraphicsPixmapItem |
| 224 | { |
| 225 | public: |
| 226 | MarkSmallStoneTerr(int x, int y, double s, StoneColor c, QList<QPixmap> * p, QGraphicsScene *canvas); |
| 227 | virtual ~MarkSmallStoneTerr() {}; |
| 228 | virtual int type() const { return RTTI_MARK_TERR; } |
| 229 | virtual MarkType getType() const { if (col == stoneBlack) return markTerrBlack; else return markTerrWhite; } |
| 230 | int getX() const { return _x; }; |
| 231 | int getY() const { return _y; }; |
| 232 | virtual void setPos(double x, double y) { QGraphicsPixmapItem::setPos((qreal)x , (qreal)y); } |
| 233 | virtual void setSize(double, double ) {}; |
| 234 | virtual double getSizeX() const { return size; } |
| 235 | virtual double getSizeY() const { return size; } |
| 236 | virtual void show() { QGraphicsPixmapItem::show(); }; |
| 237 | virtual void hide() { QGraphicsPixmapItem::hide(); }; |
| 238 | virtual void setColor(const QColor &) {}; |
| 239 | virtual const QColor getColor() { return QColor(0, 0, 0);} |
| 240 | void setPixmap(QList <QPixmap> * p); |
| 241 | private: |
| 242 | int _x, _y; |
| 243 | StoneColor col; |
| 244 | double size; |
| 245 | }; |
| 246 | |
| 247 | #endif |