e0b64ea412c6e8249eba5b8cf84e30e5b78424ab
[colorflood] / colorflood / src / window.cpp
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 #include <QPushButton>
15 #include <QVBoxLayout>
16 #include <QHBoxLayout>
17 #include <QLabel>
18 #include <QSettings>
19 #include "window.hpp"
20 #include "colorbuttons.hpp"
21 #include "field.hpp"
22 #include "fullscreenexitbutton.hpp"
23
24 Window::Window ()
25     : QWidget()
26 {
27     setWindowTitle("Color Flood");
28     setWindowIcon(QIcon(":/images/icon_48x48.png"));
29
30     int turns;
31     field = new Field(this, &turns);
32     colorButtons = new ColorButtons(this);
33
34     QObject::connect(colorButtons,
35                      SIGNAL(flood(int)),
36                      field,
37                      SLOT(flood(int)));
38
39     turnsLabel = new QLabel(this);
40     turnsLabel->setAlignment(Qt::AlignRight);
41
42     QObject::connect(field,
43                      SIGNAL(turnsChanged(int)),
44                      this,
45                      SLOT(updateTurns(int)));
46
47     updateTurns(turns);
48
49     QPushButton *newGame = new QPushButton(tr("New game"), this);
50     QObject::connect(newGame, SIGNAL(pressed()), field, SLOT(randomize()));
51
52     QVBoxLayout *vl = new QVBoxLayout;
53     vl->addWidget(colorButtons);
54     vl->setAlignment(colorButtons, Qt::AlignRight | Qt::AlignTop);
55     vl->addWidget(turnsLabel);
56     vl->setAlignment(turnsLabel, Qt::AlignRight | Qt::AlignBottom);
57     vl->addWidget(newGame);
58     vl->setAlignment(newGame, Qt::AlignRight | Qt::AlignTop);
59
60     QHBoxLayout *hl = new QHBoxLayout;
61     hl->addWidget(field);
62     hl->setAlignment(field, Qt::AlignLeft);
63     hl->addLayout(vl);
64
65     setLayout(hl);
66
67     QSettings settings;
68
69     if (settings.value("fullscreen", true).toBool())
70         showFullScreen();
71
72     new FullScreenExitButton(this);
73 }
74
75 Window::~Window ()
76 {
77     bool isFullscreen = windowState() & Qt::WindowFullScreen;
78
79     QSettings settings;
80     settings.setValue("fullscreen", isFullscreen);
81 }
82
83 void Window::updateTurns (int turns)
84 {
85     /*: number of turns */
86     turnsLabel->setText(tr("<font size=\"16\">Turns: %1/%2</font>")
87                         .arg(turns)
88                         .arg(field->getNumTurnsOfSize(field->getSize())));
89 }