File indexing completed on 2024-05-12 04:06:25

0001 /*
0002     SPDX-FileCopyrightText: 2009, 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef PALAPELI_SCENE_H
0008 #define PALAPELI_SCENE_H
0009 
0010 #include <QGraphicsScene>
0011 
0012 namespace Palapeli
0013 {
0014     class ConstraintVisualizer;
0015     class Piece;
0016     class Puzzle;
0017 
0018     /**
0019      * This class holds the puzzle pieces and boundary (constraint) of a
0020      * Palapeli scene, which can be either a piece-holder or the main
0021      * puzzle table. The scene also handles adding and removing pieces,
0022      * moving pieces, merging (or joining) pieces, arranging pieces into
0023      * a grid and signaling changes in the state of the puzzle and its
0024      * pieces, wherever they may be.
0025      */
0026 
0027     class Scene : public QGraphicsScene
0028     {
0029         Q_OBJECT
0030         friend class GamePlay;
0031         public:
0032             explicit Scene(QObject* parent = nullptr);
0033 
0034             void addPieceToList(Palapeli::Piece* piece);
0035             void addPieceItemsToScene();
0036             bool isConstrained() const;
0037             QRectF extPiecesBoundingRect() const;
0038             void setMinGrid(const int minGrid);
0039             QRectF piecesBoundingRect() const;
0040             qreal margin() { return m_margin; }
0041             qreal handleWidth() { return m_handleWidth; }
0042             void addMargin(const qreal handleWidth,
0043                        const qreal spacer);
0044 
0045             void validatePiecePosition(Palapeli::Piece* piece);
0046             void mergeLoadedPieces();
0047             const QSizeF& pieceAreaSize()
0048                     { return m_pieceAreaSize; }
0049             void setPieceAreaSize(const QSizeF& pieceAreaSize)
0050                     { m_pieceAreaSize = pieceAreaSize; }
0051             QList<Palapeli::Piece*> pieces() { return m_pieces; }
0052 
0053             void dispatchPieces(const QList<Palapeli::Piece*> &pcs);
0054             void clearPieces();
0055 
0056             void initializeGrid(const QPointF& gridTopLeft);
0057             void addToGrid(Piece* piece);
0058 
0059         public Q_SLOTS:
0060             void setConstrained(bool constrained);
0061         Q_SIGNALS:
0062             void constrainedChanged(bool constrained);
0063             void saveMove(int reduction);
0064         private Q_SLOTS:
0065             void pieceMoved(bool finished);
0066             void pieceInstanceTransaction(
0067                 const QList<Palapeli::Piece*>& deletedPieces,
0068                 const QList<Palapeli::Piece*>& createdPieces);
0069         private:
0070             void searchConnections(
0071                     const QList<Palapeli::Piece*>& pieces,
0072                     const bool animatedMerging = true);
0073             //behavior parameters
0074             bool m_constrained;
0075             Palapeli::ConstraintVisualizer* m_constraintVisualizer;
0076             //game parameters
0077             Palapeli::Puzzle* m_puzzle;
0078             QList<Palapeli::Piece*> m_pieces;
0079             int m_atomicPieceCount;
0080             QSizeF m_pieceAreaSize;
0081             // Width of ConstraintVisualizer and space at edges.
0082             qreal m_margin;
0083             qreal m_handleWidth;
0084 
0085             QPointF m_gridTopLeft;
0086             QSizeF m_gridSpacing;
0087             int m_gridRank;
0088             int m_gridX;
0089             int m_gridY;
0090             // Scene has at least m_minGrid*m_minGrid piece-spaces.
0091             int m_minGrid;
0092     };
0093 }
0094 
0095 #endif // PALAPELI_SCENE_H