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.
16 #include "colorscheme.hpp"
18 static const int fieldWidth = 420;
20 const int Field::rects[Field::NUM_SIZES] = { 14, 21, 28 };
21 const int Field::turns[Field::NUM_SIZES] = { 25, 35, 50 };
23 // we declare out QVector<FieldRect> metatype
24 // and stream operators to save whole field to settings
25 Q_DECLARE_METATYPE(Field::RectVector);
27 static QDataStream &operator<< (QDataStream &out, const Field::RectVector &rv)
29 for (QVector<Field::FieldRect>::const_iterator rect = rv.begin();
40 static QDataStream &operator>> (QDataStream &in, Field::RectVector &rv)
48 in >> r.brush >> r.flood;
57 Field::Field (QWidget *parent)
60 setFixedSize(fieldWidth, fieldWidth);
62 // restore field size and field itself from settings
64 qRegisterMetaType<RectVector>("Field::RectVector");
65 qRegisterMetaTypeStreamOperators<RectVector>("Field::RectVector");
69 int size = settings.value("field/fieldSize", SIZE_SMALL).toInt();
71 if (size < SIZE_SMALL || size >= NUM_SIZES)
74 this->size = (FieldSize)size;
76 if (settings.contains("field/data"))
77 data = settings.value("field/data").value<RectVector>();
79 if (data.size() != rects[size] * rects[size])
87 settings.setValue("field/size", size);
91 settings.setValue("field/data", v);
94 Field::FieldSize Field::getSize () const
99 void Field::randomize ()
105 data = RectVector(rects[size] * rects[size], rect);
107 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
109 int numBrushes = ColorScheme::instance().getScheme().size();
111 for (QVector<FieldRect>::iterator rect = data.begin();
115 (*rect).brush = qrand() % numBrushes;
121 int Field::getNumRectsOfSize (FieldSize size)
126 int Field::getNumTurnsOfSize (FieldSize size)
131 int Field::getRectSize (FieldSize size)
133 return fieldWidth / rects[size];
136 void Field::paintEvent (QPaintEvent *event)
141 QRect rect = QRect(0, 0, getRectSize(size), getRectSize(size));
143 const QVector<QBrush> &scheme = ColorScheme::instance().getScheme();
145 for (int y = 0; y < rects[size] ;y++)
147 int n = y * rects[size];
149 for (int x = 0; x < rects[size] ;x++, n++)
151 rect.moveTo(x * rect.width(), y * rect.height());
153 if (rect.intersects(event->rect()))
154 painter.fillRect(rect, scheme.at(data[n].brush));