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.
15 #include <QDataStream>
19 #include <QPaintEvent>
21 #include "colorscheme.hpp"
23 static const int fieldWidth = 420;
25 const int Field::rects[Field::NUM_SIZES] = { 14, 21, 28 };
26 const int Field::turns[Field::NUM_SIZES] = { 25, 35, 50 };
28 // we declare out QVector<FieldRect> metatype
29 // and stream operators to save whole field to settings
30 Q_DECLARE_METATYPE(Field::RectVector);
32 static QDataStream &operator<< (QDataStream &out, const Field::RectVector &rv)
34 for (QVector<Field::FieldRect>::const_iterator rect = rv.begin();
45 static QDataStream &operator>> (QDataStream &in, Field::RectVector &rv)
53 in >> r.brush >> r.flood;
62 Field::Field (QWidget *parent)
65 setFixedSize(fieldWidth, fieldWidth);
67 // restore field size and field itself from settings
69 qRegisterMetaType<RectVector>("Field::RectVector");
70 qRegisterMetaTypeStreamOperators<RectVector>("Field::RectVector");
74 int size = settings.value("field/fieldSize", SIZE_SMALL).toInt();
76 if (size < SIZE_SMALL || size >= NUM_SIZES)
79 this->size = (FieldSize)size;
81 if (settings.contains("field/data"))
82 data = settings.value("field/data").value<RectVector>();
84 if (data.size() != rects[size] * rects[size])
92 settings.setValue("field/size", size);
96 settings.setValue("field/data", v);
99 Field::FieldSize Field::getSize () const
104 void Field::randomize ()
110 data = RectVector(rects[size] * rects[size], rect);
112 qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
114 int numBrushes = ColorScheme::instance().getScheme().size();
116 for (QVector<FieldRect>::iterator rect = data.begin();
120 (*rect).brush = qrand() % numBrushes;
126 int Field::getNumRectsOfSize (FieldSize size)
131 int Field::getNumTurnsOfSize (FieldSize size)
136 int Field::getRectSize (FieldSize size)
138 return fieldWidth / rects[size];
141 void Field::paintEvent (QPaintEvent *event)
146 QRect rect = QRect(0, 0, getRectSize(size), getRectSize(size));
148 const QVector<QBrush> &scheme = ColorScheme::instance().getScheme();
150 for (int y = 0; y < rects[size] ;y++)
152 int n = y * rects[size];
154 for (int x = 0; x < rects[size] ;x++, n++)
156 rect.moveTo(x * rect.width(), y * rect.height());
158 if (rect.intersects(event->rect()))
159 painter.fillRect(rect, scheme.at(data[n].brush));