Changeset 2794


Ignore:
Timestamp:
Feb 22, 2011, 8:13:49 PM (11 years ago)
Author:
kopal
Message:

working on operations and the UndoRedoManager of the new model

Location:
trunk/WorkspaceManagerModel
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/WorkspaceManagerModel/Model/Elements/VisualElementModel.cs

    r2791 r2794  
    2222using System.Windows;
    2323using WorkspaceManagerModel.Model.Interfaces;
     24using Cryptool.PluginBase.Miscellaneous;
    2425
    2526namespace WorkspaceManager.Model
     
    4243        /// </summary>
    4344        internal Point Position;
    44        
     45
    4546        /// <summary>
    4647        /// Minimum Width of this VisualElementModel
    4748        /// </summary>
    4849        internal double MinWidth = 250;
    49        
     50
    5051        /// <summary>
    5152        /// Minimum Height of this VisualElementModel
     
    133134        }
    134135
    135         [field: NonSerialized]
    136         public event EventHandler<PositionArgs> PositionChanged;
    137 
    138         [field: NonSerialized]
    139         public event EventHandler<SizeArgs> SizeChanged;
    140 
    141         [field: NonSerialized]
    142         public event EventHandler<ModelArgs> NewChildElement;
    143 
    144         [field: NonSerialized]
    145         public event EventHandler<ModelArgs> DeletedChildElement;
    146 
    147         internal void OnPositionChanged(Point oldPosition, Point newPosition)
    148         {
    149             PositionChanged.Invoke(this, new PositionArgs(this,oldPosition, newPosition));
    150         }
    151 
    152         internal void OnSizeChanged(double oldWidth, double newWidth, double oldHeight, double newHeight)
    153         {
    154             SizeChanged.Invoke(this, new SizeArgs(this, oldWidth, newWidth, oldHeight, newHeight));
    155         }
    156 
    157         internal void OnNewChildElement(VisualElementModel effectedModelElement)
    158         {
    159             NewChildElement.Invoke(this, new ModelArgs(effectedModelElement));
    160         }
    161 
    162         internal void OnDeletedChildElement(VisualElementModel effectedModelElement)
    163         {
    164             DeletedChildElement.Invoke(this, new ModelArgs(effectedModelElement));
    165         }
    166136    }
    167 
    168     public class ModelArgs : EventArgs
    169     {
    170         public VisualElementModel EffectedModelElement { get; private set; }
    171 
    172         public ModelArgs(VisualElementModel effectedModelElement)
    173         {
    174             this.EffectedModelElement = effectedModelElement;
    175         }
    176     }
    177 
    178     public class PositionArgs : ModelArgs
    179     {
    180         public Point OldPosition{get;internal set;}
    181         public Point NewPosition { get; internal set; }
    182         internal PositionArgs(VisualElementModel model, Point oldPosition, Point newPosition):
    183             base(model)
    184         {
    185             this.OldPosition = oldPosition;
    186             this.NewPosition = newPosition;
    187         }
    188     }
    189 
    190     public class SizeArgs : ModelArgs
    191     {
    192         public double OldWidth{get;internal set;}
    193         public double NewWidth { get; internal set; }
    194         public double OldHeight { get; internal set; }
    195         public double NewHeight { get; internal set; }
    196 
    197         internal SizeArgs(VisualElementModel model, double oldWidth, double newWidth, double oldHeight, double newHeight):
    198             base(model)
    199         {
    200             this.OldWidth = oldWidth;
    201             this.NewWidth = newWidth;
    202             this.OldHeight = oldHeight;
    203             this.NewHeight = newHeight;
    204         }
    205     }
    206 
    207137   
    208138}
  • trunk/WorkspaceManagerModel/Model/Elements/WorkspaceModel.cs

    r2791 r2794  
    5353            this.AllImageModels = new List<ImageModel>();
    5454            this.AllTextModels = new List<TextModel>();
    55             this.UndoRedoManager = new UndoRedoManager();
     55            this.UndoRedoManager = new UndoRedoManager(this);
    5656        }     
    5757
     
    381381                foreach (ConnectionModel connectionModel in new List<ConnectionModel>(connectorModel.InputConnections))
    382382                {
    383                     deleteConnectionModel(connectionModel);
     383                    //deleteConnectionModel(connectionModel);
     384                    this.ModifyModel(new DeleteConnectionModelOperation(connectionModel));
    384385                }
    385386
     
    387388                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
    388389                {
    389                     deleteConnectionModel(outputConnection);
     390                    //deleteConnectionModel(outputConnection);
     391                    this.ModifyModel(new DeleteConnectionModelOperation(outputConnection));
    390392                }
    391393                this.HasChanges = true;
     
    441443        /// </summary>
    442444        /// <param name="operation"></param>
    443         public object ModifyModel(Operation operation){           
    444             this.UndoRedoManager.DidOperation(operation);
    445             return operation.Execute(this);
    446         }
    447 
     445        public object ModifyModel(Operation operation){
     446
     447            try
     448            {
     449                return operation.Execute(this);
     450            }
     451            finally
     452            {
     453                this.UndoRedoManager.DidOperation(operation);
     454            }
     455
     456        }
     457
     458        /// <summary>
     459        /// A childs position of this WorkspaceModel changed
     460        /// </summary>
     461        [field: NonSerialized]
     462        public event PositionChangedEventHandler ChildPositionChanged;
     463
     464        /// <summary>
     465        /// A childs size of this WorkspaceModel changed
     466        /// </summary>
     467        [field: NonSerialized]
     468        public event SizeChangedEventHandler ChildSizeChanged;
     469
     470        /// <summary>
     471        /// A child of this WorkspaceModel is created
     472        /// </summary>
     473        [field: NonSerialized]
     474        public event NewChildElementEventHandler NewChildElement;
     475
     476        /// <summary>
     477        /// A child of this WorkspaceModel is deleted
     478        /// </summary>
     479        [field: NonSerialized]
     480        public event DeleteChildElementEventHandler DeletedChildElement;
     481
     482        /// <summary>
     483        /// Call this to tell the environment that a childs position changed
     484        /// </summary>
     485        /// <param name="effectedModelElement"></param>
     486        /// <param name="oldPosition"></param>
     487        /// <param name="newPosition"></param>
     488        internal void OnChildPositionChanged(VisualElementModel effectedModelElement, Point oldPosition, Point newPosition)
     489        {
     490            if (ChildPositionChanged != null)
     491            {
     492                ChildPositionChanged(this, new PositionArgs(effectedModelElement, oldPosition, newPosition));
     493            }
     494        }
     495
     496        /// <summary>
     497        ///  Call this to tell the environment that a childs size changed
     498        /// </summary>
     499        /// <param name="effectedModelElement"></param>
     500        /// <param name="oldWidth"></param>
     501        /// <param name="newWidth"></param>
     502        /// <param name="oldHeight"></param>
     503        /// <param name="newHeight"></param>
     504        internal void OnChildSizeChanged(VisualElementModel effectedModelElement, double oldWidth, double newWidth, double oldHeight, double newHeight)
     505        {
     506            if (ChildSizeChanged != null)
     507            {
     508                ChildSizeChanged(this, new SizeArgs(effectedModelElement, oldWidth, newWidth, oldHeight, newHeight));
     509            }
     510        }
     511
     512        /// <summary>
     513        /// Call this to tell the environment that we created a new child
     514        /// </summary>
     515        /// <param name="effectedModelElement"></param>
     516        internal void OnNewChildElement(VisualElementModel effectedModelElement)
     517        {
     518            if (NewChildElement != null)
     519            {
     520                NewChildElement(this, new ModelArgs(effectedModelElement));
     521            }
     522        }
     523
     524        /// <summary>
     525        /// Call this to tell the environment that we deleted a child
     526        /// </summary>
     527        /// <param name="effectedModelElement"></param>
     528        internal void OnDeletedChildElement(VisualElementModel effectedModelElement)
     529        {
     530            if (DeletedChildElement != null)
     531            {
     532                DeletedChildElement(this, new ModelArgs(effectedModelElement));
     533            }
     534        }
     535   
    448536        /// <summary>
    449537        /// Checks wether a Connector and a Connector are compatible to be connected
     
    485573        }
    486574    }
     575
     576    /// <summary>
     577    /// Event args which "knows" the effected model
     578    /// </summary>
     579    public class ModelArgs : EventArgs
     580    {
     581        public VisualElementModel EffectedModelElement { get; private set; }
     582
     583        public ModelArgs(VisualElementModel effectedModelElement)
     584        {
     585            this.EffectedModelElement = effectedModelElement;
     586        }
     587    }
     588
     589    /// <summary>
     590    /// Event args which also "knows" old and new positions
     591    /// </summary>
     592    public class PositionArgs : ModelArgs
     593    {
     594        public Point OldPosition { get; internal set; }
     595        public Point NewPosition { get; internal set; }
     596        internal PositionArgs(VisualElementModel model, Point oldPosition, Point newPosition) :
     597            base(model)
     598        {
     599            this.OldPosition = oldPosition;
     600            this.NewPosition = newPosition;
     601        }
     602    }
     603
     604    /// <summary>
     605    /// Event args which also "knows" old and new size (Width, Height)
     606    /// </summary>
     607    public class SizeArgs : ModelArgs
     608    {
     609        public double OldWidth { get; internal set; }
     610        public double NewWidth { get; internal set; }
     611        public double OldHeight { get; internal set; }
     612        public double NewHeight { get; internal set; }
     613
     614        internal SizeArgs(VisualElementModel model, double oldWidth, double newWidth, double oldHeight, double newHeight) :
     615            base(model)
     616        {
     617            this.OldWidth = oldWidth;
     618            this.NewWidth = newWidth;
     619            this.OldHeight = oldHeight;
     620            this.NewHeight = newHeight;
     621        }
     622    }
     623   
     624    public delegate void PositionChangedEventHandler(VisualElementModel model, PositionArgs args);
     625    public delegate void SizeChangedEventHandler(VisualElementModel model, SizeArgs args);
     626    public delegate void NewChildElementEventHandler(VisualElementModel model, ModelArgs args);
     627    public delegate void DeleteChildElementEventHandler(VisualElementModel model, ModelArgs args);
    487628}
  • trunk/WorkspaceManagerModel/Model/Operations/Operations.cs

    r2791 r2794  
    205205
    206206            Model.Position = NewPosition;
    207             Model.OnPositionChanged(OldPosition, NewPosition);
     207            workspaceModel.OnChildPositionChanged(Model, OldPosition, NewPosition);
    208208            return true;
    209209        }
     
    212212        {
    213213            Model.Position = OldPosition;
    214             Model.OnPositionChanged(NewPosition, OldPosition);
     214            workspaceModel.OnChildPositionChanged(Model, NewPosition, OldPosition);
    215215        }
    216216
     
    243243            Model.Width = NewWidth;
    244244            Model.Height = NewHeight;
    245             Model.OnSizeChanged(this.OldWidth, this.NewWidth, this.OldHeight, this.NewHeight);
     245            workspaceModel.OnChildSizeChanged(Model, this.OldWidth, this.NewWidth, this.OldHeight, this.NewHeight);
    246246            return true;
    247247        }
     
    251251            Model.Width = OldWidth;
    252252            Model.Height = OldHeight;
    253             Model.OnSizeChanged(this.NewWidth, this.OldWidth, this.NewHeight, this.OldHeight);
     253            workspaceModel.OnChildSizeChanged(Model, this.NewWidth, this.OldWidth, this.NewHeight, this.OldHeight);
    254254        }
    255255
  • trunk/WorkspaceManagerModel/Model/Tools/UndoRedoManager.cs

    r2756 r2794  
    2929    public class UndoRedoManager
    3030    {
    31         internal UndoRedoManager()
     31        private WorkspaceModel WorkspaceModel = null;
     32
     33        private bool CurrentlyWorking { get; set; }
     34
     35        internal UndoRedoManager(WorkspaceModel workspaceModel)
    3236        {
    33 
     37            WorkspaceModel = workspaceModel;
    3438        }
    3539
     
    6064        public void Undo()
    6165        {
     66            if (!CanUndo())
     67            {
     68                return;
     69            }
    6270           
     71            this.CurrentlyWorking = true;
     72            try
     73            {
     74                Operation op = UndoStack.Pop();
     75                op.Undo(WorkspaceModel);
     76                RedoStack.Push(op);
     77
     78                VisualElementModel model = op.Model;
     79                while (UndoStack.Count > 0 &&
     80                    Object.ReferenceEquals(UndoStack.Peek().Model, model) &&
     81                    UndoStack.Peek() is MoveModelElementOperation)
     82                {
     83                    op = UndoStack.Pop();
     84                    op.Undo(WorkspaceModel);
     85                    RedoStack.Push(op);
     86                    model = op.Model;
     87                }
     88            }
     89            finally
     90            {
     91                this.CurrentlyWorking = false;
     92            }
    6393        }
    6494
     
    6898        public void Redo()
    6999        {
    70            
     100            if (!CanRedo())
     101            {
     102                return;
     103            }
     104
     105            this.CurrentlyWorking = true;
     106            try
     107            {
     108                Operation op = RedoStack.Pop();
     109                op.Execute(WorkspaceModel);
     110                UndoStack.Push(op);
     111
     112                VisualElementModel model = op.Model;
     113                while (RedoStack.Count > 0 &&
     114                    Object.ReferenceEquals(RedoStack.Peek().Model, model) &&
     115                    RedoStack.Peek() is MoveModelElementOperation)
     116                {
     117                    op = RedoStack.Pop();
     118                    op.Execute(WorkspaceModel);
     119                    UndoStack.Push(op);
     120                    model = op.Model;
     121                }
     122            }
     123            finally
     124            {
     125                this.CurrentlyWorking = false;
     126            }
    71127        }
    72128
     
    86142        public void DidOperation(Operation op)
    87143        {
     144            //we do not notice any operation if we are currently working
     145            //(means we undo or redo at this moment)
     146            if (CurrentlyWorking)
     147            {
     148                return;
     149            }
     150
    88151            if (RedoStack.Count > 0)
    89152            {
  • trunk/WorkspaceManagerModel/WorkspaceManagerModel.csproj

    r2791 r2794  
    6060    <Compile Include="Model\Elements\ImageModel.cs" />
    6161    <Compile Include="Model\Elements\PluginModel.cs" />
    62     <Compile Include="Model\Elements\PositionArgs.cs" />
    6362    <Compile Include="Model\Elements\TextModel.cs" />
    6463    <Compile Include="Model\Elements\VisualElementModel.cs" />
Note: See TracChangeset for help on using the changeset viewer.