annotate frontends/primitivus/custom_widgets.py @ 144:80661755ea8d

Primitivus: Tarot card game implementation - quick frontend: card_game added - wix: card_game splitted with quick frontend - tools: new game library - primitivus: new card_game widget (not finished yet) - primitivus: SàT XML UI management: first draft
author Goffi <goffi@goffi.org>
date Mon, 26 Jul 2010 19:43:44 +0800
parents e7b8871e9f52
children 7fcb4f083686
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
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
23 from urwid.escape import utf8decode
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
24
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
25 class Password(urwid.Edit):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
26 """Edit box which doesn't show what is entered (show '*' or other char instead)"""
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
27
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def __init__(self, *args, **kwargs):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
29 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char'
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
30 @param hidden_char: char to show instead of what is actually entered: default '*'
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
31 """
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.hidden_char=kwargs['hidden_char'] if kwargs.has_key('hidden_char') else '*'
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self.__real_text=''
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
34 super(Password, self).__init__(*args, **kwargs)
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 set_edit_text(self, text):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
37 self.__real_text = text
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
38 hidden_txt = len(text)*'*'
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
39 super(Password, self).set_edit_text(hidden_txt)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
40
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
41 def get_edit_text(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
42 return self.__real_text
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
43
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
44 def insert_text(self, text):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
45 self._edit_text = self.__real_text
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
46 super(Password,self).insert_text(text)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
47
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
48 def render(self, size, focus=False):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
49 return super(Password, self).render(size, focus)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
50
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
51 class AdvancedEdit(urwid.Edit):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
52 """Edit box with some custom improvments
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
53 new chars:
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
54 - C-a: like 'home'
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
55 - C-e: like 'end'
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
56 - C-k: remove everything on the right of the cursor
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
57 - C-w: remove the word on the back
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
58 new behaviour: emit a 'click' signal when enter is pressed"""
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
59 signals = urwid.Edit.signals + ['click']
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
60
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
61 def keypress(self, size, key):
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
62 #TODO: insert mode is not managed yet
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
63 if key == 'ctrl a':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
64 key = 'home'
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
65 elif key == 'ctrl e':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
66 key = 'end'
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
67 elif key == 'ctrl k':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
68 self._delete_highlighted()
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
69 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
70 elif key == 'ctrl w':
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
71 before = self.edit_text[:self.edit_pos]
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
72 pos = before.rstrip().rfind(" ")+1
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
73 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
74 self.set_edit_pos(pos)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
75 elif key == 'enter':
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
76 self._emit('click')
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
77 return super(AdvancedEdit, self).keypress(size, key)
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
78
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
79
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
80 class SurroundedText(urwid.FlowWidget):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
81 """Text centered on a repeated character (like a Divider, but with a text in the center)"""
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
82
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
83 def __init__(self,text,car=utf8decode('─')):
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
84 self.text=text
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
85 self.car=car
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
86
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
87 def rows(self,size,focus=False):
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
88 return self.display_widget(size, focus).rows(size, focus)
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
89
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
90 def render(self, size, focus=False):
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
91 return self.display_widget(size, focus).render(size, focus)
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
92
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
93 def display_widget(self, size, focus):
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
94 (maxcol,) = size
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
95 middle = (maxcol-len(self.text))/2
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
96 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
97 return urwid.Text(render_text)
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
98
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
99
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
100
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
101 class SelectableText(urwid.FlowWidget):
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
102 """Text which can be selected with space"""
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
103 signals = ['change']
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
104
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
105 def __init__(self, text, align='left', header='', select_attr=None, default_attr=None, selected = False, data=None):
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
106 self.text=unicode(text)
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
107 self.header=header
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
108 if data:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
109 self.data=data
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
110 if select_attr:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
111 self.selected = select_attr
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
112 if default_attr:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
113 self.default = default_attr
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.align = align
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
115 self.__selected=selected
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
116
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def getValue(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
118 return self.text
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
119
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
120 def setAttribute(self, name, value):
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
121 """Change attribut used for rendering widget
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
122 @param name: one of
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
123 -default: when not selected
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
124 -selected: when selected
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
125 @param value: name of the attribute
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
126 /!\ the attribute name followed by _focus is used when widget has focus"""
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
127 assert name in ['default', 'selected']
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
128 self.__setattr__(name,value)
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
129 self._invalidate()
113
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 setState(self, selected, invisible=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
132 """Change state
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @param selected: boolean state value
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
134 @param invisible: don't emit change signal if True"""
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
135 assert(type(selected)==bool)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
136 self.__selected=selected
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
137 self._invalidate()
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if not invisible:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self._emit("change", self.__selected)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
140
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def getState(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
142 return self.__selected
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
143
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
144 def selectable(self):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
145 return True
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
146
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
147 def keypress(self, size, key):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if key==' ' or key=='enter':
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
149 self.setState(not self.__selected)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
150 else:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
151 return key
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
152
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
153 def rows(self,size,focus=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
154 return self.display_widget(size, focus).rows(size, focus)
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 render(self, size, focus=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
157 return self.display_widget(size, focus).render(size, focus)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
158
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
159 def display_widget(self, size, focus):
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
160 try:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
161 select_attr = self.selected
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
162 except AttributeError:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
163 select_attr = 'selected'
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
164 try:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
165 default_attr = self.default
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
166 except AttributeError:
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
167 default_attr = 'default'
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
168 attr = select_attr if self.__selected else default_attr
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
169 if focus:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
170 attr+="_focus"
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
171 return urwid.Text((attr,self.header+self.text), align=self.align)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
172
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
173 class ClickableText(SelectableText):
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
174 signals = SelectableText.signals + ['click']
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
175
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
176 def setState(self, selected, invisible=False):
125
8d611eb9ae48 primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 124
diff changeset
177 self._emit('click')
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
178
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
179 class GenericList(urwid.WidgetWrap):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
180 signals = ['click','change']
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
181
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
182 def __init__(self, options, style=[], align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None):
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
183 """
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
184 Widget managing list of string and their selection
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
185 @param options: list of strings used for options
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
186 @param style: list of string:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
187 - 'single' if only one must be selected
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
188 - 'no_first_select' nothing selected when list is first displayed
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
189 - 'can_select_none' if we can select nothing
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
190 @param align: alignement of text inside the list
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
191 @param on_click: method called when click signal is emited
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
192 @param user_data: data sent to the callback for click signal
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
193 """
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
194 self.single = 'single' in style
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
195 self.no_first_select = 'no_first_select' in style
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
196 self.can_select_none = 'can_select_none' in style
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
197 self.align = align
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
198 self.option_type = option_type
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
199 self.first_display = True
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
200
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
201 if on_click:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
202 urwid.connect_signal(self, 'click', on_click, user_data)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
203
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
204 if on_change:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
205 urwid.connect_signal(self, 'change', on_change, user_data)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
206
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
207 self.content = urwid.SimpleListWalker([])
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
208 self.list_box = urwid.ListBox(self.content)
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
209 urwid.WidgetWrap.__init__(self, self.list_box)
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
210 self.changeValues(options)
113
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 __onStateChange(self, widget, selected):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
213 if self.single:
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
214 if not selected and not self.can_select_none:
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
215 #if in single mode, it's forbidden to unselect a value
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
216 widget.setState(True, invisible=True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
217 return
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
218 if selected:
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
219 self.unselectAll(invisible=True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
220 widget.setState(True, invisible=True)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
221 self._emit("click")
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
222
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
223
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def unselectAll(self, invisible=False):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
225 for widget in self.content:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
226 if widget.getState():
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
227 widget.setState(False, invisible)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
228 widget._invalidate()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
229
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
230 def deleteValue(self, value):
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
231 """Delete the first value equal to the param given"""
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
232 for widget in self.content:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
233 if widget.getValue() == value:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
234 self.content.remove(widget)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
235 self._emit('change')
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
236 return
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
237 raise ValueError("%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
238
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
239 def getSelectedValue(self):
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
240 """Convenience method to get the value selected as a string in single mode, or None"""
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
241 values = self.getSelectedValues()
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
242 return values[0] if values else None
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
243
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
244 def getAllValues(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
245 """Return values of all items"""
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
246 return [widget.getValue() for widget in self.content]
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
247
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
248 def getSelectedValues(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
249 """Return values of selected items"""
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
250 result = []
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
251 for widget in self.content:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if widget.getState():
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
253 result.append(widget.getValue())
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
254 return result
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
255
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
256 def getDisplayWidget(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
257 return self.list_box
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
258
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
259 def changeValues(self, new_values):
116
7c482ecac0ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 114
diff changeset
260 """Change all value in one shot"""
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
261 if not self.first_display:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
262 old_selected = self.getSelectedValues()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
263 widgets = []
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
264 for option in new_values:
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
265 widget = self.option_type(option, self.align)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
266 if not self.first_display and option in old_selected:
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
267 widget.setState(True)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
268 widgets.append(widget)
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
269 try:
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
270 urwid.connect_signal(widget, 'change', self.__onStateChange)
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
271 except NameError:
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
272 pass #the widget given doesn't support 'change' signal
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
273 self.content[:] = widgets
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
274 if self.first_display and self.single and new_values and not self.no_first_select:
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
275 self.content[0].setState(True)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
276 display_widget = self.getDisplayWidget()
118
76055a209ed9 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 117
diff changeset
277 self._set_w(display_widget)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
278 self._emit('change')
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
279 self.first_display = False
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
280
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
281 def selectValue(self, value):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
282 self.unselectAll()
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
283 idx = 0
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
284 for widget in self.content:
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
285 if widget.getSelectedValue() == value:
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
286 widget.setState(True)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
287 self.list_box.set_focus(idx)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
288 return
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
289 idx+=1
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
290
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
291 class List(urwid.FlowWidget):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
292 """FlowWidget list, same arguments as GenericList, with an additional one 'max_height'"""
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
293 signals = ['click','change']
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
294
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
295 def __init__(self, options, style=[], max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None):
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
296 self.genericList = GenericList(options, style, align, option_type, on_click, on_change, user_data)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
297 self.max_height = max_height
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
298
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
299 def selectable(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
300 return True
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
301
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
302 def keypress(self, size, key):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
303 return self.displayWidget(size,True).keypress(size, key)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
304
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
305 def unselectAll(self, invisible=False):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
306 return self.genericList.unselectAll(invisible)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
307
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
308 def deleteValue(self, value):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
309 return self.genericList.deleteValue(value)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
310
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
311 def getSelectedValue(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
312 return self.genericList.getSelectedValue()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
313
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
314 def getAllValues(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
315 return self.genericList.getAllValues()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
316
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
317 def getSelectedValues(self):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
318 return self.genericList.getSelectedValues()
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
319
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
320 def changeValues(self, new_values):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
321 return self.genericList.changeValues(new_values)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
322
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
323 def selectValue(self, value):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
324 return self.genericList.selectValue(value)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
325
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
326 def render(self, size, focus=False):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
327 return self.displayWidget(size, focus).render(size, focus)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
328
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
329 def rows(self, size, focus=False):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
330 return self.displayWidget(size, focus).rows(size, focus)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
331
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
332 def displayWidget(self, size, focus):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
333 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content])
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
334 height = min(list_size,self.max_height) or 1
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
335 return urwid.BoxAdapter(self.genericList, height)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
336
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
337 ## MISC ##
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
338
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
339 class MenuBox(urwid.WidgetWrap):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
340 signals = ['click']
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
341
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
342 def __init__(self,parent,items):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
343 self.parent = parent
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
344 self.selected = None
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
345 content = urwid.SimpleListWalker([ClickableText(text,default_attr='menuitem') for text in items])
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
346 for wid in content:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
347 urwid.connect_signal(wid, 'click', self.onClick)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
348
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
349 self.listBox = urwid.ListBox(content)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
350 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items)))
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
351 urwid.WidgetWrap.__init__(self,menubox)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
352
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
353 def getValue(self):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
354 return self.selected
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
355
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
356 def keypress(self, size, key):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
357 if key=='up':
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
358 if self.listBox.get_focus()[1] == 0:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
359 self.parent.keypress(size, key)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
360 elif key=='left' or key=='right':
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
361 self.parent.keypress(size,'up')
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
362 self.parent.keypress(size,key)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
363 return super(MenuBox,self).keypress(size,key)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
364
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
365 def onClick(self, wid):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
366 self.selected = wid.getValue()
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
367 self._emit('click')
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
368
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
369 class Menu(urwid.FlowWidget):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
370
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
371 def __init__(self,loop, x_orig=0):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
372 """Menu widget
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
373 @param loop: main loop of urwid
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
374 @param x_orig: absolute start of the abscissa
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
375 """
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
376 super(Menu, self).__init__()
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
377 self.loop = loop
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
378 self.menu_keys = []
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
379 self.menu = {}
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
380 self.x_orig = x_orig
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
381 self.shortcuts = {} #keyboard shortcuts
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
382 self.focus_menu = 0
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
383 self.save_bottom = None
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
384
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
385 def selectable(self):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
386 return True
139
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
387
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
388 def getMenuSize(self):
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
389 """return the current number of categories in this menu"""
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
390 return len(self.menu_keys)
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
391
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
392 def setOrigX(self, orig_x):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
393 self.x_orig = orig_x
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
394
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
395 def __buildOverlay(self,menu_key,columns):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
396 """Build the overlay menu which show menuitems
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
397 @param menu_key: name of the category
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
398 @colums: column number where the menubox must be displayed"""
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
399 max_len = 0
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
400 for item in self.menu[menu_key]:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
401 if len(item[0]) > max_len:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
402 max_len = len(item[0])
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
403
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
404 self.save_bottom = self.loop.widget
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
405 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]])
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
406 urwid.connect_signal(menu_box, 'click', self.onClick)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
407
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
408 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
409
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
410 def keypress(self, size, key):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
411 if key == 'right' and self.focus_menu < len(self.menu)-1:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
412 self.focus_menu += 1
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
413 self._invalidate()
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
414 elif key == 'left' and self.focus_menu > 0:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
415 self.focus_menu -= 1
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
416 self._invalidate()
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
417 return
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
418 elif key == 'down':
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
419 if self.menu_keys and not self.save_bottom:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
420 column = sum([len(menu)+4 for menu in self.menu_keys[0:self.focus_menu]],self.focus_menu+self.x_orig)
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
421 self.__buildOverlay(self.menu_keys[self.focus_menu],column)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
422 elif key == 'up':
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
423 if self.save_bottom:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
424 self.loop.widget = self.save_bottom
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
425 self.save_bottom = None
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
426
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
427 return key
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
428
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
429 def checkShortcuts(self, key):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
430 for shortcut in self.shortcuts.keys():
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
431 if key == shortcut:
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
432 category, item, callback = self.shortcuts[shortcut]
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
433 callback((category, item))
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
434 return key
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
435
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
436 def addMenu(self, category, item, callback, shortcut=None):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
437 """Add a menu item, create the category if new
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
438 @param category: category of the menu (e.g. File/Edit)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
439 @param item: menu item (e.g. new/close/about)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
440 @callback: method to call when item is selected"""
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
441 if not category in self.menu.keys():
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
442 self.menu_keys.append(category)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
443 self.menu[category] = []
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
444 self.menu[category].append((item, callback))
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
445 if shortcut:
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
446 assert(shortcut not in self.shortcuts.keys())
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
447 self.shortcuts[shortcut] = (category, item, callback)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
448
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
449 def rows(self,size,focus=False):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
450 return self.display_widget(size, focus).rows(size, focus)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
451
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
452 def render(self, size, focus=False):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
453 return self.display_widget(size, focus).render(size, focus)
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
454
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
455 def display_widget(self, size, focus):
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
456 render_txt = []
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
457 idx = 0
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
458 for menu in self.menu_keys:
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
459 if focus and idx == self.focus_menu:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
460 render_txt.append(('selected_menu', '[ %s ]' % menu))
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
461 render_txt.append(' ')
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
462 else:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
463 render_txt.append('[ %s ] ' % menu)
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
464 idx += 1
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
465 return urwid.AttrMap(urwid.Text(render_txt), 'menubar')
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
466
131
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
467 def onClick(self, widget):
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
468 category = self.menu_keys[self.focus_menu]
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
469 item = widget.getValue()
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
470 for menu_item in self.menu[category]:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
471 if item == menu_item[0]:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
472 callback = menu_item[1]
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
473 break
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
474 if callback:
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
475 self.keypress(None,'up')
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
476 callback((category, item))
6cad483a6d84 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 128
diff changeset
477
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
478 class MenuRoller(urwid.WidgetWrap):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
479
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
480 def __init__(self,menus_list):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
481 """Create a MenuRoller
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
482 @param menus_list: list of tuple with (name, Menu_instance), name can be None
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
483 """
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
484 assert (menus_list)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
485 self.selected = 0
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
486 self.name_list = []
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
487 self.menus = {}
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
488
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
489 self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')])
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
490 urwid.WidgetWrap.__init__(self, self.columns)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
491
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
492 for menu_tuple in menus_list:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
493 name,menu = menu_tuple
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
494 self.addMenu(name, menu)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
495
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
496 def __showSelected(self):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
497 """show menu selected"""
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
498 name_txt = u'\u21c9 '+self.name_list[self.selected]+u' \u21c7 '
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
499 current_name = ClickableText(name_txt)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
500 name_len = len(name_txt)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
501 current_menu = self.menus[self.name_list[self.selected]]
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
502 current_menu.setOrigX(name_len)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
503 self.columns.widget_list[0] = current_name
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
504 self.columns.column_types[0]=('fixed', name_len)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
505 self.columns.widget_list[1] = current_menu
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
506
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
507 def keypress(self, size, key):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
508 if key=='up':
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
509 if self.columns.get_focus_column()==0 and self.selected > 0:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
510 self.selected -= 1
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
511 self.__showSelected()
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
512 elif key=='down':
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
513 if self.columns.get_focus_column()==0 and self.selected < len(self.name_list)-1:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
514 self.selected += 1
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
515 self.__showSelected()
139
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
516 elif key=='right':
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
517 if self.columns.get_focus_column()==0 and \
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
518 (self.columns.widget_list[1].__class__ == urwid.Text or \
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
519 self.menus[self.name_list[self.selected]].getMenuSize()==0):
e7b8871e9f52 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 137
diff changeset
520 return #if we have no menu or the menu is empty, we don't go the right column
137
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
521
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
522 return super(MenuRoller, self).keypress(size, key)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
523
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
524 def addMenu(self, name_param, menu):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
525 name = name_param or ''
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
526 if name not in self.name_list:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
527 self.name_list.append(name)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
528 self.menus[name] = menu
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
529 if self.name_list[self.selected] == name:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
530 self.__showSelected() #if we are on the menu, we update it
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
531
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
532 def checkShortcuts(self, key):
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
533 for menu in self.name_list:
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
534 key = self.menus[menu].checkShortcuts(key)
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
535 return key
227394eb080c Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 131
diff changeset
536
128
2240f34f6452 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 125
diff changeset
537
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
538 ## DIALOGS ##
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
539
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
540 class GenericDialog(urwid.WidgetWrap):
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
541
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
542 def __init__(self, widgets_lst, title, style=[], **kwargs):
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
543 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title')
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
544
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
545 buttons = None
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
546
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
547 if "OK/CANCEL" in style:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
548 buttons = [urwid.Button(_("Cancel"), kwargs['cancel_cb']),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
549 urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
550 elif "YES/NO" in style:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
551 buttons = [urwid.Button(_("Yes"), kwargs['yes_cb']),
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
552 urwid.Button(_("No"), kwargs['no_cb'], kwargs['yes_value'])]
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
553 if "OK" in style:
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
554 buttons = [urwid.Button(_("Ok"), kwargs['ok_cb'], kwargs['ok_value'])]
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
555 if buttons:
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
556 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
557 widgets_lst.append(buttons_flow)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
558 body_content = urwid.SimpleListWalker(widgets_lst)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
559 frame_body = urwid.ListBox(body_content)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
560 frame = urwid.Frame(frame_body, frame_header)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
561 decorated_frame = urwid.LineBox(frame)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
562 urwid.WidgetWrap.__init__(self, decorated_frame)
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
563
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
564
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
565
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
566 class InputDialog(GenericDialog):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
567 """Dialog with an edit box"""
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
568
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
569 def __init__(self, title, instrucions, style=['OK/CANCEL'], default_txt = '', **kwargs):
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
570 instr_wid = urwid.Text(instrucions+':')
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
571 edit_box = urwid.Edit(edit_text=default_txt)
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
572 GenericDialog.__init__(self, [instr_wid,edit_box], title, style, ok_value=edit_box, **kwargs)
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
573
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
574 class ConfirmDialog(GenericDialog):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
575 """Dialog with buttons for confirm or cancel an action"""
113
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
576
e5ca22113280 Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
577 def __init__(self, title, style=['YES/NO'], **kwargs):
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
578 GenericDialog.__init__(self, [], title, style, yes_value=None, **kwargs)
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
579
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
580 class Alert(GenericDialog):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
581 """Dialog with just a message and a OK button"""
114
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
582
77f48939ad6e primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 113
diff changeset
583 def __init__(self, title, message, style=['OK'], **kwargs):
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
584 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
585
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
586 ## CONTAINERS ##
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
587
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
588 class FocusFrame(urwid.Frame):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
589 """Frame which manage 'tab' key"""
119
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
590
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
591 def keypress(self, size, key):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
592 if key == 'tab':
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
593 focus_list = ('header','body','footer')
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
594 focus_idx = focus_list.index(self.focus_part)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
595 for i in range(2):
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
596 focus_idx = (focus_idx + 1) % len(focus_list)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
597 focus_name = focus_list[focus_idx]
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
598 widget = getattr(self,'_'+focus_name)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
599 if widget!=None and widget.selectable():
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
600 self.set_focus(focus_name)
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
601
ded2431cea5a Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 118
diff changeset
602 return urwid.Frame.keypress(self, size, key)
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
603
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
604 ## DECORATORS ##
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
605 class LabelLine(urwid.LineBox):
121
03d8bcc67182 misc documentation
Goffi <goffi@goffi.org>
parents: 120
diff changeset
606 """Like LineBox, but with a Label centered in the top line"""
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
607
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
608 def __init__(self, original_widget, label_widget):
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
609 urwid.LineBox.__init__(self, original_widget)
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
610 top_columns = self._w.widget_list[0]
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
611 top_columns.widget_list[1] = label_widget
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
612
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
613 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap):
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
614 def __init__(self, original_widget, left_char = utf8decode("│"), right_char = ''):
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
615 """Draw a separator on left and/or of original_widget."""
120
1ca5f254ce41 primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 119
diff changeset
616
124
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
617 widgets = [original_widget]
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
618 if left_char:
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
619 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char)))
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
620 if right_char:
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
621 widgets.append(('fixed', 1, urwid.SolidFill(right_char)))
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
622 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1)
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
623 urwid.WidgetDecoration.__init__(self, original_widget)
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
624 urwid.WidgetWrap.__init__(self, columns)
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
625
961e0898271f primitivus chat window
Goffi <goffi@goffi.org>
parents: 121
diff changeset
626