SmoothSizerMixin / SmoothBoxSizer .1!

Mike Rooney wrote:

The only thing, and maybe someone can help me here, is that I couldn't find a concise way to add a spacer before or after an item. To find the position to insert the spacer, given an item ('item') to hide, I thought I could do:

   sizerItem = sizer.GetItem(item)
   pos = sizer.GetChildren().index(sizerItem)

but that always gives an exception for the second line saying it couldn't find the element in the list. So, what I had to do was:

           if isinstance(item, int):
               pos = item
           else:
               for i, sizerItem in enumerate(self.GetChildren()):
                   if isinstance(item, wx.Window):
                       obj = sizerItem.Window
                   else:
                       obj = sizerItem.Sizer
                                         if obj is item:
                       pos = i
                       break

Can I do this a better way? I sure can't seem to find one.

You could get tricky and inject a __cmp__ method into the wx.SizerItem class that does a smart compare like what you've done above. Then the index function should work.

I think the mixin now works in a generic enough way that it can be used with any sizer, although no specific behavior probably needs to be implemented, they just need to subclass to get the methods. How does this look, Robin?

I like it, however I'd like to see one more use case supported. I think that we'll want to have some way to be able to use the smooth insert and smooth hide in a sizer that is not subclassed from SmoothSizerMixin. For example I may have an existing layout made with normal sizers (or perhaps sizers specified in XRC so they can't be subclassed) and I want to be able to do some smooth changes with those sizers. One way to do this is to have an adapter class that would take a reference to an existing sizer in it's constructor, and provide the smooth Insert and Hide methods. Something like this:

  sa = SmoothAdapter(otherSizer)
  sa.Insert(...)

Or since it's just a temporary adapter it could even be used like this:

  SmoothAdapter(otherSizer).Insert(...)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Mike Rooney wrote:

I think the mixin now works in a generic enough way that it can be used with any sizer, although no specific behavior probably needs to be implemented, they just need to subclass to get the methods. How does this look, Robin?

I like it, however I'd like to see one more use case supported. I think that we'll want to have some way to be able to use the smooth insert and smooth hide in a sizer that is not subclassed from SmoothSizerMixin. For example I may have an existing layout made with normal sizers (or perhaps sizers specified in XRC so they can't be subclassed) and I want to be able to do some smooth changes with those sizers. One way to do this is to have an adapter class that would take a reference to an existing sizer in it's constructor, and provide the smooth Insert and Hide methods. Something like this:

    sa = SmoothAdapter(otherSizer)
    sa.Insert(...)

Or since it's just a temporary adapter it could even be used like this:

    SmoothAdapter(otherSizer).Insert(...)

I agree that this is a nice use-case. I will give this a shot! I took a stab or two at it earlier and couldn't come up with anything that didn't permanently change the otherSizer into a SmoothSizer. I feel like if I understood __new__ well enough I could probably use that mechanism but I have not quite wrapped my head around that yet.

Where SmoothSizer calls wx.Sizer.Insert/Hide etc and passes in itself as the first argument, in the case of an adapter it needs to pass in the otherSizer instead. These methods could take an optional argument but that does not seem like a good solution at all.

I am sure there is an elegant, pythonic way to do it, but I can't figure it out. I basically need a new object which acts like it is the otherSizer but also has the methods of the SmoothSizer, which work appropriately. Maybe it is easy and I am just missing it completely somehow? Are there any examples I might look at?

Also, I realized I don't have a SmoothShow, though this should be almost trivial to implement.

Thanks!
- Mike Rooney