File indexing completed on 2024-04-28 04:04:47

0001 /***************************************************************************
0002  *   Copyright 2007      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 _KSUDOKUGAMEVARIANTS_H_
0022 #define _KSUDOKUGAMEVARIANTS_H_
0023 
0024 #include <QAbstractListModel>
0025 #include <QItemDelegate>
0026 #include <QList>
0027 #include <QObject>
0028 #include <QUrl>
0029 
0030 class SKGraph;
0031 namespace ksudoku {
0032 
0033 class KsView;
0034 class Game;
0035 class GameVariantCollection;
0036 class GameVariant {
0037 public:
0038     explicit GameVariant(const QString& name, GameVariantCollection* collection=nullptr);
0039     virtual ~GameVariant() {}
0040 
0041 public:
0042     QString name() const { return m_name; }
0043     QString description() const { return m_description; }
0044     void setDescription(const QString& descr);
0045     QString icon() const { return m_icon; }
0046     void setIcon(const QString& icon);
0047 
0048     /// This method returs whether the variant has an configure option
0049     virtual bool canConfigure() const = 0;
0050 
0051     /// Shows a configure dialog and changes the settings
0052     virtual bool configure() = 0;
0053 
0054     /// Whether this variant can be started without any values in the grid
0055     virtual bool canStartEmpty() const = 0;
0056 
0057     /// Creates a game without a puzzle but with an empty grid
0058     virtual Game startEmpty() = 0;
0059 
0060     /// Creates an instance of this game variant
0061     virtual Game createGame(int difficulty, int symmetry) = 0;
0062 
0063     /// Creates the correct view for the game.
0064     /// Game needs to be compatible with this GameVariant
0065     virtual KsView* createView(const Game& game) const = 0;
0066 
0067 private:
0068     QString m_name;
0069     QString m_description;
0070     QString m_icon;
0071 };
0072 
0073 class GameVariantCollection : public QAbstractListModel {
0074 friend class GameVariant;
0075 Q_OBJECT
0076 public:
0077     GameVariantCollection(QObject* parent, bool autoDel);
0078     ~GameVariantCollection() override;
0079 
0080 public:
0081     void addVariant(GameVariant* variant);
0082 
0083 public:
0084     QVariant data(const QModelIndex &index, int role) const override;
0085     int rowCount(const QModelIndex&) const override;
0086     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0087     GameVariant* variant(const QModelIndex&) const;
0088 
0089 Q_SIGNALS:
0090     void newVariant(GameVariant* variant);
0091 
0092 public:
0093     QList<GameVariant*> m_variants;
0094     bool m_autoDelete;
0095 };
0096 
0097 class GameVariantDelegate : public QItemDelegate {
0098 Q_OBJECT
0099 public:
0100     enum Roles {
0101         Description = 33
0102     };
0103 
0104 public:
0105     explicit GameVariantDelegate(QObject* parent = nullptr, QWidget * viewport = nullptr);
0106 public:
0107     QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
0108     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
0109 
0110 protected:
0111     bool eventFilter(QObject* watched, QEvent* event) override;
0112 
0113 public:
0114     QString title(const QModelIndex& index) const;
0115     QString description(const QModelIndex& index) const;
0116     QString icon(const QModelIndex& index) const;
0117     bool configurable(const QModelIndex& index) const;
0118 private:
0119     QWidget * m_viewport;
0120 
0121     static const int m_iconWidth = 48;
0122     static const int m_iconHeight = 48;
0123     static const int m_leftMargin = 16;
0124     static const int m_rightMargin = 12;
0125     static const int m_separatorPixels = 8;
0126     static const int m_minWidth = m_leftMargin + m_rightMargin +
0127                       4 * (m_iconWidth + m_separatorPixels*2);
0128 };
0129 
0130 class SudokuGame : public GameVariant {
0131 public:
0132     SudokuGame(const QString& name, uint order, GameVariantCollection* collection=nullptr);
0133     ~SudokuGame() override;
0134 
0135 public:
0136     bool canConfigure() const override;
0137     bool configure() override;
0138     bool canStartEmpty() const override;
0139     Game startEmpty() override;
0140     Game createGame(int difficulty, int symmetry) override;
0141     KsView* createView(const Game& game) const override;
0142 
0143 private:
0144     uint m_order;
0145     uint m_symmetry;
0146 
0147     SKGraph *m_graph;
0148 };
0149 
0150 class RoxdokuGame : public GameVariant {
0151 public:
0152     RoxdokuGame(const QString& name, uint order, GameVariantCollection* collection=nullptr);
0153     ~RoxdokuGame() override;
0154 
0155 public:
0156     bool canConfigure() const override;
0157     bool configure() override;
0158     bool canStartEmpty() const override;
0159     Game startEmpty() override;
0160     Game createGame(int difficulty, int symmetry) override;
0161     KsView* createView(const Game& game) const override;
0162 
0163 private:
0164     uint m_order;
0165     uint m_symmetry;
0166 
0167     SKGraph* m_graph;
0168 };
0169 
0170 class CustomGame : public GameVariant {
0171 public:
0172     CustomGame(const QString& name, const QUrl& url, GameVariantCollection* collection=nullptr);
0173     ~CustomGame() override;
0174 
0175 public:
0176     bool canConfigure() const override;
0177     bool configure() override;
0178     bool canStartEmpty() const override;
0179     Game startEmpty() override;
0180     Game createGame(int difficulty, int symmetry) override;
0181     KsView* createView(const Game& game) const override;
0182 
0183 private:
0184     uint m_order;
0185     uint m_symmetry;
0186 
0187     QUrl m_url;
0188     SKGraph* m_graph;
0189     bool createSKGraphObject();
0190 };
0191 
0192 }
0193 
0194 #endif