# HG changeset patch # User IBBoard # Date 1265144199 0 # Node ID 3d0c9cf1b9245e18b8be7f517abf89104b3a6e3b # Parent 6bbc5c08c06d160f9bc97500746575986548b57d 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) diff -r 6bbc5c08c06d -r 3d0c9cf1b924 MainWindow.cs --- a/MainWindow.cs Sun Jan 31 20:45:55 2010 +0000 +++ b/MainWindow.cs Tue Feb 02 20:56:39 2010 +0000 @@ -4,22 +4,39 @@ using System; using System.Collections.Generic; using Qyoto; +using log4net; +using IBBoard.Commands; +using IBBoard.IO; +using IBBoard.WarFoundry.API; +using IBBoard.WarFoundry.API.Factories; +using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Savers; namespace IBBoard.WarFoundry.GUI.QtSharp { public class MainWindow : QMainWindow { + private static readonly string AppTitle = "WarFoundry"; + private Ui_MainWindowLayout layout; + private readonly ILog log = LogManager.GetLogger(typeof(MainWindow)); + private string loadedFilePath; + private CommandStack commandStack; + private QFileDialog saveArmyDialog; public MainWindow () { layout = new Ui_MainWindowLayout(); layout.SetupUi(this); WindowIcon = new QIcon("icons/App.png"); + saveArmyDialog = new QFileDialog(this); + saveArmyDialog.acceptMode = QFileDialog.AcceptMode.AcceptSave; SetUpActionIcons(); ConnectMenuActions(); SetUpToolbar(); layout.unitTabs.Clear(); + WarFoundryCore.ArmyChanged+= HandleWarFoundryCoreArmyChanged; + CommandStack.CommandStackUpdated+= HandleCommandStackCommandStackUpdated; } private void SetUpActionIcons() @@ -39,12 +56,32 @@ private void ConnectMenuActions() { QObject.Connect(layout.actionCreateArmy, SIGNAL("triggered()"), CreateNewArmy); + QObject.Connect(layout.actionUndo, SIGNAL("triggered()"), UndoAction); + QObject.Connect(layout.actionRedo, SIGNAL("triggered()"), RedoAction); } private void CreateNewArmy() { - NewArmyDialog dialog = new NewArmyDialog(); - dialog.Show(); + NewArmyDialog dialog = new NewArmyDialog(this); + int result = dialog.Exec (); + + if (result == (int)QDialog.DialogCode.Accepted) + { + try + { + CurrentArmy = new Army(dialog.GetSelectedRace(), dialog.GetArmyName(), dialog.GetArmySize()); + } + catch (RequiredDataMissingException ex) + { + log.Error("Required data missing from race file", ex); + QMessageBox.Warning(this, "Invalid race file data", "the race file for the requested data could not be loaded as it did not contain some required data"); + } + catch (InvalidFileException ex) + { + log.Error("Race file was invalid", ex); + QMessageBox.Warning(this, ex.Message, "invalid race file"); + } + } } private void SetUpToolbar() @@ -58,6 +95,260 @@ layout.toolBar.AddAction(layout.actionUndo); layout.toolBar.AddAction(layout.actionRedo); layout.toolBar.AddSeparator(); + } + + private void HandleWarFoundryCoreArmyChanged (Army oldValue, Army newValue) + { + CommandStack.Reset(); + loadedFilePath = null; + layout.actionSaveArmy.Enabled = false; + SetPointsPanelText(); + SetAppTitle(); + } + + private void SetPointsPanelText () + { + //TODO: implement panel and points panel + } + + + private void SetAppTitle() + { + string str = AppTitle; + + if (CurrentGameSystem!=null) + { + str+= " - " + CurrentGameSystem.Name; + + if (CurrentArmy!=null) + { + str+= " - " + CurrentArmy.Name; + } + } + + this.WindowTitle = str; + } + + public CommandStack CommandStack + { + get + { + if (commandStack == null) + { + commandStack = new CommandStack(); + } + + return commandStack; + } + } + + private void HandleCommandStackCommandStackUpdated () + { + + } + + private void UndoAction() + { + if (commandStack.CanUndo()) + { + commandStack.Undo(); + } + } + + private void RedoAction() + { + if (commandStack.CanRedo()) + { + commandStack.Redo(); + } + } + + private bool SaveCurrentArmy() + { + bool saved = false; + + string filePath = loadedFilePath; + + if (filePath == null) + { + filePath = PromptForArmyFilePath(); + } + + if (filePath != null) + { + saved = SaveCurrentArmyToFile(filePath); + } + + return saved; + } + + private bool SaveCurrentArmyAs() + { + bool saved = false; + string filePath = PromptForArmyFilePath(); + + if (filePath != null) + { + saved = SaveCurrentArmyToFile(filePath); + } + + return saved; + } + + private bool SaveCurrentArmyToFile(string filePath) + { + if (WarFoundrySaver.GetSaver().Save(CurrentArmy, filePath)) + { + loadedFilePath = filePath; + layout.actionSaveArmy.Enabled = false; + CommandStack.setCleanMark(); + return true; + } + else + { + QMessageBox.Critical(this, "file save failed", "file save failed - check log for details"); + return false; + } + } + + private string PromptForArmyFilePath() + { + int result = saveArmyDialog.Exec(); + + if (result == (int)QDialog.DialogCode.Accepted) + { + return saveArmyDialog.SelectedFiles()[0]; + } + else + { + return null; + } + } + + public GameSystem CurrentGameSystem + { + get { return WarFoundryCore.CurrentGameSystem; } + set { WarFoundryCore.CurrentGameSystem = value; } + } + + public Army CurrentArmy + { + get { return WarFoundryCore.CurrentArmy; } + set { SetArmy(value); } + } + + private void SetArmy(Army newArmy) + { + IgnoreArmy(CurrentArmy); + CloseAllUnitWindows(); + + if (newArmy == null) + { + SetNullArmyState(); + } + else + { + WarFoundryCore.CurrentGameSystem = newArmy.GameSystem; + ListenToArmy(newArmy); + SetNonNullArmyState(newArmy); + } + + WarFoundryCore.CurrentArmy = newArmy; + } + + private void IgnoreArmy(Army oldArmy) + { + if (oldArmy != null) + { + oldArmy.UnitAdded -= UnitAddedMethod; + oldArmy.UnitRemoved -= UnitRemovedMethod; + oldArmy.PointsValueChanged -= PointsValueChangedMethod; + } + } + private void UnitAddedMethod(object unitObj) + { + if (unitObj is Unit) + { + Unit unit = (Unit)unitObj; + //TODO set error panel + //sbErrorPanel.Text = ""; + } + } + + private void UnitRemovedMethod(object unitObj) + { + if (unitObj is Unit) + { + Unit unit = (Unit)unitObj; + //TODO set error panel + //sbErrorPanel.Text = ""; + + //TODO check if window is open, and close it if it is + + } + } + + private void PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal) + { + if (obj is Army) + { + SetPointsPanelText(); + } + } + + private void CloseAllUnitWindows() + { +// FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows); +// +// foreach (FrmUnit window in unitForms) +// { +// window.Close(); +// } +// +// unitWindows.Clear(); + } + + private void ListenToArmy(Army newArmy) + { + if (newArmy != null) + { + newArmy.UnitAdded += UnitAddedMethod; + newArmy.UnitRemoved += UnitRemovedMethod; + newArmy.PointsValueChanged += PointsValueChangedMethod; + } + } + + private void SetNullArmyState() + { + layout.actionSaveArmyAs.Enabled = false; + layout.actionCloseArmy.Enabled = false; + layout.menuExportArmyAs.Enabled = false; + DisableCategoryButtons(); + } + + void DisableCategoryButtons () + { + //TODO handle category buttons + } + + + private void SetNonNullArmyState(Army newArmy) + { + SetCategoryButtons(newArmy.Race.Categories); + EnableCategoryButtons(); + layout.actionSaveArmyAs.Enabled = true; + layout.actionCloseArmy.Enabled = true; + layout.menuExportArmyAs.Enabled = true; + } + + void SetCategoryButtons (Category[] categories) + { + //TODO create category buttons + } + + void EnableCategoryButtons () + { + //TODO enable category buttons } } } \ No newline at end of file diff -r 6bbc5c08c06d -r 3d0c9cf1b924 NewArmyDialog.cs --- a/NewArmyDialog.cs Sun Jan 31 20:45:55 2010 +0000 +++ b/NewArmyDialog.cs Tue Feb 02 20:56:39 2010 +0000 @@ -19,7 +19,7 @@ private GameSystem[] gameSystems; private Race[] races; - public NewArmyDialog () + public NewArmyDialog(QWidget parent) : base(parent) { layout = new Ui_CreateNewArmyLayout(); layout.SetupUi(this); @@ -85,6 +85,29 @@ 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();