1/*
2 Copyright (C) 1998-2001 Andreas Zehender <az@azweb.de>
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 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "topwidget.h"
20
21
22#include <kicon.h>
23#include <kshortcutsdialog.h>
24#include <klocale.h>
25#include <kstandardaction.h>
26#include <kstandardgameaction.h>
27#include <kstatusbar.h>
28#include <ktoggleaction.h>
29#include <kactioncollection.h>
30
31#include "mainview.h"
32#include "playerinfo.h"
33
34#include <QBoxLayout>
35
36MyTopLevelWidget::MyTopLevelWidget()
37{
38 initGameWidgets();
39 initStatusBar( );
40 setupActions( );
41 setupGUI( );
42}
43
44void MyTopLevelWidget::initGameWidgets( ){
45 QWidget *w = new QWidget(this);
46
47 PlayerInfo::loadPixmaps();
48 playerinfo[0]=new PlayerInfo(0,w);
49 playerinfo[1]=new PlayerInfo(1,w);
50 playfield=new MyMainView(w);
51
52 QBoxLayout *toplayout=new QHBoxLayout(w);
53 toplayout->setMargin(0);
54 toplayout->addWidget(playerinfo[0]);
55 toplayout->addWidget(playfield);
56 toplayout->addWidget(playerinfo[1]);
57 toplayout->activate();
58
59 playfield->setFocusPolicy(Qt::StrongFocus);
60 playfield->setFocus();
61
62 QObject::connect(playfield,SIGNAL(energy(int,int)),
63 SLOT(energy(int,int)));
64 QObject::connect(playfield,SIGNAL(hitPoints(int,int)),
65 SLOT(hitPoints(int,int)));
66 QObject::connect(playfield,SIGNAL(wins(int,int)),SLOT(wins(int,int)));
67 QObject::connect(playfield,SIGNAL(setStatusText(QString,int)),
68 SLOT(setStatusText(QString,int)));
69
70 setCentralWidget(w);
71}
72
73void MyTopLevelWidget::energy(int pn,int en)
74{
75 playerinfo[pn]->setEnergy(en);
76}
77
78void MyTopLevelWidget::hitPoints(int pn,int hp)
79{
80 playerinfo[pn]->setHitpoints(hp);
81}
82
83void MyTopLevelWidget::wins(int pn,int w)
84{
85 playerinfo[pn]->setWins(w);
86}
87
88void MyTopLevelWidget::setupActions()
89{
90 KAction* ac;
91
92 // Game
93 KStandardGameAction::gameNew(playfield, SLOT(newGame()), actionCollection());
94 KStandardGameAction::quit(this, SLOT(close()), actionCollection());
95
96 KAction* newRoundAct = actionCollection()->addAction( QLatin1String( "new_round" ) );
97 newRoundAct->setIcon( KIcon( QLatin1String( "preferences-desktop-notification-bell" )) );
98 newRoundAct->setText( i18n( "&New Round" ) );
99 newRoundAct->setShortcut( Qt::CTRL + Qt::Key_R );
100 connect( newRoundAct, SIGNAL(triggered(bool)), playfield, SLOT(newRound()) );
101
102 MyMainView::pauseAction =
103 KStandardGameAction::pause(playfield, SLOT(togglePause()), actionCollection());
104 MyMainView::pauseAction->setChecked( false );
105 KAction *gameStart = actionCollection()->addAction( QLatin1String( "game_start" ) );
106 gameStart->setText( i18nc( "start game","Start" ) );
107 connect(gameStart, SIGNAL(triggered(bool)), playfield, SLOT(start()));
108 gameStart->setShortcut(GAME_START_SHORTCUT);
109 playfield->addAction(gameStart);
110
111 KStandardAction::preferences(playfield, SLOT(gameSetup()), actionCollection());
112
113 // Default keys
114#ifdef __GNUC__
115#warning assuming this is not necessary anymore
116#endif
117 // actionCollection()->setAutoConnectShortcuts(false);
118 ac = actionCollection()->addAction( QLatin1String( "P1KeyLeft" ));
119 ac->setText(i18n("Player 1 Rotate Left"));
120 ac->setShortcut(Qt::Key_S);
121 ac->setEnabled( false );
122 ac = actionCollection()->addAction( QLatin1String( "P1KeyRight" ));
123 ac->setText(i18n("Player 1 Rotate Right"));
124 ac->setShortcut(Qt::Key_F);
125 ac->setEnabled( false );
126 ac = actionCollection()->addAction( QLatin1String( "P1KeyAcc" ));
127 ac->setText(i18n("Player 1 Accelerate"));
128 ac->setShortcut(Qt::Key_E);
129 ac->setEnabled( false );
130 ac = actionCollection()->addAction( QLatin1String( "P1Shot" ));
131 ac->setText(i18n("Player 1 Shot"));
132 ac->setShortcut(Qt::Key_D);
133 ac->setEnabled( false );
134 ac = actionCollection()->addAction( QLatin1String( "P1Mine" ));
135 ac->setText(i18n("Player 1 Mine"));
136 ac->setShortcut(Qt::Key_A);
137 ac->setEnabled( false );
138
139 ac = actionCollection()->addAction( QLatin1String( "P2KeyLeft" ));
140 ac->setText(i18n("Player 2 Rotate Left"));
141 ac->setShortcut(Qt::Key_Left);
142 ac->setEnabled( false );
143 ac = actionCollection()->addAction( QLatin1String( "P2KeyRight" ));
144 ac->setText(i18n("Player 2 Rotate Right"));
145 ac->setShortcut(Qt::Key_Right);
146 ac->setEnabled( false );
147 ac = actionCollection()->addAction( QLatin1String( "P2KeyAcc" ));
148 ac->setText(i18n("Player 2 Accelerate"));
149 ac->setShortcut(Qt::Key_Up);
150 ac->setEnabled( false );
151 ac = actionCollection()->addAction( QLatin1String( "P2Shot" ));
152 ac->setText(i18n("Player 2 Shot"));
153 ac->setShortcut(Qt::Key_Down);
154 ac->setEnabled( false );
155 ac = actionCollection()->addAction( QLatin1String( "P2Mine" ));
156 ac->setText(i18n("Player 2 Mine"));
157 ac->setShortcut(Qt::Key_Insert);
158 ac->setEnabled( false );
159
160 // actionCollection()->setAutoConnectShortcuts(true);
161 playfield->setActionCollection(actionCollection());
162}
163
164void MyTopLevelWidget::initStatusBar( )
165{
166 statusBar( )->insertItem(i18n(" paused "),IDS_PAUSE);
167 statusBar( )->insertItem(QLatin1String( " " ),IDS_MAIN ,1);
168 statusBar( )->insertItem(QLatin1String( "" ),42);
169}
170
171void MyTopLevelWidget::start()
172{
173 playfield->newGame();
174}
175
176void MyTopLevelWidget::setStatusText(const QString & str,int id)
177{
178 statusBar( )->changeItem(str,id);
179}
180
181void MyTopLevelWidget::keySetup()
182{
183 playfield->pause();
184 KShortcutsDialog::configure( actionCollection( ), KShortcutsEditor::LetterShortcutsAllowed, this, true );
185}
186
187#include "topwidget.moc"
188