File indexing completed on 2024-04-14 04:02:08

0001 /*
0002     SPDX-FileCopyrightText: 1999-2006 Éric Bischoff <ebischoff@nerim.net>
0003     SPDX-FileCopyrightText: 2007 Albert Astals Cid <aacid@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 /* Play ground widget */
0009 
0010 #ifndef _PLAYGROUND_H_
0011 #define _PLAYGROUND_H_
0012 
0013 #include <QGraphicsView>
0014 #include <QMap>
0015 
0016 #include <QSvgRenderer>
0017 #include <QUndoGroup>
0018 
0019 
0020 class Action;
0021 class ToDraw;
0022 class QPagedPaintDevice;
0023 
0024 class PlayGroundCallbacks
0025 {
0026 public:
0027   virtual ~PlayGroundCallbacks() {}
0028   virtual void playSound(const QString &ref) = 0;
0029   virtual void changeGameboard(const QString &gameboard) = 0;
0030   virtual void registerGameboard(const QString& menuText, const QString& boardFile, const QPixmap& pixmap) = 0;
0031 };
0032 
0033 class PlayGround : public QGraphicsView
0034 {
0035   Q_OBJECT
0036 
0037 public:
0038   explicit PlayGround(PlayGroundCallbacks *callbacks, QWidget *parent = nullptr);
0039   ~PlayGround() override;
0040 
0041   enum LoadError { NoError, OldFileVersionError, OtherError };
0042 
0043   void reset();
0044   LoadError loadFrom(const QString &name);
0045   bool saveAs(const QString &name);
0046   bool printPicture(QPagedPaintDevice &printer);
0047   QPixmap getPicture();
0048 
0049   void connectRedoAction(QAction *action);
0050   void connectUndoAction(QAction *action);
0051 
0052   void registerPlayGrounds();
0053   bool loadPlayGround(const QString &gameboardFile);
0054 
0055   void setAllowOnlyDrag(bool allowOnlyDrag);
0056 
0057   QString currentGameboard() const;
0058 
0059   bool isAspectRatioLocked() const;
0060 
0061 public Q_SLOTS:
0062   void lockAspectRatio(bool lock);
0063 
0064 protected:
0065 
0066   void mousePressEvent(QMouseEvent *event) override;
0067   void mouseMoveEvent(QMouseEvent *event) override;
0068   void mouseReleaseEvent(QMouseEvent *event) override;
0069   void resizeEvent(QResizeEvent *event) override;
0070 
0071 private:
0072   QPointF clipPos(const QPointF &p, ToDraw *item) const;
0073   QRectF backgroundRect() const;
0074   bool insideBackground(const QSizeF &size, const QPointF &pos) const;
0075   void placeDraggedItem(const QPoint &pos);
0076   void placeNewItem(const QPoint &pos);
0077   void playGroundPixmap(const QString &playgroundName, QPixmap &pixmap);
0078 
0079   void recenterView();
0080   
0081   QGraphicsScene *scene() const;
0082   QUndoStack *undoStack() const;
0083 
0084   PlayGroundCallbacks *m_callbacks;
0085   QString m_gameboardFile;              // the file the board
0086   QMap<QString, QString> m_objectsNameSound;        // map between element name and sound
0087   QMap<QString, double> m_objectsNameRatio;     // map between element name and scaling ratio
0088 
0089   QPoint m_mousePressPos;
0090   QPointF m_itemDraggedPos;
0091   ToDraw *m_newItem;                    // the new item we are moving
0092   ToDraw *m_dragItem;                   // the existing item we are dragging
0093   QSvgRenderer m_SvgRenderer;               // the SVG renderer
0094   int m_nextZValue;                 // the next Z value to use
0095 
0096   bool m_lockAspect;                    // whether we are locking aspect ratio
0097   bool m_allowOnlyDrag;
0098   QUndoGroup m_undoGroup;
0099   
0100   class SceneData
0101   {
0102     public:
0103       QGraphicsScene *scene;
0104       QUndoStack *undoStack;
0105   };
0106   QMap <QString, SceneData> m_scenes;  // caches the items of each playground
0107 };
0108 
0109 #endif
0110 
0111 /* kate: replace-tabs on; indent-width 2; */