1 import wx 2 3 BG_COLOR = 0xe0, 0xe0, 0xe0 4 FG_COLOR = 0x20, 0x20, 0x20 5 6 LABEL_ARGS = dict() # Arguments used for wx.StaticText 7 CTRL_ARGS = dict() # Arguments used for wx.<Control> 8 9 try: 10 import poser 11 except ImportError: 12 from PoserLibs import POSER_FAKE as poser 13 14 aui = poser.WxAuiManager() 15 root = aui.GetManagedWindow() 16 SYS_FONT = root.GetFont() 17 CURRENT_GRB_MGR = None 18 GLOBAL_PARENTWINDOW = None 19 20 21 class GridBagManager(object): 22 __slots__ = "parent", "widgets", "sizer", "flag", "border", "current_row", "font" 23 24 def __init__(self, *args, **kwargs): 25 global CURRENT_GRB_MGR, GLOBAL_PARENTWINDOW 26 27 for varname in self.__slots__: 28 setattr(self, varname, kwargs.get(varname, None)) 29 30 for idx, entry in enumerate(args): 31 if self.__slots__[idx] not in kwargs: 32 setattr(self, self.__slots__[idx], entry) 33 34 self.current_row = 0 35 self.widgets = [] 36 CURRENT_GRB_MGR = self 37 GLOBAL_PARENTWINDOW = self.parent 38 39 def addrow(self, *args, **kwargs): 40 row = int(kwargs.get("row", self.current_row)) 41 col = int(kwargs.get("startcol", 0)) 42 flag = kwargs.get("flag", self.flag) 43 font = kwargs.pop("font", self.font) 44 sz = self.sizer # type: wx.GridBagSizer 45 for idx, widget in enumerate(args): 46 if font: 47 widget.SetFont(font) 48 49 if widget is None: 50 sz.AddGrowableRow(row) 51 else: 52 self.sizer.Add(widget, pos=(row, col + idx), 53 flag=flag, 54 border=self.border) 55 if widget.__class__ != wx.StaticText: 56 self.widgets.append(widget) 57 58 self.current_row += 1 59 60 def get_items_by_name(self, name, filter=None): 61 if not filter: 62 return [w for w in self.widgets if w.GetName().startswith(name)] 63 return [w for w in self.widgets if w.GetName().startswith(name) 64 and w.__class__.__name__ in filter] 65 66 def get_value_by_name(self, name, filter=None): 67 obj = self.get_items_by_name(name, filter) 68 if not obj: 69 return None 70 if len(obj) == 1: 71 return obj[0].Value() 72 return [o.Value() for o in obj] 73 74 def items_as_dict(self, filter=None): 75 d = dict() 76 for widget in [w for w in self.widgets 77 if filter is None or w.__class__.__name__ in filter]: 78 if hasattr(widget, "GetCheckedStrings"): 79 d[widget.GetName()] = widget.GetCheckedStrings() 80 elif hasattr(widget, "GetValue"): 81 d[widget.GetName()] = widget.GetValue() 82 elif hasattr(widget, "GetSelection"): 83 v = widget.GetSelection() 84 if hasattr(widget, "GetString"): 85 v = widget.GetString(v) 86 d[widget.GetName()] = v 87 88 return d 89 90 def addrows(self, *rows2add): 91 for row in rows2add: 92 if row is None: 93 self.addrow(None) 94 else: 95 self.addrow(*row) 96 97 def setStyles(self, ctrltype, **kwargs): 98 for c in self.widgets: 99 if c.__class__ is ctrltype: 100 c.SetWindowStyle(**kwargs) 101 102 def bindCtrls(self, ctrltype, ev, target): 103 for c in self.widgets: 104 if c.__class__ is ctrltype: 105 c.Bind(ev, target) 106 107 108 def setTooltip(ctrl, text): 109 if text and ctrl: 110 t = wx.ToolTip(text) 111 t.SetAutoPop(5000) 112 ctrl.SetToolTip(t) 113 return ctrl 114 115 116 def ColoredCtrl(ctrl, **kwargs): 117 if "bgcolor" in kwargs: 118 ctrl.SetBackgroundColour(kwargs.pop("bgcolor", BG_COLOR)) 119 if "fgcolor" in kwargs: 120 ctrl.SetForegroundColour(kwargs.pop("fgcolor", FG_COLOR)) 121 return setTooltip(ctrl, kwargs.pop("tooltip", None)) 122 123 124 def LabelCtrl(**kwargs): 125 parent = GLOBAL_PARENTWINDOW 126 bg_color = kwargs.pop("bgcolor", BG_COLOR) 127 fg_color = kwargs.pop("fgcolor", FG_COLOR) 128 font = kwargs.pop("font", SYS_FONT) 129 tooltip = kwargs.pop("tooltip", None) 130 ctrl = wx.StaticText(parent, **kwargs) 131 ctrl.SetFont(font) 132 ctrl.SetBackgroundColour(bg_color) 133 ctrl.SetForegroundColour(fg_color) 134 return setTooltip(ctrl, tooltip) 135 136 137 def PrepCtrl(ctrl, **kwargs): 138 parent = GLOBAL_PARENTWINDOW 139 bind = kwargs.pop("bind", []) 140 fgcolor = kwargs.pop("fgcolor", FG_COLOR) 141 bgcolor = kwargs.pop("bgcolor", BG_COLOR) 142 font = kwargs.pop("font", None) 143 value = kwargs.pop("value", None) 144 index = kwargs.pop("index", None) 145 tooltip = kwargs.pop("tooltip", None) 146 if "key" in kwargs: 147 kwargs["name"] = kwargs["key"] 148 kwargs.pop("key") 149 ctrl = ctrl(parent, **kwargs) 150 151 if bind: 152 if isinstance(bind[0], (tuple, list)): 153 for bind_entry in bind: 154 assert len(bind_entry) == 2 and callable(bind[1]), \ 155 "Bind entries must be: int(EVT_ID), func<callable>" 156 ctrl.Bind(bind_entry[0], bind_entry[1]) 157 else: 158 assert len(bind) == 2 and callable(bind[1]), \ 159 "Bind entries must be: int(EVT_ID), func<callable>" 160 ctrl.Bind(bind[0], bind[1]) 161 162 ctrl.SetForegroundColour(fgcolor) 163 ctrl.SetBackgroundColour(bgcolor) 164 if font: 165 ctrl.SetFont(font) 166 if value: 167 ctrl.SetValue(value) 168 if index: 169 ctrl.SetSelection(index) 170 return setTooltip(ctrl, tooltip) 171 172 173 def Labeled(ctrl, tooltip=None, **kwargs): 174 label_args = LABEL_ARGS.copy() 175 tx = kwargs.pop("text", kwargs.pop("label", "")) 176 if tx: 177 label_args["label"] = tx 178 ctrl_args = CTRL_ARGS.copy() 179 for k in kwargs.keys(): 180 if k.startswith("lbl_"): 181 label_args[k[4:]] = kwargs.pop(k) 182 elif k.startswith("ctl_"): 183 ctrl_args[k[4:]] = kwargs.pop(k) 184 ctrl_args.update(kwargs) 185 186 return (setTooltip(LabelCtrl(**label_args), tooltip), 187 setTooltip(PrepCtrl(ctrl=ctrl, **ctrl_args), tooltip)) 188 189 190 def ButtonRow(*buttons, **sizerflags): 191 parent = GLOBAL_PARENTWINDOW 192 sz = wx.BoxSizer(wx.LI_HORIZONTAL) 193 for btn in buttons: 194 if isinstance(btn, basestring): 195 btn = wx.Button(parent, -1, btn) 196 sz.Add(btn, **sizerflags) 197 198 sz.Layout() 199 return sz 200