File indexing completed on 2022-10-04 14:29:26

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "mainwindow.h"
0008 
0009 #include <QLayout>
0010 #include <QLabel>
0011 #include <QGraphicsView>
0012 #include <QPointer>
0013 
0014 #include <QAction>
0015 #include <KActionCollection>
0016 #include <KgDifficulty>
0017 #include <KScoreDialog>
0018 #include <KStandardGameAction>
0019 #include <QStatusBar>
0020 #include <KToggleAction>
0021 
0022 #include "mainarea.h"
0023 #include "kollisionconfig.h"
0024 
0025 MainWindow::MainWindow()
0026 {
0027     m_main = new MainArea();
0028     QGraphicsView* view = new QGraphicsView(m_main, this);
0029     view->setOptimizationFlags( 
0030                                 QGraphicsView::DontSavePainterState |
0031                                 QGraphicsView::DontAdjustForAntialiasing );
0032 //    view->setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
0033     view->setCacheMode( QGraphicsView::CacheBackground );
0034     view->setFrameStyle(QFrame::NoFrame);
0035 
0036     setCentralWidget(view);
0037 
0038     Kg::difficulty()->addStandardLevelRange(
0039         KgDifficultyLevel::Easy, KgDifficultyLevel::Hard,
0040         KgDifficultyLevel::Hard //default
0041     );
0042     KgDifficultyGUI::init(this);
0043     connect(Kg::difficulty(), &KgDifficulty::currentLevelChanged, m_main, &MainArea::abort);
0044 
0045     setupActions();
0046 
0047     QLayout* l = layout();
0048     Q_ASSERT(l);
0049     l->setSizeConstraint(QLayout::SetFixedSize);
0050 
0051     // setup status bar
0052     QStatusBar* bar = statusBar();
0053     Q_ASSERT(bar);
0054     m_time_label = new QLabel;
0055     bar->addPermanentWidget(m_time_label);
0056 
0057     m_balls_label = new QLabel;
0058     bar->addWidget(m_balls_label);
0059 //     bar->setItemAlignment(STATUSBAR_BALLS, Qt::AlignLeft);
0060 
0061     connect(m_main, &MainArea::changeGameTime, this, &MainWindow::setGameTime);
0062     connect(m_main, &MainArea::changeBallNumber, this, &MainWindow::setBallNumber);
0063     connect(m_main, &MainArea::changeState, this, &MainWindow::changeState);
0064     connect(m_main, &MainArea::pause, this, &MainWindow::pause);
0065 
0066     stateChanged(QStringLiteral("playing"), KXMLGUIClient::StateReverse);
0067     connect(m_main, &MainArea::starting, this, &MainWindow::newGame);
0068     connect(m_main, &MainArea::gameOver, this, &MainWindow::gameOver);
0069 }
0070 
0071 MainWindow::~MainWindow()
0072 {
0073     delete m_main;
0074 }
0075 
0076 void MainWindow::setupActions()
0077 {
0078     // Game
0079     QAction * abort = actionCollection()->addAction( QStringLiteral( "game_abort" ));
0080     abort->setText(i18n("End game"));
0081     connect(abort, &QAction::triggered, m_main, &MainArea::abort);
0082 
0083     QAction * pause = KStandardGameAction::pause(m_main, &MainArea::togglePause, actionCollection());
0084     m_main->setPauseAction(pause);
0085     KStandardGameAction::highscores(this, &MainWindow::highscores, actionCollection());
0086     KStandardGameAction::quit(this, &QWidget::close, actionCollection());
0087 
0088     QAction * ballSize = new KToggleAction(i18n("&Increase Ball Size"), this);
0089     ballSize->setChecked(KollisionConfig::increaseBallSize());
0090     actionCollection()->addAction( QStringLiteral( "increase_ball_size" ), ballSize);
0091     connect(ballSize, &QAction::triggered, m_main, &MainArea::increaseBallSize);
0092 
0093     QAction * action;
0094     action = new KToggleAction(i18n("&Play Sounds"), this);
0095     action->setChecked(KollisionConfig::enableSounds());
0096     actionCollection()->addAction( QStringLiteral( "options_sounds" ), action);
0097     connect(action, &QAction::triggered, m_main, &MainArea::enableSounds);
0098 
0099     setupGUI(Create | Save | Keys | StatusBar);
0100 }
0101 
0102 void MainWindow::newGame()
0103 {
0104     stateChanged(QStringLiteral("playing"));
0105     m_lastUsedDifficulty = Kg::difficulty()->currentLevel();
0106 }
0107 
0108 void MainWindow::gameOver(int time)
0109 {
0110     stateChanged(QStringLiteral("playing"), KXMLGUIClient::StateReverse);
0111 
0112     QPointer<KScoreDialog> ksdialog = new KScoreDialog(KScoreDialog::Name, this);
0113     ksdialog->initFromDifficulty(Kg::difficulty(), /*setConfigGroup=*/ false);
0114     ksdialog->setConfigGroup(qMakePair(
0115         m_lastUsedDifficulty->key(),
0116         m_lastUsedDifficulty->title()
0117     ));
0118     KScoreDialog::FieldInfo scoreInfo;
0119     scoreInfo[KScoreDialog::Score].setNum(time);
0120     if (ksdialog->addScore(scoreInfo, KScoreDialog::AskName)) {
0121         ksdialog->exec();
0122     }
0123     delete ksdialog;
0124 }
0125 
0126 void MainWindow::highscores()
0127 {
0128     KScoreDialog ksdialog(KScoreDialog::Name | KScoreDialog::Time, this);
0129     ksdialog.initFromDifficulty(Kg::difficulty());
0130     ksdialog.exec();
0131 }
0132 
0133 void MainWindow::setBallNumber(int number)
0134 {
0135     m_balls_label->setText(i18n("Balls: %1", number));
0136 }
0137 
0138 void MainWindow::setGameTime(int time)
0139 {
0140     m_time_label->setText(i18np("Time: %1 second", "Time: %1 seconds", time));
0141 }
0142 
0143 void MainWindow::changeState(bool running) {
0144     showCursor(!running);
0145     Kg::difficulty()->setGameRunning(running);
0146 }
0147 
0148 void MainWindow::pause(bool p)
0149 {
0150     showCursor(p);
0151 }
0152 
0153 void MainWindow::showCursor(bool show)
0154 {
0155     if (show) {
0156         centralWidget()->setCursor(QCursor());
0157     }
0158     else {
0159         centralWidget()->setCursor(Qt::BlankCursor);
0160     }
0161 }
0162 
0163