2 Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
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.
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.
17 static const int fieldWidth = 420;
19 const int Field::rects[Field::NUM_SIZES] = { 14, 21, 28 };
20 const int Field::turns[Field::NUM_SIZES] = { 25, 35, 50 };
22 Field::Field (QWidget *parent)
25 // FIXME -- restore saved state
28 Field::Field (QWidget *parent, const QVector<QBrush> &brushes, FieldSize size)
36 // FIXME -- save state
39 Field::FieldSize Field::getSize () const
49 void Field::randomize ()
51 Field::FieldRect rect;
55 data = QVector<FieldRect> (rects[size] * rects[size], rect);
57 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
59 for (QVector<Field::FieldRect>::iterator rect = data.begin();
63 (*rect).brush = qrand() % brushes.size();
67 int Field::getNumRectsOfSize (FieldSize size)
72 int Field::getNumTurnsOfSize (FieldSize size)
77 void Field::init (const QVector<QBrush> &brushes, FieldSize size)
80 this->brushes = brushes;
82 setFixedSize(fieldWidth, fieldWidth);
86 int Field::getRectSize (FieldSize size)
88 return fieldWidth / rects[size];
91 void Field::paintEvent (QPaintEvent *event)
96 QRect rect = QRect(0, 0, getRectSize(size), getRectSize(size));
98 for (int y = 0; y < rects[size] ;y++)
100 int n = y * rects[size];
102 for (int x = 0; x < rects[size] ;x++, n++)
104 rect.moveTo(x * rect.width(), y * rect.height());
106 if (rect.intersects(event->rect()))
107 painter.fillRect(rect, brushes.at(data[n].brush));