changeset 32:e405b43a405b

Re #302: Handle units being added to army * Switch to a method that matches WinForms - cache objects in a dictionary rather than searching for them, since we can then work by object instead of relying on unit name Also: * Minor code clean-up
author IBBoard <dev@ibboard.co.uk>
date Sat, 07 Aug 2010 13:39:35 +0000
parents d586244177ff
children be876c03054f
files MainWindow.cs
diffstat 1 files changed, 11 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MainWindow.cs	Sat Aug 07 13:34:05 2010 +0000
+++ b/MainWindow.cs	Sat Aug 07 13:39:35 2010 +0000
@@ -22,15 +22,16 @@
 	public class MainWindow : QMainWindow
 	{
 		private static readonly string AppTitle = "WarFoundry";
+		private readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
 		
 		private Ui_MainWindowLayout layout;
-		private readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
 		private string loadedFilePath;
 		private CommandStack commandStack;
 		private QFileDialog saveArmyDialog;
 		private QFileDialog openArmyDialog;
 		private Preferences preferences;
 		private QAction[] categoryActions;
+		private Dictionary<string, QStandardItem> treeNodes;
 		
 		public MainWindow ()
 		{
@@ -38,6 +39,7 @@
 			layout.SetupUi(this);
 			WindowIcon = new QIcon("icons/App.png");
 			Preferences = new Preferences("WarFoundryQt");
+			treeNodes = new Dictionary<string, QStandardItem>();
 			
 			try
 			{
@@ -375,16 +377,9 @@
 			if (unitObj is Unit)
 			{
 				Unit unit = (Unit)unitObj;
-				QStandardItemModel treeModel = (QStandardItemModel)layout.armyTree.Model();
-				List<QStandardItem> items = treeModel.FindItems(unit.Category.Name, (uint)MatchFlag.MatchExactly, 0);
-				
-				if (items.Count == 1)
-				{
-					QStandardItem item = items[0];
-					CreateTreeSubItem(unit, item);
-					layout.armyTree.Expand(item.Index());
-				}
-				//Else something odd happened and we need to handle it
+				QStandardItem item = treeNodes[unit.Category.ID];
+				CreateTreeSubItem(unit, item);
+				layout.armyTree.Expand(item.Index());
 				
 				//TODO set error panel
 				//sbErrorPanel.Text = "";
@@ -498,6 +493,7 @@
 			QVariant wrappedObject = WrapObject(obj);
 			QStandardItem item = new QStandardItem(obj.Name);
 			item.SetData(wrappedObject);
+			treeNodes[obj.ID] = item;
 			return item;
 		}
 
@@ -574,9 +570,9 @@
 		{
 			bool closed = false;
 			
-			if (CurrentArmy!=null)
+			if (CurrentArmy != null)
 			{
-				log.Debug("Closing "+CurrentArmy.Name);
+				log.Debug("Closing " + CurrentArmy.Name);
 				bool canClose = false;
 
 				if (CommandStack.IsDirty())
@@ -584,7 +580,8 @@
 					log.Debug("Unsaved changes");
 					string saveChanges = Translation.GetTranslation("SaveChangesQuestion", "the army \"{0}\" has been modified\r\nsave changes before closing army?", CurrentArmy.Name);
 					string saveChangesTitle = Translation.GetTranslation("SaveChangesTitle", "unsaved changes");
-					QMessageBox.StandardButton response = QMessageBox.Question(this, saveChangesTitle, saveChanges, (uint) (QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel), QMessageBox.StandardButton.Cancel);
+					uint buttons = (uint)(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel);
+					QMessageBox.StandardButton response = QMessageBox.Question(this, saveChangesTitle, saveChanges, buttons, QMessageBox.StandardButton.Cancel);
 					
 					if (response == QMessageBox.StandardButton.Yes)
 					{