How to unsynchronize an editor trait and a user object trait using 'sync_value'?

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

How to unsynchronize an editor trait and a user object trait using 'sync_value'?

Jonathan Blakes
Hi all,

I am working on an improved File trait that can have a value that is
relative to a 'directory' set in its initialiser or that of its editor
(an improved Qt4 FileEditor which correctly restores the invalid state.)

I have also enabled a 'directory_name' Trait attribute/editor trait that
names a trait from which to obtain the directory value.

I am >this< close to getting my code to work but I just need to finish
setting up the syncing in the editor of the directory value with the
value pointed to by the directory_name trait.

The problem is that I can't seem to unsync the previous association
between my editor trait 'directory' and the value specified by the user
object trait resolve by 'directory_name' (see 'unsync here!? below).

# file_editor2.py ---

class SimpleEditor(SimpleTextEditor):

     directory = Directory(exists=True)
     directory_name = Str

     def _directory_name_changed(self, old, new):
        # was self.sync_value(s
         self.sync_value(old, 'directory', None) # <-- unsync here!?
         self.sync_value(new, 'directory', 'from') # resync

     def init(self, parent):
         factory = self.factory # FileEditor (below)

         if factory.directory_name:
             factory.sync_trait('directory_name', self)
         else:
             factory.sync_trait('directory', self)

        # ...

class FileEditor(enthought.traits.ui.api.FileEditor): # EditorFactory

     directory = Directory(exists=True)
     directory_name = Str(desc='...overrides directory trait')

     def _directory_default(self):
         # handles editor=FileEditor()
         return os.getcwd()

     def _get_simple_editor_class(self):
         return SimpleEditor
     def _get_custom_editor_class(self):
         return SimpleEditor

# end of file_editor2.py ---

Looking at the documentation for sync_value
(http://code.enthought.com/projects/files/ETS32_API/enthought.traits.ui.editor.Editor.html#sync_value)
it says it can unset the synchronization:

"sync_value(self, user_name, editor_name, mode = 'both', is_list = False)
Sets or unsets synchronization between an editor trait and a user object
trait."

but looking at the code for sync_value
(https://svn.enthought.com/enthought/browser/Traits/trunk/enthought/traits/ui/editor.py?line#450)
I don't see if this is done or what arguments are needed to make it happen.

Any ideas?

Jon

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

Re: How to unsynchronize an editor trait and a user object trait using 'sync_value'?

bryce hendrix-2
Jonathan Blakes wrote:

> Hi all,
>
> I am working on an improved File trait that can have a value that is
> relative to a 'directory' set in its initialiser or that of its editor
> (an improved Qt4 FileEditor which correctly restores the invalid state.)
>
> I have also enabled a 'directory_name' Trait attribute/editor trait that
> names a trait from which to obtain the directory value.
>
> I am >this< close to getting my code to work but I just need to finish
> setting up the syncing in the editor of the directory value with the
> value pointed to by the directory_name trait.
>
> The problem is that I can't seem to unsync the previous association
> between my editor trait 'directory' and the value specified by the user
> object trait resolve by 'directory_name' (see 'unsync here!? below).
>
> # file_editor2.py ---
>
> class SimpleEditor(SimpleTextEditor):
>
>      directory = Directory(exists=True)
>      directory_name = Str
>
>      def _directory_name_changed(self, old, new):
> # was self.sync_value(s
>          self.sync_value(old, 'directory', None) # <-- unsync here!?
>          self.sync_value(new, 'directory', 'from') # resync
>
>      def init(self, parent):
>          factory = self.factory # FileEditor (below)
>
>          if factory.directory_name:
>              factory.sync_trait('directory_name', self)
>          else:
>              factory.sync_trait('directory', self)
>
> # ...
>
> class FileEditor(enthought.traits.ui.api.FileEditor): # EditorFactory
>
>      directory = Directory(exists=True)
>      directory_name = Str(desc='...overrides directory trait')
>
>      def _directory_default(self):
>          # handles editor=FileEditor()
>          return os.getcwd()
>
>      def _get_simple_editor_class(self):
>          return SimpleEditor
>      def _get_custom_editor_class(self):
>          return SimpleEditor
>
> # end of file_editor2.py ---
>
> Looking at the documentation for sync_value
> (http://code.enthought.com/projects/files/ETS32_API/enthought.traits.ui.editor.Editor.html#sync_value)
> it says it can unset the synchronization:
>
> "sync_value(self, user_name, editor_name, mode = 'both', is_list = False)
> Sets or unsets synchronization between an editor trait and a user object
> trait."
>
> but looking at the code for sync_value
> (https://svn.enthought.com/enthought/browser/Traits/trunk/enthought/traits/ui/editor.py?line#450)
> I don't see if this is done or what arguments are needed to make it happen.
>
> Any ideas?
>
>  

Jon,

I've had problems in the past using sync_value in similar ways. I found
it easier/faster to refactor my code. Perhaps you can use properties
instead? If not, I guess we'll finally have to break down & look for the
root cause...

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