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 diff
     1.1 --- a/MainWindow.cs	Sat Aug 07 13:34:05 2010 +0000
     1.2 +++ b/MainWindow.cs	Sat Aug 07 13:39:35 2010 +0000
     1.3 @@ -22,15 +22,16 @@
     1.4  	public class MainWindow : QMainWindow
     1.5  	{
     1.6  		private static readonly string AppTitle = "WarFoundry";
     1.7 +		private readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
     1.8  		
     1.9  		private Ui_MainWindowLayout layout;
    1.10 -		private readonly ILog log = LogManager.GetLogger(typeof(MainWindow));
    1.11  		private string loadedFilePath;
    1.12  		private CommandStack commandStack;
    1.13  		private QFileDialog saveArmyDialog;
    1.14  		private QFileDialog openArmyDialog;
    1.15  		private Preferences preferences;
    1.16  		private QAction[] categoryActions;
    1.17 +		private Dictionary<string, QStandardItem> treeNodes;
    1.18  		
    1.19  		public MainWindow ()
    1.20  		{
    1.21 @@ -38,6 +39,7 @@
    1.22  			layout.SetupUi(this);
    1.23  			WindowIcon = new QIcon("icons/App.png");
    1.24  			Preferences = new Preferences("WarFoundryQt");
    1.25 +			treeNodes = new Dictionary<string, QStandardItem>();
    1.26  			
    1.27  			try
    1.28  			{
    1.29 @@ -375,16 +377,9 @@
    1.30  			if (unitObj is Unit)
    1.31  			{
    1.32  				Unit unit = (Unit)unitObj;
    1.33 -				QStandardItemModel treeModel = (QStandardItemModel)layout.armyTree.Model();
    1.34 -				List<QStandardItem> items = treeModel.FindItems(unit.Category.Name, (uint)MatchFlag.MatchExactly, 0);
    1.35 -				
    1.36 -				if (items.Count == 1)
    1.37 -				{
    1.38 -					QStandardItem item = items[0];
    1.39 -					CreateTreeSubItem(unit, item);
    1.40 -					layout.armyTree.Expand(item.Index());
    1.41 -				}
    1.42 -				//Else something odd happened and we need to handle it
    1.43 +				QStandardItem item = treeNodes[unit.Category.ID];
    1.44 +				CreateTreeSubItem(unit, item);
    1.45 +				layout.armyTree.Expand(item.Index());
    1.46  				
    1.47  				//TODO set error panel
    1.48  				//sbErrorPanel.Text = "";
    1.49 @@ -498,6 +493,7 @@
    1.50  			QVariant wrappedObject = WrapObject(obj);
    1.51  			QStandardItem item = new QStandardItem(obj.Name);
    1.52  			item.SetData(wrappedObject);
    1.53 +			treeNodes[obj.ID] = item;
    1.54  			return item;
    1.55  		}
    1.56  
    1.57 @@ -574,9 +570,9 @@
    1.58  		{
    1.59  			bool closed = false;
    1.60  			
    1.61 -			if (CurrentArmy!=null)
    1.62 +			if (CurrentArmy != null)
    1.63  			{
    1.64 -				log.Debug("Closing "+CurrentArmy.Name);
    1.65 +				log.Debug("Closing " + CurrentArmy.Name);
    1.66  				bool canClose = false;
    1.67  
    1.68  				if (CommandStack.IsDirty())
    1.69 @@ -584,7 +580,8 @@
    1.70  					log.Debug("Unsaved changes");
    1.71  					string saveChanges = Translation.GetTranslation("SaveChangesQuestion", "the army \"{0}\" has been modified\r\nsave changes before closing army?", CurrentArmy.Name);
    1.72  					string saveChangesTitle = Translation.GetTranslation("SaveChangesTitle", "unsaved changes");
    1.73 -					QMessageBox.StandardButton response = QMessageBox.Question(this, saveChangesTitle, saveChanges, (uint) (QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel), QMessageBox.StandardButton.Cancel);
    1.74 +					uint buttons = (uint)(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No | QMessageBox.StandardButton.Cancel);
    1.75 +					QMessageBox.StandardButton response = QMessageBox.Question(this, saveChangesTitle, saveChanges, buttons, QMessageBox.StandardButton.Cancel);
    1.76  					
    1.77  					if (response == QMessageBox.StandardButton.Yes)
    1.78  					{