1/*
2 Copyright 2000 Roman Merzlyakov <roman@sbrf.barrt.ru>
3 Copyright 2000 Roman Razilov <roman@sbrf.barrt.ru>
4 Copyright 2007 Dimitry Suzdalev <dimsuz@gmail.com>
5 Copyright 2007 Simon Hürlimann <simon.huerlimann@huerlisi.ch>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#include "klines.h"
23#include "renderer.h"
24#include "prefs.h"
25#include "mwidget.h"
26#include "scene.h"
27
28#include <KConfig>
29#include <KAction>
30#include <KActionCollection>
31#include <KStandardAction>
32#include <KScoreDialog>
33#include <KToggleAction>
34#include <KStatusBar>
35#include <KLocale>
36#include <KConfigDialog>
37#include <KMessageBox>
38
39#include <KStandardGameAction>
40#include <KGameRenderer>
41#include <KgThemeSelector>
42
43KLinesMainWindow::KLinesMainWindow()
44{
45 KLinesRenderer::Init();
46
47 mwidget = new MainWidget(this);
48 setCentralWidget( mwidget );
49
50 connect(mwidget->scene(), SIGNAL(scoreChanged(int)), SLOT(updateScore(int)));
51 connect(mwidget->scene(), SIGNAL(stateChanged(QString)), SLOT(slotStateChanged(QString)));
52 connect(mwidget->scene(), SIGNAL(gameOver(int)), SLOT(gameOver(int)));
53
54 statusBar()->insertItem(i18n("Score:"), 0);
55 updateScore(0);
56
57 KgThemeProvider* prov = KLinesRenderer::renderer()->themeProvider();
58 connect(prov, SIGNAL(currentThemeChanged(const KgTheme*)), SLOT(loadSettings()));
59 mselector = new KgThemeSelector(KLinesRenderer::renderer()->themeProvider());
60
61 setupActions();
62
63 stateChanged(QLatin1String( "init" ));
64}
65
66KLinesMainWindow::~KLinesMainWindow()
67{
68 KLinesRenderer::UnInit();
69}
70
71void KLinesMainWindow::setupActions()
72{
73 // Game
74 KStandardGameAction::gameNew(this, SLOT(startGame()), actionCollection());
75 KStandardGameAction::highscores(this, SLOT(viewHighScore()), actionCollection());
76 KStandardGameAction::quit(this, SLOT(close()), actionCollection());
77
78 // Move
79 KStandardGameAction::undo(mwidget->scene(), SLOT(undo()), actionCollection());
80 KStandardGameAction::endTurn(mwidget->scene(), SLOT(endTurn()), actionCollection());
81
82 // Preferences
83 KToggleAction *showNext = actionCollection()->add<KToggleAction>(QLatin1String( "show_next" ));
84 showNext->setText( i18n( "Show Next" ) );
85 connect(showNext, SIGNAL(triggered(bool)), SLOT(showNextToggled(bool)));
86
87 showNext->setChecked(Prefs::showNext());
88 mwidget->setShowNextColors(Prefs::showNext());
89
90 // Navigation
91 KAction* naviLeft = new KAction( KIcon( QLatin1String( "arrow-left") ), i18n("Move Left" ), this );
92 naviLeft->setShortcut( Qt::Key_Left );
93 actionCollection()->addAction( QLatin1String( "navi_left" ), naviLeft);
94
95 KAction* naviRight = new KAction( KIcon( QLatin1String( "arrow-right") ), i18n("Move Right" ), this );
96 naviRight->setShortcut( Qt::Key_Right );
97 actionCollection()->addAction( QLatin1String( "navi_right" ), naviRight);
98
99 KAction* naviUp = new KAction( KIcon( QLatin1String( "arrow-up") ), i18n("Move Up" ), this );
100 naviUp->setShortcut( Qt::Key_Up );
101 actionCollection()->addAction( QLatin1String( "navi_up" ), naviUp);
102
103 KAction* naviDown = new KAction( KIcon( QLatin1String( "arrow-down") ), i18n("Move Down" ), this );
104 naviDown->setShortcut( Qt::Key_Down );
105 actionCollection()->addAction( QLatin1String( "navi_down" ), naviDown);
106
107 KAction* naviSelect = new KAction( i18n("Select"), this );
108 naviSelect->setShortcut( Qt::Key_Space );
109 actionCollection()->addAction( QLatin1String( "navi_select" ), naviSelect);
110
111 connect( naviLeft, SIGNAL(triggered(bool)), mwidget->scene(), SLOT(moveFocusLeft()));
112 connect( naviRight, SIGNAL(triggered(bool)), mwidget->scene(), SLOT(moveFocusRight()));
113 connect( naviUp, SIGNAL(triggered(bool)), mwidget->scene(), SLOT(moveFocusUp()));
114 connect( naviDown, SIGNAL(triggered(bool)), mwidget->scene(), SLOT(moveFocusDown()));
115 connect( naviSelect, SIGNAL(triggered(bool)), mwidget->scene(), SLOT(cellSelected()));
116
117 KStandardAction::preferences( mselector, SLOT(showAsDialog()), actionCollection() );
118 setupGUI();
119}
120
121void KLinesMainWindow::updateScore(int score)
122{
123 statusBar()->changeItem(i18n("Score: %1", score), 0);
124}
125
126void KLinesMainWindow::gameOver(int score)
127{
128 KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
129 d.setConfigGroup( QLatin1String( "Highscore" ) );
130 d.addScore(score, KScoreDialog::AskName);
131 d.exec();
132}
133
134void KLinesMainWindow::viewHighScore()
135{
136 KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
137 d.setConfigGroup( QLatin1String( "Highscore" ) );
138 d.exec();
139}
140
141void KLinesMainWindow::startGame()
142{
143 updateScore(0);
144 mwidget->scene()->startNewGame();
145}
146
147void KLinesMainWindow::showNextToggled(bool show)
148{
149 mwidget->setShowNextColors(show);
150 Prefs::setShowNext(show);
151 Prefs::self()->writeConfig();
152}
153
154
155// FIXME these are strings from old tutorial
156// leave them if I ever want to use them when I'll impelement tutorial mode
157/**
158 msg = i18n("The goal of the game is to put\n"
159 msg = i18n("You can make horizontal, vertical\n"
160 "and diagonal lines.");
161 msg = i18n("Each turn, three new balls are placed on the board.");
162 msg = i18n("Every turn, you can move one ball.");
163 msg = i18n("To move a ball, click on it with the mouse,\n"
164 "then click where you want the ball to go.");
165 msg = i18n("You just moved the blue ball!");
166 msg = i18n("Balls can be moved to every position on the board,\n"
167 "as long as there are no other balls in their way.");
168 msg = i18n("Now we only need one more blue ball.");
169 msg = i18n("It seems to be our lucky day!");
170 msg = i18n("Hurray! And away they go!\n"
171 "Now lets try the green balls.");
172 msg = i18n("Now you try!\n"
173 "Click on the green ball and move it to the others!");
174 msg = i18n("Almost, try again!");
175 msg = i18n("Very good!");
176 msg = i18n("Whenever you complete a line you get an extra turn.");
177 msg = i18n("This is the end of this tutorial.\n"
178 "Feel free to finish the game!");
179 */
180
181void KLinesMainWindow::loadSettings()
182{
183 KLinesRenderer::loadTheme();
184 QRectF r = mwidget->scene()->sceneRect();
185 mwidget->scene()->invalidate( r, QGraphicsScene::BackgroundLayer ); // redraw background
186 mwidget->scene()->resizeScene( (int)r.width(), (int)r.height() ); // redraw scene
187}
188
189#include "klines.moc"
190