File indexing completed on 2024-04-21 04:02:38

0001 /*
0002     SPDX-FileCopyrightText: 2007 Dmitry Suzdalev <dimsuz@gmail.com>
0003     SPDX-FileCopyrightText: 2010 Brian Croom <brian.s.croom@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef CELLITEM_H
0009 #define CELLITEM_H
0010 
0011 // own
0012 #include "commondefs.h"
0013 // KDEGames
0014 #include <KGameRenderedItem>
0015 
0016 class KGameRenderer;
0017 
0018 /**
0019  * Graphics item representing single cell on
0020  * the game field.
0021  * Handles clicks, emits signals when something important happens :)
0022  */
0023 class CellItem : public KGameRenderedItem
0024 {
0025 public:
0026     CellItem(KGameRenderer* renderer, QGraphicsItem* parent);
0027     /**
0028      * Updates item pixmap according to its current
0029      * state and properties
0030      */
0031     void updatePixmap();
0032     /**
0033      * Reimplemented to pass the call on to any child items as well
0034      */
0035     void setRenderSize(const QSize &renderSize);
0036     // FIXME: will it EVER be needed to setHasMine(false)???
0037     /**
0038      * Sets whether this item holds mine or not
0039      */
0040     void setHasMine(bool hasMine);
0041     /**
0042      * @return whether this item holds mine
0043      */
0044     bool hasMine() const;
0045     /**
0046      * Sets this item so it holds a digit
0047      *
0048      * @param digit digit number (1 to 8)
0049      */
0050     void setDigit(int digit);
0051     /**
0052      * @return digit this item holds or 0 if none
0053      */
0054     int digit() const;
0055     /**
0056      * Shows what this item hides :)
0057      * Can be a bomb, a digit, an empty square
0058      */
0059     void reveal();
0060     /**
0061      * Hides what this item shows ;).
0062      * I.e. resets revealed state
0063      */
0064     void unreveal();
0065     /**
0066      * Removes the flag
0067      */
0068     void unflag();
0069     /**
0070      * Stops the mine from being exploded
0071      */
0072     void unexplode();
0073     /**
0074      * @return whether this cell is revealed
0075      */
0076     bool isRevealed() const;
0077     /**
0078      * @return whether this cell is marked with flag
0079      */
0080     bool isFlagged() const;
0081     /**
0082      * @return whether this cell is marked with question
0083      */
0084     bool isQuestioned() const;
0085     /**
0086      * @return whether this cell is exploded
0087      */
0088     bool isExploded() const;
0089     /**
0090      * Resets all properties & state of an item to default ones
0091      */
0092     void reset();
0093     // TODO docs
0094     void press();
0095     void release(bool force=false);
0096     void undoPress();
0097     void mark();
0098     // enable use of qgraphicsitem_cast
0099     enum { Type = UserType + 1 };
0100     int type() const override;
0101 Q_SIGNALS:
0102     /**
0103      * Emitted when this item is revealed with mouse click
0104      */
0105     void revealed();
0106 private:
0107     static QHash<int, QString> s_digitNames;
0108     static QHash<KMinesState::CellState, QList<QString> > s_stateNames;
0109     static void fillNameHashes();
0110     /**
0111      * Current state of this item
0112      */
0113     KMinesState::CellState m_state;
0114     /**
0115      * True if this item holds mine
0116      */
0117     bool m_hasMine;
0118     /**
0119      * True if mine is exploded
0120      */
0121     bool m_exploded;
0122     /**
0123      * Specifies a digit this item holds. 0 if none
0124      */
0125     int m_digit;
0126     /**
0127      * Add a child object to display an overlayed pixmap
0128      */
0129     void addOverlay(const QString& spriteKey);
0130 };
0131 
0132 #endif