1/*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8*/
9
10#include "mainwindow.h"
11
12#include <QLayout>
13#include <QLabel>
14#include <QGraphicsView>
15
16#include <KAction>
17#include <KActionCollection>
18#include <KDebug>
19#include <KgDifficulty>
20#include <KScoreDialog>
21#include <KStandardGameAction>
22#include <KStatusBar>
23#include <KToggleAction>
24
25#include "mainarea.h"
26#include "kollisionconfig.h"
27
28MainWindow::MainWindow()
29{
30 m_main = new MainArea();
31 QGraphicsView* view = new QGraphicsView(m_main, this);
32 view->setOptimizationFlags( QGraphicsView::DontClipPainter |
33 QGraphicsView::DontSavePainterState |
34 QGraphicsView::DontAdjustForAntialiasing );
35// view->setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
36 view->setCacheMode( QGraphicsView::CacheBackground );
37 view->setFrameStyle(QFrame::NoFrame);
38
39 setCentralWidget(view);
40
41 Kg::difficulty()->addStandardLevelRange(
42 KgDifficultyLevel::Easy, KgDifficultyLevel::Hard,
43 KgDifficultyLevel::Hard //default
44 );
45 KgDifficultyGUI::init(this);
46 connect(Kg::difficulty(), SIGNAL(currentLevelChanged(const KgDifficultyLevel*)), m_main, SLOT(abort()));
47
48 setupActions();
49
50 QLayout* l = layout();
51 Q_ASSERT(l);
52 l->setSizeConstraint(QLayout::SetFixedSize);
53
54 // setup status bar
55 KStatusBar* bar = statusBar();
56 Q_ASSERT(bar);
57 m_time_label = new QLabel("");
58 bar->addPermanentWidget(m_time_label);
59
60 m_balls_label = new QLabel("");
61 bar->addWidget(m_balls_label);
62// bar->setItemAlignment(STATUSBAR_BALLS, Qt::AlignLeft);
63
64 connect(m_main, SIGNAL(changeGameTime(int)), this, SLOT(setGameTime(int)));
65 connect(m_main, SIGNAL(changeBallNumber(int)), this, SLOT(setBallNumber(int)));
66 connect(m_main, SIGNAL(changeState(bool)), this, SLOT(changeState(bool)));
67 connect(m_main, SIGNAL(pause(bool)), this, SLOT(pause(bool)));
68
69 stateChanged("playing", KXMLGUIClient::StateReverse);
70 connect(m_main, SIGNAL(starting()), this, SLOT(newGame()));
71 connect(m_main, SIGNAL(gameOver(int)), this, SLOT(gameOver(int)));
72}
73
74MainWindow::~MainWindow()
75{
76 delete m_main;
77}
78
79void MainWindow::setupActions()
80{
81 // Game
82 KAction* abort = actionCollection()->addAction( QLatin1String( "game_abort" ));
83 abort->setText(i18n("End game"));
84 connect(abort, SIGNAL(triggered()), m_main, SLOT(abort()));
85
86 KAction* pause = KStandardGameAction::pause(m_main, SLOT(togglePause()), actionCollection());
87 m_main->setPauseAction(pause);
88 KStandardGameAction::highscores(this, SLOT(highscores()), actionCollection());
89 KStandardGameAction::quit(this, SLOT(close()), actionCollection());
90
91 KAction* action;
92 action = new KToggleAction(i18n("&Play Sounds"), this);
93 action->setChecked(KollisionConfig::enableSounds());
94 actionCollection()->addAction( QLatin1String( "options_sounds" ), action);
95 connect(action, SIGNAL(triggered(bool)), m_main, SLOT(enableSounds(bool)));
96
97 setupGUI(Create | Save | Keys | StatusBar);
98}
99
100void MainWindow::newGame()
101{
102 stateChanged("playing");
103 m_lastUsedDifficulty = Kg::difficulty()->currentLevel();
104}
105
106void MainWindow::gameOver(int time)
107{
108 stateChanged("playing", KXMLGUIClient::StateReverse);
109
110 KScoreDialog ksdialog(KScoreDialog::Name, this);
111 ksdialog.initFromDifficulty(Kg::difficulty(), /*setConfigGroup=*/ false);
112 ksdialog.setConfigGroup(qMakePair(
113 m_lastUsedDifficulty->key(),
114 m_lastUsedDifficulty->title()
115 ));
116 KScoreDialog::FieldInfo scoreInfo;
117 scoreInfo[KScoreDialog::Score].setNum(time);
118 if (ksdialog.addScore(scoreInfo, KScoreDialog::AskName)) {
119 ksdialog.exec();
120 }
121}
122
123void MainWindow::highscores()
124{
125 KScoreDialog ksdialog(KScoreDialog::Name | KScoreDialog::Time, this);
126 ksdialog.initFromDifficulty(Kg::difficulty());
127 ksdialog.exec();
128}
129
130void MainWindow::setBallNumber(int number)
131{
132 m_balls_label->setText(i18n("Balls: %1", number));
133}
134
135void MainWindow::setGameTime(int time)
136{
137 m_time_label->setText(i18np("Time: %1 second", "Time: %1 seconds", time));
138}
139
140void MainWindow::changeState(bool running) {
141 showCursor(!running);
142 Kg::difficulty()->setGameRunning(running);
143}
144
145void MainWindow::pause(bool p)
146{
147 showCursor(p);
148}
149
150void MainWindow::showCursor(bool show)
151{
152 if (show) {
153 centralWidget()->setCursor(QCursor());
154 }
155 else {
156 centralWidget()->setCursor(Qt::BlankCursor);
157 }
158}
159
160#include "mainwindow.moc"
161