view NewUnitDialog.cs @ 34:c3c64e139e5f default tip

Re #245: Create unit tab implementation * Create layout for unit tab widget * Start to populate values WARNING: Qyoto/Qt# seems unstable now and segfaults when new units are added!
author IBBoard <dev@ibboard.co.uk>
date Sat, 07 Aug 2010 16:02:53 +0000
parents d586244177ff
children
line wrap: on
line source

//  This file (NewUnitDialog.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 Qyoto;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Objects;

namespace IBBoard.WarFoundry.GUI.QtSharp
{
	public class NewUnitDialog : QDialog
	{
		private Ui_CreateNewUnitLayout layout;
		
		private Army army;
		private UnitType[] units;
		private bool[] allowed;
		
		public NewUnitDialog(QWidget parent, Race race, Category cat, Army army) : base(parent)
		{
			layout = new Ui_CreateNewUnitLayout();
			layout.SetupUi(this);
			
			units = race.GetUnitTypes(cat);
			allowed = new bool[units.Length];
			this.army = army;
			
			for (int i = 0; i < units.Length; i++)
			{
				UnitType unit = units[i];
				allowed[i] = army.CanAddUnitType(unit).Count == 0;
				layout.unitTypeList.AddItem(unit.Name);
			}
			
			QObject.Connect(layout.unitTypeList, SIGNAL("currentRowChanged(int)"), UnitTypeSelectionChanged);
			QObject.Connect(layout.unitTypeList, SIGNAL("itemDoubleClicked(QListWidgetItem*)"), UnitTypeDoubleClicked);
			SetOkayButtonState(false);
		}

		private void UnitTypeSelectionChanged()
		{
			SetOkayButtonState(layout.unitTypeList.CurrentRow != -1);
		}
		
		private void UnitTypeDoubleClicked()
		{
			QPushButton button = OkayButton;
			
			if (button.Enabled)
			{
				button.Click();
			}
		}
		
		public UnitType SelectedUnit
		{
			get { return units[layout.unitTypeList.CurrentRow]; }
		}

		private QPushButton OkayButton
		{
			get
			{
				return layout.buttonBox.Button(QDialogButtonBox.StandardButton.Ok);
			}
		}

		private void SetOkayButtonState(bool boolValue)
		{
			OkayButton.Enabled = boolValue;
		}
	}
}