view NewArmyDialog.cs @ 27:7eaa8a1715e2

Re #242: Complete initial Qt# UI for WarFoundry * Made New Army dialog behave more like WinForms: * Disable everything until we need it * Make default selections where we can (single items)
author IBBoard <dev@ibboard.co.uk>
date Sat, 31 Jul 2010 20:30:30 +0000
parents 4ec2083eb755
children
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);
			}
			
			if (gameSystems.Length == 1)
			{
				layout.gameSystems.CurrentIndex = 0;
			}
		}
		
		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);
				}
				
				if (races.Length == 1)
				{
					layout.raceList.SetCurrentRow(0, (uint)QItemSelectionModel.SelectionFlag.ClearAndSelect);
				}
				
				layout.raceList.Enabled = true;
			}
			else
			{
				layout.raceList.Enabled = false;
			}
		}
		
		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();
			bool raceSelected = (layout.raceList.CurrentRow != -1);
			layout.armyName.Enabled = raceSelected;
			layout.armySize.Enabled = raceSelected;
		}
		
		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();
		}
	}
}