Undo-Redo, copy instance, custom events and a problem

Hi,

The Undo-Redo implementation is based on storing pre & post state of
an attribute.
You store the instance before changing the value and store the
instance after changing the values.
While undoing or redoing, you copy/replace the current state with
stored once.

For color attribute as soon as you choose a color, here is the code:

# Custom Event
evt = ValueChangeEvent(EVT_COLOR_CHANGED.typeId, self.GetId())
# Store Pre State
evt.SetPreState(copy.copy(self.attribute))
# Change the newly selected color
self.attribute.setValue(R,G,B)
# Store Post State
evt.SetPostState(copy.copy(self.attribute))
# Throw Custom Event
self.GetEventHandler().ProcessEvent(evt)

Both states are copied as new instance with correct values.
The problem is when this event is getting fired.

evt.GetPreState().GetValue() is showing the post color value. Although
GetPreState() & GetPostState()
are showing two different instances but I have no idea why values are
not coming/stored correctly. Some where
in between is messed up and post-state values are copied in pre-state.

On a side note, self.attribute.setValue takes three floating values
for R,G & B colors but stored them
as a text. Similarly self.attribute.getValue() converts text into
float and returns.

Is there anything related to scope or something?

Cheers

Prashant

Hi,

The Undo-Redo implementation is based on storing pre& post state of
an attribute.
You store the instance before changing the value and store the
instance after changing the values.
While undoing or redoing, you copy/replace the current state with
stored once.

For color attribute as soon as you choose a color, here is the code:

# Custom Event
evt = ValueChangeEvent(EVT_COLOR_CHANGED.typeId, self.GetId())
# Store Pre State
evt.SetPreState(copy.copy(self.attribute))
# Change the newly selected color
self.attribute.setValue(R,G,B)
# Store Post State
evt.SetPostState(copy.copy(self.attribute))
# Throw Custom Event
self.GetEventHandler().ProcessEvent(evt)

Both states are copied as new instance with correct values.
The problem is when this event is getting fired.

evt.GetPreState().GetValue() is showing the post color value. Although
GetPreState()& GetPostState()
are showing two different instances but I have no idea why values are
not coming/stored correctly. Some where
in between is messed up and post-state values are copied in pre-state.

On a side note, self.attribute.setValue takes three floating values
for R,G& B colors but stored them
as a text. Similarly self.attribute.getValue() converts text into
float and returns.

Is there anything related to scope or something?

The Python copy module is not able to copy wx objects that have a C++ object within them because Python doesn't know anything about that object. They will both end up pointing to the same C++ object and you'll have problems.

Instead you'll need make a new instance of the object that has the same color components. For example, since the wx.Colour class has a Get() method that returns a tuple of color components you can do this:

  >>> c1 = wx.Colour(1, 2, 3)
  >>> c2 = wx.Colour(*c1.Get())
  >>> c1
  wx.Colour(1, 2, 3, 255)
  >>> c2
  wx.Colour(1, 2, 3, 255)
  >>> c1 == c2
  True
  >>> c1 is c2
  False
  >>> c1.this
  <Swig Object of type 'wxColour *' at 0x2a5480>
  >>> c2.this
  <Swig Object of type 'wxColour *' at 0x19c05410>

···

On 7/25/10 3:50 AM, King wrote:

--
Robin Dunn
Software Craftsman

1 Like