Package libxyz :: Package ui :: Module entry
[hide private]
[frames] | no frames]

Source Code for Module libxyz.ui.entry

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander 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 Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  import traceback 
 18   
 19  from libxyz.core.utils import ustring 
 20  from libxyz.ui import lowui 
 21  from libxyz.vfs import VFSObject 
 22   
 23  import libxyz.ui 
 24   
25 -class ListEntry(lowui.FlowWidget):
26 """ 27 List entry 28 """ 29
30 - def __init__(self, msg, selected_attr, entry_attr=None):
31 """ 32 @param msg: Message 33 @param selected_attr: Atrribute of selected entry 34 @param entry_attr: Entry text attribute 35 """ 36 37 super(ListEntry, self).__init__() 38 39 self._text = msg 40 self._sel_attr = selected_attr 41 self._entry_attr = entry_attr 42 self._content = lowui.Text(self._text)
43 44 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 45
46 - def selectable(self):
47 return True
48 49 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 50
51 - def rows(self, (maxcol,), focus=False):
52 return len(self._content.get_line_translation(maxcol))
53 54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55
56 - def render(self, (maxcol,), focus=False):
57 if focus: 58 self._content.set_text((self._sel_attr, self._text)) 59 else: 60 if self._entry_attr is not None: 61 self._content.set_text((self._entry_attr, self._text)) 62 else: 63 self._content.set_text(self._text) 64 65 return self._content.render((maxcol,), focus)
66 67 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 68
69 - def keypress(self, (maxcol,), key):
70 return key
71 72 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 73 74 text = property(lambda self: self._text)
75 76 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 77
78 -class NumEntry(ListEntry):
79 """ 80 Entry in list box which can be activated by pressing corresponding number 81 """ 82
83 - def __init__(self, msg, selected_attr, num_order, entry_attr=None, 84 enter_cb=None):
85 """ 86 @param msg: Message 87 @param selected_attr: Atrribute of selected entry 88 @param entry_attr: Entry text attribute 89 @param num_order: Entry number 90 @param enter_cb: Callback to be executed upon ENTER pressed 91 """ 92 93 self._num = [] 94 95 if callable(enter_cb): 96 self._enter_cb = enter_cb 97 else: 98 self._enter_cb = None 99 100 self.num_order = num_order 101 self._keys = libxyz.ui.Keys() 102 _msg = u"%d: %s" % (num_order, msg) 103 104 super(NumEntry, self).__init__(_msg, selected_attr, entry_attr)
105 106 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 107
108 - def keypress(self, (maxcol,), key):
109 if key == self._keys.ENTER and callable(self._enter_cb): 110 if self._num: 111 _index = int("".join(self._num)) 112 self._num = [] 113 else: 114 _index = self.num_order 115 try: 116 self._enter_cb(_index) 117 except Exception, e: 118 xyzlog.error(_(u"Error in entry callback: %s") % 119 unicode(e)) 120 xyzlog.debug(ustring(traceback.format_exc())) 121 122 return key 123 elif key.isdigit(): 124 self._num.append(key) 125 else: 126 return key
127 128 #++++++++++++++++++++++++++++++++++++++++++++++++ 129
130 -class BlockEntries(list):
131 """ 132 Wrapper for list of block entries 133 """ 134
135 - def __init__(self, xyz, data, trans=None):
136 self.xyz = xyz 137 self.length = len(data) 138 self.palettes = {} 139 140 if callable(trans): 141 self.trans = trans 142 else: 143 self.trans = lambda x: x 144 145 try: 146 self._rules = self.xyz.skin["fs.rules"] 147 except KeyError: 148 self._rules = {} 149 150 super(BlockEntries, self).__init__(data)
151 152 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 153
154 - def insert(self, idx, obj):
155 list.insert(self, idx, obj) 156 self._set_palette(idx, obj) 157 self.length = len(self)
158 159 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 160
161 - def __getslice__(self, i, j):
162 if j > self.length: 163 j = self.length 164 165 return [self[x] for x in range(i, j)]
166 167 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 168
169 - def __getitem__(self, idx):
170 item = list.__getitem__(self, idx) 171 172 if not isinstance(item, VFSObject): 173 item = self.xyz.vfs.dispatch(self.trans(item)) 174 175 self[idx] = item 176 self._set_palette(idx, item) 177 178 return item
179 180 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 181
182 - def __iter__(self):
183 for i in xrange(self.length): 184 yield self[i]
185 186 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 187
188 - def _set_palette(self, idx, item):
189 for _exp, _attr in self._rules.iteritems(): 190 if _exp.match(item): 191 self.palettes[idx] = _attr.name 192 break
193