Package gui :: Package components :: Module combo_list
[hide private]
[frames] | no frames]

Source Code for Module gui.components.combo_list

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2011 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """The combo list GUI element.""" 
 25   
 26  # Python module imports. 
 27  import wx 
 28   
 29  # relax GUI module imports. 
 30  from gui.paths import icon_16x16 
 31   
 32   
33 -class Combo_list:
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 # Store some args. 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 # Init. 74 self._main_sizer = wx.BoxSizer(wx.VERTICAL) 75 self._combo_boxes = [] 76 self._sub_sizers = [] 77 78 # The divider. 79 if not divider: 80 self._divider = self._parent._div_left 81 else: 82 self._divider = divider 83 84 # Build the first rows. 85 for i in range(n): 86 self._build_row() 87 88 # Add the main sizer. 89 self._sizer.Add(self._main_sizer, 0, wx.EXPAND|wx.ALL, 0) 90 91 # Spacing below the widget. 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 # Add another row. 106 self._build_row() 107 108 # Re-perform the window layout. 109 self._parent.Layout()
110 111
112 - def _build_row(self, text=None):
113 """Construct a row of the GUI element. 114 115 @param text: The text description of the 116 """ 117 118 # Init. 119 sub_sizer = wx.BoxSizer(wx.HORIZONTAL) 120 index = len(self._combo_boxes) 121 122 # Left padding. 123 sub_sizer.AddSpacer(self._padding) 124 125 # The description. 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 # Spacing. 131 x, y = text.GetSize() 132 sub_sizer.AddSpacer((self._divider - x, 0)) 133 134 # No description for other rows, so add a blank space. 135 else: 136 sub_sizer.AddSpacer((self._divider, 0)) 137 138 # The combo box element. 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 # The add button. 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 # The delete button. 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 # Otherwise empty spacing. 162 else: 163 sub_sizer.AddSpacer((27, 0)) 164 165 # Right padding. 166 sub_sizer.AddSpacer(self._padding) 167 168 # Add to the main sizer. 169 self._sub_sizers.append(sub_sizer) 170 self._main_sizer.Add(sub_sizer, 1, wx.EXPAND|wx.ALL, 0) 171 172 # Bind events. 173 if self._evt_fn: 174 self._parent.Bind(wx.EVT_COMBOBOX, self._evt_fn, combo) 175 176 # Tooltip. 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
185 - def _delete(self, event):
186 """Add a new combo box. 187 188 @param event: The wx event. 189 @type event: wx event 190 """ 191 192 # Remove the combo box element from the list. 193 self._combo_boxes.pop() 194 195 # Destroy the subsizer. 196 sub_sizer = self._sub_sizers.pop() 197 sub_sizer.DeleteWindows() 198 self._main_sizer.Remove(sub_sizer) 199 200 # Re-perform the window layout. 201 self._parent.Layout()
202 203
204 - def GetValue(self):
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 # Build the string form of the list. 212 text = u'[' 213 214 # Loop over the combo boxes. 215 for i in range(len(self._combo_boxes)): 216 # Get the value. 217 val = self._combo_boxes[i].GetValue() 218 219 # Nothing, so skip. 220 if not len(val): 221 continue 222 223 # Add a comma. 224 if len(text) > 1: 225 text = "%s, " % text 226 227 # Add the value. 228 text = "%s'%s'" % (text, val) 229 230 # End. 231 text = "%s]" % text 232 233 # Return the list. 234 return text
235