MVC design question while using wxPython

I've been playing with wxPython for a while now, and I think that I'm ready to
put together a real app. I normally do mostly server-side work, so I'm a bit
new to GUI programming and *very* new to wx.

I already have some business objects that encapsulate the data and behavior of
the domain. In the UI, the user will be editing properties of these business
objects, but there may be multiple visible or hidden controls whose view is
affected by a change. For example, say we have a fairly standard set up: a
vertical splitter with a navigation tree on the left and a horizontal
splitter on the right with a list control on top and a properties panel
(multiple controls for editing an item's properties) on bottom.

Now, when the user clicks on the navigation control and then selects an item
in the list control, all of the property fields need to be populated based on
that item. As the user edits those properties, the changes need to be
reflected in the business object, and these changes may impact the
information displayed in the list control and in the navigation tree.

I know that some sort of model-view-controller/observer pattern works well in
this type of situation, but I'm wondering whether there's a standard way to
implement MVC with wxPython. For example, do you write a controller object
that implements some sort of observable API with an explicit list of
observers to notify whenever the controlled business object changes? Or do
you just push the observable behavior down into the business objects
themselves? How do you connect the business objects with the controls that
need to be updated? Do you just maintain a list of observers that have asked
to be notified in the observable object/controller? Or do you use the wx
event system to send notification that a business object was updated and then
make all of the controls listen for those changes? When pushing data back
and forth between the business objects and the GUI controls, do you make a
Panel subclass that shows all of the properties for a specific business class
and handles pushing data back and forth to standard wx controls? Or do you
subclass each control, so that you have MySocialSecurityNumberTextCtrl and
MyEmployeeDepartmentCodeCombCtrl and such?

Or are all of these suggestions valid and used by various applications
depending on the needs?

Thanks,
---Tom

I don't know about a "standard" way.
I find, since the view and controller tend to be so closely
coupled, that the simpler Document-View pattern works well.

I have a similar usage in an app I'm working on now. I've
structured it as follows. Note that this is *my* implementation;
others will certainly have their own approaches that differ in
various ways from this.

** DOMAIN OBJECT (Document in Doc-View, Model in MVC)

Domain objects handle DB persistence and can check
themselves for validity. They track their state
relative the their stored version (new, dirty, etc.),
and are cached for sharing to eliminate duplicate
instances.
   Domain objects know nothing about UI or presentation.
   The domain object is the Document.

** EDITOR (View in Doc-View, View+Controller in MVC)

An editor panel is responsible for all editing of the
objects of a given type. (Using a panel allows it to
be included easily in various contexts, including an
editor window for editing all objects of that type or
for editing selected groups of objects of that type.)

The editor panel contains:
- On the left, a selector (a ListBox, ListCtrl, or Grid),
   that displays all the items.
- On the right, a detail panel for editing details of
   a single object (often a Notebook with multiple pages
   for different logical groups of attributes of the object
   being edited, or for associating other objects).

The detail panel (or its pages, if a Notebook) has a
reference to the object being edited. It uses object
attribute validators and formatters to transfer and
translate values between the object and the UI
(http://wiki.wxpython.org/index.cgi/Validator_20for_20Object_20Attributes
and http://wiki.wxpython.org/index.cgi/DataFormatters).

When an item is selected in the selector, the editor
panel only switches if the data in the detail panel
is valid:
    if self.detail.Validate() and self.detail.TransferDataFromWindow():
       ...
If this test fails, the selector is set back to the
current object; if it succeeds, the new object is
assigned to the detail panel and the selector is
allowed to stay on the new item.

Note that if the detail panel is a Notebook with
multiple pages, each presenting some aspect of the
same object, then all pages must be Validated before
the data is transferred:
    if self.page1.Validate() \
          and self.page2.Validate() \
          and self.page3.Validate() \
          and self.page1.TransferDataFromWindow() \
          and self.page2.TransferDataFromWindow() \
          and self.page3.TransferDataFromWindow():
       ...

I hope this is helpful (and not more than you wanted to know :-/ ).

- Sam

···

At 2004-10-22 10:31 AM -0400, you wrote:

I already have some business objects that encapsulate the data and behavior of
the domain. In the UI, the user will be editing properties of these business
objects, but there may be multiple visible or hidden controls whose view is
affected by a change. For example, say we have a fairly standard set up: a
vertical splitter with a navigation tree on the left and a horizontal
splitter on the right with a list control on top and a properties panel
(multiple controls for editing an item's properties) on bottom.

Now, when the user clicks on the navigation control and then selects an item
in the list control, all of the property fields need to be populated based on
that item. As the user edits those properties, the changes need to be
reflected in the business object, and these changes may impact the
information displayed in the list control and in the navigation tree.

I know that some sort of model-view-controller/observer pattern works well in
this type of situation, but I'm wondering whether there's a standard way to
implement MVC with wxPython.

__________________________________________________________
Spinward Stars, LLC Samuel Reynolds
Software Consulting and Development 303-805-1446
http://SpinwardStars.com/ sam@SpinwardStars.com

>I know that some sort of model-view-controller/observer pattern works well
> in this type of situation, but I'm wondering whether there's a standard
> way to implement MVC with wxPython.

I don't know about a "standard" way.
I find, since the view and controller tend to be so closely
coupled, that the simpler Document-View pattern works well.

...

I hope this is helpful

Yes. Thanks for the lengthy reply. It spurred some more interesting reading
on my side.

(and not more than you wanted to know :-/ ).

When talking about software design and implementation, you'd be hard-pressed
to tell me more than I want to know. :wink:

---Tom

···

On Friday 22 October 2004 11:23 am, Samuel Reynolds wrote: