topwidget.cpp 6.11 KB
Newer Older
Simon Huerlimann's avatar
Simon Huerlimann committed
1 2
/*
    Copyright (C) 1998-2001 Andreas Zehender <az@azweb.de>
3

Simon Huerlimann's avatar
Simon Huerlimann committed
4
    This program is free software; you can redistribute it and/or modify
5 6 7 8 9 10 11 12 13 14 15 16 17 18
    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
*/

19 20
#include "topwidget.h"

21
#include <QBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
22
#include <QIcon>
23 24 25
#include <QStatusBar>

#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
26
#include <KLocalizedString>
27 28 29 30
#include <KShortcutsDialog>
#include <KStandardAction>
#include <KStandardGameAction>
#include <KToggleAction>
31

32
#include "mainview.h"
33
#include "playerinfo.h"
34

Laurent Montel's avatar
Laurent Montel committed
35

36
MyTopLevelWidget::MyTopLevelWidget()
Andreas Zehender's avatar
Andreas Zehender committed
37
{
38
   initGameWidgets();
39
   initStatusBar( );
40
   setupActions( );
41
   setupGUI( );
42
}
Andreas Zehender's avatar
Andreas Zehender committed
43

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

47
   PlayerInfo::loadPixmaps();
48 49 50 51 52
   playerinfo[0]=new PlayerInfo(0,w);
   playerinfo[1]=new PlayerInfo(1,w);
   playfield=new MyMainView(w);

   QBoxLayout *toplayout=new QHBoxLayout(w);
53
   toplayout->setMargin(0);
54 55 56 57 58
   toplayout->addWidget(playerinfo[0]);
   toplayout->addWidget(playfield);
   toplayout->addWidget(playerinfo[1]);
   toplayout->activate();

Laurent Montel's avatar
Laurent Montel committed
59
   playfield->setFocusPolicy(Qt::StrongFocus);
60 61 62 63 64 65 66
   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)));
Laurent Montel's avatar
Laurent Montel committed
67 68
   QObject::connect(playfield,SIGNAL(setStatusText(QString,int)),
                    SLOT(setStatusText(QString,int)));
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
   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);
86 87
}

88
void MyTopLevelWidget::setupActions()
89
{
Laurent Montel's avatar
Laurent Montel committed
90
   QAction * ac;
91

Simon Huerlimann's avatar
Simon Huerlimann committed
92 93 94
   // Game
   KStandardGameAction::gameNew(playfield, SLOT(newGame()), actionCollection());
   KStandardGameAction::quit(this, SLOT(close()), actionCollection());
Laurent Montel's avatar
Laurent Montel committed
95

Laurent Montel's avatar
Laurent Montel committed
96
   QAction * newRoundAct = actionCollection()->addAction( QLatin1String(  "new_round" ) );
Laurent Montel's avatar
Laurent Montel committed
97
   newRoundAct->setIcon( QIcon::fromTheme( QLatin1String( "preferences-desktop-notification-bell" )) );
98
   newRoundAct->setText( i18n( "&New Round" ) );
99
   newRoundAct->setShortcut( Qt::CTRL + Qt::Key_R );
Laurent Montel's avatar
Laurent Montel committed
100
   connect( newRoundAct, SIGNAL(triggered(bool)), playfield, SLOT(newRound()) );
101

102
   MyMainView::pauseAction =
Simon Huerlimann's avatar
Simon Huerlimann committed
103
       KStandardGameAction::pause(playfield, SLOT(togglePause()), actionCollection());
104
   MyMainView::pauseAction->setChecked( false );
Laurent Montel's avatar
Laurent Montel committed
105
   QAction *gameStart = actionCollection()->addAction( QLatin1String(  "game_start" ) );
Dirk Rathlev's avatar
Dirk Rathlev committed
106
   gameStart->setText( i18nc( "start game","Start" ) );
Laurent Montel's avatar
Laurent Montel committed
107
   connect(gameStart, SIGNAL(triggered(bool)), playfield, SLOT(start()));
108
   gameStart->setShortcut(Qt::Key_Space);
109
   playfield->addAction(gameStart);
110

Simon Huerlimann's avatar
Simon Huerlimann committed
111
   KStandardAction::preferences(playfield, SLOT(gameSetup()), actionCollection());
112

113
   // Default keys
114
#ifdef __GNUC__
Jaison Lee's avatar
Jaison Lee committed
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"));
Stephan Kulow's avatar
Stephan Kulow committed
120
   ac->setShortcut(Qt::Key_S);
Andreas Zehender's avatar
Andreas Zehender committed
121
   ac->setEnabled( false );
122
   ac = actionCollection()->addAction( QLatin1String( "P1KeyRight" ));
123
   ac->setText(i18n("Player 1 Rotate Right"));
Stephan Kulow's avatar
Stephan Kulow committed
124
   ac->setShortcut(Qt::Key_F);
Andreas Zehender's avatar
Andreas Zehender committed
125
   ac->setEnabled( false );
126
   ac = actionCollection()->addAction( QLatin1String( "P1KeyAcc" ));
127
   ac->setText(i18n("Player 1 Accelerate"));
Stephan Kulow's avatar
Stephan Kulow committed
128
   ac->setShortcut(Qt::Key_E);
Andreas Zehender's avatar
Andreas Zehender committed
129
   ac->setEnabled( false );
130
   ac = actionCollection()->addAction( QLatin1String( "P1Shot" ));
131
   ac->setText(i18n("Player 1 Shot"));
Stephan Kulow's avatar
Stephan Kulow committed
132
   ac->setShortcut(Qt::Key_D);
Andreas Zehender's avatar
Andreas Zehender committed
133
   ac->setEnabled( false );
134
   ac = actionCollection()->addAction( QLatin1String( "P1Mine" ));
135
   ac->setText(i18n("Player 1 Mine"));
Stephan Kulow's avatar
Stephan Kulow committed
136
   ac->setShortcut(Qt::Key_A);
Andreas Zehender's avatar
Andreas Zehender committed
137 138
   ac->setEnabled( false );

139
   ac = actionCollection()->addAction( QLatin1String( "P2KeyLeft" ));
140
   ac->setText(i18n("Player 2 Rotate Left"));
Stephan Kulow's avatar
Stephan Kulow committed
141
   ac->setShortcut(Qt::Key_Left);
Andreas Zehender's avatar
Andreas Zehender committed
142
   ac->setEnabled( false );
143
   ac = actionCollection()->addAction( QLatin1String( "P2KeyRight" ));
144
   ac->setText(i18n("Player 2 Rotate Right"));
Stephan Kulow's avatar
Stephan Kulow committed
145
   ac->setShortcut(Qt::Key_Right);
Andreas Zehender's avatar
Andreas Zehender committed
146
   ac->setEnabled( false );
147
   ac = actionCollection()->addAction( QLatin1String( "P2KeyAcc" ));
148
   ac->setText(i18n("Player 2 Accelerate"));
Stephan Kulow's avatar
Stephan Kulow committed
149
   ac->setShortcut(Qt::Key_Up);
Andreas Zehender's avatar
Andreas Zehender committed
150
   ac->setEnabled( false );
151
   ac = actionCollection()->addAction( QLatin1String( "P2Shot" ));
152
   ac->setText(i18n("Player 2 Shot"));
Stephan Kulow's avatar
Stephan Kulow committed
153
   ac->setShortcut(Qt::Key_Down);
Andreas Zehender's avatar
Andreas Zehender committed
154
   ac->setEnabled( false );
155
   ac = actionCollection()->addAction( QLatin1String( "P2Mine" ));
156
   ac->setText(i18n("Player 2 Mine"));
Stephan Kulow's avatar
Stephan Kulow committed
157
   ac->setShortcut(Qt::Key_Insert);
Andreas Zehender's avatar
Andreas Zehender committed
158 159
   ac->setEnabled( false );

160
   // actionCollection()->setAutoConnectShortcuts(true);
161
   playfield->setActionCollection(actionCollection());
162 163 164 165
}

void MyTopLevelWidget::initStatusBar( )
{
166 167 168 169 170 171 172 173 174 175
   for (auto &label : m_statusBarLabel)
   {
      label = new QLabel(this);
      label->setAlignment(Qt::AlignCenter);
      statusBar()->addWidget(label, 1);
   }

   m_statusBarLabel[IDS_PAUSE]->setText(i18n(" paused "));
   m_statusBarLabel[IDS_MAIN]->setText(QLatin1String());
   m_statusBarLabel[2]->setText(QLatin1String());
Andreas Zehender's avatar
Andreas Zehender committed
176 177 178 179
}

void MyTopLevelWidget::start()
{
180
   playfield->newGame();
Andreas Zehender's avatar
Andreas Zehender committed
181 182
}

Hans Petter Bieker's avatar
Hans Petter Bieker committed
183
void MyTopLevelWidget::setStatusText(const QString & str,int id)
Andreas Zehender's avatar
Andreas Zehender committed
184
{
185
   m_statusBarLabel[id]->setText(str);
Andreas Zehender's avatar
Andreas Zehender committed
186 187
}

Andreas Zehender's avatar
Andreas Zehender committed
188 189
void MyTopLevelWidget::keySetup()
{
190
   playfield->pause();
191
   KShortcutsDialog::configure( actionCollection( ), KShortcutsEditor::LetterShortcutsAllowed, this, true );
Andreas Zehender's avatar
Andreas Zehender committed
192
}