changeset 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 6bbc5c08c06d
children 72bcf6457227
files MainWindow.cs NewArmyDialog.cs
diffstat 2 files changed, 317 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/MainWindow.cs	Sun Jan 31 20:45:55 2010 +0000
     1.2 +++ b/MainWindow.cs	Tue Feb 02 20:56:39 2010 +0000
     1.3 @@ -4,22 +4,39 @@
     1.4  using System;
     1.5  using System.Collections.Generic;
     1.6  using Qyoto;
     1.7 +using log4net;
     1.8 +using IBBoard.Commands;
     1.9 +using IBBoard.IO;
    1.10 +using IBBoard.WarFoundry.API;
    1.11 +using IBBoard.WarFoundry.API.Factories;
    1.12 +using IBBoard.WarFoundry.API.Objects;
    1.13 +using IBBoard.WarFoundry.API.Savers;
    1.14  
    1.15  namespace IBBoard.WarFoundry.GUI.QtSharp
    1.16  {
    1.17  	public class MainWindow : QMainWindow
    1.18  	{
    1.19 +		private static readonly string AppTitle = "WarFoundry";
    1.20 +		
    1.21  		private Ui_MainWindowLayout layout;
    1.22 +		private readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
    1.23 +		private string loadedFilePath;
    1.24 +		private CommandStack commandStack;
    1.25 +		private QFileDialog saveArmyDialog;
    1.26  		
    1.27  		public MainWindow ()
    1.28  		{
    1.29  			layout = new Ui_MainWindowLayout();
    1.30  			layout.SetupUi(this);
    1.31  			WindowIcon = new QIcon("icons/App.png");
    1.32 +			saveArmyDialog = new QFileDialog(this);
    1.33 +			saveArmyDialog.acceptMode = QFileDialog.AcceptMode.AcceptSave;
    1.34  			SetUpActionIcons();
    1.35  			ConnectMenuActions();
    1.36  			SetUpToolbar();
    1.37  			layout.unitTabs.Clear();
    1.38 +			WarFoundryCore.ArmyChanged+= HandleWarFoundryCoreArmyChanged;
    1.39 +			CommandStack.CommandStackUpdated+= HandleCommandStackCommandStackUpdated;
    1.40  		}
    1.41  		
    1.42  		private void SetUpActionIcons()
    1.43 @@ -39,12 +56,32 @@
    1.44  		private void ConnectMenuActions()
    1.45  		{
    1.46  			QObject.Connect(layout.actionCreateArmy, SIGNAL("triggered()"), CreateNewArmy);
    1.47 +			QObject.Connect(layout.actionUndo, SIGNAL("triggered()"), UndoAction);
    1.48 +			QObject.Connect(layout.actionRedo, SIGNAL("triggered()"), RedoAction);
    1.49  		}
    1.50  		
    1.51  		private void CreateNewArmy()
    1.52  		{
    1.53 -			NewArmyDialog dialog = new NewArmyDialog();
    1.54 -			dialog.Show();
    1.55 +			NewArmyDialog dialog = new NewArmyDialog(this);
    1.56 +			int result = dialog.Exec ();
    1.57 +			
    1.58 +			if (result == (int)QDialog.DialogCode.Accepted)
    1.59 +			{
    1.60 +				try
    1.61 +				{
    1.62 +					CurrentArmy = new Army(dialog.GetSelectedRace(), dialog.GetArmyName(), dialog.GetArmySize());
    1.63 +				}
    1.64 +				catch (RequiredDataMissingException ex)
    1.65 +				{
    1.66 +					log.Error("Required data missing from race file", ex);
    1.67 +					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");
    1.68 +				}
    1.69 +				catch (InvalidFileException ex)
    1.70 +				{
    1.71 +					log.Error("Race file was invalid", ex);
    1.72 +					QMessageBox.Warning(this, ex.Message,  "invalid race file");
    1.73 +				}
    1.74 +			}
    1.75  		}
    1.76  		
    1.77  		private void SetUpToolbar()
    1.78 @@ -58,6 +95,260 @@
    1.79  			layout.toolBar.AddAction(layout.actionUndo);
    1.80  			layout.toolBar.AddAction(layout.actionRedo);
    1.81  			layout.toolBar.AddSeparator();
    1.82 +		}		
    1.83 +
    1.84 +		private void HandleWarFoundryCoreArmyChanged (Army oldValue, Army newValue)
    1.85 +		{
    1.86 +			CommandStack.Reset();
    1.87 +			loadedFilePath = null;
    1.88 +			layout.actionSaveArmy.Enabled = false;
    1.89 +			SetPointsPanelText();
    1.90 +			SetAppTitle();
    1.91 +		}
    1.92 +
    1.93 +		private void SetPointsPanelText ()
    1.94 +		{
    1.95 +			//TODO: implement panel and points panel
    1.96 +		}
    1.97 +
    1.98 +		
    1.99 +		private void SetAppTitle()
   1.100 +		{
   1.101 +			string str = AppTitle;
   1.102 +
   1.103 +			if (CurrentGameSystem!=null)
   1.104 +			{
   1.105 +				str+= " - " + CurrentGameSystem.Name;
   1.106 +			
   1.107 +				if (CurrentArmy!=null)
   1.108 +				{
   1.109 +					str+= " - " + CurrentArmy.Name;
   1.110 +				}
   1.111 +			}
   1.112 +
   1.113 +			this.WindowTitle = str;
   1.114 +		}
   1.115 +		
   1.116 +		public CommandStack CommandStack
   1.117 +		{
   1.118 +			get 
   1.119 +			{
   1.120 +				if (commandStack == null)
   1.121 +				{					
   1.122 +					commandStack = new CommandStack();
   1.123 +				}
   1.124 +
   1.125 +				return commandStack; 
   1.126 +			}
   1.127 +		}
   1.128 +
   1.129 +		private void HandleCommandStackCommandStackUpdated ()
   1.130 +		{
   1.131 +			
   1.132 +		}
   1.133 +
   1.134 +		private void UndoAction()
   1.135 +		{
   1.136 +			if (commandStack.CanUndo())
   1.137 +			{
   1.138 +				commandStack.Undo();
   1.139 +			}
   1.140 +		}
   1.141 +
   1.142 +		private void RedoAction()
   1.143 +		{
   1.144 +			if (commandStack.CanRedo())
   1.145 +			{
   1.146 +				commandStack.Redo();
   1.147 +			}
   1.148 +		}
   1.149 +
   1.150 +		private bool SaveCurrentArmy()
   1.151 +		{
   1.152 +			bool saved = false;
   1.153 +
   1.154 +			string filePath = loadedFilePath;
   1.155 +
   1.156 +			if (filePath == null)
   1.157 +			{
   1.158 +				filePath = PromptForArmyFilePath();
   1.159 +			}
   1.160 +
   1.161 +			if (filePath != null)
   1.162 +			{
   1.163 +				saved = SaveCurrentArmyToFile(filePath);
   1.164 +			}
   1.165 +
   1.166 +			return saved;
   1.167 +		}
   1.168 +
   1.169 +		private bool SaveCurrentArmyAs()
   1.170 +		{
   1.171 +			bool saved = false;
   1.172 +			string filePath = PromptForArmyFilePath();
   1.173 +
   1.174 +			if (filePath != null)
   1.175 +			{
   1.176 +				saved = SaveCurrentArmyToFile(filePath);
   1.177 +			}
   1.178 +			
   1.179 +			return saved;
   1.180 +		}
   1.181 +
   1.182 +		private bool SaveCurrentArmyToFile(string filePath)
   1.183 +		{
   1.184 +			if (WarFoundrySaver.GetSaver().Save(CurrentArmy, filePath))
   1.185 +			{
   1.186 +				loadedFilePath = filePath;
   1.187 +				layout.actionSaveArmy.Enabled = false;
   1.188 +				CommandStack.setCleanMark();
   1.189 +				return true;
   1.190 +			}
   1.191 +			else
   1.192 +			{
   1.193 +				QMessageBox.Critical(this, "file save failed", "file save failed - check log for details");
   1.194 +				return false;
   1.195 +			}
   1.196 +		}
   1.197 +
   1.198 +		private string PromptForArmyFilePath()
   1.199 +		{
   1.200 +			int result = saveArmyDialog.Exec();
   1.201 +
   1.202 +			if (result == (int)QDialog.DialogCode.Accepted)
   1.203 +			{
   1.204 +				return saveArmyDialog.SelectedFiles()[0];
   1.205 +			}
   1.206 +			else
   1.207 +			{
   1.208 +				return null;
   1.209 +			}
   1.210 +		}		
   1.211 +
   1.212 +		public GameSystem CurrentGameSystem
   1.213 +		{
   1.214 +			get { return WarFoundryCore.CurrentGameSystem; }
   1.215 +			set { WarFoundryCore.CurrentGameSystem = value; }
   1.216 +		}
   1.217 +
   1.218 +		public Army CurrentArmy
   1.219 +		{
   1.220 +			get { return WarFoundryCore.CurrentArmy; }
   1.221 +			set { SetArmy(value); }
   1.222 +		}
   1.223 +
   1.224 +		private void SetArmy(Army newArmy)
   1.225 +		{
   1.226 +			IgnoreArmy(CurrentArmy);
   1.227 +			CloseAllUnitWindows();
   1.228 +
   1.229 +			if (newArmy == null)
   1.230 +			{
   1.231 +				SetNullArmyState();
   1.232 +			}
   1.233 +			else
   1.234 +			{
   1.235 +				WarFoundryCore.CurrentGameSystem = newArmy.GameSystem;
   1.236 +				ListenToArmy(newArmy);
   1.237 +				SetNonNullArmyState(newArmy);
   1.238 +			}
   1.239 +			
   1.240 +			WarFoundryCore.CurrentArmy = newArmy;
   1.241 +		}
   1.242 +
   1.243 +		private void IgnoreArmy(Army oldArmy)
   1.244 +		{
   1.245 +			if (oldArmy != null)
   1.246 +			{
   1.247 +				oldArmy.UnitAdded -= UnitAddedMethod;
   1.248 +				oldArmy.UnitRemoved -= UnitRemovedMethod;
   1.249 +				oldArmy.PointsValueChanged -= PointsValueChangedMethod;
   1.250 +			}
   1.251 +		}
   1.252 +		private void UnitAddedMethod(object unitObj)
   1.253 +		{
   1.254 +			if (unitObj is Unit)
   1.255 +			{
   1.256 +				Unit unit = (Unit)unitObj;
   1.257 +				//TODO set error panel
   1.258 +				//sbErrorPanel.Text = "";
   1.259 +			}
   1.260 +		}
   1.261 +
   1.262 +		private void UnitRemovedMethod(object unitObj)
   1.263 +		{
   1.264 +			if (unitObj is Unit)
   1.265 +			{
   1.266 +				Unit unit = (Unit)unitObj;
   1.267 +				//TODO set error panel
   1.268 +				//sbErrorPanel.Text = "";
   1.269 +
   1.270 +				//TODO check if window is open, and close it if it is
   1.271 +				
   1.272 +			}
   1.273 +		}
   1.274 +		
   1.275 +		private void PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal)
   1.276 +		{
   1.277 +			if (obj is Army)
   1.278 +			{
   1.279 +				SetPointsPanelText();
   1.280 +			}
   1.281 +		}
   1.282 +
   1.283 +		private void CloseAllUnitWindows()
   1.284 +		{
   1.285 +//			FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows);
   1.286 +//
   1.287 +//			foreach (FrmUnit window in unitForms)
   1.288 +//			{
   1.289 +//				window.Close();
   1.290 +//			}
   1.291 +//
   1.292 +//			unitWindows.Clear();
   1.293 +		}
   1.294 +
   1.295 +		private void ListenToArmy(Army newArmy)
   1.296 +		{
   1.297 +			if (newArmy != null)
   1.298 +			{
   1.299 +				newArmy.UnitAdded += UnitAddedMethod;
   1.300 +				newArmy.UnitRemoved += UnitRemovedMethod;
   1.301 +				newArmy.PointsValueChanged += PointsValueChangedMethod;
   1.302 +			}
   1.303 +		}
   1.304 +
   1.305 +		private void SetNullArmyState()
   1.306 +		{
   1.307 +			layout.actionSaveArmyAs.Enabled = false;
   1.308 +			layout.actionCloseArmy.Enabled = false;
   1.309 +			layout.menuExportArmyAs.Enabled = false;
   1.310 +			DisableCategoryButtons();
   1.311 +		}
   1.312 +
   1.313 +		void DisableCategoryButtons ()
   1.314 +		{
   1.315 +			//TODO handle category buttons
   1.316 +		}
   1.317 +
   1.318 +
   1.319 +		private void SetNonNullArmyState(Army newArmy)
   1.320 +		{
   1.321 +			SetCategoryButtons(newArmy.Race.Categories);
   1.322 +			EnableCategoryButtons();
   1.323 +			layout.actionSaveArmyAs.Enabled = true;
   1.324 +			layout.actionCloseArmy.Enabled = true;
   1.325 +			layout.menuExportArmyAs.Enabled = true;
   1.326 +		}
   1.327 +
   1.328 +		void SetCategoryButtons (Category[] categories)
   1.329 +		{
   1.330 +			//TODO create category buttons
   1.331 +		}
   1.332 +		
   1.333 +		void EnableCategoryButtons ()
   1.334 +		{
   1.335 +			//TODO enable category buttons
   1.336  		}
   1.337  	}
   1.338  }
   1.339 \ No newline at end of file
     2.1 --- a/NewArmyDialog.cs	Sun Jan 31 20:45:55 2010 +0000
     2.2 +++ b/NewArmyDialog.cs	Tue Feb 02 20:56:39 2010 +0000
     2.3 @@ -19,7 +19,7 @@
     2.4  		private GameSystem[] gameSystems;
     2.5  		private Race[] races;
     2.6  		
     2.7 -		public NewArmyDialog ()
     2.8 +		public NewArmyDialog(QWidget parent) : base(parent)
     2.9  		{
    2.10  			layout = new Ui_CreateNewArmyLayout();
    2.11  			layout.SetupUi(this);
    2.12 @@ -85,6 +85,29 @@
    2.13  			return system;
    2.14  		}
    2.15  		
    2.16 +		public Race GetSelectedRace()
    2.17 +		{
    2.18 +			Race race = null;			
    2.19 +			int selectedIndex = layout.raceList.CurrentRow;
    2.20 +			
    2.21 +			if (selectedIndex != -1)
    2.22 +			{
    2.23 +				race = races[selectedIndex];
    2.24 +			}
    2.25 +			
    2.26 +			return race;
    2.27 +		}
    2.28 +		
    2.29 +		public string GetArmyName()
    2.30 +		{
    2.31 +			return layout.armyName.Text;
    2.32 +		}
    2.33 +		
    2.34 +		public int GetArmySize()
    2.35 +		{
    2.36 +			return layout.armySize.Value;
    2.37 +		}
    2.38 +		
    2.39  		private void ArmyNameChanged()
    2.40  		{
    2.41  			ValidateForm();