1 | /* |
2 | Copyright 2003 Russell Steffen <rsteffen@bayarea.net> |
3 | Copyright 2003 Stephan Zehetner <s.zehetner@nevox.org> |
4 | Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com> |
5 | Copyright 2006 Inge Wallin <inge@lysator.liu.se> |
6 | Copyright 2006 Pierre Ducroquet <pinaraf@gmail.com> |
7 | |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "mainwin.h" |
24 | |
25 | #include <QDockWidget> |
26 | #include <QPushButton> |
27 | #include <QLabel> |
28 | #include <QDebug> |
29 | |
30 | #include <klocale.h> |
31 | #include <kglobal.h> |
32 | #include <kmenubar.h> |
33 | #include <ktoolbar.h> |
34 | #include <kiconloader.h> |
35 | #include <kaction.h> |
36 | #include <kactioncollection.h> |
37 | #include <kstandardaction.h> |
38 | #include <kstandardgameaction.h> |
39 | #include <kicon.h> |
40 | #include <kstatusbar.h> |
41 | |
42 | #include "game.h" |
43 | #include "localgame.h" |
44 | #include "gameview.h" |
45 | |
46 | |
47 | // KonquestMainWindow |
48 | MainWindow::MainWindow() |
49 | { |
50 | setCaption( i18n("Galactic Conquest" ) ); |
51 | |
52 | setupActions(); |
53 | setupGameView(); |
54 | setupGUI(); |
55 | |
56 | // The status bar. |
57 | m_statusBarText = new QLabel(i18n("Galactic Conquest" )); |
58 | statusBar()->addWidget(m_statusBarText); |
59 | } |
60 | |
61 | |
62 | MainWindow::~MainWindow() |
63 | { |
64 | } |
65 | |
66 | |
67 | QSize |
68 | MainWindow::sizeHint() const |
69 | { |
70 | return KXmlGuiWindow::sizeHint().expandedTo(QSize(600, 650)); |
71 | } |
72 | |
73 | |
74 | void |
75 | MainWindow::setupActions() |
76 | { |
77 | KStandardGameAction::gameNew( this, SLOT( startNewGame() ), actionCollection() ); |
78 | KStandardGameAction::quit( this, SLOT( close() ), actionCollection() ); |
79 | |
80 | m_endTurnAction = KStandardGameAction::endTurn(this, NULL, actionCollection()); |
81 | m_endTurnAction->setShortcut(Qt::CTRL + Qt::Key_E); |
82 | m_endTurnAction->setEnabled(false); |
83 | |
84 | m_endGameAction = KStandardGameAction::end( this, NULL, actionCollection() ); |
85 | m_endGameAction->setEnabled(false); |
86 | |
87 | //AB: there is no icon for disabled - KToolBar::insertButton shows the |
88 | //different state - KAction not :-( |
89 | m_measureAction = actionCollection()->addAction( QLatin1String( "game_measure" ) ); |
90 | m_measureAction->setIcon( KIcon( QLatin1String( "go-jump" )) ); |
91 | m_measureAction->setText( i18n("&Measure Distance" ) ); |
92 | m_measureAction->setEnabled(false); |
93 | |
94 | // Show fleet overview |
95 | m_fleetAction = actionCollection()->addAction( QLatin1String( "game_fleets" ) ); |
96 | m_fleetAction->setIcon( KIcon( QLatin1String( "fork" )) ); |
97 | m_fleetAction->setText( i18n("&Fleet Overview" ) ); |
98 | m_fleetAction->setEnabled(false); |
99 | |
100 | // Toolbar |
101 | addToolBar ( Qt::LeftToolBarArea, toolBar() ); |
102 | toolBar()->setMovable(false); |
103 | |
104 | // docking area - messages |
105 | |
106 | m_messagesDock = new QDockWidget(i18n("Messages" ), this); |
107 | m_messagesDock->setObjectName("dock-messages" ); |
108 | |
109 | addDockWidget(Qt::BottomDockWidgetArea, m_messagesDock); |
110 | |
111 | m_messagesAction = actionCollection()->addAction(QLatin1String("view_messages" )); |
112 | m_messagesAction->setText(i18n("Show &Messages" )); |
113 | m_messagesAction->setCheckable(true); |
114 | m_messagesAction->setChecked(m_messagesDock->isVisible()); |
115 | |
116 | // The action signal "toggled" is fired even in case the state is changed |
117 | // via code using setChecked(). "triggered" however is only fired if the |
118 | // user actually triggered the change. |
119 | |
120 | // The dock signal "visibilityChanged" is fired if the dock is shown or |
121 | // hidden. But this includes hidden in a tab as well. The action should not |
122 | // represent the visibility state, but if the dock is present somewhere in |
123 | // the GUI, regardless of currently visible in an active tab or invisible |
124 | // in a not currently active tab. |
125 | |
126 | connect(m_messagesAction, SIGNAL(triggered(bool)), m_messagesDock, SLOT(setVisible(bool))); |
127 | connect(m_messagesDock, SIGNAL(visibilityChanged(bool)), this, SLOT(updateMessagesActionSlot())); |
128 | |
129 | // docking area - standings |
130 | |
131 | m_standingsDock = new QDockWidget(i18n("Standings" ), this); |
132 | m_standingsDock->setObjectName("dock-standings" ); |
133 | |
134 | tabifyDockWidget(m_messagesDock, m_standingsDock); |
135 | |
136 | m_standingsAction = actionCollection()->addAction(QLatin1String("view_standings" )); |
137 | m_standingsAction->setIcon(KIcon(QLatin1String("help-contents" ))); |
138 | m_standingsAction->setText(i18n("Show &Standings" )); |
139 | m_standingsAction->setCheckable(true); |
140 | m_standingsAction->setChecked(m_standingsDock->isVisible()); |
141 | |
142 | connect(m_standingsAction, SIGNAL(triggered(bool)), m_standingsDock, SLOT(setVisible(bool))); |
143 | connect(m_standingsDock, SIGNAL(visibilityChanged(bool)), this, SLOT(updateStandingsActionSlot())); |
144 | } |
145 | |
146 | |
147 | void |
148 | MainWindow::setupGameView() |
149 | { |
150 | m_game = new LocalGame( this ); |
151 | m_gameView = new GameView(this, m_game, m_messagesDock, m_standingsDock); |
152 | setCentralWidget( m_gameView ); |
153 | |
154 | connect ( m_game, SIGNAL( gameMsg(const KLocalizedString &, |
155 | Player *, Planet *, |
156 | Player * ) ), |
157 | m_gameView, SLOT( gameMsg(const KLocalizedString &, |
158 | Player *, Planet *, |
159 | Player * ) ) ); |
160 | connect (m_gameView, SIGNAL( newGUIState( GUIState )), |
161 | this, SLOT( guiStateChange( GUIState ) ) ); |
162 | |
163 | connect(m_measureAction, SIGNAL(triggered(bool)), m_gameView, SLOT( measureDistance() )); |
164 | connect(m_fleetAction, SIGNAL(triggered(bool)), m_gameView, SLOT( showFleets() )); |
165 | connect(m_endTurnAction, SIGNAL(triggered()), m_gameView, SLOT(nextPlayer())); |
166 | connect(m_endGameAction, SIGNAL(triggered()), m_gameView, SLOT(shutdownGame())); |
167 | } |
168 | |
169 | |
170 | void |
171 | MainWindow::setupGUI() |
172 | { |
173 | KXmlGuiWindow::setupGUI(); |
174 | |
175 | /** |
176 | * @todo The docks should not be visible on the main screen, and neither |
177 | * should it be possible to open the docks. During the game and later on, |
178 | * this is handled by GameView::changeGameView() and by |
179 | * MainWindow::guiStateChange(). Just the initial setup does not work that |
180 | * way. - Rework the GUI setup sequence so that the following hack is not |
181 | * required. |
182 | */ |
183 | |
184 | m_messagesAction->setEnabled(false); |
185 | m_standingsAction->setEnabled(false); |
186 | |
187 | m_messagesDock->toggleViewAction()->setEnabled(false); |
188 | m_standingsDock->toggleViewAction()->setEnabled(false); |
189 | |
190 | m_messagesDock->hide(); |
191 | m_standingsDock->hide(); |
192 | } |
193 | |
194 | |
195 | void |
196 | MainWindow::startNewGame() |
197 | { |
198 | if (m_gameView->confirmNewGame()) |
199 | { |
200 | m_gameView->deleteLater(); |
201 | m_game->deleteLater(); |
202 | setupGameView(); |
203 | m_gameView->startNewGame(); |
204 | } |
205 | } |
206 | |
207 | void |
208 | MainWindow::guiStateChange( GUIState newState ) |
209 | { |
210 | if (newState == NONE) { |
211 | m_gameView->deleteLater(); |
212 | m_game->deleteLater(); |
213 | this->setupGameView(); |
214 | } |
215 | |
216 | // An alternative to disabling the "end turn" action during "send fleet |
217 | // command sequence" is to simply abort this sequence if the user decides |
218 | // to "end turn" before completion. |
219 | |
220 | /** |
221 | * @todo The game view handles the state of the actions, so the game view |
222 | * should be able to update the enabled state of the actions as well. This |
223 | * should be implemented via signals, instead of copying the conditions here |
224 | * again. |
225 | */ |
226 | |
227 | m_endTurnAction ->setEnabled( m_game->isRunning() && (newState == SOURCE_PLANET) ); |
228 | m_endGameAction ->setEnabled( m_game->isRunning() ); |
229 | m_measureAction ->setEnabled( newState == SOURCE_PLANET ); |
230 | m_fleetAction ->setEnabled( newState == SOURCE_PLANET ); |
231 | |
232 | m_messagesAction->setEnabled(m_game->isRunning()); |
233 | m_standingsAction->setEnabled(m_game->isRunning()); |
234 | |
235 | m_messagesDock->toggleViewAction()->setEnabled(m_game->isRunning()); |
236 | m_standingsDock->toggleViewAction()->setEnabled(m_game->isRunning()); |
237 | |
238 | m_statusBarText->setText(i18n("Turn # %1" , m_game->turnCounter())); |
239 | } |
240 | |
241 | |
242 | void |
243 | MainWindow::updateMessagesActionSlot() |
244 | { |
245 | m_messagesAction->setChecked(m_messagesDock->toggleViewAction()->isChecked()); |
246 | } |
247 | |
248 | |
249 | void |
250 | MainWindow::updateStandingsActionSlot() |
251 | { |
252 | m_standingsAction->setChecked(m_standingsDock->toggleViewAction()->isChecked()); |
253 | } |
254 | |