File indexing completed on 2024-04-28 07:51:54

0001 /*
0002     SPDX-FileCopyrightText: 2000 Roman Merzlyakov <roman@sbrf.barrt.ru>
0003     SPDX-FileCopyrightText: 2000 Roman Razilov <roman@sbrf.barrt.ru>
0004     SPDX-FileCopyrightText: 2007 Dimitry Suzdalev <dimsuz@gmail.com>
0005     SPDX-FileCopyrightText: 2007 Simon Hürlimann <simon.huerlimann@huerlisi.ch>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "klines.h"
0011 #include "renderer.h"
0012 #include "prefs.h"
0013 #include "mwidget.h"
0014 #include "scene.h"
0015 
0016 #include <QStatusBar>
0017 #include <QAction>
0018 
0019 #include <KConfig>
0020 #include <KActionCollection>
0021 #include <KStandardAction>
0022 #include <KToggleAction>
0023 #include <KLocalizedString>
0024 
0025 #include <KGameHighScoreDialog>
0026 #include <KGameStandardAction>
0027 #include <KGameThemeSelector>
0028 #include <KGameRenderer>
0029 
0030 KLinesMainWindow::KLinesMainWindow()
0031 {
0032     KLinesRenderer::Init();
0033 
0034     mwidget = new MainWidget(this);
0035     setCentralWidget( mwidget );
0036 
0037     connect(mwidget->scene(), &KLinesScene::scoreChanged, this, &KLinesMainWindow::updateScore);
0038     connect(mwidget->scene(), &KLinesScene::stateChanged, this,
0039             qOverload<const QString &>(&KLinesMainWindow::slotStateChanged));
0040     connect(mwidget->scene(), &KLinesScene::gameOver, this, &KLinesMainWindow::gameOver);
0041 
0042     scoreLabel->setText(i18n("Score:"));
0043     statusBar()->addPermanentWidget(scoreLabel);
0044     
0045     updateScore(0);
0046 
0047     KGameThemeProvider* prov = KLinesRenderer::renderer()->themeProvider();
0048     connect(prov, &KGameThemeProvider::currentThemeChanged, this, &KLinesMainWindow::loadSettings);
0049     mselector = new KGameThemeSelector(KLinesRenderer::renderer()->themeProvider());
0050 
0051     setupActions();
0052 
0053     stateChanged(QStringLiteral( "init" ));
0054 }
0055 
0056 KLinesMainWindow::~KLinesMainWindow()
0057 {
0058   KLinesRenderer::UnInit();
0059 }
0060 
0061 void KLinesMainWindow::setupActions()
0062 {
0063   // Game
0064   KGameStandardAction::gameNew(this, &KLinesMainWindow::startGame, actionCollection());
0065   KGameStandardAction::highscores(this, &KLinesMainWindow::viewHighScore, actionCollection());
0066   KGameStandardAction::quit(this, &QWidget::close, actionCollection());
0067   KGameStandardAction::end(mwidget->scene(), &KLinesScene::endGame, actionCollection());
0068 
0069   // Move
0070   KGameStandardAction::undo(mwidget->scene(), &KLinesScene::undo, actionCollection());
0071   KGameStandardAction::endTurn(mwidget->scene(), &KLinesScene::endTurn, actionCollection());
0072 
0073   // Preferences
0074   KToggleAction *showNext = actionCollection()->add<KToggleAction>(QStringLiteral( "show_next" ));
0075   showNext->setText( i18n( "Show Next" ) );
0076   connect(showNext, &KToggleAction::triggered, this, &KLinesMainWindow::showNextToggled);
0077 
0078   showNext->setChecked(Prefs::showNext());
0079   mwidget->setShowNextColors(Prefs::showNext());
0080 
0081   // Navigation
0082   QAction* naviLeft = new QAction( i18n("Move Left" ), this );
0083   naviLeft->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-left"))); 
0084   KActionCollection::setDefaultShortcut(naviLeft, Qt::Key_Left);
0085   actionCollection()->addAction( QStringLiteral( "navi_left" ), naviLeft);
0086 
0087   QAction* naviRight = new QAction( i18n("Move Right" ), this );
0088   naviRight->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-right")));
0089   KActionCollection::setDefaultShortcut(naviRight, Qt::Key_Right);
0090   actionCollection()->addAction( QStringLiteral( "navi_right" ), naviRight);
0091 
0092   QAction* naviUp = new QAction( i18n("Move Up" ), this );
0093   naviUp->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-up")));
0094   KActionCollection::setDefaultShortcut(naviUp, Qt::Key_Up);
0095   actionCollection()->addAction( QStringLiteral( "navi_up" ), naviUp);
0096 
0097   QAction* naviDown = new QAction( i18n("Move Down" ), this );
0098   naviDown->setIcon(QIcon::fromTheme(QStringLiteral( "arrow-down")));
0099   KActionCollection::setDefaultShortcut(naviDown, Qt::Key_Down);
0100   actionCollection()->addAction( QStringLiteral( "navi_down" ), naviDown);
0101 
0102   QAction* naviSelect = new QAction( i18n("Select"), this );
0103   KActionCollection::setDefaultShortcut(naviSelect, Qt::Key_Space);
0104   actionCollection()->addAction( QStringLiteral( "navi_select" ), naviSelect);
0105 
0106   connect( naviLeft, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusLeft);
0107   connect( naviRight, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusRight);
0108   connect( naviUp, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusUp);
0109   connect( naviDown, &QAction::triggered, mwidget->scene(), &KLinesScene::moveFocusDown);
0110   connect( naviSelect, &QAction::triggered, mwidget->scene(), &KLinesScene::cellSelected);
0111 
0112   KStandardAction::preferences(mselector, [this]() { mselector->showAsDialog(); }, actionCollection());
0113   setupGUI();
0114 }
0115 
0116 void KLinesMainWindow::updateScore(int score)
0117 {
0118     scoreLabel->setText(i18n("Score: %1", score));
0119 }
0120 
0121 void KLinesMainWindow::gameOver(int score)
0122 {
0123     KGameHighScoreDialog d(KGameHighScoreDialog::Name | KGameHighScoreDialog::Score, this);
0124     d.setConfigGroup(qMakePair(QByteArray("Highscore"), i18n("High Scores")));
0125     d.addScore(score, KGameHighScoreDialog::AskName);
0126     d.exec();
0127 }
0128 
0129 void KLinesMainWindow::viewHighScore()
0130 {
0131    KGameHighScoreDialog d(KGameHighScoreDialog::Name | KGameHighScoreDialog::Score, this);
0132    d.setConfigGroup(qMakePair(QByteArray("Highscore"), i18n("High Scores")));
0133    d.exec();
0134 }
0135 
0136 void KLinesMainWindow::startGame()
0137 {
0138     updateScore(0);
0139     mwidget->scene()->startNewGame();
0140 }
0141 
0142 void KLinesMainWindow::showNextToggled(bool show)
0143 {
0144     mwidget->setShowNextColors(show);
0145     Prefs::setShowNext(show);
0146     Prefs::self()->save();
0147 }
0148 
0149 
0150 // FIXME these are strings from old tutorial
0151 // leave them if I ever want to use them when I'll impelement tutorial mode
0152 /**
0153        msg = i18n("The goal of the game is to put\n"
0154        msg = i18n("You can make horizontal, vertical\n"
0155                   "and diagonal lines.");
0156        msg = i18n("Each turn, three new balls are placed on the board.");
0157        msg = i18n("Every turn, you can move one ball.");
0158        msg = i18n("To move a ball, click on it with the mouse,\n"
0159                   "then click where you want the ball to go.");
0160        msg = i18n("You just moved the blue ball!");
0161        msg = i18n("Balls can be moved to every position on the board,\n"
0162                   "as long as there are no other balls in their way.");
0163        msg = i18n("Now we only need one more blue ball.");
0164        msg = i18n("It seems to be our lucky day!");
0165        msg = i18n("Hurray! And away they go!\n"
0166                   "Now lets try the green balls.");
0167        msg = i18n("Now you try!\n"
0168                   "Click on the green ball and move it to the others!");
0169        msg = i18n("Almost, try again!");
0170        msg = i18n("Very good!");
0171        msg = i18n("Whenever you complete a line you get an extra turn.");
0172        msg = i18n("This is the end of this tutorial.\n"
0173                   "Feel free to finish the game!");
0174                   */
0175 
0176 void KLinesMainWindow::loadSettings()
0177 {
0178     KLinesRenderer::loadTheme();
0179     QRectF r = mwidget->scene()->sceneRect();
0180     mwidget->scene()->invalidate( r, QGraphicsScene::BackgroundLayer ); // redraw background
0181     mwidget->scene()->resizeScene( (int)r.width(), (int)r.height() ); // redraw scene
0182 }
0183 
0184 #include "moc_klines.cpp"