Use shared SVG renderers.
[sloppygui.git] / src / graphicschessboardsquareitem.h
blob146abbabef0a2a43ef320330a26f10df4931b8a5
1 /*
2 This file is part of SloppyGUI.
4 SloppyGUI is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 SloppyGUI is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with SloppyGUI. If not, see <http://www.gnu.org/licenses/>.
18 #ifndef GRAPHICSCHESSBOARDSQUAREITEM_H
19 #define GRAPHICSCHESSBOARDSQUAREITEM_H
21 #include <QGraphicsItem>
23 #include "chessboard/chessboard.h"
25 class QPainter;
27 /**
28 * GraphicsChessboardSquareItem represents a single square in the chessboard.
30 * GraphicsChessboardSquareItem inherits QGraphicsItem:
31 * http://doc.trolltech.com/4.4/qgraphicsitem.html
33 * Each square have different states. These states include selected, focused
34 * and the default state. Within a chessboard only one item can have selected
35 * state and one can have a focused state. A single square can have however
36 * can also have these both states.
38 * The painting is done depending on the square's state. If the square is
39 * selected the square's color is overridden with the selected color. If the
40 * square is focused a marker is painted over the square using the focus marker
41 * color. If the square is in the default state painting is done using light
42 * or dark color depending on the square's type.
44 class GraphicsChessboardSquareItem : public QGraphicsItem
46 public:
47 /** Specifies the square's type: light or dark. */
48 enum ChessboardSquareType
50 LightSquare,
51 DarkSquare
54 /**
55 * Creates a new GraphicsChessboardSquareItem object.
56 * @param parent Square's parent object (i.e. the chessboard).
57 * @param type Square's type
59 GraphicsChessboardSquareItem(QGraphicsItem* parent, ChessboardSquareType type);
61 /** Size of the square. */
62 static const qreal size;
64 /**
65 * Sets the color of all light squares.
66 * @param color Color for all light squares.
68 static void setLightSquareColor(const QColor& color);
69 /**
70 * Sets the color of all dark squares.
71 * @param color Color for all dark squares.
73 static void setDarkSquareColor(const QColor& color);
74 /**
75 * Sets the color of all selected squares.
76 * The selected color overrides squares default color
77 * when the square is selected.
78 * @param color Color for all selected squares.
80 static void setSelectedSquareColor(const QColor& color);
81 /**
82 * Sets the color of the focus marker.
83 * @param color Color for the focus marker.
85 static void setFocusMarkerColor(const QColor& color);
86 /**
87 * Returns the color of light squares.
88 * @return Color of light squares.
90 static QColor lightSquareColor();
91 /**
92 * Returns the color of dark squares.
93 * @return Color of dark squares.
95 static QColor darkSquareColor();
96 /**
97 * Returns the color of selected squares.
98 * @return Color of selected squares.
100 static QColor selectedSquareColor();
102 * Returns the color of the focus marker.
103 * @return Color of the focus marker.
105 static QColor focusMarkerColor();
108 * Returns the square's type: light or dark.
109 * @return Square's type.
111 ChessboardSquareType squareType() const;
113 * Returns true if the square is light.
114 * @return True if the square is light.
116 bool isLightSquare() const;
118 * Returns true if the square is dark.
119 * @return True if the square is dark.
121 bool isDarkSquare() const;
123 * Sets the square's type.
124 * @param type Type for this square.
126 void setSquareType(ChessboardSquareType type);
128 * Returns true if this square is occupied (has a chess piece).
129 * @return True if square is occupied.
131 bool isOccupied() const;
133 void setPositionInChessboard(Chessboard::ChessSquare pos);
134 Chessboard::ChessSquare positionInChessboard() const;
136 QRectF boundingRect() const;
137 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
139 protected:
140 void mousePressEvent(QGraphicsSceneMouseEvent* event);
141 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
142 void focusOutEvent (QFocusEvent* event);
143 void focusInEvent (QFocusEvent* event);
145 private:
146 static QColor m_lightSquareColor;
147 static QColor m_darkSquareColor;
148 static QColor m_selectedSquareColor;
149 static QColor m_focusMarkerColor;
151 ChessboardSquareType m_type;
152 Chessboard::ChessSquare m_posInChessboard;
153 bool m_selectionWasHandled;
156 #endif // GRAPHICSCHESSBOARDSQUAREITEM_H