view NewArmyDialog.cs @ 26:55d4f16c982b

Fixes #250: QtSharp app doesn't always quit properly * Override Close event to dispose of file dialogs (open and save) * Remove unnecessary event handler hooked to close * Change app initialisation to match pattern used by Synapse * Create file dialogs without specifying "this" as parent - seems to be main fix, for some reason.
author IBBoard <dev@ibboard.co.uk>
date Sat, 31 Jul 2010 16:03:14 +0000
parents 4ec2083eb755
children 7eaa8a1715e2
line wrap: on
line source

// This file (NewArmyDialog.cs) is a part of the IBBoard.WarFoundry.GUI.QtSharp project and is copyright 2010 IBBoard
// 
// 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.

using System;
using System.Collections.Generic;
using Qyoto;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.GUI.QtSharp
{
	public class NewArmyDialog : QDialog
	{
		private Ui_CreateNewArmyLayout layout;
		
		//TODO: Replace local cached arrays with some kind of storing as user data on controls, if possible
		//Qt doesn't seem to support it by default, but there should be a better alternative
		private GameSystem[] gameSystems;
		private Race[] races;
		
		public NewArmyDialog(QWidget parent) : base(parent)
		{
			layout = new Ui_CreateNewArmyLayout();
			layout.SetupUi(this);
			
			PopulateControls();
			layout.gameSystems.CurrentIndex = -1;
			QObject.Connect(layout.gameSystems, SIGNAL("currentIndexChanged(int)"), GameSystemSelectionChanged);
			QObject.Connect(layout.raceList, SIGNAL("currentRowChanged(int)"),RaceTypeChanged);
			QObject.Connect(layout.armyName, SIGNAL("textChanged(QString)"), ArmyNameChanged);
			QObject.Connect(layout.armySize, SIGNAL("valueChanged(int)"), ArmySizeChanged);
			SetOkayButtonState(false);
		}
		
		private void SetOkayButtonState (bool boolValue)
		{
			layout.buttonBox.Button(QDialogButtonBox.StandardButton.Ok).Enabled = boolValue;
		}
		
		private void PopulateControls()
		{
			gameSystems = WarFoundryLoader.GetDefault().GetGameSystems();
			
			foreach (GameSystem system in gameSystems)
			{
				layout.gameSystems.AddItem(system.Name);
			}
		}
		
		private void GameSystemSelectionChanged()
		{
			ValidateForm();
			layout.raceList.Clear();
			
			if (layout.gameSystems.CurrentIndex != -1)
			{
				races = WarFoundryLoader.GetDefault().GetRaces(GetSelectedSystem());
				
				foreach (Race race in races)
				{
					layout.raceList.AddItem(race.Name);
				}
			}				
		}
		
		private void ValidateForm()
		{
			bool complete = (layout.gameSystems.CurrentIndex != -1);
			complete &= (layout.raceList.CurrentRow != -1);
			complete &= (layout.armyName.Text != "");
			complete &= (layout.armySize.Value > 0);
			SetOkayButtonState(complete);
		}
		
		private GameSystem GetSelectedSystem()
		{
			GameSystem system = null;			
			int selectedIndex = layout.gameSystems.CurrentIndex;
			
			if (selectedIndex != -1)
			{
				system = gameSystems[selectedIndex];
			}
			
			return system;
		}
		
		private void RaceTypeChanged() 
		{
			ValidateForm(); 
		}
		
		public Race GetSelectedRace()
		{
			Race race = null;			
			int selectedIndex = layout.raceList.CurrentRow;
			
			if (selectedIndex != -1)
			{
				race = races[selectedIndex];
			}
			
			return race;
		}
		
		public string GetArmyName()
		{
			return layout.armyName.Text;
		}
		
		public int GetArmySize()
		{
			return layout.armySize.Value;
		}
		
		private void ArmyNameChanged()
		{
			ValidateForm();
		}
		
		private void ArmySizeChanged()
		{
			ValidateForm();
		}
	}
}