| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
Supporting Model/View/Controller (MVC) pattern |
| 13 |
|
| 14 |
Demonstrates one approach to writing Model/View/Controller (MVC)-based |
| 15 |
applications using Traits UI. |
| 16 |
|
| 17 |
This example contains a trivial model containing only one data object, the |
| 18 |
string 'myname'. |
| 19 |
|
| 20 |
In this example, the Controller contains the View. A more rigorous example |
| 21 |
would separate these. |
| 22 |
|
| 23 |
A few key points: |
| 24 |
|
| 25 |
- the Controller itself accesses the model as self.model |
| 26 |
- the Controller's View can access model traits directly ('myname') |
| 27 |
""" |
| 28 |
|
| 29 |
from traits.api import HasTraits, Str, Bool, TraitError |
| 30 |
from traitsui.api import View, VGroup, HGroup, Item, Controller |
| 31 |
|
| 32 |
|
| 33 |
class MyModel(HasTraits): |
| 34 |
"""Define a simple model containing a single string, 'myname'""" |
| 35 |
|
| 36 |
# Simple model data: |
| 37 |
myname = Str() |
| 38 |
|
| 39 |
|
| 40 |
class MyViewController(Controller): |
| 41 |
"""Define a combined controller/view class that validates that |
| 42 |
MyModel.myname is consistent with the 'allow_empty_string' flag. |
| 43 |
""" |
| 44 |
|
| 45 |
# When False, the model.myname trait is not allowed to be empty: |
| 46 |
allow_empty_string = Bool() |
| 47 |
|
| 48 |
# Last attempted value of model.myname to be set by user: |
| 49 |
last_name = Str() |
| 50 |
|
| 51 |
# Define the view associated with this controller: |
| 52 |
view = View( |
| 53 |
VGroup( |
| 54 |
HGroup( |
| 55 |
Item('myname', springy=True), |
| 56 |
'10', |
| 57 |
Item('controller.allow_empty_string', label='Allow Empty'), |
| 58 |
), |
| 59 |
# Add an empty vertical group so the above items don't end up |
| 60 |
# centered vertically: |
| 61 |
VGroup(), |
| 62 |
), |
| 63 |
resizable=True, |
| 64 |
) |
| 65 |
|
| 66 |
# -- Handler Interface ---------------------------------------------------- |
| 67 |
|
| 68 |
def myname_setattr(self, info, object, traitname, value): |
| 69 |
"""Validate the request to change the named trait on object to the |
| 70 |
specified value. Validation errors raise TraitError, which by |
| 71 |
default causes the editor's entry field to be shown in red. |
| 72 |
(This is a specially named method <model trait name>_setattr, |
| 73 |
which is available inside a Controller.) |
| 74 |
""" |
| 75 |
self.last_name = value |
| 76 |
if (not self.allow_empty_string) and (value.strip() == ''): |
| 77 |
raise TraitError('Empty string not allowed.') |
| 78 |
|
| 79 |
return super().setattr(info, object, traitname, value) |
| 80 |
|
| 81 |
# -- Event handlers ------------------------------------------------------- |
| 82 |
|
| 83 |
def controller_allow_empty_string_changed(self, info): |
| 84 |
"""'allow_empty_string' has changed, check the myname trait to ensure |
| 85 |
that it is consistent with the current setting. |
| 86 |
""" |
| 87 |
if (not self.allow_empty_string) and (self.model.myname == ''): |
| 88 |
self.model.myname = '?' |
| 89 |
else: |
| 90 |
self.model.myname = self.last_name |
| 91 |
|
| 92 |
|
| 93 |
# Create the model and (demo) view/controller: |
| 94 |
demo = MyViewController(MyModel()) |
| 95 |
|
| 96 |
# Run the demo (if invoked from the command line): |
| 97 |
if __name__ == '__main__': |
| 98 |
demo.configure_traits() |