Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Undo and Redo don't maintain state of diagram node. (Read 12369 times)
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Undo and Redo don't maintain state of diagram node.
Apr 24th, 2013 at 7:34am
Print Post  
Hi Stoyo,

Thank you for your help. In my project, There are two buttons named "Undo" and "Redo". I have write code for undo and redo,code is here:-

  Private Sub UndoRedo_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
   Select Case sender.name.ToString
   Case "btnUndo"
   If diagram.CanUndo = True Then diagram.Undo()
   btnRedo.IsEnabled = True
   Case "btnRedo"
   If diagram.CanRedo = True Then diagram.Redo()
   End Select
   End Sub


Yes those buttons are working fine. But in our project, we need to add more functionality to undo and redo Likewise inserting new image in shape node and clicking on undo button undo the image inserted and clicking on redo button again inserts the image. And another scenario is If i have insert any text in nodes then undo, text gets invisible, after this click on redo , text gets visible. How can i maintain each state of nodes with respect to undo and redo ?

I am sending you the sample project can you please modify the sample project a bit to encapsulate these scenarios? It would be a great help of yours, if you please. Thanks in advance. Smiley
  

DiagramShapeNode_002.rar (Attachment deleted)
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Undo and Redo don't maintain state of diagram node.
Reply #1 - Apr 24th, 2013 at 10:51am
Print Post  
Hi,

You must create a ChangeItemCommand to explicitly save the item's state for undo of property changes. You can find sample code here:
http://www.mindfusion.eu/onlinehelp/diagramlite/index.htm?M_MindFusion_Diagrammi...

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Re: Undo and Redo don't maintain state of diagram node.
Reply #2 - Apr 30th, 2013 at 5:45am
Print Post  
Hi Stoyo,

As much as I understand from the documentation is, we have to call the "diagram.ExecuteCommand(change);" on every change that we perform the user commands like changing of text, foreground, background etc. etc.

Let me know if my understanding is wrong. If I correctly understood the process, is there any simple process to track/call it from a single location once the node inserted? IMO, in that case we don't have to call the said command in every user interaction.

Looking forward for your response.

Regards,
Yogendra
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Undo and Redo don't maintain state of diagram node.
Reply #3 - Apr 30th, 2013 at 7:50am
Print Post  
Hi,

You will have to call it for every set of property assignments that you consider an atomic undoable operation. For example, after showing a font dialog, you'll probably set several properties such as FontFamily, FontSize and FontWeight, but will want only one undo record created and so surround all Font* assignments with a single ChangeItemCmd / Execute pair.

The control does not create such commands automatically from property setters because it cannot know whether you are initializing or animating some item and don't need undo, or if you want it saved together with other property values as a single undo record. That falls squarely into the application logic layer, and you will have to create the commands explicitly when you need them.

As a shortcut when you need undo records created for individual  property assignments, you could bring them to one-liners using this method:

Code
Select All
void SetWithUndo(DiagramItem item, DependencyProperty property, object value)
{
	var change = new ChangeItemCommand(diagram, item);
	item.SetValue(property, value);
	diagram.ExecuteCommand(change);
}

SetWithUndo(node, DiagramItem.BrushProperty, Brushes.Red); 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Re: Undo and Redo don't maintain state of diagram node.
Reply #4 - May 15th, 2013 at 11:41am
Print Post  
Hi Syoyo,

Thanks for reply back. Yes your solution working with undo command with respect to node's font family, font size, node background etc. But I have problem in another scenario.
First is I can add effect in node , here is code:-

Code
Select All
For Lop = 0 To diagram.Nodes.Count - 1
            If diagram.Nodes(Lop).Selected = True Then
                Dim n As ShapeNode = diagram.Nodes(Lop)
                Dim CurrentShadow As Effects.DropShadowEffect = n.Effect
                Dim Shadow As New Effects.DropShadowEffect
                With Shadow
                    .Color = CurrentShadow.Color
                    .ShadowDepth = spnShadowDepth.Value
                    .Direction = spnShadowDirection.Value
                    .BlurRadius = spnShadowBlur.Value
                End With
                Dim shadowColor = Shadow.Color
                n.Effect = Shadow
               SetWithUndo(n,n.Effect)
            End If
Next Lop 



Note:-"spnShadowDepth", "spnShadowDirection" and "spnShadowBlur" are numeric button.

Diagram Node Effects not working with undo button.

Second is I Can change the order of control. Code is here:-

Code
Select All
Dim LowZorder As Integer = 9999
        Dim HiZorder As Integer = -9999
        Dim diagramSelectionItems = diagram.Selection.Items(0)

        Select Case OrderType
            Case "BringToFront"

                diagram.Selection.Items(0).ZIndex = HiZorder
            Case "SendToBack"

                diagramSelectionItems(0).ZIndex = LowZorder

            Case "BringForward"
                diagramSelectionItems(0).ZIndex += 1
            Case "SendBackward"
                diagramSelectionItems(0).ZIndex = 1
        End Select
        SetWithUndo(diagramSelectionItems(0), diagramSelectionItems(0).ZIndex) 



and here is method code:-
Code
Select All
 Private Sub SetWithUndo(item As DiagramItem)
        Dim change = New ChangeItemCommand(diagram, item)
        diagram.ExecuteCommand(change)
    End Sub 



In these two scenario Undo command not working.

please suggest me something.......

Thanks in advanced. Smiley
« Last Edit: May 15th, 2013 at 1:05pm by Yogendra »  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Undo and Redo don't maintain state of diagram node.
Reply #5 - May 16th, 2013 at 11:12am
Print Post  
Hi,

Effect is not saved by ChangeItemCommand, you will need a custom command to save it, such as:

Code
Select All
public class EffectChangeCommand : Command
{
	public EffectChangeCommand(Diagram diagram, DiagramItem item) :
		base(diagram)
	{
		this.item = item;
		oldEffect = item.Effect;
	}

	public override void Execute()
	{
		newEffect = item.Effect;
	}

	public override void Redo()
	{
		item.Effect = newEffect;
	}

	public override void Undo()
	{
		item.Effect = oldEffect;
	}

	Effect oldEffect;
	Effect newEffect;
	DiagramItem item;
} 



The problem with ZIndex happens because you are setting the property before creating the ChangeItemCmd instance, whose constructor saves the item's initial state. ZIndex should be changed after calling the command constructor and before Execute:

Code
Select All
Dim change = New ChangeItemCommand(diagram, item)
item.ZIndex = newValue;
diagram.ExecuteCommand(change) 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Re: Undo and Redo don't maintain state of diagram node.
Reply #6 - May 20th, 2013 at 9:56am
Print Post  
Hi stoyo,

Thanks to reply back. Your solution is working fine with effects but not with the order of control.

And I have some problem with Following another scenario :-

1 Lock the control

I am using this code:-
Code
Select All
Private Sub LockAllNodes(ByVal Locked As Boolean)
        On Error Resume Next

        Dim Lop As Long
        For Lop = 1 To diagram.Nodes.Count - 1
            If diagram.Nodes(Lop).Id <> BackgroundID Then
                diagram.Nodes(Lop).Locked = Locked
                SetWithUndo(diagram.Nodes(Lop))
            End If
        Next Lop

    End Sub 



when insert control on the diagram and resize it I have write this code on "NodeModified" event. Code is here:-
Code
Select All
SetWithUndo(e.Node) 

then lock the control and perform undo operation.

Problem is it is not sequenced, Click on undo button, Control is not unlock and again click on undo button then directly gets resize not unlock. sometimes i have to click more times on undo button then
undo operation is working.

Our project requirement is undo and redo button should be disabled if there is no changes made on diagram and if changes made then undo redo button should be enabled and disabled with respect to each other. for this i have written this code :-

Code
Select All
Private Sub UndoRedo_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        On Error Resume Next

          Select Case sender.name.ToString
            Case "cmdUndo"
                If diagram.CanUndo = True Then diagram.Undo()
                cmdRedo.IsEnabled = True
            Case "cmdRedo"
                If diagram.CanRedo = True Then diagram.Redo()

        End Select
        Next

    End Sub 



But sometimes redo button is disabled while no of redo operation is pending to be performed. Because of i am using This method:-
Code
Select All
SetWithUndo(selectedNode) 



Please suggest me something to over come of these problems.

Thanks in advanced. Smiley



  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Undo and Redo don't maintain state of diagram node.
Reply #7 - May 20th, 2013 at 10:33am
Print Post  
My post above explains why it does not work for ZIndex, and the same is valid for Locked. The initial property value is saved by the ChangeItemCmd constructor, and the final value is saved by the Execute method, so you must change the property between the constructor and execute calls:

Code
Select All
Dim change = New ChangeItemCommand(diagram, item)
item.Locked = newValue;
diagram.ExecuteCommand(change); 



Your SetWithUndo implementation doesn't do anything useful, since you create the command after the change, and its constructor cannot know what the old value of the property was for later undo.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Re: Undo and Redo don't maintain state of diagram node.
Reply #8 - May 20th, 2013 at 11:05am
Print Post  
Hi stoyo,

thanks to reply back. Yes i got it.....
But i using same thing with ordering of control. You can tell me , what's the problem with my code.
Code
Select All
Dim HiZorder As Integer = -9999
        Dim Lop As Long
        Dim diagramItem = diagram.Items
        For Lop = 0 To diagramItem.Count - 1
            Dim diagramNode = diagramItem(Lop)
            If diagramNode.Id <> BackgroundID Then
                If diagramNode.ZIndex > HiZorder Then HiZorder = diagramNode.ZIndex
                If diagramNode.ZIndex < LowZorder Then LowZorder = diagramNode.ZIndex
            End If
        Next Lop
        Dim diagramSelectionItems = diagram.Selection.Items
        Dim change = New ChangeItemCommand(diagram, diagramSelectionItems(0))
        Dim diagramNodes = diagram.Nodes
        Select Case OrderType
            Case "BringToFront"
                If Not (TypeOf diagramSelectionItems(0) Is MultimediaControl OrElse TypeOf diagramSelectionItems(0) Is ControlNodeEx OrElse TypeOf diagramSelectionItems(0) Is ControlNode) Then
                    Dim zIndex = diagramNodes.Where(Function(x) TypeOf x Is MultimediaControl OrElse TypeOf x Is ControlNode).Count
                    HiZorder -= zIndex
                End If
                diagram.Selection.Items(0).ZIndex = HiZorder
            Case "SendToBack"
                If TypeOf diagramSelectionItems(0) Is MultimediaControl OrElse TypeOf diagramSelectionItems(0) Is ControlNode OrElse TypeOf diagramSelectionItems(0) Is ControlNodeEx Then
                    Dim zIndex = diagramNodes.Where(Function(x) TypeOf x Is MultimediaControl OrElse TypeOf x Is ControlNode).Count
                    Dim shpeNodeZindex = diagramNodes.Where(Function(x) Not (TypeOf x Is MultimediaControl OrElse TypeOf x Is ControlNode)).Count
                    If shpeNodeZindex > 1 Then
                        LowZorder = shpeNodeZindex
                    End If
                End If
                diagramSelectionItems(0).ZIndex = LowZorder

            Case "BringForward"
                Dim node As DiagramNode = diagramNodes.FirstOrDefault(Function(x) x.ZIndex = (diagramSelectionItems(0).ZIndex + 1))
                If node.Tag = diagramSelectionItems(0).Tag Then
                    diagramSelectionItems(0).ZIndex += 1
                End If
            Case "SendBackward"
                Dim node As DiagramNode = diagramNodes.FirstOrDefault(Function(x) x.ZIndex = (diagramSelectionItems(0).ZIndex - 1))
                If node.Tag = diagramSelectionItems(0).Tag Then
                    diagramSelectionItems(0).ZIndex -= 1
                End If
                If diagramSelectionItems(0).ZIndex = 0 Then
                    diagramSelectionItems(0).ZIndex = 1
                End If

        End Select
        diagram.ExecuteCommand(change) 



i am still facing problem with ordering the control.

Please suggest me something....

Thanks in advanced Smiley
  
Back to top
 
IP Logged
 
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Re: Undo and Redo don't maintain state of diagram node.
Reply #9 - May 21st, 2013 at 6:38am
Print Post  
Hi Stoyo,

It's been great help of yours for the code on undo redo that you've provided.
I've used it and it's working fine. But in the project of mine, the requirement is so much complex that
the normal methodology for undoing and redoing is not working properly. Can you please suggest a more advanced way through
which it is possible to track even the minute changes in the application of mine, related to the diagram control.
Such as by creating a change log file etc. I would be very thankful to you if you please help in getting out such a complex scenario
that I am in.

Or either can you please give me your Skype ID, so that I can discuss with you one on one, the way to do that. As I see no other person who
could help me with that. It would be great help of you.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Undo and Redo don't maintain state of diagram node.
Reply #10 - May 21st, 2013 at 9:12am
Print Post  
What problems exactly are you facing with Z order? You can track as minute changes as you need by using custom commands, but I could not quite understand your example about change log file.
  
Back to top
 
IP Logged
 
Yogendra
Junior Member
**
Offline


I Love MindFusion!

Posts: 68
Joined: Dec 27th, 2012
Re: Undo and Redo don't maintain state of diagram node.
Reply #11 - May 21st, 2013 at 11:00am
Print Post  
Hi Stoyo,

Thanks to reply back. Problem is I can insert many control on diagram and change the order using z order then perform undo operations, previous ordering is not reflect on the controls.

Another scenario is. In my project, there are 3 type of controls, media Chanel, Clock control and Shape control. Media control and clock control are user control and Media control are inherits from "shapeNode" class and clock control are inherits from "ControlNode" class. These are user control have no of Dependency property which can be assigned at run time. If changes made in these property so changes are reflected on control then perform undo operation. Can we done this by using Custom Command.

Please suggest me something..

Thanks in advanced.  Smiley
« Last Edit: May 21st, 2013 at 1:03pm by Yogendra »  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Undo and Redo don't maintain state of diagram node.
Reply #12 - May 21st, 2013 at 3:20pm
Print Post  
Hi,

Please attach a sample project that shows the Z order problem.

You could use the standard ChangeItemCmd but override the CreateState, SaveState and RestoreState methods to save your additional state, as shown here:

http://mindfusion.eu/Forum/YaBB.pl?num=1368397331/5#5

You could as well create separate commands for setting individual properties as the example for Effect from one of the previous posts.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint