1 /*
2 SPDX-FileCopyrightText: 1998-2001 Andreas Zehender <az@azweb.de>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "topwidget.h"
8
9 #include <QBoxLayout>
10 #include <QIcon>
11 #include <QStatusBar>
12
13 #include <KActionCollection>
14 #include <KLocalizedString>
15 #include <KShortcutsDialog>
16 #include <KStandardAction>
17 #include <KStandardGameAction>
18 #include <KToggleAction>
19
20 #include "mainview.h"
21 #include "playerinfo.h"
22
23
MyTopLevelWidget()24 MyTopLevelWidget::MyTopLevelWidget()
25 {
26 initGameWidgets();
27 initStatusBar( );
28 setupActions( );
29 setupGUI( );
30 }
31
initGameWidgets()32 void MyTopLevelWidget::initGameWidgets( ){
33 QWidget *w = new QWidget(this);
34
35 PlayerInfo::loadPixmaps();
36 playerinfo[0]=new PlayerInfo(0,w);
37 playerinfo[1]=new PlayerInfo(1,w);
38 playfield=new MyMainView(w);
39
40 QBoxLayout *toplayout=new QHBoxLayout(w);
41 toplayout->setContentsMargins(0, 0, 0, 0);
42 toplayout->addWidget(playerinfo[0]);
43 toplayout->addWidget(playfield);
44 toplayout->addWidget(playerinfo[1]);
45 toplayout->activate();
46
47 playfield->setFocusPolicy(Qt::StrongFocus);
48 playfield->setFocus();
49
50 QObject::connect(playfield, &MyMainView::energy, this, &MyTopLevelWidget::energy);
51 QObject::connect(playfield, &MyMainView::hitPoints, this, &MyTopLevelWidget::hitPoints);
52 QObject::connect(playfield, &MyMainView::wins, this, &MyTopLevelWidget::wins);
53 QObject::connect(playfield, &MyMainView::setStatusText, this, &MyTopLevelWidget::setStatusText);
54
55 setCentralWidget(w);
56 }
57
energy(int pn,int en)58 void MyTopLevelWidget::energy(int pn,int en)
59 {
60 playerinfo[pn]->setEnergy(en);
61 }
62
hitPoints(int pn,int hp)63 void MyTopLevelWidget::hitPoints(int pn,int hp)
64 {
65 playerinfo[pn]->setHitpoints(hp);
66 }
67
wins(int pn,int w)68 void MyTopLevelWidget::wins(int pn,int w)
69 {
70 playerinfo[pn]->setWins(w);
71 }
72
setupActions()73 void MyTopLevelWidget::setupActions()
74 {
75 QAction * ac;
76
77 // Game
78 KStandardGameAction::gameNew(playfield, &MyMainView::newGame, actionCollection());
79 KStandardGameAction::quit(this, &MyTopLevelWidget::close, actionCollection());
80
81 QAction * newRoundAct = actionCollection()->addAction( QStringLiteral( "new_round" ) );
82 newRoundAct->setIcon( QIcon::fromTheme( QStringLiteral( "preferences-desktop-notification-bell" )) );
83 newRoundAct->setText( i18n( "&New Round" ) );
84 actionCollection()->setDefaultShortcut(newRoundAct, Qt::CTRL | Qt::Key_R);
85 connect( newRoundAct, &QAction::triggered, playfield, &MyMainView::newRound );
86
87 MyMainView::pauseAction =
88 KStandardGameAction::pause(playfield, &MyMainView::togglePause, actionCollection());
89 MyMainView::pauseAction->setChecked( false );
90 QAction *gameStart = actionCollection()->addAction( QStringLiteral( "game_start" ) );
91 gameStart->setText( i18nc( "start game","Start" ) );
92 connect(gameStart, &QAction::triggered, playfield, &MyMainView::start);
93 actionCollection()->setDefaultShortcut(gameStart, Qt::Key_Space);
94 playfield->addAction(gameStart);
95
96 KStandardAction::preferences(playfield, &MyMainView::gameSetup, actionCollection());
97
98 // Default keys
99 ac = actionCollection()->addAction( QStringLiteral( "P1KeyLeft" ));
100 ac->setText(i18n("Player 1 Rotate Left"));
101 actionCollection()->setDefaultShortcut(ac, Qt::Key_S);
102 ac->setEnabled( false );
103 ac = actionCollection()->addAction( QStringLiteral( "P1KeyRight" ));
104 ac->setText(i18n("Player 1 Rotate Right"));
105 actionCollection()->setDefaultShortcut(ac, Qt::Key_F);
106 ac->setEnabled( false );
107 ac = actionCollection()->addAction( QStringLiteral( "P1KeyAcc" ));
108 ac->setText(i18n("Player 1 Accelerate"));
109 actionCollection()->setDefaultShortcut(ac, Qt::Key_E);
110 ac->setEnabled( false );
111 ac = actionCollection()->addAction( QStringLiteral( "P1Shot" ));
112 ac->setText(i18n("Player 1 Shot"));
113 actionCollection()->setDefaultShortcut(ac, Qt::Key_D);
114 ac->setEnabled( false );
115 ac = actionCollection()->addAction( QStringLiteral( "P1Mine" ));
116 ac->setText(i18n("Player 1 Mine"));
117 actionCollection()->setDefaultShortcut(ac, Qt::Key_A);
118 ac->setEnabled( false );
119
120 ac = actionCollection()->addAction( QStringLiteral( "P2KeyLeft" ));
121 ac->setText(i18n("Player 2 Rotate Left"));
122 actionCollection()->setDefaultShortcut(ac, Qt::Key_Left);
123 ac->setEnabled( false );
124 ac = actionCollection()->addAction( QStringLiteral( "P2KeyRight" ));
125 ac->setText(i18n("Player 2 Rotate Right"));
126 actionCollection()->setDefaultShortcut(ac, Qt::Key_Right);
127 ac->setEnabled( false );
128 ac = actionCollection()->addAction( QStringLiteral( "P2KeyAcc" ));
129 ac->setText(i18n("Player 2 Accelerate"));
130 actionCollection()->setDefaultShortcut(ac, Qt::Key_Up);
131 ac->setEnabled( false );
132 ac = actionCollection()->addAction( QStringLiteral( "P2Shot" ));
133 ac->setText(i18n("Player 2 Shot"));
134 actionCollection()->setDefaultShortcut(ac, Qt::Key_Down);
135 ac->setEnabled( false );
136 ac = actionCollection()->addAction( QStringLiteral( "P2Mine" ));
137 ac->setText(i18n("Player 2 Mine"));
138 actionCollection()->setDefaultShortcut(ac, Qt::Key_Insert);
139 ac->setEnabled( false );
140
141 // actionCollection()->setAutoConnectShortcuts(true);
142 playfield->setActionCollection(actionCollection());
143 }
144
initStatusBar()145 void MyTopLevelWidget::initStatusBar( )
146 {
147 for (auto &label : m_statusBarLabel)
148 {
149 label = new QLabel(this);
150 label->setAlignment(Qt::AlignCenter);
151 statusBar()->addWidget(label, 1);
152 }
153
154 m_statusBarLabel[IDS_PAUSE]->setText(i18n(" paused "));
155 m_statusBarLabel[IDS_MAIN]->setText(QLatin1String());
156 m_statusBarLabel[2]->setText(QLatin1String());
157 }
158
start()159 void MyTopLevelWidget::start()
160 {
161 playfield->newGame();
162 }
163
setStatusText(const QString & str,int id)164 void MyTopLevelWidget::setStatusText(const QString & str,int id)
165 {
166 m_statusBarLabel[id]->setText(str);
167 }
168
keySetup()169 void MyTopLevelWidget::keySetup()
170 {
171 playfield->pause();
172
173 KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this);
174 }
175