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