File indexing completed on 2024-04-14 04:01:59

0001 /*
0002     SPDX-FileCopyrightText: 1998-2001 Andreas Zehender <az@azweb.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "topwidget.h"
0008 
0009 #include <QBoxLayout>
0010 #include <QIcon>
0011 #include <QStatusBar>
0012 
0013 #include <KActionCollection>
0014 #include <KLocalizedString>
0015 #include <KShortcutsDialog>
0016 #include <KStandardAction>
0017 #include <KGameStandardAction>
0018 
0019 #include "mainview.h"
0020 #include "playerinfo.h"
0021 
0022 
0023 MyTopLevelWidget::MyTopLevelWidget()
0024 {
0025    initGameWidgets();
0026    initStatusBar( );
0027    setupActions( );
0028    setupGUI( );
0029 }
0030 
0031 void MyTopLevelWidget::initGameWidgets( ){
0032    QWidget *w = new QWidget(this);
0033 
0034    PlayerInfo::loadPixmaps();
0035    playerinfo[0]=new PlayerInfo(0,w);
0036    playerinfo[1]=new PlayerInfo(1,w);
0037    playfield=new MyMainView(w);
0038 
0039    QBoxLayout *toplayout=new QHBoxLayout(w);
0040    toplayout->setContentsMargins(0, 0, 0, 0);
0041    toplayout->addWidget(playerinfo[0]);
0042    toplayout->addWidget(playfield);
0043    toplayout->addWidget(playerinfo[1]);
0044    toplayout->activate();
0045 
0046    playfield->setFocusPolicy(Qt::StrongFocus);
0047    playfield->setFocus();
0048 
0049    QObject::connect(playfield, &MyMainView::energy, this, &MyTopLevelWidget::energy);
0050    QObject::connect(playfield, &MyMainView::hitPoints, this, &MyTopLevelWidget::hitPoints);
0051    QObject::connect(playfield, &MyMainView::wins, this, &MyTopLevelWidget::wins);
0052    QObject::connect(playfield, &MyMainView::setStatusText, this, &MyTopLevelWidget::setStatusText);
0053 
0054    setCentralWidget(w);
0055 }
0056 
0057 void MyTopLevelWidget::energy(int pn,int en)
0058 {
0059    playerinfo[pn]->setEnergy(en);
0060 }
0061 
0062 void MyTopLevelWidget::hitPoints(int pn,int hp)
0063 {
0064    playerinfo[pn]->setHitpoints(hp);
0065 }
0066 
0067 void MyTopLevelWidget::wins(int pn,int w)
0068 {
0069    playerinfo[pn]->setWins(w);
0070 }
0071 
0072 void MyTopLevelWidget::setupActions()
0073 {
0074    QAction * ac;
0075 
0076    // Game
0077    KGameStandardAction::gameNew(playfield, &MyMainView::newGame, actionCollection());
0078    KGameStandardAction::quit(this, &MyTopLevelWidget::close, actionCollection());
0079 
0080    QAction * newRoundAct = actionCollection()->addAction( QStringLiteral(  "new_round" ) );
0081    newRoundAct->setIcon( QIcon::fromTheme( QStringLiteral( "preferences-desktop-notification-bell" )) );
0082    newRoundAct->setText( i18n( "&New Round" ) );
0083    KActionCollection::setDefaultShortcut(newRoundAct, Qt::CTRL | Qt::Key_R);
0084    connect( newRoundAct, &QAction::triggered, playfield, &MyMainView::newRound );
0085 
0086    MyMainView::pauseAction =
0087        KGameStandardAction::pause(playfield, &MyMainView::togglePause, actionCollection());
0088    MyMainView::pauseAction->setChecked( false );
0089    QAction *gameStart = actionCollection()->addAction( QStringLiteral(  "game_start" ) );
0090    gameStart->setText( i18nc( "start game","Start" ) );
0091    connect(gameStart, &QAction::triggered, playfield, &MyMainView::start);
0092    KActionCollection::setDefaultShortcut(gameStart, Qt::Key_Space);
0093    playfield->addAction(gameStart);
0094 
0095    KStandardAction::preferences(playfield, &MyMainView::gameSetup, actionCollection());
0096 
0097    // Default keys
0098    ac = actionCollection()->addAction( QStringLiteral( "P1KeyLeft" ));
0099    ac->setText(i18n("Player 1 Rotate Left"));
0100    KActionCollection::setDefaultShortcut(ac, Qt::Key_S);
0101    ac->setEnabled( false );
0102    ac = actionCollection()->addAction( QStringLiteral( "P1KeyRight" ));
0103    ac->setText(i18n("Player 1 Rotate Right"));
0104    KActionCollection::setDefaultShortcut(ac, Qt::Key_F);
0105    ac->setEnabled( false );
0106    ac = actionCollection()->addAction( QStringLiteral( "P1KeyAcc" ));
0107    ac->setText(i18n("Player 1 Accelerate"));
0108    KActionCollection::setDefaultShortcut(ac, Qt::Key_E);
0109    ac->setEnabled( false );
0110    ac = actionCollection()->addAction( QStringLiteral( "P1Shot" ));
0111    ac->setText(i18n("Player 1 Shot"));
0112    KActionCollection::setDefaultShortcut(ac, Qt::Key_D);
0113    ac->setEnabled( false );
0114    ac = actionCollection()->addAction( QStringLiteral( "P1Mine" ));
0115    ac->setText(i18n("Player 1 Mine"));
0116    KActionCollection::setDefaultShortcut(ac, Qt::Key_A);
0117    ac->setEnabled( false );
0118 
0119    ac = actionCollection()->addAction( QStringLiteral( "P2KeyLeft" ));
0120    ac->setText(i18n("Player 2 Rotate Left"));
0121    KActionCollection::setDefaultShortcut(ac, Qt::Key_Left);
0122    ac->setEnabled( false );
0123    ac = actionCollection()->addAction( QStringLiteral( "P2KeyRight" ));
0124    ac->setText(i18n("Player 2 Rotate Right"));
0125    KActionCollection::setDefaultShortcut(ac, Qt::Key_Right);
0126    ac->setEnabled( false );
0127    ac = actionCollection()->addAction( QStringLiteral( "P2KeyAcc" ));
0128    ac->setText(i18n("Player 2 Accelerate"));
0129    KActionCollection::setDefaultShortcut(ac, Qt::Key_Up);
0130    ac->setEnabled( false );
0131    ac = actionCollection()->addAction( QStringLiteral( "P2Shot" ));
0132    ac->setText(i18n("Player 2 Shot"));
0133    KActionCollection::setDefaultShortcut(ac, Qt::Key_Down);
0134    ac->setEnabled( false );
0135    ac = actionCollection()->addAction( QStringLiteral( "P2Mine" ));
0136    ac->setText(i18n("Player 2 Mine"));
0137    KActionCollection::setDefaultShortcut(ac, Qt::Key_Insert);
0138    ac->setEnabled( false );
0139 
0140    // actionCollection()->setAutoConnectShortcuts(true);
0141    playfield->setActionCollection(actionCollection());
0142 }
0143 
0144 void MyTopLevelWidget::initStatusBar( )
0145 {
0146    for (auto &label : m_statusBarLabel)
0147    {
0148       label = new QLabel(this);
0149       label->setAlignment(Qt::AlignCenter);
0150       statusBar()->addWidget(label, 1);
0151    }
0152 
0153    m_statusBarLabel[IDS_PAUSE]->setText(i18n(" paused "));
0154    m_statusBarLabel[IDS_MAIN]->setText(QLatin1String());
0155    m_statusBarLabel[2]->setText(QLatin1String());
0156 }
0157 
0158 void MyTopLevelWidget::start()
0159 {
0160    playfield->newGame();
0161 }
0162 
0163 void MyTopLevelWidget::setStatusText(const QString & str,int id)
0164 {
0165    m_statusBarLabel[id]->setText(str);
0166 }
0167 
0168 void MyTopLevelWidget::keySetup()
0169 {
0170    playfield->pause();
0171 
0172    KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this);
0173 }
0174 
0175 #include "moc_topwidget.cpp"