view NewArmyDialog.cs @ 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 8a8735679d55
children 4ec2083eb755
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.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;
		}
		
		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();
		}
	}
}