Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Package textpatterncontrol :: Module controller
[frames] | no frames]

Source Code for Module Gnumed.timelinelib.wxgui.components.textpatterncontrol.controller

  1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg 
  2  # 
  3  # This file is part of Timeline. 
  4  # 
  5  # Timeline is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Timeline is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>. 
 17   
 18   
 19  import humblewx 
 20  import wx 
 21   
 22   
23 -class TextPatternControlController(humblewx.Controller):
24
25 - def on_init(self):
26 self.separators = [] 27 self.last_selected_group = None 28 self.validator = None 29 self.up_handlers = {} 30 self.down_handlers = {}
31
32 - def on_after_set_focus(self):
33 if self.view.GetSelection() != (0, len(self.view.GetValue())): 34 return 35 elif self.last_selected_group is None: 36 self._select_group(self.get_selected_group()) 37 else: 38 self._select_group(self.last_selected_group)
39
40 - def on_kill_focus(self, event):
41 self.last_selected_group = self.get_selected_group() 42 self.view.SetSelection(0, 0)
43
44 - def on_text(self, event):
45 self.validate()
46
47 - def on_char(self, event):
48 skip = True 49 if event.GetKeyCode() == wx.WXK_TAB: 50 if event.ShiftDown(): 51 skip = self.on_shift_tab() 52 flag = wx.NavigationKeyEvent.IsBackward 53 else: 54 skip = self.on_tab() 55 flag = wx.NavigationKeyEvent.IsForward 56 if skip: 57 event.EventObject.Navigate(flags=flag) 58 skip = False 59 elif (event.GetKeyCode() == wx.WXK_UP and 60 self.view.GetSelectedGroup() in self.up_handlers and 61 self._is_text_valid()): 62 self.up_handlers[self.view.GetSelectedGroup()]() 63 skip = False 64 elif (event.GetKeyCode() == wx.WXK_DOWN and 65 self.view.GetSelectedGroup() in self.down_handlers and 66 self._is_text_valid()): 67 self.down_handlers[self.view.GetSelectedGroup()]() 68 skip = False 69 event.Skip(skip)
70
71 - def on_tab(self):
72 return not self._select_group(self.get_selected_group() + 1)
73
74 - def on_shift_tab(self):
75 return not self._select_group(self.get_selected_group() - 1)
76
77 - def get_parts(self):
78 if self._get_groups() is not None: 79 return [value for (value, _, _) in self._get_groups()] 80 return None
81
82 - def get_selected_group(self):
83 (selection_start, _) = self.view.GetSelection() 84 if self._get_groups() is not None: 85 for (index, (_, start, end)) in enumerate(self._get_groups()): 86 if selection_start >= start and selection_start <= end: 87 return index 88 return 0
89
90 - def set_separators(self, separators):
91 self.separators = separators 92 self.validate()
93
94 - def set_parts(self, parts):
95 (start, end) = self.view.GetSelection() 96 text = "" 97 for (index, value) in enumerate(parts): 98 if index > 0: 99 text += self.separators[index-1] 100 text += value 101 self.view.SetValue(text) 102 self.validate() 103 self.view.SetSelection(start, end)
104
105 - def set_validator(self, validator):
106 self.validator = validator 107 self.validate()
108
109 - def set_up_handler(self, group, up_handler):
110 self.up_handlers[group] = up_handler
111
112 - def set_down_handler(self, group, down_handler):
113 self.down_handlers[group] = down_handler
114
115 - def validate(self):
116 if self._is_text_valid(): 117 self.view.SetBackgroundColour(wx.NullColour) 118 else: 119 self.view.SetBackgroundColour("pink") 120 self.view.Refresh()
121
122 - def _get_groups(self):
123 text = self.view.GetValue() 124 groups = [] 125 start = 0 126 for separator in self.separators: 127 separator_pos = text[start:].find(separator) 128 if separator_pos == -1: 129 return None 130 groups.append(self._extract_section(start, start+separator_pos)) 131 start += separator_pos + len(separator) 132 groups.append(self._extract_section(start, len(text))) 133 return groups
134
135 - def _extract_section(self, start, end):
136 return (self.view.GetValue()[start:end], start, end)
137
138 - def _is_text_valid(self):
139 if self.get_parts() is None: 140 return False 141 elif self.validator is None: 142 return True 143 else: 144 return self.validator()
145
146 - def _select_group(self, section_to_focus):
147 if self._get_groups() is not None: 148 for (index, (_, start, end)) in enumerate(self._get_groups()): 149 if index == section_to_focus: 150 self.view.SetSelection(start, end) 151 return True 152 return False
153