File indexing completed on 2024-04-21 04:04:57

0001 /*
0002     SPDX-FileCopyrightText: 2006 Matthew Williams <matt@milliams.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef GAMEBOARDSCENE_H
0008 #define GAMEBOARDSCENE_H
0009 
0010 #include <QGraphicsScene>
0011 
0012 class QGraphicsEllipseItem;
0013 
0014 /**
0015  * @short Scene for displaying the game board
0016  *
0017  * Created anew at the beginning of each game.
0018  * Contains the visual representation of the board. Is used to interperet mouse clicks from the user (if the view is unlocked)
0019  * Contains QGraphicsLineItems for lines, QGraphicsEllipseItems for the dots and QGraphicsRectItem for when a square is complete.
0020  *
0021  * @author Matt Williams <matt@milliams.com>
0022  */
0023 class GameBoardScene : public QGraphicsScene
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     /**
0029      * Create a new gameboard scene with the appropriate size.
0030      * @param newWidth the number of squares wide the board is
0031      * @param newHeight the number of squares tall the board is
0032      * @param parent passed to QGraphicsScene's constructor
0033      */
0034     GameBoardScene(int newWidth, int newHeight, QObject *parent = nullptr);
0035     ///Destructor
0036     ~GameBoardScene() override;
0037     /**
0038      * The smallest the view can be when 'auto-zoom' is off
0039      * @return the minimum size the view should be
0040      */
0041     const QSize minimumSizeHint() const;
0042 
0043     void acknowledgeMove(int x1, int y1, int x2, int y2);
0044 
0045 public Q_SLOTS:
0046     /**
0047      * Add the line to the scene so it shows up in the view
0048      * @param index the line-index of the line
0049      * @param colour the colour of the line
0050      */
0051     void drawLine(int index, const QColor &colour);
0052     /**
0053       * Draw a temporary halo behind a line.
0054       * @param index the line-index of the line
0055       */
0056     void highlightLine(int index);
0057     /**
0058      * Fill a box to show it is owned be a particular player.
0059      * @param index the square-index of the square
0060      * @param colour the colour fill of the square
0061      */
0062     void drawSquare(int index, const QColor &colour);
0063     /// disables mouse events
0064     void enableEvents()
0065     {
0066         acceptEvents = true;
0067     }
0068     /// enables mouse events
0069     void disableEvents()
0070     {
0071         acceptEvents = false;
0072     }
0073 
0074 protected:
0075     /**
0076      * Given a single location in the scene, gives the two nearest QGraphicsEllipseItem
0077      * @param pos the point in question
0078      * @return QList of (hopefully 2) QGraphicsEllipseItem*s
0079      */
0080     QList<QGraphicsEllipseItem *> getTwoNearestPoints(const QPointF &pos) const;
0081 
0082     /**
0083      * Given a pair of points, returns whether there is already a line there.
0084      * @param pointPair QList of (hopefully 2) QGraphicsEllipseItem*s
0085      * @return trur if there is a line there
0086      */
0087     bool isLineAlready(const QList<QGraphicsEllipseItem *> &pointPair) const;
0088     /**
0089      * Adds the line to the index for a specified pair of points.
0090      * @param pointPair QList of (hopefully 2) QGraphicsEllipseItem*s
0091      */
0092     void addLineToIndex(const QList<QGraphicsEllipseItem *> &pointPair);
0093 
0094     //conversion functions
0095     /**
0096      * Takes a pair of QGraphicsEllipseItems and finds the index that relates to the line that's between them
0097      * @param pointPair QList of (hopefully 2) QGraphicsEllipseItem*s
0098      * @return the line-index
0099      */
0100     int indexFromPointPair(const QList<QGraphicsEllipseItem *> &pointPair) const;   //given a pointPair, returns the index of the line between them. If not a valid line, returns -1
0101     /**
0102      * Takes a line-index and returns a QLineF located at that position
0103      * @param index the line-index
0104      * @return line located at the correct position
0105      */
0106     QLineF lineFromIndex(int index) const;  //all external calls will need to be passed through this to convert to local coords
0107 
0108     ///Moves to show where the next line will be drawn
0109     QGraphicsLineItem *indicatorLine;
0110 
0111     ///A local list of lines (non-canon)
0112     QList<bool> lineList;   //Just kept in sync with the KSquaresGame one
0113 
0114     ///QGraphicsEllipseItem::type()
0115     int QGraphicsEllipseItemType;
0116     ///Width of the board
0117     int width;
0118     ///Height of the board
0119     int height;
0120     ///Pixel spacing for standard zoom
0121     int spacing;
0122     ///This property holds whether mouse events are enabled for this widget.
0123     bool acceptEvents;
0124 
0125     //event handlers
0126     //void mousePressEvent (QGraphicsSceneMouseEvent* mouseEvent);
0127     void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
0128     void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
0129 
0130 Q_SIGNALS:
0131     ///Emits the index of the closet (undrawn) line when a click is detected
0132     void lineDrawn(int);
0133 };
0134 
0135 #endif // GAMEBOARDSCENE_H