File indexing completed on 2024-03-24 04:06:23

0001 /*
0002     SPDX-FileCopyrightText: 2003 Russell Steffen <rsteffen@bayarea.net>
0003     SPDX-FileCopyrightText: 2003 Stephan Zehetner <s.zehetner@nevox.org>
0004     SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com>
0005     SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se>
0006     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "mainwin.h"
0012 
0013 #include <QAction>
0014 #include <QDockWidget>
0015 #include <QIcon>
0016 #include <QLabel>
0017 #include <QPushButton>
0018 #include <QStatusBar>
0019 
0020 #include <KActionCollection>
0021 #include <KGameStandardAction>
0022 #include <KLocalizedString>
0023 #include <KToolBar>
0024 
0025 #include "game.h"
0026 #include "localgame.h"
0027 #include "gameview.h"
0028 
0029 
0030 // KonquestMainWindow
0031 MainWindow::MainWindow()
0032 {
0033     setWindowTitle(i18nc("@title:window", "Galactic Conquest"));
0034 
0035     setupActions();
0036     setupGameView();
0037     setupGUI();
0038 
0039     // The status bar.
0040     m_statusBarText = new QLabel(i18n("Galactic Conquest"));
0041     statusBar()->addWidget(m_statusBarText);
0042 }
0043 
0044 
0045 MainWindow::~MainWindow()
0046 {
0047 }
0048 
0049 
0050 QSize
0051 MainWindow::sizeHint() const
0052 {
0053     return KXmlGuiWindow::sizeHint().expandedTo(QSize(600, 650));
0054 }
0055 
0056 
0057 void
0058 MainWindow::setupActions()
0059 {
0060     KGameStandardAction::gameNew(this, &MainWindow::startNewGame, actionCollection());
0061     KGameStandardAction::quit(this, &MainWindow::close, actionCollection());
0062 
0063     m_endTurnAction = KGameStandardAction::endTurn(this, nullptr, actionCollection());
0064     KActionCollection::setDefaultShortcut(m_endTurnAction, Qt::CTRL | Qt::Key_E);
0065     m_endTurnAction->setEnabled(false);
0066 
0067     m_endGameAction = KGameStandardAction::end( this, nullptr, actionCollection() );
0068     m_endGameAction->setEnabled(false);
0069 
0070     //AB: there is no icon for disabled - KToolBar::insertButton shows the
0071     //different state - QAction not :-(
0072     m_measureAction = actionCollection()->addAction( QStringLiteral(  "game_measure" ) );
0073     m_measureAction->setIcon( QIcon::fromTheme( QStringLiteral( "go-jump" )) );
0074     m_measureAction->setText( i18n("&Measure Distance...") );
0075     m_measureAction->setEnabled(false);
0076 
0077     // Show fleet overview
0078     m_fleetAction = actionCollection()->addAction( QStringLiteral(  "game_fleets" ) );
0079     m_fleetAction->setIcon( QIcon::fromTheme( QStringLiteral( "fork" )) );
0080     m_fleetAction->setText( i18n("&Fleet Overview...") );
0081     m_fleetAction->setEnabled(false);
0082 
0083     // Toolbar
0084     addToolBar ( Qt::LeftToolBarArea, toolBar() );
0085     toolBar()->setMovable(false);
0086 
0087     // docking area - messages
0088 
0089     m_messagesDock = new QDockWidget(i18n("Messages"), this);
0090     m_messagesDock->setObjectName(QStringLiteral("dock-messages"));
0091 
0092     addDockWidget(Qt::BottomDockWidgetArea, m_messagesDock);
0093 
0094     m_messagesAction = actionCollection()->addAction(QStringLiteral("view_messages"));
0095     m_messagesAction->setText(i18n("Show &Messages"));
0096     m_messagesAction->setCheckable(true);
0097     m_messagesAction->setChecked(m_messagesDock->isVisible());
0098 
0099     // The action signal "toggled" is fired even in case the state is changed
0100     // via code using setChecked(). "triggered" however is only fired if the
0101     // user actually triggered the change.
0102 
0103     // The dock signal "visibilityChanged" is fired if the dock is shown or
0104     // hidden. But this includes hidden in a tab as well. The action should not
0105     // represent the visibility state, but if the dock is present somewhere in
0106     // the GUI, regardless of currently visible in an active tab or invisible
0107     // in a not currently active tab.
0108 
0109     connect(m_messagesAction, &QAction::triggered, m_messagesDock, &QDockWidget::setVisible);
0110     connect(m_messagesDock, &QDockWidget::visibilityChanged, this, &MainWindow::updateMessagesActionSlot);
0111 
0112     // docking area - standings
0113 
0114     m_standingsDock = new QDockWidget(i18n("Standings"), this);
0115     m_standingsDock->setObjectName(QStringLiteral("dock-standings"));
0116 
0117     tabifyDockWidget(m_messagesDock, m_standingsDock);
0118 
0119     m_standingsAction = actionCollection()->addAction(QStringLiteral("view_standings"));
0120     m_standingsAction->setIcon(QIcon::fromTheme(QStringLiteral("help-contents")));
0121     m_standingsAction->setText(i18n("Show &Standings"));
0122     m_standingsAction->setCheckable(true);
0123     m_standingsAction->setChecked(m_standingsDock->isVisible());
0124 
0125     connect(m_standingsAction, &QAction::triggered, m_standingsDock, &QDockWidget::setVisible);
0126     connect(m_standingsDock, &QDockWidget::visibilityChanged, this, &MainWindow::updateStandingsActionSlot);
0127 }
0128 
0129 
0130 void
0131 MainWindow::setupGameView()
0132 {
0133     m_game      = new LocalGame( this );
0134     m_gameView  = new GameView(this, m_game, m_messagesDock, m_standingsDock);
0135     setCentralWidget( m_gameView );
0136 
0137     connect(m_game, &Game::gameMsg, m_gameView, &GameView::gameMsg);
0138     connect(m_gameView, &GameView::newGUIState, this, &MainWindow::guiStateChange);
0139 
0140     connect(m_measureAction, &QAction::triggered, m_gameView, &GameView::measureDistance);
0141     connect(m_fleetAction, &QAction::triggered, m_gameView, &GameView::showFleets);
0142     connect(m_endTurnAction, &QAction::triggered, m_gameView, &GameView::nextPlayer);
0143     connect(m_endGameAction, &QAction::triggered, m_gameView, &GameView::shutdownGame);
0144 }
0145 
0146 
0147 void
0148 MainWindow::setupGUI()
0149 {
0150     KXmlGuiWindow::setupGUI();
0151 
0152     /**
0153      * @todo The docks should not be visible on the main screen, and neither
0154      * should it be possible to open the docks. During the game and later on,
0155      * this is handled by GameView::changeGameView() and by
0156      * MainWindow::guiStateChange(). Just the initial setup does not work that
0157      * way. - Rework the GUI setup sequence so that the following hack is not
0158      * required.
0159      */
0160 
0161     m_messagesAction->setEnabled(false);
0162     m_standingsAction->setEnabled(false);
0163 
0164     m_messagesDock->toggleViewAction()->setEnabled(false);
0165     m_standingsDock->toggleViewAction()->setEnabled(false);
0166 
0167     m_messagesDock->hide();
0168     m_standingsDock->hide();
0169 }
0170 
0171 
0172 void
0173 MainWindow::startNewGame()
0174 {
0175     if (m_gameView->confirmNewGame())
0176     {
0177         m_gameView->deleteLater();
0178         m_game->deleteLater();
0179         setupGameView();
0180         m_gameView->startNewGame();
0181     }
0182 }
0183 
0184 void
0185 MainWindow::guiStateChange( GUIState newState )
0186 {
0187     if (newState == NONE) {
0188         m_gameView->deleteLater();
0189         m_game->deleteLater();
0190         this->setupGameView();
0191     }
0192 
0193     // An alternative to disabling the "end turn" action during "send fleet
0194     // command sequence" is to simply abort this sequence if the user decides
0195     // to "end turn" before completion.
0196 
0197     /**
0198      * @todo The game view handles the state of the actions, so the game view
0199      * should be able to update the enabled state of the actions as well. This
0200      * should be implemented via signals, instead of copying the conditions here
0201      * again.
0202      */
0203 
0204     m_endTurnAction ->setEnabled( m_game->isRunning() && (newState == SOURCE_PLANET) );
0205     m_endGameAction ->setEnabled( m_game->isRunning() );
0206     m_measureAction ->setEnabled( newState == SOURCE_PLANET );
0207     m_fleetAction   ->setEnabled( newState == SOURCE_PLANET );
0208 
0209     m_messagesAction->setEnabled(m_game->isRunning());
0210     m_standingsAction->setEnabled(m_game->isRunning());
0211 
0212     m_messagesDock->toggleViewAction()->setEnabled(m_game->isRunning());
0213     m_standingsDock->toggleViewAction()->setEnabled(m_game->isRunning());
0214 
0215     m_statusBarText->setText(i18n("Turn # %1", m_game->turnCounter()));
0216 }
0217 
0218 
0219 void
0220 MainWindow::updateMessagesActionSlot()
0221 {
0222     m_messagesAction->setChecked(m_messagesDock->toggleViewAction()->isChecked());
0223 }
0224 
0225 
0226 void
0227 MainWindow::updateStandingsActionSlot()
0228 {
0229     m_standingsAction->setChecked(m_standingsDock->toggleViewAction()->isChecked());
0230 }
0231 
0232 #include "moc_mainwin.cpp"