Delegating from an Enum trait into an Enaml form

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Delegating from an Enum trait into an Enaml form

Jaidev Deshpande
Hello,

I have this class:

from traits.api import HasTraits, Enum
class Camera(HasTraits):
    gain = Enum(1,2,3)
mycam = Camera()


And the following enaml file

enamldef Main(MainWindow):
    attr camera
    Form:
        Label:
            text = 'Gain'
        Field:
            value := ???

What should occupy the question marks? I could make this work with
ComboBox as follows:

Label:
    text = 'Gain'
ComboBox:
    value := camera.gain
    items << list(camera.trait('gain').handler.values)

But this seems a very bad way of doing it, as the enumerated values
should straightaway be taken from the gain trait.

Thanks
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Chris Colbert
What are you trying to do:

value := camera.gain

would work, but you would get traits exceptions if the user types in the wrong value. 

Enaml is not as tightly integrated with Traits as TraitsUI is, on purpose. We found that tightly coupling the two actual limits some flexibility that is desired in more complex UI. 

Instead of relying on the model to validated user input (which is bad), Enaml introduces the concept of validators to validate the user's input before submitting the change to the model.

To use a validator, you must create a concrete implementation of enaml.validation.abstract_validator.AbstractValidator and assign an instance to Field.validator

There are some pre-defined validators for common things in the enaml.validation directory, but none which validate an enum-style list of choices. It would be trivial to write one on your own however.

See the employee example for a custom validator which validates a phone number input.

Hope that helps,

Chris

On Thu, Jun 14, 2012 at 7:26 AM, Jaidev Deshpande <[hidden email]> wrote:
Hello,

I have this class:

from traits.api import HasTraits, Enum
class Camera(HasTraits):
   gain = Enum(1,2,3)
mycam = Camera()


And the following enaml file

enamldef Main(MainWindow):
   attr camera
   Form:
       Label:
           text = 'Gain'
       Field:
           value := ???

What should occupy the question marks? I could make this work with
ComboBox as follows:

Label:
   text = 'Gain'
ComboBox:
   value := camera.gain
   items << list(camera.trait('gain').handler.values)

But this seems a very bad way of doing it, as the enumerated values
should straightaway be taken from the gain trait.

Thanks
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Martin Chilvers
G'day Chris and All!

On 17/06/2012 23:25, Chris Colbert wrote:
> Instead of relying on the model to validated user input (which is bad),
> Enaml introduces the concept of validators to validate the user's input
> before submitting the change to the model.

Yes there are times when you need more control over the validation than
the model provides, but the danger with validators is that business
logic creeps insidiously into the UI layer. A common pattern that we've
used in the past is to do the validation in the model directly when that
is appropriate, and use a Presenter (kind of like the one in the MVP
pattern) that handles the more interesting validation requirments. Of
course, the presenter couls be seen as just a collection of validators,
but for some reason, I think it encourages developers to only include
the validation rules needed for successful presentation.

As usual, just my £0.0127477851 worth!

Martin
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Chris Colbert


On Mon, Jun 18, 2012 at 7:17 AM, Martin Chilvers <[hidden email]> wrote:
G'day Chris and All!


On 17/06/2012 23:25, Chris Colbert wrote:
Instead of relying on the model to validated user input (which is bad),
Enaml introduces the concept of validators to validate the user's input
before submitting the change to the model.

Yes there are times when you need more control over the validation than the model provides, but the danger with validators is that business logic creeps insidiously into the UI layer. A common pattern that we've used in the past is to do the validation in the model directly when that is appropriate, and use a Presenter (kind of like the one in the MVP pattern) that handles the more interesting validation requirments. Of course, the presenter couls be seen as just a collection of validators, but for some reason, I think it encourages developers to only include the validation rules needed for successful presentation.

As usual, just my £0.0127477851 worth!

Martin

Of course. But what you mention, and what I mentioned, are not mutually exclusive. Enaml simply provides a formal way of separating validation from the underlying Traits type validators, since not everyone using Enaml will be using Traits (a key point!). There is nothing preventing the develop by defining the Enaml validator instance as an attribute of a ModelView-ish class.

Chris


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Jaidev Deshpande
In reply to this post by Chris Colbert
Hi,


On Mon, Jun 18, 2012 at 3:55 AM, Chris Colbert <[hidden email]> wrote:

> What are you trying to do:
>
> value := camera.gain
>
> would work, but you would get traits exceptions if the user types in the
> wrong value.
>
> Enaml is not as tightly integrated with Traits as TraitsUI is, on purpose.
> We found that tightly coupling the two actual limits some flexibility that
> is desired in more complex UI.

What I was trying to achieve, primarily, was to find the right widgets
in enaml, corresponding to the kind of widgets we would expect to see
in a TraitsUI. An Enum trait would produce a dropdown box widget in a
traitsUI. What is the corresponding widget in enaml? The validation
can be performed independent of the visualization, right?

Thanks
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Chris Colbert
On Tue, Jun 19, 2012 at 4:41 PM, Jaidev Deshpande <[hidden email]> wrote:
Hi,


On Mon, Jun 18, 2012 at 3:55 AM, Chris Colbert <[hidden email]> wrote:
> What are you trying to do:
>
> value := camera.gain
>
> would work, but you would get traits exceptions if the user types in the
> wrong value.
>
> Enaml is not as tightly integrated with Traits as TraitsUI is, on purpose.
> We found that tightly coupling the two actual limits some flexibility that
> is desired in more complex UI.

What I was trying to achieve, primarily, was to find the right widgets
in enaml, corresponding to the kind of widgets we would expect to see
in a TraitsUI. An Enum trait would produce a dropdown box widget in a
traitsUI. What is the corresponding widget in enaml? The validation
can be performed independent of the visualization, right?

Thanks

Use a ComboBox for a drop-down list.

Since the items are provided ahead of time, there is no need for validation in a ComboBox.
 

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Warren Weckesser


On Tue, Jun 19, 2012 at 4:31 PM, Chris Colbert <[hidden email]> wrote:
On Tue, Jun 19, 2012 at 4:41 PM, Jaidev Deshpande <[hidden email]> wrote:
Hi,


On Mon, Jun 18, 2012 at 3:55 AM, Chris Colbert <[hidden email]> wrote:
> What are you trying to do:
>
> value := camera.gain
>
> would work, but you would get traits exceptions if the user types in the
> wrong value.
>
> Enaml is not as tightly integrated with Traits as TraitsUI is, on purpose.
> We found that tightly coupling the two actual limits some flexibility that
> is desired in more complex UI.

What I was trying to achieve, primarily, was to find the right widgets
in enaml, corresponding to the kind of widgets we would expect to see
in a TraitsUI. An Enum trait would produce a dropdown box widget in a
traitsUI. What is the corresponding widget in enaml? The validation
can be performed independent of the visualization, right?

Thanks

Use a ComboBox for a drop-down list.

Since the items are provided ahead of time, there is no need for validation in a ComboBox.


A true ComboBox has both a dropdown menu and an editable text field, so it could require validation.

Warren


 
 

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Delegating from an Enum trait into an Enaml form

Chris Colbert


On Tue, Jun 19, 2012 at 5:38 PM, Warren Weckesser <[hidden email]> wrote:


On Tue, Jun 19, 2012 at 4:31 PM, Chris Colbert <[hidden email]> wrote:
On Tue, Jun 19, 2012 at 4:41 PM, Jaidev Deshpande <[hidden email]> wrote:
Hi,


On Mon, Jun 18, 2012 at 3:55 AM, Chris Colbert <[hidden email]> wrote:
> What are you trying to do:
>
> value := camera.gain
>
> would work, but you would get traits exceptions if the user types in the
> wrong value.
>
> Enaml is not as tightly integrated with Traits as TraitsUI is, on purpose.
> We found that tightly coupling the two actual limits some flexibility that
> is desired in more complex UI.

What I was trying to achieve, primarily, was to find the right widgets
in enaml, corresponding to the kind of widgets we would expect to see
in a TraitsUI. An Enum trait would produce a dropdown box widget in a
traitsUI. What is the corresponding widget in enaml? The validation
can be performed independent of the visualization, right?

Thanks

Use a ComboBox for a drop-down list.

Since the items are provided ahead of time, there is no need for validation in a ComboBox.


A true ComboBox has both a dropdown menu and an editable text field, so it could require validation.


Editable combo boxes are a completely different widget, which Enaml does not yet support, since it typically comes with auto-completion, for which we have yet to define an API. 
 

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev