gamevariants.h 5.53 KB
Newer Older
1 2
/***************************************************************************
 *   Copyright 2007      Johannes Bergmeier <johannes.bergmeier@gmx.net>   *
3
 *   Copyright 2015      Ian Wadham <iandw.au@gmail.com>                   *
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
Johannes Bergmeier's avatar
Johannes Bergmeier committed
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19 20
 ***************************************************************************/

21 22 23
#ifndef _KSUDOKUGAMEVARIANTS_H_
#define _KSUDOKUGAMEVARIANTS_H_

24 25
#include <QAbstractListModel>
#include <QItemDelegate>
26
#include <QList>
27
#include <QObject>
28
#include <QUrl>
29

30
class SKGraph;
31 32 33 34 35 36 37
namespace ksudoku {

class KsView;
class Game;
class GameVariantCollection;
class GameVariant {
public:
Johannes Bergmeier's avatar
Johannes Bergmeier committed
38
	explicit GameVariant(const QString& name, GameVariantCollection* collection=0);
39 40 41 42
	virtual ~GameVariant() {}

public:
	QString name() const { return m_name; }
43 44
	QString description() const { return m_description; }
	void setDescription(const QString& descr);
45 46
	QString icon() const { return m_icon; }
	void setIcon(const QString& icon);
47

48 49
	/// This method returs whether the variant has an configure option
	virtual bool canConfigure() const = 0;
50

51 52
	/// Shows a configure dialog and changes the settings
	virtual bool configure() = 0;
53

54 55
	/// Whether this variant can be started without any values in the grid
	virtual bool canStartEmpty() const = 0;
56

57
	/// Creates a game without a puzzle but with an empty grid
58
	virtual Game startEmpty() = 0;
59

60
	/// Creates an instance of this game variant
61
	virtual Game createGame(int difficulty, int symmetry) = 0;
62

63 64 65
	/// Creates the correct view for the game.
	/// Game needs to be compatible with this GameVariant
	virtual KsView* createView(const Game& game) const = 0;
66

67 68
private:
	QString m_name;
69
	QString m_description;
70
	QString m_icon;
71 72
};

73
class GameVariantCollection : public QAbstractListModel {
74 75 76 77 78
friend class GameVariant;
Q_OBJECT
public:
	GameVariantCollection(QObject* parent, bool autoDel);
	~GameVariantCollection();
79

80 81
public:
	void addVariant(GameVariant* variant);
82

83 84 85 86 87
public:
	QVariant data(const QModelIndex &index, int role) const;
	int rowCount(const QModelIndex&) const;
	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
	GameVariant* variant(const QModelIndex&) const;
88 89 90

signals:
	void newVariant(GameVariant* variant);
91

92 93 94 95 96
public:
	QList<GameVariant*> m_variants;
	bool m_autoDelete;
};

97 98 99 100 101 102
class GameVariantDelegate : public QItemDelegate {
Q_OBJECT
public:
	enum Roles {
		Description = 33
	};
103

104
public:
105
	GameVariantDelegate(QObject* parent = 0, QWidget * viewport = 0);
106 107 108 109 110 111 112 113 114 115
public:
	QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
	void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;

protected:
	virtual bool eventFilter(QObject* watched, QEvent* event);

public:
	QString title(const QModelIndex& index) const;
	QString description(const QModelIndex& index) const;
116
	QString icon(const QModelIndex& index) const;
117
	bool configurable(const QModelIndex& index) const;
118
private:
119 120
	QWidget * m_viewport;

121 122 123 124 125
	static const int m_iconWidth = 48;
	static const int m_iconHeight = 48;
	static const int m_leftMargin = 16;
	static const int m_rightMargin = 12;
	static const int m_separatorPixels = 8;
126 127
	static const int m_minWidth = m_leftMargin + m_rightMargin +
				      4 * (m_iconWidth + m_separatorPixels*2);
128 129
};

130 131 132
class SudokuGame : public GameVariant {
public:
	SudokuGame(const QString& name, uint order, GameVariantCollection* collection=0);
133

134 135 136
public:
	bool canConfigure() const;
	bool configure();
137
	bool canStartEmpty() const;
138 139
	Game startEmpty();
	Game createGame(int difficulty, int symmetry);
140
	KsView* createView(const Game& game) const;
141

142 143 144
private:
	uint m_order;
	uint m_symmetry;
145

146
	SKGraph *m_graph;
147 148 149 150 151
};

class RoxdokuGame : public GameVariant {
public:
	RoxdokuGame(const QString& name, uint order, GameVariantCollection* collection=0);
152

153 154 155
public:
	bool canConfigure() const;
	bool configure();
156
	bool canStartEmpty() const;
157 158
	Game startEmpty();
	Game createGame(int difficulty, int symmetry);
159
	KsView* createView(const Game& game) const;
160

161 162 163
private:
	uint m_order;
	uint m_symmetry;
164

165
	SKGraph* m_graph;
166 167 168 169
};

class CustomGame : public GameVariant {
public:
170
	CustomGame(const QString& name, const QUrl& url, GameVariantCollection* collection=0);
171

172 173 174
public:
	bool canConfigure() const;
	bool configure();
175
	bool canStartEmpty() const;
176 177
	Game startEmpty();
	Game createGame(int difficulty, int symmetry);
178
	KsView* createView(const Game& game) const;
179

180
private:
181 182 183
	uint m_order;
	uint m_symmetry;

184
	QUrl m_url;
185
	SKGraph* m_graph;
186
	bool createSKGraphObject();
187 188 189 190 191
};

}

#endif