File indexing completed on 2024-05-12 04:05:54

0001 /***************************************************************************
0002  *   Copyright 2008      Johannes Bergmeier <johannes.bergmeier@gmx.net>   *
0003  *   Copyright 2015      Ian Wadham <iandw.au@gmail.com>                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 
0021 #ifndef _VIEW2D_H_
0022 #define _VIEW2D_H_
0023 
0024 #include <QGraphicsView>
0025 
0026 #include "ksview.h"
0027 
0028 #include "ksudokugame.h"
0029 
0030 #include "renderer.h"
0031 
0032 class QGraphicsPixmapItem;
0033 namespace ksudoku {
0034 
0035 class GroupGraphicsItem;
0036 class CellGraphicsItem;
0037 class GameActions;
0038 
0039 class View2DScene : public QGraphicsScene {
0040     Q_OBJECT
0041 public:
0042     explicit View2DScene(GameActions* gameActions);
0043     ~View2DScene() override;
0044 public:
0045     void init(const Game& game);
0046 
0047     /**
0048      * Set up the graphics for drawing a Mathdoku or Killer Sudoku cage.
0049      * The list of cages goes at the end of the list of rows, columns and
0050      * blocks, in the vector QList<GroupGraphicsItem*> m_groups. Each
0051      * GroupGraphicsItem is a graphical structure having cells, an outline
0052      * or boundary and highlighting. In addition, cages have a cage-label
0053      * containing the cage's value and operator.
0054      *
0055      * @param cageNum      The position of the cage (0 to n) in model-data
0056      *                     in the puzzle's SKGraph. Its position in the
0057      *                     scene and view list is (offset + cageNum), where
0058      *                     "offset" is the number of rows, cols and blocks.
0059      * @param drawLabel    Whether the cage-label is to be drawn.
0060      */
0061     void initCageGroup (int cageNum, bool drawLabel = true);
0062     
0063     void setSceneSize(const QSize& size);
0064 
0065     void hover(int cell);
0066     void press(int cell, bool rightButton = false);
0067 
0068     inline int maxValue() const { return m_game.order(); }
0069 
0070 public Q_SLOTS:
0071     void selectValue(int val);
0072     void enterValue(int val, int cell=-1);
0073     void markValue(int val, int cell=-1, bool set=true);
0074     void flipMarkValue(int val, int cell=-1);
0075     void moveCursor(int dx, int dy);
0076     void update(int cell = -1);
0077     /**
0078      * Add, replace or delete graphics for a Mathdoku or Killer Sudoku cage.
0079      *
0080      * @param cageNumP1    The cage-index (in lists of cages) + 1.
0081      *                     Value 1 to N: Add or replace the cage-graphics.
0082      *                     Value -1 to -N: delete the cage-graphics.
0083      *                     Value 0: invalid.
0084      */
0085     void updateCage (int cageNumP1, bool drawLabel);
0086 Q_SIGNALS:
0087     void valueSelected(int val);
0088 protected:
0089     void wheelEvent(QGraphicsSceneWheelEvent* event) override;
0090 private:
0091     QGraphicsPixmapItem* m_background;
0092     QGraphicsItem* m_groupLayer;
0093     QGraphicsItem* m_cellLayer;
0094     QList<GroupGraphicsItem*> m_groups;
0095     QList<CellGraphicsItem*> m_cells;
0096     QGraphicsPixmapItem* m_cursor;
0097     Game m_game;
0098 
0099     GameActions* m_gameActions;
0100 
0101     int m_cursorPos;
0102     int m_selectedValue;
0103     bool m_highlightsOn;
0104 };
0105 
0106 class View2D : public QGraphicsView, public ViewInterface {
0107     Q_OBJECT
0108 public:
0109     View2D(QWidget* parent, const Game& game, GameActions* gameActions);
0110     ~View2D() override;
0111 
0112 public:
0113     QWidget* widget() override { return this; }
0114 public Q_SLOTS:
0115     void selectValue(int value) override;
0116     void settingsChanged();
0117 Q_SIGNALS:
0118     void valueSelected(int value);
0119 protected:
0120     void resizeEvent(QResizeEvent* e) override;
0121 private:
0122     View2DScene* m_scene;
0123 };
0124 
0125 }
0126 
0127 #endif // _VIEW2D_H_