comparison MainWindow.cs @ 10:3d0c9cf1b924

Fixes #243: Create "New Army" dialog in Qt# app * Make dialog not show in task bar (add parent to constructor) * Add methods to retrieve values Re #242: Create Qt# UI for WarFoundry * Copy lots of implementation from the WinForms app to get some core army loading working (looks like we need to investigate refactoring commonality)
author IBBoard <dev@ibboard.co.uk>
date Tue, 02 Feb 2010 20:56:39 +0000
parents bbf40d66dfe4
children 72bcf6457227
comparison
equal deleted inserted replaced
9:6bbc5c08c06d 10:3d0c9cf1b924
2 // 2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. 3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
4 using System; 4 using System;
5 using System.Collections.Generic; 5 using System.Collections.Generic;
6 using Qyoto; 6 using Qyoto;
7 using log4net;
8 using IBBoard.Commands;
9 using IBBoard.IO;
10 using IBBoard.WarFoundry.API;
11 using IBBoard.WarFoundry.API.Factories;
12 using IBBoard.WarFoundry.API.Objects;
13 using IBBoard.WarFoundry.API.Savers;
7 14
8 namespace IBBoard.WarFoundry.GUI.QtSharp 15 namespace IBBoard.WarFoundry.GUI.QtSharp
9 { 16 {
10 public class MainWindow : QMainWindow 17 public class MainWindow : QMainWindow
11 { 18 {
19 private static readonly string AppTitle = "WarFoundry";
20
12 private Ui_MainWindowLayout layout; 21 private Ui_MainWindowLayout layout;
22 private readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
23 private string loadedFilePath;
24 private CommandStack commandStack;
25 private QFileDialog saveArmyDialog;
13 26
14 public MainWindow () 27 public MainWindow ()
15 { 28 {
16 layout = new Ui_MainWindowLayout(); 29 layout = new Ui_MainWindowLayout();
17 layout.SetupUi(this); 30 layout.SetupUi(this);
18 WindowIcon = new QIcon("icons/App.png"); 31 WindowIcon = new QIcon("icons/App.png");
32 saveArmyDialog = new QFileDialog(this);
33 saveArmyDialog.acceptMode = QFileDialog.AcceptMode.AcceptSave;
19 SetUpActionIcons(); 34 SetUpActionIcons();
20 ConnectMenuActions(); 35 ConnectMenuActions();
21 SetUpToolbar(); 36 SetUpToolbar();
22 layout.unitTabs.Clear(); 37 layout.unitTabs.Clear();
38 WarFoundryCore.ArmyChanged+= HandleWarFoundryCoreArmyChanged;
39 CommandStack.CommandStackUpdated+= HandleCommandStackCommandStackUpdated;
23 } 40 }
24 41
25 private void SetUpActionIcons() 42 private void SetUpActionIcons()
26 { 43 {
27 layout.actionCreateArmy.icon = new QIcon("icons/ui/filenew.png"); 44 layout.actionCreateArmy.icon = new QIcon("icons/ui/filenew.png");
37 } 54 }
38 55
39 private void ConnectMenuActions() 56 private void ConnectMenuActions()
40 { 57 {
41 QObject.Connect(layout.actionCreateArmy, SIGNAL("triggered()"), CreateNewArmy); 58 QObject.Connect(layout.actionCreateArmy, SIGNAL("triggered()"), CreateNewArmy);
59 QObject.Connect(layout.actionUndo, SIGNAL("triggered()"), UndoAction);
60 QObject.Connect(layout.actionRedo, SIGNAL("triggered()"), RedoAction);
42 } 61 }
43 62
44 private void CreateNewArmy() 63 private void CreateNewArmy()
45 { 64 {
46 NewArmyDialog dialog = new NewArmyDialog(); 65 NewArmyDialog dialog = new NewArmyDialog(this);
47 dialog.Show(); 66 int result = dialog.Exec ();
67
68 if (result == (int)QDialog.DialogCode.Accepted)
69 {
70 try
71 {
72 CurrentArmy = new Army(dialog.GetSelectedRace(), dialog.GetArmyName(), dialog.GetArmySize());
73 }
74 catch (RequiredDataMissingException ex)
75 {
76 log.Error("Required data missing from race file", ex);
77 QMessageBox.Warning(this, "Invalid race file data", "the race file for the requested data could not be loaded as it did not contain some required data");
78 }
79 catch (InvalidFileException ex)
80 {
81 log.Error("Race file was invalid", ex);
82 QMessageBox.Warning(this, ex.Message, "invalid race file");
83 }
84 }
48 } 85 }
49 86
50 private void SetUpToolbar() 87 private void SetUpToolbar()
51 { 88 {
52 List<QAction> actions = new List<QAction>(){ 89 List<QAction> actions = new List<QAction>(){
56 layout.toolBar.AddActions(actions); 93 layout.toolBar.AddActions(actions);
57 layout.toolBar.AddSeparator(); 94 layout.toolBar.AddSeparator();
58 layout.toolBar.AddAction(layout.actionUndo); 95 layout.toolBar.AddAction(layout.actionUndo);
59 layout.toolBar.AddAction(layout.actionRedo); 96 layout.toolBar.AddAction(layout.actionRedo);
60 layout.toolBar.AddSeparator(); 97 layout.toolBar.AddSeparator();
98 }
99
100 private void HandleWarFoundryCoreArmyChanged (Army oldValue, Army newValue)
101 {
102 CommandStack.Reset();
103 loadedFilePath = null;
104 layout.actionSaveArmy.Enabled = false;
105 SetPointsPanelText();
106 SetAppTitle();
107 }
108
109 private void SetPointsPanelText ()
110 {
111 //TODO: implement panel and points panel
112 }
113
114
115 private void SetAppTitle()
116 {
117 string str = AppTitle;
118
119 if (CurrentGameSystem!=null)
120 {
121 str+= " - " + CurrentGameSystem.Name;
122
123 if (CurrentArmy!=null)
124 {
125 str+= " - " + CurrentArmy.Name;
126 }
127 }
128
129 this.WindowTitle = str;
130 }
131
132 public CommandStack CommandStack
133 {
134 get
135 {
136 if (commandStack == null)
137 {
138 commandStack = new CommandStack();
139 }
140
141 return commandStack;
142 }
143 }
144
145 private void HandleCommandStackCommandStackUpdated ()
146 {
147
148 }
149
150 private void UndoAction()
151 {
152 if (commandStack.CanUndo())
153 {
154 commandStack.Undo();
155 }
156 }
157
158 private void RedoAction()
159 {
160 if (commandStack.CanRedo())
161 {
162 commandStack.Redo();
163 }
164 }
165
166 private bool SaveCurrentArmy()
167 {
168 bool saved = false;
169
170 string filePath = loadedFilePath;
171
172 if (filePath == null)
173 {
174 filePath = PromptForArmyFilePath();
175 }
176
177 if (filePath != null)
178 {
179 saved = SaveCurrentArmyToFile(filePath);
180 }
181
182 return saved;
183 }
184
185 private bool SaveCurrentArmyAs()
186 {
187 bool saved = false;
188 string filePath = PromptForArmyFilePath();
189
190 if (filePath != null)
191 {
192 saved = SaveCurrentArmyToFile(filePath);
193 }
194
195 return saved;
196 }
197
198 private bool SaveCurrentArmyToFile(string filePath)
199 {
200 if (WarFoundrySaver.GetSaver().Save(CurrentArmy, filePath))
201 {
202 loadedFilePath = filePath;
203 layout.actionSaveArmy.Enabled = false;
204 CommandStack.setCleanMark();
205 return true;
206 }
207 else
208 {
209 QMessageBox.Critical(this, "file save failed", "file save failed - check log for details");
210 return false;
211 }
212 }
213
214 private string PromptForArmyFilePath()
215 {
216 int result = saveArmyDialog.Exec();
217
218 if (result == (int)QDialog.DialogCode.Accepted)
219 {
220 return saveArmyDialog.SelectedFiles()[0];
221 }
222 else
223 {
224 return null;
225 }
226 }
227
228 public GameSystem CurrentGameSystem
229 {
230 get { return WarFoundryCore.CurrentGameSystem; }
231 set { WarFoundryCore.CurrentGameSystem = value; }
232 }
233
234 public Army CurrentArmy
235 {
236 get { return WarFoundryCore.CurrentArmy; }
237 set { SetArmy(value); }
238 }
239
240 private void SetArmy(Army newArmy)
241 {
242 IgnoreArmy(CurrentArmy);
243 CloseAllUnitWindows();
244
245 if (newArmy == null)
246 {
247 SetNullArmyState();
248 }
249 else
250 {
251 WarFoundryCore.CurrentGameSystem = newArmy.GameSystem;
252 ListenToArmy(newArmy);
253 SetNonNullArmyState(newArmy);
254 }
255
256 WarFoundryCore.CurrentArmy = newArmy;
257 }
258
259 private void IgnoreArmy(Army oldArmy)
260 {
261 if (oldArmy != null)
262 {
263 oldArmy.UnitAdded -= UnitAddedMethod;
264 oldArmy.UnitRemoved -= UnitRemovedMethod;
265 oldArmy.PointsValueChanged -= PointsValueChangedMethod;
266 }
267 }
268 private void UnitAddedMethod(object unitObj)
269 {
270 if (unitObj is Unit)
271 {
272 Unit unit = (Unit)unitObj;
273 //TODO set error panel
274 //sbErrorPanel.Text = "";
275 }
276 }
277
278 private void UnitRemovedMethod(object unitObj)
279 {
280 if (unitObj is Unit)
281 {
282 Unit unit = (Unit)unitObj;
283 //TODO set error panel
284 //sbErrorPanel.Text = "";
285
286 //TODO check if window is open, and close it if it is
287
288 }
289 }
290
291 private void PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal)
292 {
293 if (obj is Army)
294 {
295 SetPointsPanelText();
296 }
297 }
298
299 private void CloseAllUnitWindows()
300 {
301 // FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
302 //
303 // foreach (FrmUnit window in unitForms)
304 // {
305 // window.Close();
306 // }
307 //
308 // unitWindows.Clear();
309 }
310
311 private void ListenToArmy(Army newArmy)
312 {
313 if (newArmy != null)
314 {
315 newArmy.UnitAdded += UnitAddedMethod;
316 newArmy.UnitRemoved += UnitRemovedMethod;
317 newArmy.PointsValueChanged += PointsValueChangedMethod;
318 }
319 }
320
321 private void SetNullArmyState()
322 {
323 layout.actionSaveArmyAs.Enabled = false;
324 layout.actionCloseArmy.Enabled = false;
325 layout.menuExportArmyAs.Enabled = false;
326 DisableCategoryButtons();
327 }
328
329 void DisableCategoryButtons ()
330 {
331 //TODO handle category buttons
332 }
333
334
335 private void SetNonNullArmyState(Army newArmy)
336 {
337 SetCategoryButtons(newArmy.Race.Categories);
338 EnableCategoryButtons();
339 layout.actionSaveArmyAs.Enabled = true;
340 layout.actionCloseArmy.Enabled = true;
341 layout.menuExportArmyAs.Enabled = true;
342 }
343
344 void SetCategoryButtons (Category[] categories)
345 {
346 //TODO create category buttons
347 }
348
349 void EnableCategoryButtons ()
350 {
351 //TODO enable category buttons
61 } 352 }
62 } 353 }
63 } 354 }