File: topwidget.cpp

package info (click to toggle)
kspaceduel 4%3A16.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,204 kB
  • ctags: 483
  • sloc: cpp: 3,149; makefile: 3; sh: 2
file content (187 lines) | stat: -rw-r--r-- 6,126 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
    Copyright (C) 1998-2001 Andreas Zehender <az@azweb.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "topwidget.h"


#include <kicon.h>
#include <kshortcutsdialog.h>
#include <klocale.h>
#include <kstandardaction.h>
#include <kstandardgameaction.h>
#include <kstatusbar.h>
#include <ktoggleaction.h>
#include <kactioncollection.h>

#include "mainview.h"
#include "playerinfo.h"

#include <QBoxLayout>

MyTopLevelWidget::MyTopLevelWidget()
{
   initGameWidgets();
   initStatusBar( );
   setupActions( );
   setupGUI( );
}

void MyTopLevelWidget::initGameWidgets( ){
   QWidget *w = new QWidget(this);

   PlayerInfo::loadPixmaps();
   playerinfo[0]=new PlayerInfo(0,w);
   playerinfo[1]=new PlayerInfo(1,w);
   playfield=new MyMainView(w);

   QBoxLayout *toplayout=new QHBoxLayout(w);
   toplayout->setMargin(0);
   toplayout->addWidget(playerinfo[0]);
   toplayout->addWidget(playfield);
   toplayout->addWidget(playerinfo[1]);
   toplayout->activate();

   playfield->setFocusPolicy(Qt::StrongFocus);
   playfield->setFocus();

   QObject::connect(playfield,SIGNAL(energy(int,int)),
                    SLOT(energy(int,int)));
   QObject::connect(playfield,SIGNAL(hitPoints(int,int)),
                    SLOT(hitPoints(int,int)));
   QObject::connect(playfield,SIGNAL(wins(int,int)),SLOT(wins(int,int)));
   QObject::connect(playfield,SIGNAL(setStatusText(QString,int)),
                    SLOT(setStatusText(QString,int)));

   setCentralWidget(w);
}

void MyTopLevelWidget::energy(int pn,int en)
{
   playerinfo[pn]->setEnergy(en);
}

void MyTopLevelWidget::hitPoints(int pn,int hp)
{
   playerinfo[pn]->setHitpoints(hp);
}

void MyTopLevelWidget::wins(int pn,int w)
{
   playerinfo[pn]->setWins(w);
}

void MyTopLevelWidget::setupActions()
{
   KAction* ac;

   // Game
   KStandardGameAction::gameNew(playfield, SLOT(newGame()), actionCollection());
   KStandardGameAction::quit(this, SLOT(close()), actionCollection());

   KAction* newRoundAct = actionCollection()->addAction( QLatin1String(  "new_round" ) );
   newRoundAct->setIcon( KIcon( QLatin1String( "preferences-desktop-notification-bell" )) );
   newRoundAct->setText( i18n( "&New Round" ) );
   newRoundAct->setShortcut( Qt::CTRL + Qt::Key_R );
   connect( newRoundAct, SIGNAL(triggered(bool)), playfield, SLOT(newRound()) );

   MyMainView::pauseAction =
       KStandardGameAction::pause(playfield, SLOT(togglePause()), actionCollection());
   MyMainView::pauseAction->setChecked( false );
   KAction *gameStart = actionCollection()->addAction( QLatin1String(  "game_start" ) );
   gameStart->setText( i18nc( "start game","Start" ) );
   connect(gameStart, SIGNAL(triggered(bool)), playfield, SLOT(start()));
   gameStart->setShortcut(GAME_START_SHORTCUT);
   playfield->addAction(gameStart);

   KStandardAction::preferences(playfield, SLOT(gameSetup()), actionCollection());

   // Default keys
#ifdef __GNUC__
#warning assuming this is not necessary anymore
#endif
   // actionCollection()->setAutoConnectShortcuts(false);
   ac = actionCollection()->addAction( QLatin1String( "P1KeyLeft" ));
   ac->setText(i18n("Player 1 Rotate Left"));
   ac->setShortcut(Qt::Key_S);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P1KeyRight" ));
   ac->setText(i18n("Player 1 Rotate Right"));
   ac->setShortcut(Qt::Key_F);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P1KeyAcc" ));
   ac->setText(i18n("Player 1 Accelerate"));
   ac->setShortcut(Qt::Key_E);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P1Shot" ));
   ac->setText(i18n("Player 1 Shot"));
   ac->setShortcut(Qt::Key_D);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P1Mine" ));
   ac->setText(i18n("Player 1 Mine"));
   ac->setShortcut(Qt::Key_A);
   ac->setEnabled( false );

   ac = actionCollection()->addAction( QLatin1String( "P2KeyLeft" ));
   ac->setText(i18n("Player 2 Rotate Left"));
   ac->setShortcut(Qt::Key_Left);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P2KeyRight" ));
   ac->setText(i18n("Player 2 Rotate Right"));
   ac->setShortcut(Qt::Key_Right);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P2KeyAcc" ));
   ac->setText(i18n("Player 2 Accelerate"));
   ac->setShortcut(Qt::Key_Up);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P2Shot" ));
   ac->setText(i18n("Player 2 Shot"));
   ac->setShortcut(Qt::Key_Down);
   ac->setEnabled( false );
   ac = actionCollection()->addAction( QLatin1String( "P2Mine" ));
   ac->setText(i18n("Player 2 Mine"));
   ac->setShortcut(Qt::Key_Insert);
   ac->setEnabled( false );

   // actionCollection()->setAutoConnectShortcuts(true);
   playfield->setActionCollection(actionCollection());
}

void MyTopLevelWidget::initStatusBar( )
{
   statusBar( )->insertItem(i18n(" paused "),IDS_PAUSE);
   statusBar( )->insertItem(QLatin1String( "   " ),IDS_MAIN ,1);
   statusBar( )->insertItem(QLatin1String( "" ),42);
}

void MyTopLevelWidget::start()
{
   playfield->newGame();
}

void MyTopLevelWidget::setStatusText(const QString & str,int id)
{
   statusBar( )->changeItem(str,id);
}

void MyTopLevelWidget::keySetup()
{
   playfield->pause();
   KShortcutsDialog::configure( actionCollection( ), KShortcutsEditor::LetterShortcutsAllowed, this, true );
}

#include "topwidget.moc"