klines.cpp 7.36 KB
Newer Older
Simon Huerlimann's avatar
Simon Huerlimann committed
1 2 3
/*
    Copyright 2000 Roman Merzlyakov <roman@sbrf.barrt.ru>
    Copyright 2000 Roman Razilov <roman@sbrf.barrt.ru>
4
    Copyright 2007 Dimitry Suzdalev <dimsuz@gmail.com>
Simon Huerlimann's avatar
Simon Huerlimann committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    Copyright 2007 Simon Hürlimann <simon.huerlimann@huerlisi.ch>

    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; either version 2 of the License, or
    (at your option) any later version.

    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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

22
#include "klines.h"
23
#include "renderer.h"
24 25 26 27
#include "prefs.h"
#include "mwidget.h"
#include "scene.h"

Anuj Pahuja's avatar
Anuj Pahuja committed
28 29 30
#include <QStatusBar>
#include <QAction>

31
#include <KConfig>
32
#include <KActionCollection>
33
#include <KStandardAction>
34
#include <KScoreDialog>
35
#include <KToggleAction>
36 37
#include <KConfigDialog>
#include <KMessageBox>
Anuj Pahuja's avatar
Anuj Pahuja committed
38
#include <KLocalizedString>
39
#include <KStandardGameAction>
Stefan Majewsky's avatar
Stefan Majewsky committed
40 41
#include <KGameRenderer>
#include <KgThemeSelector>
Stephan Kulow's avatar
Stephan Kulow committed
42

43
KLinesMainWindow::KLinesMainWindow()
Stephan Kulow's avatar
Stephan Kulow committed
44
{
45 46
    KLinesRenderer::Init();

47 48
    mwidget = new MainWidget(this);
    setCentralWidget( mwidget );
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
49

50
    connect(mwidget->scene(), &KLinesScene::scoreChanged, this, &KLinesMainWindow::updateScore);
Laurent Montel's avatar
Laurent Montel committed
51
    connect(mwidget->scene(), SIGNAL(stateChanged(QString)), SLOT(slotStateChanged(QString)));
52
    connect(mwidget->scene(), &KLinesScene::gameOver, this, &KLinesMainWindow::gameOver);
53

Anuj Pahuja's avatar
Anuj Pahuja committed
54
    scoreLabel->setText(i18n("Score:"));
Laurent Montel's avatar
Laurent Montel committed
55
    statusBar()->addPermanentWidget(scoreLabel);
Anuj Pahuja's avatar
Anuj Pahuja committed
56
    
57 58
    updateScore(0);

Stefan Majewsky's avatar
Stefan Majewsky committed
59
    KgThemeProvider* prov = KLinesRenderer::renderer()->themeProvider();
Laurent Montel's avatar
Laurent Montel committed
60
    connect(prov, &KgThemeProvider::currentThemeChanged, this, &KLinesMainWindow::loadSettings);
Stefan Majewsky's avatar
Stefan Majewsky committed
61 62
    mselector = new KgThemeSelector(KLinesRenderer::renderer()->themeProvider());

63
    setupActions();
Simon Huerlimann's avatar
Simon Huerlimann committed
64

65
    stateChanged(QStringLiteral( "init" ));
Stephan Kulow's avatar
Stephan Kulow committed
66 67
}

68
KLinesMainWindow::~KLinesMainWindow()
Andreas Beckermann's avatar
Andreas Beckermann committed
69
{
70
  KLinesRenderer::UnInit();
Andreas Beckermann's avatar
Andreas Beckermann committed
71 72
}

Simon Huerlimann's avatar
Simon Huerlimann committed
73
void KLinesMainWindow::setupActions()
Andreas Beckermann's avatar
Andreas Beckermann committed
74
{
75 76 77 78
  // Game
  KStandardGameAction::gameNew(this, SLOT(startGame()), actionCollection());
  KStandardGameAction::highscores(this, SLOT(viewHighScore()), actionCollection());
  KStandardGameAction::quit(this, SLOT(close()), actionCollection());
Laurent Montel's avatar
Laurent Montel committed
79
  KStandardGameAction::end(mwidget->scene(), SLOT(endGame()), actionCollection());
80

81
  // Move
Simon Huerlimann's avatar
Simon Huerlimann committed
82 83
  KStandardGameAction::undo(mwidget->scene(), SLOT(undo()), actionCollection());
  KStandardGameAction::endTurn(mwidget->scene(), SLOT(endTurn()), actionCollection());
Nicolas Hadacek's avatar
Nicolas Hadacek committed
84

85
  // Preferences
86
  KToggleAction *showNext = actionCollection()->add<KToggleAction>(QStringLiteral( "show_next" ));
Malcolm Hunter's avatar
Malcolm Hunter committed
87
  showNext->setText( i18n( "Show Next" ) );
Laurent Montel's avatar
Laurent Montel committed
88
  connect(showNext, &KToggleAction::triggered, this, &KLinesMainWindow::showNextToggled);
89

90 91
  showNext->setChecked(Prefs::showNext());
  mwidget->setShowNextColors(Prefs::showNext());
Andreas Beckermann's avatar
Andreas Beckermann committed
92

Simon Huerlimann's avatar
Simon Huerlimann committed
93
  // Navigation
Anuj Pahuja's avatar
Anuj Pahuja committed
94
  QAction* naviLeft = new QAction( i18n("Move Left" ), this );
95
  naviLeft->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-left"))); 
Laurent Montel's avatar
Laurent Montel committed
96
  actionCollection()->setDefaultShortcut(naviLeft, Qt::Key_Left );
97
  actionCollection()->addAction( QStringLiteral( "navi_left" ), naviLeft);
98

Anuj Pahuja's avatar
Anuj Pahuja committed
99
  QAction* naviRight = new QAction( i18n("Move Right" ), this );
100
  naviRight->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-right")));
Laurent Montel's avatar
Laurent Montel committed
101
  actionCollection()->setDefaultShortcut(naviRight, Qt::Key_Right );
102
  actionCollection()->addAction( QStringLiteral( "navi_right" ), naviRight);
103

Anuj Pahuja's avatar
Anuj Pahuja committed
104
  QAction* naviUp = new QAction( i18n("Move Up" ), this );
105
  naviUp->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-up")));
Laurent Montel's avatar
Laurent Montel committed
106
  actionCollection()->setDefaultShortcut(naviUp, Qt::Key_Up );
107
  actionCollection()->addAction( QStringLiteral( "navi_up" ), naviUp);
108

Anuj Pahuja's avatar
Anuj Pahuja committed
109
  QAction* naviDown = new QAction( i18n("Move Down" ), this );
110
  naviDown->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-down")));
Laurent Montel's avatar
Laurent Montel committed
111
  actionCollection()->setDefaultShortcut(naviDown, Qt::Key_Down );
112
  actionCollection()->addAction( QStringLiteral( "navi_down" ), naviDown);
113

Anuj Pahuja's avatar
Anuj Pahuja committed
114
  QAction* naviSelect = new QAction( i18n("Select"), this );
Laurent Montel's avatar
Laurent Montel committed
115
  actionCollection()->setDefaultShortcut(naviSelect, Qt::Key_Space );
116
  actionCollection()->addAction( QStringLiteral( "navi_select" ), naviSelect);
117

118 119 120 121 122
  connect( naviLeft, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusLeft);
  connect( naviRight, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusRight);
  connect( naviUp, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusUp);
  connect( naviDown, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusDown);
  connect( naviSelect, &QAction::triggered, mwidget->scene(), &KLinesScene::cellSelected);
123

Stefan Majewsky's avatar
Stefan Majewsky committed
124
  KStandardAction::preferences( mselector, SLOT(showAsDialog()), actionCollection() );
125
  setupGUI();
Andreas Beckermann's avatar
Andreas Beckermann committed
126 127
}

128
void KLinesMainWindow::updateScore(int score)
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
129
{
Anuj Pahuja's avatar
Anuj Pahuja committed
130
    scoreLabel->setText(i18n("Score: %1", score));
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
131 132
}

133
void KLinesMainWindow::gameOver(int score)
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
134 135
{
    KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
136
    d.setConfigGroup(qMakePair(QByteArray("Highscore"), i18n("High Scores")));
137
    d.addScore(score, KScoreDialog::AskName);
138
    d.exec();
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
139 140
}

141 142 143
void KLinesMainWindow::viewHighScore()
{
   KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score, this);
144
   d.setConfigGroup(qMakePair(QByteArray("Highscore"), i18n("High Scores")));
145 146 147
   d.exec();
}

148
void KLinesMainWindow::startGame()
149
{
150 151
    updateScore(0);
    mwidget->scene()->startNewGame();
Stephan Kulow's avatar
Stephan Kulow committed
152
}
153

154
void KLinesMainWindow::showNextToggled(bool show)
155
{
156 157
    mwidget->setShowNextColors(show);
    Prefs::setShowNext(show);
Anuj Pahuja's avatar
Anuj Pahuja committed
158
    Prefs::self()->save();
159 160
}

161

162
// FIXME these are strings from old tutorial
163
// leave them if I ever want to use them when I'll impelement tutorial mode
164
/**
165 166 167
       msg = i18n("The goal of the game is to put\n"
       msg = i18n("You can make horizontal, vertical\n"
                  "and diagonal lines.");
168 169 170
       msg = i18n("Each turn, three new balls are placed on the board.");
       msg = i18n("Every turn, you can move one ball.");
       msg = i18n("To move a ball, click on it with the mouse,\n"
171 172
                  "then click where you want the ball to go.");
       msg = i18n("You just moved the blue ball!");
173
       msg = i18n("Balls can be moved to every position on the board,\n"
174 175 176
                  "as long as there are no other balls in their way.");
       msg = i18n("Now we only need one more blue ball.");
       msg = i18n("It seems to be our lucky day!");
177
       msg = i18n("Hurray! And away they go!\n"
178 179 180 181 182 183 184 185
                  "Now lets try the green balls.");
       msg = i18n("Now you try!\n"
                  "Click on the green ball and move it to the others!");
       msg = i18n("Almost, try again!");
       msg = i18n("Very good!");
       msg = i18n("Whenever you complete a line you get an extra turn.");
       msg = i18n("This is the end of this tutorial.\n"
                  "Feel free to finish the game!");
186
                  */
Dmitry Suzdalev's avatar
Dmitry Suzdalev committed
187

188 189
void KLinesMainWindow::loadSettings()
{
Stefan Majewsky's avatar
Stefan Majewsky committed
190
    KLinesRenderer::loadTheme();
191 192 193
    QRectF r = mwidget->scene()->sceneRect();
    mwidget->scene()->invalidate( r, QGraphicsScene::BackgroundLayer ); // redraw background
    mwidget->scene()->resizeScene( (int)r.width(), (int)r.height() ); // redraw scene
194 195
}

196