1/*
2 Copyright 2007 Dmitry Suzdalev <dimsuz@gmail.com>
3 Copyright 2010 Brian Croom <brian.s.croom@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#ifndef CELLITEM_H
20#define CELLITEM_H
21
22#include <KGameRenderedItem>
23
24#include "commondefs.h"
25
26class KGameRenderer;
27
28/**
29 * Graphics item representing single cell on
30 * the game field.
31 * Handles clicks, emits signals when something important happens :)
32 */
33class CellItem : public KGameRenderedItem
34{
35public:
36 CellItem(KGameRenderer* renderer, QGraphicsItem* parent);
37 /**
38 * Updates item pixmap according to its current
39 * state and properties
40 */
41 void updatePixmap();
42 /**
43 * Reimplemented to pass the call on to any child items as well
44 */
45 void setRenderSize(const QSize &renderSize);
46 // FIXME: will it EVER be needed to setHasMine(false)???
47 /**
48 * Sets whether this item holds mine or not
49 */
50 void setHasMine(bool hasMine) { m_hasMine = hasMine; }
51 /**
52 * @return whether this item holds mine
53 */
54 bool hasMine() const { return m_hasMine; }
55 /**
56 * Sets this item so it holds a digit
57 *
58 * @param digit digit number (1 to 8)
59 */
60 void setDigit(int digit) { m_digit = digit; updatePixmap(); }
61 /**
62 * @return digit this item holds or 0 if none
63 */
64 int digit() const { return m_digit; }
65 /**
66 * Shows what this item hides :)
67 * Can be a bomb, a digit, an empty square
68 */
69 void reveal();
70 /**
71 * Hides what this item shows ;).
72 * I.e. resets revealed state
73 */
74 void unreveal() { m_state = KMinesState::Released; updatePixmap(); }
75 /**
76 * @return whether this cell is revealed
77 */
78 bool isRevealed() const { return ( m_state == KMinesState::Revealed || m_state == KMinesState::Error); }
79 /**
80 * @return whether this cell is marked with flag
81 */
82 bool isFlagged() const { return m_state == KMinesState::Flagged; }
83 /**
84 * @return whether this cell is marked with question
85 */
86 bool isQuestioned() const { return m_state == KMinesState::Questioned; }
87 /**
88 * @return whether this cell is exploded
89 */
90 bool isExploded() const { return m_exploded; }
91 /**
92 * Resets all properties & state of an item to default ones
93 */
94 void reset();
95 // TODO docs
96 void press();
97 void release(bool force=false);
98 void undoPress();
99 void mark();
100 // enable use of qgraphicsitem_cast
101 enum { Type = UserType + 1 };
102 virtual int type() const { return Type; }
103signals:
104 /**
105 * Emitted when this item is revealed with mouse click
106 */
107 void revealed();
108 /**
109 * Emitted when flag (not question mark) is set or unset on this item
110 * New flagged state can be retrieved via isFlagged()
111 */
112 void flaggedStateChanged();
113private:
114 static QHash<int, QString> s_digitNames;
115 static QHash<KMinesState::CellState, QList<QString> > s_stateNames;
116 static void fillNameHashes();
117 /**
118 * Current state of this item
119 */
120 KMinesState::CellState m_state;
121 /**
122 * True if this item holds mine
123 */
124 bool m_hasMine;
125 /**
126 * True if mine is exploded
127 */
128 bool m_exploded;
129 /**
130 * Specifies a digit this item holds. 0 if none
131 */
132 int m_digit;
133 /**
134 * Add a child object to display an overlayed pixmap
135 */
136 void addOverlay(const QString& spriteKey);
137};
138
139#endif
140