File: parameter_choice_editor.py

package info (click to toggle)
python-enthoughtbase 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 960 kB
  • ctags: 1,034
  • sloc: python: 6,104; makefile: 9; sh: 5
file content (151 lines) | stat: -rw-r--r-- 5,915 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
# 
# Author: Enthought, Inc.
# Description: <Enthought util package component>
#------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#
#  Define a new Choice editor
#
#  Written by: Lowell G. Vaughn
#
#  Date: 01/04/2005
#
#  Symbols defined: ParameterChoiceEditorFactory
#
#  (c) Copyright 2005 by Enthought, Inc.
#
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#  imports:
#-------------------------------------------------------------------------------

import wx

from enthought.traits.ui.wx.editor import Editor
from enthought.traits.ui.editor_factory import EditorFactory as WxEditorFactory
from enthought.traits.api import Any, HasTraits, Int

class ChoiceEditorModel(HasTraits):
    "An interface for model for use with the ChoiceEditorFactory"
    def __init__(self, object):
        pass

    def get_labels(self):
        return []

    def get_object(self, index):
        return None

    def index_of(self, obj):
        return 0

class ParameterChoiceEditorFactory (WxEditorFactory):
    #---------------------------------------------------------------------------
    #  Trait definitions:
    #---------------------------------------------------------------------------
    model_class = Any
    width=Int(100)

    def __init__(self, *args, **kwargs):
        #self.model_class = kwargs['model_class']
        # print self.model_class
        WxEditorFactory.__init__(self, *args, **kwargs)
        
    #---------------------------------------------------------------------------
    #  Performs any initialization needed after all constructor traits have 
    #  been set:
    #---------------------------------------------------------------------------
    def init(self, *args):
        pass 

    #---------------------------------------------------------------------------
    #  'Editor' factory methods:
    #---------------------------------------------------------------------------
    
    def simple_editor ( self, ui, object, name, description, parent ):
        model = self.model_class(object)
        # model.__init__(object)
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object,
                             model       = model,
                             name        = name, 
                             description = description,
                             width       = self.width ) 
    
    def custom_editor ( self, ui, object, name, description, parent ):
        model = self.model_class.__new__(self.model_class, object)
        model.__init__(object)
        return SimpleEditor( parent,
                             factory     = self, 
                             ui          = ui, 
                             object      = object,
                             model       = model,
                             name        = name, 
                             description = description,
                             width       = self.width ) 

                                      
#-------------------------------------------------------------------------------
#  'SimpleEditor' class:
#-------------------------------------------------------------------------------
                               
class SimpleEditor ( Editor ):
    model = Any
    width = Int(100)
        
#    def __init__(self, *args, **kwargs):
#        self.model = kwargs['model']
#        # print self.model
#        Editor.__init__(self, *args, **kwargs)
    #---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #---------------------------------------------------------------------------
        
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.control = wx.Choice( parent, -1, wx.Point( 0, 0 ), 
                                  wx.Size( self.width, 20 ), self.model.get_labels() )
        wx.EVT_CHOICE( parent, self.control.GetId(), self.update_object )
        self.update_editor()

    #---------------------------------------------------------------------------
    #  Handles the user selecting a new value from the combo box:
    #---------------------------------------------------------------------------
  
    def update_object ( self, event ):
        """ Handles the user selecting a new value from the combo box.
        """
        self.value = self.model.get_object(event.GetSelection())
        
    #---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #---------------------------------------------------------------------------
        
    def update_editor ( self ):
        """ Updates the editor when the object trait changes external to the 
            editor.
        """
        #print ("Update Editor")
        idx = self.model.index_of(self.value)
        try:
            self.control.SetSelection( idx )
        except:
            #print "Pass"
            pass