update version
[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 <QMenuBar>
20 #include <QMessageBox>
21 #include "window.hpp"
22 #include "colorbuttons.hpp"
23 #include "field.hpp"
24 #include "fullscreenexitbutton.hpp"
25 #include "colorscheme.hpp"
26
27 Window::Window ()
28     : QWidget()
29 {
30     setWindowTitle("Color Flood");
31     setWindowIcon(QIcon(":/images/icon_48x48.png"));
32
33     int turns;
34     field = new Field(this, &turns);
35     colorButtons = new ColorButtons(this);
36
37     QObject::connect(colorButtons,
38                      SIGNAL(flood(int)),
39                      field,
40                      SLOT(flood(int)));
41
42     turnsLabel = new QLabel(this);
43     turnsLabel->setAlignment(Qt::AlignRight);
44
45     QObject::connect(field,
46                      SIGNAL(turnsChanged(int)),
47                      this,
48                      SLOT(updateTurns(int)));
49
50     updateTurns(turns);
51
52     QPushButton *newGame = new QPushButton(tr("New game"), this);
53     QObject::connect(newGame, SIGNAL(pressed()), field, SLOT(randomize()));
54
55     QPushButton *help = new QPushButton(tr("Help"), this);
56     QObject::connect(help, SIGNAL(pressed()), this, SLOT(help()));
57
58     QHBoxLayout *lowerLayout = new QHBoxLayout;
59     lowerLayout->addWidget(help);
60     lowerLayout->addWidget(newGame);
61
62     QVBoxLayout *vl = new QVBoxLayout;
63     vl->addWidget(colorButtons);
64     vl->setAlignment(colorButtons, Qt::AlignRight | Qt::AlignTop);
65     vl->addWidget(turnsLabel);
66     vl->setAlignment(turnsLabel, Qt::AlignRight | Qt::AlignBottom);
67     vl->addLayout(lowerLayout);
68     vl->setAlignment(lowerLayout, Qt::AlignRight | Qt::AlignTop);
69
70     QHBoxLayout *hl = new QHBoxLayout;
71     hl->addWidget(field);
72     hl->setAlignment(field, Qt::AlignLeft);
73     hl->addLayout(vl);
74
75     setLayout(hl);
76
77     /* menu bar */
78     QMenuBar *bar = new QMenuBar(this);
79
80     QObject::connect(bar->addAction(tr("Fullscreen mode")),
81                      SIGNAL(triggered()),
82                      this,
83                      SLOT(fullScreenMode()));
84
85     QObject::connect(bar->addAction(
86                          ColorScheme::getSchemeName(
87                              ColorScheme::getNextColorScheme())),
88                      SIGNAL(triggered()),
89                      this,
90                      SLOT(colorScheme()));
91
92     less = bar->addAction(tr("Less cells"));
93
94     QObject::connect(less,
95                      SIGNAL(triggered()),
96                      this,
97                      SLOT(lessCells()));
98
99     more = bar->addAction(tr("More cells"));
100
101     QObject::connect(more,
102                      SIGNAL(triggered()),
103                      this,
104                      SLOT(moreCells()));
105
106     if (!field->getSize())
107         less->setEnabled(false);
108     else if (field->getSize() == Field::NUM_SIZES - 1)
109         more->setEnabled(false);
110
111     new FullScreenExitButton(this);
112     showFullScreen();
113 }
114
115 void Window::updateTurns (int turns)
116 {
117     /*: number of turns */
118     turnsLabel->setText(tr("Turns: %1/%2")
119                         .arg(turns)
120                         .arg(field->getNumTurnsOfSize(field->getSize())));
121 }
122
123 void Window::fullScreenMode ()
124 {
125     showFullScreen();
126 }
127
128 void Window::lessCells ()
129 {
130     int s = field->getSize() - 1;
131
132     field->setSize(s);
133     more->setEnabled(true);
134
135     if (!s)
136         less->setEnabled(false);
137 }
138
139 void Window::moreCells ()
140 {
141     int s = field->getSize() + 1;
142
143     field->setSize(s);
144     less->setEnabled(true);
145
146     if (s == Field::NUM_SIZES - 1)
147         more->setEnabled(false);
148 }
149
150 void Window::colorScheme ()
151 {
152     QAction *action = static_cast<typeof(action)>(QObject::sender());
153
154     ColorScheme::setScheme(ColorScheme::getNextColorScheme());
155
156     field->update();
157     colorButtons->update();
158
159     action->setText(ColorScheme::getSchemeName(
160                         ColorScheme::getNextColorScheme()));
161 }
162
163 void Window::help ()
164 {
165         QMessageBox box;
166         box.setWindowTitle("Color Flood");
167         box.setText(tr("The object of the game is to turn a board into one single color. Number of moves is limited. You start from top-left corner with one cell already flooded.\nGood luck!"));
168         box.exec();
169 }