field.cpp
main.cpp
window.cpp
+ colorscheme.cpp
)
set(moc
--- /dev/null
+/*
+ Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+ 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; version 2 of the License.
+
+ 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.
+*/
+
+#include <QSettings>
+#include "colorscheme.hpp"
+
+static int currentScheme = 0;
+static QVector<QPair<QString, QVector<QBrush> > > schemes;
+
+ColorScheme::ColorScheme ()
+{
+ schemes.clear();
+
+ QVector<QBrush> s;
+
+ s << QColor(0x00, 0x00, 0xff); // blue
+ s << QColor(0xff, 0x00, 0x00); // red
+ s << QColor(0x00, 0xff, 0x00); // green
+ s << QColor(0xff, 0xff, 0x00); // yellow
+ s << QColor(0xff, 0x00, 0xff); // magenta
+ s << QColor(0x80, 0x00, 0x80); // purple
+ schemes << QPair<QString, QVector<QBrush> >(QObject::tr("Default"), s);
+
+ s.clear();
+ s << QBrush(QColor(0x00, 0x00, 0x00), Qt::SolidPattern);
+ s << QBrush(QColor(0x33, 0x33, 0x33), Qt::Dense3Pattern);
+ s << QBrush(QColor(0x66, 0x66, 0x66), Qt::Dense1Pattern);
+ s << QBrush(QColor(0x99, 0x99, 0x99), Qt::SolidPattern);
+ s << QBrush(QColor(0xcc, 0xcc, 0xcc), Qt::CrossPattern);
+ s << QBrush(QColor(0xff, 0xff, 0xff), Qt::SolidPattern);
+ schemes << QPair<QString, QVector<QBrush> >(QObject::tr("Black-and-white"), s);
+
+ QSettings settings;
+ currentScheme = settings.value("colorScheme", 0).toInt();
+
+ if (currentScheme < 0 || currentScheme > s.size())
+ currentScheme = 0;
+}
+
+ColorScheme::~ColorScheme ()
+{
+ QSettings settings;
+ settings.setValue("colorScheme", currentScheme);
+}
+
+int ColorScheme::getNumSchemes ()
+{
+ return schemes.size();
+}
+
+QString ColorScheme::getSchemeName (int scheme)
+{
+ Q_ASSERT(scheme > 0 && scheme < getNumSchemes());
+ return schemes.at(scheme).first;
+}
+
+const QVector<QBrush> &ColorScheme::getScheme (int scheme)
+{
+ Q_ASSERT(scheme > 0 && scheme < getNumSchemes());
+ return schemes.at(scheme).second;
+}
+
+QString ColorScheme::getSchemeName ()
+{
+ return schemes.at(currentScheme).first;
+}
+
+const QVector<QBrush> &ColorScheme::getScheme ()
+{
+ return schemes.at(currentScheme).second;
+}
+
+void ColorScheme::setScheme (int scheme)
+{
+ Q_ASSERT(scheme >= 0 && scheme < getNumSchemes());
+ currentScheme = scheme;
+}
--- /dev/null
+/*
+ Copyright 2010 Serge Ziryukin <ftrvxmtrx@gmail.com>
+
+ 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; version 2 of the License.
+
+ 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.
+*/
+
+#ifndef _COLORSCHEME_HPP
+#define _COLORSCHEME_HPP
+
+#include <QVector>
+#include <QPair>
+#include <QBrush>
+#include <QString>
+
+class ColorScheme
+{
+public:
+ static ColorScheme & instance ()
+ {
+ static ColorScheme instance;
+ return instance;
+ }
+
+ static int getNumSchemes ();
+ static QString getSchemeName (int scheme);
+ const QVector<QBrush> &getScheme (int scheme);
+ static QString getSchemeName ();
+ const QVector<QBrush> &getScheme ();
+ static void setScheme (int scheme);
+
+private:
+ ColorScheme ();
+ ~ColorScheme ();
+ ColorScheme (const ColorScheme &);
+ ColorScheme & operator= (const ColorScheme &);
+};
+
+#endif /* !_COLORSCHEME_HPP */
#include <QtGui>
#include "field.hpp"
+#include "colorscheme.hpp"
static const int fieldWidth = 420;
const int Field::rects[Field::NUM_SIZES] = { 14, 21, 28 };
const int Field::turns[Field::NUM_SIZES] = { 25, 35, 50 };
-Field::Field (QWidget *parent)
- : QWidget (parent)
+// we declare out QVector<FieldRect> metatype
+// and stream operators to save whole field to settings
+Q_DECLARE_METATYPE(Field::RectVector);
+
+static QDataStream &operator<< (QDataStream &out, const Field::RectVector &rv)
{
- // FIXME -- restore saved state
+ for (QVector<Field::FieldRect>::const_iterator rect = rv.begin();
+ rect != rv.end();
+ rect++)
+ {
+ out << (*rect).brush;
+ out << (*rect).flood;
+ }
+
+ return out;
}
-Field::Field (QWidget *parent, const QVector<QBrush> &brushes, FieldSize size)
+static QDataStream &operator>> (QDataStream &in, Field::RectVector &rv)
+{
+ Field::FieldRect r;
+
+ rv.clear();
+
+ for (; !in.atEnd() ;)
+ {
+ in >> r.brush >> r.flood;
+ rv << r;
+ }
+
+ rv.pop_back();
+
+ return in;
+}
+
+Field::Field (QWidget *parent)
: QWidget (parent)
{
- init(brushes, size);
+ setFixedSize(fieldWidth, fieldWidth);
+
+ // restore field size and field itself from settings
+
+ qRegisterMetaType<RectVector>("Field::RectVector");
+ qRegisterMetaTypeStreamOperators<RectVector>("Field::RectVector");
+
+ QSettings settings;
+
+ int size = settings.value("field/fieldSize", SIZE_SMALL).toInt();
+
+ if (size < SIZE_SMALL || size >= NUM_SIZES)
+ size = SIZE_SMALL;
+
+ this->size = (FieldSize)size;
+
+ if (settings.contains("field/data"))
+ data = settings.value("field/data").value<RectVector>();
+
+ if (data.size() != rects[size] * rects[size])
+ randomize();
}
Field::~Field ()
{
- // FIXME -- save state
+ QSettings settings;
+
+ settings.setValue("field/size", size);
+
+ QVariant v;
+ v.setValue(data);
+ settings.setValue("field/data", v);
}
Field::FieldSize Field::getSize () const
return size;
}
-/*
-=================
-randomize
-=================
-*/
void Field::randomize ()
{
- Field::FieldRect rect;
+ FieldRect rect;
rect.flood = false;
data.clear();
- data = QVector<FieldRect> (rects[size] * rects[size], rect);
+ data = RectVector(rects[size] * rects[size], rect);
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
- for (QVector<Field::FieldRect>::iterator rect = data.begin();
+ int numBrushes = ColorScheme::instance().getScheme().size();
+
+ for (QVector<FieldRect>::iterator rect = data.begin();
rect != data.end();
rect++)
{
- (*rect).brush = qrand() % brushes.size();
+ (*rect).brush = qrand() % numBrushes;
}
+
+ update();
}
int Field::getNumRectsOfSize (FieldSize size)
return turns[size];
}
-void Field::init (const QVector<QBrush> &brushes, FieldSize size)
-{
- this->size = size;
- this->brushes = brushes;
-
- setFixedSize(fieldWidth, fieldWidth);
- randomize();
-}
-
int Field::getRectSize (FieldSize size)
{
return fieldWidth / rects[size];
QRect rect = QRect(0, 0, getRectSize(size), getRectSize(size));
+ const QVector<QBrush> &scheme = ColorScheme::instance().getScheme();
+
for (int y = 0; y < rects[size] ;y++)
{
int n = y * rects[size];
rect.moveTo(x * rect.width(), y * rect.height());
if (rect.intersects(event->rect()))
- painter.fillRect(rect, brushes.at(data[n].brush));
+ painter.fillRect(rect, scheme.at(data[n].brush));
}
}
NUM_SIZES
}FieldSize;
- Field (QWidget *parent, const QVector<QBrush> &brushes, FieldSize size);
+ typedef struct
+ {
+ quint8 brush;
+ bool flood;
+ }FieldRect;
+
+ typedef QVector<Field::FieldRect> RectVector;
+
Field (QWidget *parent);
~Field ();
static int getNumTurnsOfSize (FieldSize size);
private:
- typedef struct
- {
- char brush;
- bool flood;
- }FieldRect;
-
static const int rects[NUM_SIZES];
static const int turns[NUM_SIZES];
void init (const QVector<QBrush> &brushes, FieldSize size);
static int getRectSize (FieldSize size);
- FieldSize size;
- QVector<QBrush> brushes;
- QVector<FieldRect> data;
+ FieldSize size;
+ RectVector data;
protected:
void paintEvent (QPaintEvent *event);
Window::Window ()
: QWidget()
{
- setWindowTitle(tr("Color flood"));
-
- setWindowState(windowState() | Qt::WindowFullScreen);
-
- QVector<QBrush> brushes;
-
-#if 1
- // standart color scheme
- brushes << QBrush(QColor(0x00, 0x00, 0xff)); // blue
- brushes << QBrush(QColor(0xff, 0x00, 0x00)); // red
- brushes << QBrush(QColor(0x00, 0xff, 0x00)); // green
- brushes << QBrush(QColor(0xff, 0xff, 0x00)); // yellow
- brushes << QBrush(QColor(0xff, 0x00, 0xff)); // magenta
- brushes << QBrush(QColor(0x80, 0x00, 0x80)); // purple
-#else
- // color-blind color scheme
- brushes << QBrush(QColor(0x00, 0x00, 0x00));
- brushes << QBrush(QColor(0x31, 0x31, 0x31), Qt::Dense1Pattern);
- brushes << QBrush(QColor(0x62, 0x62, 0x62), Qt::Dense3Pattern);
- brushes << QBrush(QColor(0x93, 0x93, 0x93), Qt::CrossPattern);
- brushes << QBrush(QColor(0xc4, 0xc4, 0xc4));
- brushes << QBrush(QColor(0xff, 0xff, 0xff));
-#endif
-
- field = new Field(this, brushes, Field::SIZE_LARGE);
+ setWindowTitle(tr("Color Flood"));
+
+ //setWindowState(windowState() | Qt::WindowFullScreen);
+
+ QPushButton *button = new QPushButton("randomize", this);
+ field = new Field(this);
+
+ QObject::connect(button, SIGNAL(pressed()), this, SLOT(randomize()));
QHBoxLayout *layout = new QHBoxLayout;
+ layout->addWidget(button);
+ layout->setAlignment(button, Qt::AlignLeft);
layout->addWidget(field);
layout->setAlignment(field, Qt::AlignRight);
setLayout(layout);
}
+
+void Window::randomize ()
+{
+ field->randomize();
+}
public:
Window ();
+public slots:
+ void randomize ();
+
private:
Field *field;
};