1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23   
 24  """The combo list GUI element.""" 
 25   
 26   
 27  import wx 
 28   
 29   
 30  from gui.paths import icon_16x16 
 31   
 32   
 34      """The combo list GUI element.""" 
 35   
 36 -    def __init__(self, parent, sizer, desc, n=1, choices=[], evt_fn=None, tooltip=None, divider=None, padding=0, spacer=None, read_only=True): 
  37          """Build the combo box list widget for a list of list selections. 
 38   
 39          @param parent:      The parent GUI element. 
 40          @type parent:       wx object instance 
 41          @param sizer:       The sizer to put the combo box widget into. 
 42          @type sizer:        wx.Sizer instance 
 43          @param desc:        The text description. 
 44          @type desc:         str 
 45          @keyword n:         The number of initial entries. 
 46          @type n:            int 
 47          @keyword choices:   The list of choices (all combo boxes will have the same list). 
 48          @type choices:      list of str 
 49          @keyword evt_fn:    The event handling function. 
 50          @type evt_fn:       func 
 51          @keyword tooltip:   The tooltip which appears on hovering over the text or input field. 
 52          @type tooltip:      str 
 53          @keyword divider:   The optional position of the divider.  If None, the parent class variable _div_left will be used if present. 
 54          @type divider:      None or int 
 55          @keyword padding:   Spacing to the left and right of the widgets. 
 56          @type padding:      int 
 57          @keyword spacer:    The amount of spacing to add below the field in pixels.  If None, a stretchable spacer will be used. 
 58          @type spacer:       None or int 
 59          @keyword read_only: A flag which if True means that text cannot be typed into the combo box widget. 
 60          @type read_only:    bool 
 61          """ 
 62   
 63           
 64          self._parent = parent 
 65          self._sizer = sizer 
 66          self._desc = desc 
 67          self._choices = choices 
 68          self._evt_fn = evt_fn 
 69          self._tooltip = tooltip 
 70          self._padding = padding 
 71          self._read_only = read_only 
 72   
 73           
 74          self._main_sizer = wx.BoxSizer(wx.VERTICAL) 
 75          self._combo_boxes = [] 
 76          self._sub_sizers = [] 
 77   
 78           
 79          if not divider: 
 80              self._divider = self._parent._div_left 
 81          else: 
 82              self._divider = divider 
 83   
 84           
 85          for i in range(n): 
 86              self._build_row() 
 87   
 88           
 89          self._sizer.Add(self._main_sizer, 0, wx.EXPAND|wx.ALL, 0) 
 90   
 91           
 92          if spacer == None: 
 93              self._sizer.AddStretchSpacer() 
 94          else: 
 95              self._sizer.AddSpacer(spacer) 
  96   
 97   
 98 -    def _add(self, event): 
  99          """Add a new combo box. 
100   
101          @param event:   The wx event. 
102          @type event:    wx event 
103          """ 
104   
105           
106          self._build_row() 
107   
108           
109          self._parent.Layout() 
 110   
111   
113          """Construct a row of the GUI element. 
114   
115          @param text:    The text description of the  
116          """ 
117   
118           
119          sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 
120          index = len(self._combo_boxes) 
121   
122           
123          sub_sizer.AddSpacer(self._padding) 
124   
125           
126          if index == 0: 
127              text = wx.StaticText(self._parent, -1, self._desc, style=wx.ALIGN_LEFT) 
128              sub_sizer.Add(text, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 0) 
129   
130               
131              x, y = text.GetSize() 
132              sub_sizer.AddSpacer((self._divider - x, 0)) 
133   
134           
135          else: 
136              sub_sizer.AddSpacer((self._divider, 0)) 
137   
138           
139          style = wx.CB_DROPDOWN 
140          if self._read_only: 
141              style = style | wx.CB_READONLY 
142          combo = wx.ComboBox(self._parent, -1, value='', style=style, choices=self._choices) 
143          combo.SetMinSize((50, 27)) 
144          sub_sizer.Add(combo, 1, wx.ALIGN_CENTER_VERTICAL, 0) 
145          self._combo_boxes.append(combo) 
146   
147           
148          if index == 0: 
149              button = wx.BitmapButton(self._parent, -1, wx.Bitmap(icon_16x16.add, wx.BITMAP_TYPE_ANY)) 
150              button.SetMinSize((27, 27)) 
151              sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 
152              self._parent.Bind(wx.EVT_BUTTON, self._add, button) 
153   
154           
155          elif index == 1: 
156              button = wx.BitmapButton(self._parent, -1, wx.Bitmap(icon_16x16.remove, wx.BITMAP_TYPE_ANY)) 
157              button.SetMinSize((27, 27)) 
158              sub_sizer.Add(button, 0, wx.ADJUST_MINSIZE|wx.ALIGN_CENTER_VERTICAL, 0) 
159              self._parent.Bind(wx.EVT_BUTTON, self._delete, button) 
160   
161           
162          else: 
163              sub_sizer.AddSpacer((27, 0)) 
164   
165           
166          sub_sizer.AddSpacer(self._padding) 
167   
168           
169          self._sub_sizers.append(sub_sizer) 
170          self._main_sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 
171   
172           
173          if self._evt_fn: 
174              self._parent.Bind(wx.EVT_COMBOBOX, self._evt_fn, combo) 
175   
176           
177          if self._tooltip: 
178              if index == 0: 
179                  text.SetToolTipString(self._tooltip) 
180              combo.SetToolTipString(self._tooltip) 
181              if index <= 1: 
182                  button.SetToolTipString(self._tooltip) 
 183   
184   
186          """Add a new combo box. 
187   
188          @param event:   The wx event. 
189          @type event:    wx event 
190          """ 
191   
192           
193          self._combo_boxes.pop() 
194   
195           
196          sub_sizer = self._sub_sizers.pop() 
197          sub_sizer.DeleteWindows() 
198          self._main_sizer.Remove(sub_sizer) 
199   
200           
201          self._parent.Layout() 
 202   
203   
205          """Return the value represented by this GUI element. 
206   
207          @return:    The list of choices as a GUI string. 
208          @rtype:     unicode 
209          """ 
210   
211           
212          text = u'[' 
213   
214           
215          for i in range(len(self._combo_boxes)): 
216               
217              val = self._combo_boxes[i].GetValue() 
218   
219               
220              if not len(val): 
221                  continue 
222   
223               
224              if len(text) > 1: 
225                  text = "%s, " % text 
226   
227               
228              text = "%s'%s'" % (text, val) 
229   
230           
231          text = "%s]" % text 
232   
233           
234          return text 
  235