81deb1b54f065eb2c0b526daf213de1c89d1e4ee
[colorflood] / colorflood / src / field.hpp
1 /*
2   Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; version 2 of the License.
7
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   GNU General Public License for more details.
12 */
13
14 #ifndef _FIELD_HPP
15 #define _FIELD_HPP
16
17 #include <QWidget>
18 #include <QVector>
19 #include <QBrush>
20
21 class QPaintEvent;
22 class QMouseEvent;
23
24 class Field : public QWidget
25 {
26     Q_OBJECT;
27
28 public:
29     typedef enum
30     {
31         SIZE_SMALL = 0,
32         SIZE_NORMAL,
33         SIZE_LARGE,
34         NUM_SIZES
35     }FieldSize;
36
37     typedef struct
38     {
39         quint8 brush;
40         bool   flood;
41     }FieldRect;
42
43     typedef QVector<Field::FieldRect> RectVector;
44
45     Field (QWidget *parent, int *turns);
46     ~Field ();
47
48     FieldSize getSize () const;
49
50     static int getNumRectsOfSize (FieldSize size);
51     static int getNumTurnsOfSize (FieldSize size);
52
53 private:
54     static const int numRects[NUM_SIZES];
55     static const int numTurns[NUM_SIZES];
56
57     static int getRectSize (FieldSize size);
58     void tryFloodRecurse (quint8 brush, int x, int y);
59     void floodNeighbours (quint8 brush, int x, int y);
60
61     FieldSize  size;
62     RectVector data;
63     int        turns;
64     bool       finished;
65
66 protected:
67     void paintEvent (QPaintEvent *event);
68
69 signals:
70     void turnsChanged (int turns);
71
72 public slots:
73     void randomize ();
74     void flood (int colorIndex);
75 };
76
77 #endif // !_FIELD_HPP