Changeset 478

Show
Ignore:
Timestamp:
02/02/10 15:56:39 (7 months ago)
Author:
ibboard
Message:

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)

Location:
IBBoard.WarFoundry.GUI.QtSharp/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • IBBoard.WarFoundry.GUI.QtSharp/trunk/MainWindow.cs

    r469 r478  
    55using System.Collections.Generic; 
    66using Qyoto; 
     7using log4net; 
     8using IBBoard.Commands; 
     9using IBBoard.IO; 
     10using IBBoard.WarFoundry.API; 
     11using IBBoard.WarFoundry.API.Factories; 
     12using IBBoard.WarFoundry.API.Objects; 
     13using IBBoard.WarFoundry.API.Savers; 
    714 
    815namespace IBBoard.WarFoundry.GUI.QtSharp 
     
    1017    public class MainWindow : QMainWindow 
    1118    { 
     19        private static readonly string AppTitle = "WarFoundry"; 
     20         
    1221        private Ui_MainWindowLayout layout; 
     22        private readonly ILog log = LogManager.GetLogger(typeof(MainWindow)); 
     23        private string loadedFilePath; 
     24        private CommandStack commandStack; 
     25        private QFileDialog saveArmyDialog; 
    1326         
    1427        public MainWindow () 
     
    1730            layout.SetupUi(this); 
    1831            WindowIcon = new QIcon("icons/App.png"); 
     32            saveArmyDialog = new QFileDialog(this); 
     33            saveArmyDialog.acceptMode = QFileDialog.AcceptMode.AcceptSave; 
    1934            SetUpActionIcons(); 
    2035            ConnectMenuActions(); 
    2136            SetUpToolbar(); 
    2237            layout.unitTabs.Clear(); 
     38            WarFoundryCore.ArmyChanged+= HandleWarFoundryCoreArmyChanged; 
     39            CommandStack.CommandStackUpdated+= HandleCommandStackCommandStackUpdated; 
    2340        } 
    2441         
     
    4057        { 
    4158            QObject.Connect(layout.actionCreateArmy, SIGNAL("triggered()"), CreateNewArmy); 
     59            QObject.Connect(layout.actionUndo, SIGNAL("triggered()"), UndoAction); 
     60            QObject.Connect(layout.actionRedo, SIGNAL("triggered()"), RedoAction); 
    4261        } 
    4362         
    4463        private void CreateNewArmy() 
    4564        { 
    46             NewArmyDialog dialog = new NewArmyDialog(); 
    47             dialog.Show(); 
     65            NewArmyDialog dialog = new NewArmyDialog(this); 
     66            int result = dialog.Exec (); 
     67             
     68            if (result == (int)QDialog.DialogCode.Accepted) 
     69            { 
     70                try 
     71                { 
     72                    CurrentArmy = new Army(dialog.GetSelectedRace(), dialog.GetArmyName(), dialog.GetArmySize()); 
     73                } 
     74                catch (RequiredDataMissingException ex) 
     75                { 
     76                    log.Error("Required data missing from race file", ex); 
     77                    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"); 
     78                } 
     79                catch (InvalidFileException ex) 
     80                { 
     81                    log.Error("Race file was invalid", ex); 
     82                    QMessageBox.Warning(this, ex.Message,  "invalid race file"); 
     83                } 
     84            } 
    4885        } 
    4986         
     
    5996            layout.toolBar.AddAction(layout.actionRedo); 
    6097            layout.toolBar.AddSeparator(); 
     98        }        
     99 
     100        private void HandleWarFoundryCoreArmyChanged (Army oldValue, Army newValue) 
     101        { 
     102            CommandStack.Reset(); 
     103            loadedFilePath = null; 
     104            layout.actionSaveArmy.Enabled = false; 
     105            SetPointsPanelText(); 
     106            SetAppTitle(); 
     107        } 
     108 
     109        private void SetPointsPanelText () 
     110        { 
     111            //TODO: implement panel and points panel 
     112        } 
     113 
     114         
     115        private void SetAppTitle() 
     116        { 
     117            string str = AppTitle; 
     118 
     119            if (CurrentGameSystem!=null) 
     120            { 
     121                str+= " - " + CurrentGameSystem.Name; 
     122             
     123                if (CurrentArmy!=null) 
     124                { 
     125                    str+= " - " + CurrentArmy.Name; 
     126                } 
     127            } 
     128 
     129            this.WindowTitle = str; 
     130        } 
     131         
     132        public CommandStack CommandStack 
     133        { 
     134            get  
     135            { 
     136                if (commandStack == null) 
     137                {                    
     138                    commandStack = new CommandStack(); 
     139                } 
     140 
     141                return commandStack;  
     142            } 
     143        } 
     144 
     145        private void HandleCommandStackCommandStackUpdated () 
     146        { 
     147             
     148        } 
     149 
     150        private void UndoAction() 
     151        { 
     152            if (commandStack.CanUndo()) 
     153            { 
     154                commandStack.Undo(); 
     155            } 
     156        } 
     157 
     158        private void RedoAction() 
     159        { 
     160            if (commandStack.CanRedo()) 
     161            { 
     162                commandStack.Redo(); 
     163            } 
     164        } 
     165 
     166        private bool SaveCurrentArmy() 
     167        { 
     168            bool saved = false; 
     169 
     170            string filePath = loadedFilePath; 
     171 
     172            if (filePath == null) 
     173            { 
     174                filePath = PromptForArmyFilePath(); 
     175            } 
     176 
     177            if (filePath != null) 
     178            { 
     179                saved = SaveCurrentArmyToFile(filePath); 
     180            } 
     181 
     182            return saved; 
     183        } 
     184 
     185        private bool SaveCurrentArmyAs() 
     186        { 
     187            bool saved = false; 
     188            string filePath = PromptForArmyFilePath(); 
     189 
     190            if (filePath != null) 
     191            { 
     192                saved = SaveCurrentArmyToFile(filePath); 
     193            } 
     194             
     195            return saved; 
     196        } 
     197 
     198        private bool SaveCurrentArmyToFile(string filePath) 
     199        { 
     200            if (WarFoundrySaver.GetSaver().Save(CurrentArmy, filePath)) 
     201            { 
     202                loadedFilePath = filePath; 
     203                layout.actionSaveArmy.Enabled = false; 
     204                CommandStack.setCleanMark(); 
     205                return true; 
     206            } 
     207            else 
     208            { 
     209                QMessageBox.Critical(this, "file save failed", "file save failed - check log for details"); 
     210                return false; 
     211            } 
     212        } 
     213 
     214        private string PromptForArmyFilePath() 
     215        { 
     216            int result = saveArmyDialog.Exec(); 
     217 
     218            if (result == (int)QDialog.DialogCode.Accepted) 
     219            { 
     220                return saveArmyDialog.SelectedFiles()[0]; 
     221            } 
     222            else 
     223            { 
     224                return null; 
     225            } 
     226        }        
     227 
     228        public GameSystem CurrentGameSystem 
     229        { 
     230            get { return WarFoundryCore.CurrentGameSystem; } 
     231            set { WarFoundryCore.CurrentGameSystem = value; } 
     232        } 
     233 
     234        public Army CurrentArmy 
     235        { 
     236            get { return WarFoundryCore.CurrentArmy; } 
     237            set { SetArmy(value); } 
     238        } 
     239 
     240        private void SetArmy(Army newArmy) 
     241        { 
     242            IgnoreArmy(CurrentArmy); 
     243            CloseAllUnitWindows(); 
     244 
     245            if (newArmy == null) 
     246            { 
     247                SetNullArmyState(); 
     248            } 
     249            else 
     250            { 
     251                WarFoundryCore.CurrentGameSystem = newArmy.GameSystem; 
     252                ListenToArmy(newArmy); 
     253                SetNonNullArmyState(newArmy); 
     254            } 
     255             
     256            WarFoundryCore.CurrentArmy = newArmy; 
     257        } 
     258 
     259        private void IgnoreArmy(Army oldArmy) 
     260        { 
     261            if (oldArmy != null) 
     262            { 
     263                oldArmy.UnitAdded -= UnitAddedMethod; 
     264                oldArmy.UnitRemoved -= UnitRemovedMethod; 
     265                oldArmy.PointsValueChanged -= PointsValueChangedMethod; 
     266            } 
     267        } 
     268        private void UnitAddedMethod(object unitObj) 
     269        { 
     270            if (unitObj is Unit) 
     271            { 
     272                Unit unit = (Unit)unitObj; 
     273                //TODO set error panel 
     274                //sbErrorPanel.Text = ""; 
     275            } 
     276        } 
     277 
     278        private void UnitRemovedMethod(object unitObj) 
     279        { 
     280            if (unitObj is Unit) 
     281            { 
     282                Unit unit = (Unit)unitObj; 
     283                //TODO set error panel 
     284                //sbErrorPanel.Text = ""; 
     285 
     286                //TODO check if window is open, and close it if it is 
     287                 
     288            } 
     289        } 
     290         
     291        private void PointsValueChangedMethod(WarFoundryObject obj, double oldVal, double newVal) 
     292        { 
     293            if (obj is Army) 
     294            { 
     295                SetPointsPanelText(); 
     296            } 
     297        } 
     298 
     299        private void CloseAllUnitWindows() 
     300        { 
     301//          FrmUnit[] unitForms = DictionaryUtils.ToArray(unitWindows); 
     302// 
     303//          foreach (FrmUnit window in unitForms) 
     304//          { 
     305//              window.Close(); 
     306//          } 
     307// 
     308//          unitWindows.Clear(); 
     309        } 
     310 
     311        private void ListenToArmy(Army newArmy) 
     312        { 
     313            if (newArmy != null) 
     314            { 
     315                newArmy.UnitAdded += UnitAddedMethod; 
     316                newArmy.UnitRemoved += UnitRemovedMethod; 
     317                newArmy.PointsValueChanged += PointsValueChangedMethod; 
     318            } 
     319        } 
     320 
     321        private void SetNullArmyState() 
     322        { 
     323            layout.actionSaveArmyAs.Enabled = false; 
     324            layout.actionCloseArmy.Enabled = false; 
     325            layout.menuExportArmyAs.Enabled = false; 
     326            DisableCategoryButtons(); 
     327        } 
     328 
     329        void DisableCategoryButtons () 
     330        { 
     331            //TODO handle category buttons 
     332        } 
     333 
     334 
     335        private void SetNonNullArmyState(Army newArmy) 
     336        { 
     337            SetCategoryButtons(newArmy.Race.Categories); 
     338            EnableCategoryButtons(); 
     339            layout.actionSaveArmyAs.Enabled = true; 
     340            layout.actionCloseArmy.Enabled = true; 
     341            layout.menuExportArmyAs.Enabled = true; 
     342        } 
     343 
     344        void SetCategoryButtons (Category[] categories) 
     345        { 
     346            //TODO create category buttons 
     347        } 
     348         
     349        void EnableCategoryButtons () 
     350        { 
     351            //TODO enable category buttons 
    61352        } 
    62353    } 
  • IBBoard.WarFoundry.GUI.QtSharp/trunk/NewArmyDialog.cs

    r474 r478  
    2020        private Race[] races; 
    2121         
    22         public NewArmyDialog () 
     22        public NewArmyDialog(QWidget parent) : base(parent) 
    2323        { 
    2424            layout = new Ui_CreateNewArmyLayout(); 
     
    8686        } 
    8787         
     88        public Race GetSelectedRace() 
     89        { 
     90            Race race = null;            
     91            int selectedIndex = layout.raceList.CurrentRow; 
     92             
     93            if (selectedIndex != -1) 
     94            { 
     95                race = races[selectedIndex]; 
     96            } 
     97             
     98            return race; 
     99        } 
     100         
     101        public string GetArmyName() 
     102        { 
     103            return layout.armyName.Text; 
     104        } 
     105         
     106        public int GetArmySize() 
     107        { 
     108            return layout.armySize.Value; 
     109        } 
     110         
    88111        private void ArmyNameChanged() 
    89112        {