annotate frontends/primitivus/custom_widgets.py @ 118:76055a209ed9

primitivus: added edition zone at the bottom - primitivus: new AdvancedEdit widget, which is like Edit but manage some new keys (C-a, C-e, C-k, C-w)
author Goffi <goffi@goffi.org>
date Sat, 03 Jul 2010 13:56:44 +0800
parents 1f0fd6f03e2b
children ded2431cea5a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Primitivus: a SAT frontend
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
7
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
12
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
17
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
21
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import urwid
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
23
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
24 class Password(urwid.Edit):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
25
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
26 def __init__(self, *args, **kwargs):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
27 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*'
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
28 self.__real_text=''
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
29 super(Password, self).__init__(*args, **kwargs)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
30
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
31 def set_edit_text(self, text):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.__real_text = text
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
33 hidden_txt = len(text)*'*'
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
34 super(Password, self).set_edit_text(hidden_txt)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
35
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def get_edit_text(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
37 return self.__real_text
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
38
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
39 class AdvancedEdit(urwid.Edit):
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
40 """Edit box with some custom improvments"""
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
41
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
42 def keypress(self, size, key):
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
43 #TODO: insert mode is not managed yet
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
44 if key == 'ctrl a':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
45 key = 'home'
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
46 elif key == 'ctrl e':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
47 key = 'end'
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
48 elif key == 'ctrl k':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
49 self._delete_highlighted()
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
50 self.set_edit_text(self.edit_text[:self.edit_pos])
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
51 elif key == 'ctrl w':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
52 before = self.edit_text[:self.edit_pos]
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
53 pos = before.rstrip().rfind(" ")+1
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
54 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:])
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
55 self.set_edit_pos(pos)
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
56 return super(AdvancedEdit, self).keypress(size, key)
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
57
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
58
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
59 class SelectableText(urwid.FlowWidget):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
60 signals = ['change']
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
61
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def __init__(self, text, align='left'):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
63 self.text=unicode(text)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.align = align
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.__selected=False
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
66
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def getValue(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
68 return self.text
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
69
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def setState(self, selected, invisible=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
71 """Change state
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
72 @param selected: boolean state value
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
73 @param invisible: don't emit change signal if True"""
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
74 assert(type(selected)==bool)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.__selected=selected
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
76 if not invisible:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self._emit("change", self.__selected)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self._invalidate()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
79
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def getState(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return self.__selected
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
82
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def selectable(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return True
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
85
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def keypress(self, size, key):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if key==' ' or key=='enter':
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.setState(not self.__selected)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
89 else:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return key
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
91
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def rows(self,size,focus=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return self.display_widget(size, focus).rows(size, focus)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
94
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def render(self, size, focus=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return self.display_widget(size, focus).render(size, focus)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
97
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def display_widget(self, size, focus):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
99 attr = 'selected' if self.__selected else 'default'
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
100 if focus:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
101 attr+="_focus"
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
102 return urwid.Text((attr,self.text), align=self.align)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
103
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
104 class List(urwid.WidgetWrap):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
105 signals = ['change']
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
106
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def __init__(self, options, style=[], align='left', on_state_change=None, user_data=None):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
108 self.single = 'single' in style
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
109 self.align = align
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
110
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
111 if on_state_change:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
112 urwid.connect_signal(self, 'change', on_state_change, user_data)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
113
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
114 self.content = urwid.SimpleListWalker([])
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.list_box = urwid.ListBox(self.content)
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
116 urwid.WidgetWrap.__init__(self, self.list_box)
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
117 self.changeValues(options)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
118
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
119 def __onStateChange(self, widget, selected):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if self.single:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if not selected:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
122 #if in single mode, it's forbidden to unselect a value
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
123 widget.setState(True, invisible=True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
124 return
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
125 else:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self.unselectAll(invisible=True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
127 widget.setState(True, invisible=True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
128 self._emit("change")
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
129
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
130
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
131 def unselectAll(self, invisible=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
132 for widget in self.content:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
133 if widget.getState():
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
134 widget.setState(False, invisible)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
135 widget._invalidate()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
136
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
137 def deleteValue(self, value):
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
138 """Delete the first value equal to the param given"""
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
139 try:
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
140 self.content.remove(value)
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
141 except ValueError:
117
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
142 raise ValuError("%s ==> %s" % (str(value),str(self.content)))
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
143
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
144 def getValue(self):
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
145 """Convenience method to get the value selected as a string in single mode, or None"""
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
146 values = self.getValues()
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
147 return values[0] if values else None
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
148
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
149 def getValues(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
150 result = []
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
151 for widget in self.content:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if widget.getState():
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
153 result.append(widget.getValue())
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
154 return result
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
155
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
156 def changeValues(self, new_values):
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
157 """Change all value in one shot"""
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
158 widgets = [SelectableText(option, self.align) for option in new_values]
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
159 for widget in widgets:
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
160 urwid.connect_signal(widget, 'change', self.__onStateChange)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
161 self.content[:] = widgets
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
162 if self.single and new_values:
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
163 self.content[0].setState(True)
117
1f0fd6f03e2b misc fixes
Goffi <goffi@goffi.org>
parents: 116
diff changeset
164 display_widget = urwid.BoxAdapter(self.list_box, min(len(new_values),5) or 1)
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
165 self._set_w(display_widget)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
166
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
167 def selectValue(self, value):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.unselectAll()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
169 idx = 0
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
170 for widget in self.content:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
171 if widget.getValue() == value:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
172 widget.setState(True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
173 self.list_box.set_focus(idx)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
174 return
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
175 idx+=1
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
176
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
177 class genericDialog(urwid.WidgetWrap):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
178
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
179 def __init__(self, widgets_lst, title, style=[], **kwargs):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
180 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
181
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
182 buttons = None
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
183
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
184 if "OK/CANCEL" in style:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
185 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
186 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
187 elif "YES/NO" in style:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
188 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
189 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])]
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
190 if "OK" in style:
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
191 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if buttons:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
193 buttons_flow = urwid.GridFlow(buttons, max([len(button.get_label()) for button in buttons])+4, 1, 1, 'center')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
194 widgets_lst.append(buttons_flow)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
195 body_content = urwid.SimpleListWalker(widgets_lst)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
196 frame_body = urwid.ListBox(body_content)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
197 frame = urwid.Frame(frame_body, frame_header)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
198 decorated_frame = urwid.LineBox(frame)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
199 urwid.WidgetWrap.__init__(self, decorated_frame)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
200
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
201
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
202
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
203 class InputDialog(genericDialog):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
204
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
205 def __init__(self, title, instrucions, style=['OK/CANCEL'], **kwargs):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
206 instr_wid = urwid.Text(instrucions+':')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
207 edit_box = urwid.Edit()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
208 genericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
209
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
210 class ConfirmDialog(genericDialog):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
211
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
212 def __init__(self, title, style=['YES/NO'], **kwargs):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
213 genericDialog.__init__(self, [], title, style, yes_value=None, **kwargs)
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
214
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
215 class Alert(genericDialog):
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
216
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
217 def __init__(self, title, message, style=['OK'], **kwargs):
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
218 genericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)