File indexing completed on 2024-02-18 04:04:53

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003     SPDX-FileCopyrightText: 2007 Riccardo Iaconelli <ruphy@fsfe.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "mainwindow.h"
0009 
0010 #include <QAction>
0011 #include <QIcon>
0012 
0013 #include <KActionCollection>
0014 #include <KLocalizedString>
0015 #include <KStandardAction>
0016 #include <KGameStandardAction>
0017 #include <QStatusBar>
0018 #include <KToggleAction>
0019 
0020 #include "playfield.h"
0021 #include "settings.h"
0022 #include "simplemenu.h"
0023 #include "animator.h"
0024 
0025 MainWindow::MainWindow(const QUrl& url)
0026 {
0027     m_main = new PlayField(this, statusBar());
0028     
0029     setCentralWidget(m_main);
0030 
0031     KGameDifficulty::global()->addStandardLevelRange(
0032         KGameDifficultyLevel::Easy, KGameDifficultyLevel::Hard, //range
0033         KGameDifficultyLevel::Hard //default
0034     );
0035     KGameDifficultyGUI::init(this);
0036 
0037     connect(KGameDifficulty::global(), &KGameDifficulty::currentLevelChanged, m_main, &PlayField::levelChanged);
0038 
0039     setupActions();
0040 
0041     connect(m_main, &PlayField::welcomeScreen, this, &MainWindow::welcomeScreen);
0042     connect(m_main, &PlayField::placeShips, this, &MainWindow::startingGame);
0043     
0044     m_main->newGame();
0045 
0046     if(! url.isEmpty() )
0047         m_main->createClientWithUrl(url);
0048 }
0049 
0050 void MainWindow::setupActions()
0051 {
0052     KGameStandardAction::gameNew(m_main, &PlayField::newGame, actionCollection());
0053     KGameStandardAction::restart(m_main, &PlayField::restart, actionCollection());
0054     KGameStandardAction::highscores(m_main, &PlayField::highscores, actionCollection());
0055     
0056     KGameStandardAction::quit(this, &MainWindow::close, actionCollection());
0057     
0058     QAction* action;
0059     action = new QAction(i18n("&Single Player"), this);
0060     action->setIcon(QIcon::fromTheme( QLatin1String( SimpleMenu::iconLocal)));
0061     actionCollection()->addAction(QStringLiteral("game_local"), action);
0062     connect(action, &QAction::triggered, m_main, &PlayField::localGame);
0063     action = new QAction(i18n("&Host Game..."), this);
0064     action->setIcon(QIcon::fromTheme( QLatin1String( SimpleMenu::iconServer)));
0065     actionCollection()->addAction(QStringLiteral("game_create_server"), action);
0066     connect(action, &QAction::triggered, m_main, &PlayField::createServer);
0067     action = new QAction(i18n("&Connect to Game..."), this);
0068     action->setIcon(QIcon::fromTheme( QLatin1String( SimpleMenu::iconClient))),
0069     actionCollection()->addAction(QStringLiteral("game_create_client"), action);
0070     connect(action, &QAction::triggered, m_main, &PlayField::createClient);
0071     // settings
0072     action = new QAction(i18n("Change &Nickname..."), this);
0073     actionCollection()->addAction(QStringLiteral("options_nickname"), action);
0074     connect(action, &QAction::triggered, m_main, &PlayField::changeNick);
0075     action = new KToggleAction(i18n("&Play Sounds"), this);
0076     action->setChecked(Settings::enableSounds());
0077     actionCollection()->addAction(QStringLiteral("options_sounds"), action);
0078     connect(action, &QAction::triggered, m_main, &PlayField::toggleSounds);
0079     // This action will be disabled when a game is being run
0080     action = new KToggleAction(i18n("&Adjacent Ships"), this);
0081     action->setChecked(Settings::adjacentShips());
0082     actionCollection()->addAction(QStringLiteral("options_adjacent"), action);
0083     connect(action, &QAction::triggered, m_main, &PlayField::toggleAdjacent);
0084     // This action will be disabled when a game is being run
0085     action = new KToggleAction(i18n("&Multiple Ships"), this);
0086     action->setChecked(Settings::severalShips());
0087     actionCollection()->addAction(QStringLiteral("options_multiple_ships"), action);
0088     connect(action, &QAction::triggered, m_main, &PlayField::toggleMultiple);
0089     // config end of game message
0090     action = new KToggleAction(i18n("Show End-of-Game Message"), this);
0091     action->setChecked(true);
0092     actionCollection()->addAction(QStringLiteral("options_show_endgame_message"), action);
0093     connect(action, &QAction::triggered, m_main, &PlayField::toggleEndOfGameMessage);
0094     // actions for grid
0095     action = new KToggleAction(i18n("Show &Left Grid"), this);
0096     action->setChecked(true);
0097     actionCollection()->addAction(QStringLiteral("options_showleftgrid"), action);
0098     connect(action, &QAction::triggered, m_main, &PlayField::toggleLeftGrid);
0099     action = new KToggleAction(i18n("Show &Right Grid"), this);
0100     action->setChecked(true);
0101     actionCollection()->addAction(QStringLiteral("options_showrightgrid"), action);
0102     connect(action, &QAction::triggered, m_main, &PlayField::toggleRightGrid);
0103     
0104     setupGUI();
0105 }
0106 
0107 void MainWindow::startingGame()
0108 {
0109     stateChanged(QStringLiteral("playing"));
0110 }
0111 
0112 void MainWindow::welcomeScreen()
0113 {
0114     stateChanged(QStringLiteral("playing"), KXMLGUIClient::StateReverse);
0115 }
0116 
0117 void MainWindow::closeEvent(QCloseEvent *)
0118 {
0119     Animator::instance()->stop();
0120 }
0121 
0122 #include "moc_mainwindow.cpp"