annotate urwid_satext/sat_widgets.py @ 151:6689aa54b20c default tip

refactoring from camelCase -> snake_case: This libraries was using camelCase due for historical reasons (related to the use of Twisted in the initial project). This patch fixes it by using PEP8 compliant snake_case
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 15:38:18 +0200
parents aa8f46b43a71
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
3
64
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
4 # Urwid SàT extensions
122
00b012549f88 copyright update
Goffi <goffi@goffi.org>
parents: 117
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
64
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
6 #
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
7 # This program is free software: you can redistribute it and/or modify
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
8 # it under the terms of the GNU Lesser General Public License as published by
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
10 # (at your option) any later version.
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
11 #
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
12 # This program is distributed in the hope that it will be useful,
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
15 # GNU Lesser General Public License for more details.
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
16 #
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
17 # You should have received a copy of the GNU Lesser General Public License
5cef69971f23 headers fixes
Goffi <goffi@goffi.org>
parents: 59
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
19
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import urwid
89
2141f07b5fdd primitivus: no more direct error/warning methods for logging
Goffi <goffi@goffi.org>
parents: 88
diff changeset
21 import logging as log
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
22
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
23 import uuid
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
24
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
25 import collections
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
26
59
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
27 from urwid.util import is_mouse_press #XXX: is_mouse_press is not included in urwid in 1.0.0
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
28 from .keys import action_key_map as a_key
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
29
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
30 FOCUS_KEYS = (a_key['FOCUS_SWITCH'], a_key['FOCUS_UP'], a_key['FOCUS_DOWN'])
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
31
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
32
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
33 def get_focus_direction(key, inversed=False):
94
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
34 """Return direction and rotate boolean depending on key
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
35 @param key: one of FOCUS_KEYS
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
36 @param inversed: inverse directions if True
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
37 @return (tuple): (direction, rotate) where
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
38 - direction is 1 or -1
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
39 - rotate is False if we should stop at the begin/end of the widgets list
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
40 """
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
41 if not inversed:
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
42 direction = 1 if key in (a_key['FOCUS_SWITCH'], a_key['FOCUS_UP']) else -1
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
43 else:
98
8bf5a35450f0 Fixed getFocusDirection direction when inversed, and FocusFrame focus order.
Goffi <goffi@goffi.org>
parents: 97
diff changeset
44 direction = -1 if key in (a_key['FOCUS_SWITCH'], a_key['FOCUS_UP']) else 1
94
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
45 rotate = key == a_key['FOCUS_SWITCH']
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
46 return direction, rotate
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
47
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
48
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
49 class AdvancedEdit(urwid.Edit):
7
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
50 """Edit box with some custom improvments
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
51 new chars:
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
52 - C-a: like 'home'
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
53 - C-e: like 'end'
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
54 - C-k: remove everything on the right of the cursor
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
55 - C-w: remove the word on the back
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
56 new behaviour: emit a 'click' signal when enter is pressed"""
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
57 signals = urwid.Edit.signals + ['click']
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
58
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
59 def get_value(self):
109
13d608824508 added AdvancedEdit.getValue
Goffi <goffi@goffi.org>
parents: 108
diff changeset
60 return self.get_edit_text()
13d608824508 added AdvancedEdit.getValue
Goffi <goffi@goffi.org>
parents: 108
diff changeset
61
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
62 def set_completion_method(self, callback):
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
63 """Define method called when completion is asked
124
db9bc68ca0a8 minor docstring correction
Goffi <goffi@goffi.org>
parents: 122
diff changeset
64
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
65 @callback: method with 2 arguments:
124
db9bc68ca0a8 minor docstring correction
Goffi <goffi@goffi.org>
parents: 122
diff changeset
66 - the text to complete (part after cursor position is ignored)
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
67 - if there was already a completion, a dict with
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
68 - 'completed':last completion
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
69 - 'completion_pos': cursor position where the completion starts
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
70 - 'position': last completion cursor position
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
71 this dict must be used (and can be filled) to find next completion)
124
db9bc68ca0a8 minor docstring correction
Goffi <goffi@goffi.org>
parents: 122
diff changeset
72 and which return the full text completed
db9bc68ca0a8 minor docstring correction
Goffi <goffi@goffi.org>
parents: 122
diff changeset
73 """
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
74 self.completion_cb = callback
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
75 self.completion_data = {}
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
76
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
77 def keypress(self, size, key):
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
78 #TODO: insert mode is not managed yet
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
79 if key == a_key['EDIT_HOME']:
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
80 key = 'home'
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
81 elif key == a_key['EDIT_END']:
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
82 key = 'end'
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
83 elif key == a_key['EDIT_DELETE_TO_END']:
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
84 self._delete_highlighted()
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
85 self.set_edit_text(self.edit_text[:self.edit_pos])
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
86 elif key == a_key['EDIT_DELETE_LAST_WORD']:
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
87 before = self.edit_text[:self.edit_pos]
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
88 pos = before.rstrip().rfind(" ")+1
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
89 self.set_edit_text(before[:pos] + self.edit_text[self.edit_pos:])
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
90 self.set_edit_pos(pos)
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
91 elif key == a_key['EDIT_ENTER']:
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
92 self._emit('click')
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
93 elif key == a_key['EDIT_COMPLETE']:
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
94 try:
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
95 before = self.edit_text[:self.edit_pos]
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
96 if self.completion_data:
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
97 if (not self.completion_data['completed']
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
98 or self.completion_data['position'] != self.edit_pos
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
99 or not before.endswith(self.completion_data['completed'])):
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
100 self.completion_data.clear()
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
101 else:
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
102 before = before[:-len(self.completion_data['completed'])]
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
103 complet = self.completion_cb(before, self.completion_data)
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
104 self.completion_data['completed'] = complet[len(before):]
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
105 self.set_edit_text(complet+self.edit_text[self.edit_pos:])
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
106 self.set_edit_pos(len(complet))
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
107 self.completion_data['position'] = self.edit_pos
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
108 return
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
109 except AttributeError:
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
110 #No completion method defined
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
111 pass
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
112 return super(AdvancedEdit, self).keypress(size, key)
59
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
113
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
114
59
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
115 class Password(AdvancedEdit):
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
116 """Edit box which doesn't show what is entered (show '*' or other char instead)"""
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
117
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
118 def __init__(self, *args, **kwargs):
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
119 """Same args than Edit.__init__ with an additional keyword arg 'hidden_char'
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
120 @param hidden_char: char to show instead of what is actually entered: default '*'
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
121 """
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
122 self.hidden_char=kwargs['hidden_char'] if 'hidden_char' in kwargs else '*'
59
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
123 self.__real_text=''
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
124 super(Password, self).__init__(*args, **kwargs)
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
125
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
126 def set_edit_text(self, text):
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
127 self.__real_text = text
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
128 hidden_txt = len(text)*'*'
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
129 super(Password, self).set_edit_text(hidden_txt)
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
130
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
131 def get_edit_text(self):
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
132 return self.__real_text
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
133
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
134 def insert_text(self, text):
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
135 self._edit_text = self.__real_text
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
136 super(Password,self).insert_text(text)
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
137
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
138 def render(self, size, focus=False):
a99951c9ce2a Password now use AdvancedEdit to take advantage of shortcuts
Goffi <goffi@goffi.org>
parents: 57
diff changeset
139 return super(Password, self).render(size, focus)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
140
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
141
57
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
142 class ModalEdit(AdvancedEdit):
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
143 """AdvancedEdit with vi-like mode management
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
144 - there is a new 'mode' property which can be changed with properties
57
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
145 specified during init
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
146 - completion callback received a new 'mode' argument
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
147 """
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
148
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
149 def __init__(self, modes, *args, **kwargs):
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
150 """ first argument is "modes", others are the same paramaters as AdvancedEdit
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
151 @param modes: dictionnary in the form:
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
152 'key_to_change_mode': ('Mode', caption)
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
153 e.g.: 'i': ('INSERTION', '> ')
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
154 There *MUST* be a None key (for NORMAL mode)"""
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
155 assert(isinstance(modes, dict) and None in modes)
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
156 self._modes = modes
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
157 super(ModalEdit, self).__init__(*args, **kwargs)
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
158 self.mode = self._modes[None][0]
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
159
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
160 @property
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
161 def mode(self):
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
162 return self._mode
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
163
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
164 @mode.setter
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
165 def mode(self, value):
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
166 mode_key = None
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
167 for key in self._modes:
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
168 if self._modes[key][0] == value:
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
169 mode_key = key
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
170 break
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
171
57
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
172 mode, caption = self._modes[mode_key]
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
173 self._mode = mode
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
174 self.set_caption(caption)
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
175 if not mode_key: #we are in NORMAL mode
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
176 self.set_edit_text('')
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
177
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
178 def set_completion_method(self, callback):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
179 """ Same as AdvancedEdit.set_completion_method, but with a third argument: current mode"""
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
180 super(ModalEdit, self).set_completion_method(lambda text,data: callback(text, data, self._mode))
57
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
181
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
182 def keypress(self, size, key):
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
183 if key == a_key['MODAL_ESCAPE']:
57
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
184 self.mode = "NORMAL"
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
185 return
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
186 if self._mode == 'NORMAL' and key in self._modes:
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
187 self.mode = self._modes[key][0]
d493f95724a7 new ModalEdit for vi-like bahaviour
Goffi <goffi@goffi.org>
parents: 56
diff changeset
188 return
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
189 return super(ModalEdit, self).keypress(size, key)
4
c94cdbfdf3e8 primitivus: added edition zone at the bottom
Goffi <goffi@goffi.org>
parents: 3
diff changeset
190
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
191
67
c270867a80f9 updated deprecated FlowWidget use
Goffi <goffi@goffi.org>
parents: 66
diff changeset
192 class SurroundedText(urwid.Widget):
7
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
193 """Text centered on a repeated character (like a Divider, but with a text in the center)"""
67
c270867a80f9 updated deprecated FlowWidget use
Goffi <goffi@goffi.org>
parents: 66
diff changeset
194 _sizing = frozenset(['flow'])
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
195
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
196 def __init__(self,text,car='─'):
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
197 self.text=text
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
198 self.car=car
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
199
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
200 def rows(self,size,focus=False):
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
201 return self.display_widget(size, focus).rows(size, focus)
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
202
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
203 def render(self, size, focus=False):
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
204 return self.display_widget(size, focus).render(size, focus)
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
205
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
206 def display_widget(self, size, focus):
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
207 (maxcol,) = size
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
208 middle = (maxcol-len(self.text))//2
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
209 render_text = middle * self.car + self.text + (maxcol - len(self.text) - middle) * self.car
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
210 return urwid.Text(render_text)
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
211
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
212
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
213 class AlwaysSelectableText(urwid.WidgetWrap):
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
214 """Text which can be selected with space"""
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
215 signals = ['change']
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
216
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
217 def __init__(self, text, align='left', header='', focus_attr='default_focus', selected_text=None, selected=False, data=None):
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
218 """
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
219 @param text: same as urwid.Text's text parameter
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
220 @param align: same as urwid.Text's align parameter
127
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
221 @param selected_text: text to display when selected
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
222 @param selected: is the text selected ?
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
223 """
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
224 self.focus_attr = focus_attr
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
225 self._selected = False
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
226 self._was_focused = False
69
b39c81cdd863 removed __valid_text: urwid now manage basestring subclasses, it's not necessary anymore
Goffi <goffi@goffi.org>
parents: 68
diff changeset
227 self.header = header
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
228 self.text = text
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
229 urwid.WidgetWrap.__init__(self, urwid.Text("",align=align))
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
230 self.set_selected_text(selected_text)
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
231 self.set_state(selected)
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
232
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
233 def get_value(self):
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
234 if isinstance(self.text,str):
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
235 return self.text
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
236 list_attr = self.text if isinstance(self.text, list) else [self.text]
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
237 txt = ""
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
238 for attr in list_attr:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
239 if isinstance(attr,tuple):
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
240 txt+=attr[1]
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
241 else:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
242 txt+=attr
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
243 return txt
9
6650747dfdcb primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 8
diff changeset
244
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
245 def get_text(self):
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
246 """for compatibility with urwid.Text"""
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
247 return self.get_value()
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
248
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
249 def set_text(self, text):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
250 """/!\ set_text doesn't change self.selected_txt !"""
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
251 self.text = text
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
252 self.set_state(self._selected,invisible=True)
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
253
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
254 def set_selected_text(self, text=None):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
255 """Text to display when selected
127
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
256
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
257 @text: text as in urwid.Text or None for default value
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
258 """
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
259 if text == None:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
260 text = ('selected',self.get_value())
69
b39c81cdd863 removed __valid_text: urwid now manage basestring subclasses, it's not necessary anymore
Goffi <goffi@goffi.org>
parents: 68
diff changeset
261 self.selected_txt = text
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
262 if self._selected:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
263 self.set_state(self._selected)
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
264
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
265 def _set_txt(self):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
266 txt_list = [self.header]
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
267 txt = self.selected_txt if self._selected else self.text
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
268 if isinstance(txt,list):
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
269 txt_list.extend(txt)
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
270 else:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
271 txt_list.append(txt)
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
272 self._w.base_widget.set_text(txt_list)
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
273
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
274
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
275 def set_state(self, selected, invisible=False):
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
276 """Change state
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
277
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
278 @param selected: boolean state value
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
279 @param invisible: don't emit change signal if True
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
280 """
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
281 assert type(selected)==bool
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
282 self._selected=selected
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
283 self._set_txt()
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
284 self._was_focused = False
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
285 self._invalidate()
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
286 if not invisible:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
287 self._emit("change", self._selected)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
288
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
289 def get_state(self):
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
290 return self._selected
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
291
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
292 def selectable(self):
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
293 return True
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
294
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
295 def keypress(self, size, key):
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
296 if key in (a_key['TEXT_SELECT'], a_key['TEXT_SELECT2']):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
297 self.set_state(not self._selected)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
298 else:
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
299 return key
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
300
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
301 def mouse_event(self, size, event, button, x, y, focus):
53
d34f2b0f68d3 urwid 1.0.0 update: fixed missing is_mouse_press
Goffi <goffi@goffi.org>
parents: 44
diff changeset
302 if is_mouse_press(event) and button == 1:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
303 self.set_state(not self._selected)
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
304 return True
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
305
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
306 return False
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
307
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
308 def render(self, size, focus=False):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
309 attr_list = self._w.base_widget._attrib
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
310 if not focus:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
311 if self._was_focused:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
312 self._set_txt()
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
313 self._was_focused = False
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
314 else:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
315 if not self._was_focused:
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
316 if not attr_list:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
317 attr_list.append((self.focus_attr,len(self._w.base_widget.text)))
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
318 else:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
319 for idx in range(len(attr_list)):
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
320 attr,attr_len = attr_list[idx]
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
321 if attr == None:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
322 attr = self.focus_attr
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
323 attr_list[idx] = (attr,attr_len)
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
324 else:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
325 if not attr.endswith('_focus'):
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
326 attr+="_focus"
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
327 attr_list[idx] = (attr,attr_len)
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
328 self._w.base_widget._invalidate()
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
329 self._was_focused = True #bloody ugly hack :)
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
330 return self._w.render(size, focus)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
331
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
332
95
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
333 class SelectableText(AlwaysSelectableText):
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
334 """Like AlwaysSelectableText but not selectable when text is empty"""
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
335
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
336 def selectable(self):
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
337 return bool(self.text)
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
338
ca9a77f3b53e SelectableText is now unselectable if its text is empty, AlwaysSelectable class has the former behaviour.
Goffi <goffi@goffi.org>
parents: 94
diff changeset
339
9
6650747dfdcb primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 8
diff changeset
340 class ClickableText(SelectableText):
6650747dfdcb primitivus: contact list enhancement
Goffi <goffi@goffi.org>
parents: 8
diff changeset
341 signals = SelectableText.signals + ['click']
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
342
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
343 def set_state(self, selected, invisible=False):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
344 super(ClickableText,self).set_state(False,True)
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
345 if not invisible:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
346 self._emit('click')
8
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
347
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
348
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
349 class CustomButton(ClickableText):
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
350
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
351 def __init__(self, label, on_press=None, user_data=None, left_border="[ ", right_border=" ]", align="left"):
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
352 self.label = label
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
353 self.left_border = left_border
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
354 self.right_border = right_border
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
355 super(CustomButton, self).__init__([left_border, label, right_border], align=align)
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
356 self.size = len(self.get_text())
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
357 if on_press:
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
358 urwid.connect_signal(self, 'click', on_press, user_data)
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
359
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
360 def get_size(self):
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
361 """Return representation size of the button"""
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
362 return self.size
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
363
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
364 def get_label(self):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
365 return self.label[1] if isinstance(self.label,tuple) else self.label
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
366
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
367 def set_label(self, label):
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
368 self.label = label
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
369 self.set_text([self.left_border, label, self.right_border])
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
370
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
371
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
372 class ListOption(str):
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
373 """Unicode which manage label and value
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
374
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
375 This class similar to unicode, but which make the difference between value and label
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
376 label is show when use as unicode, the .value attribute contain the actual value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
377 Can be initialised with:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
378 - basestring (label = value = given string)
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
379 - a tuple with (value, label)
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
380 XXX: comparaison is made against value, not the label which is the one displayed
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
381 """
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
382
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
383 def __new__(cls, option):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
384 if (isinstance(option, cls)):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
385 return option
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
386 elif isinstance(option, str):
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
387 value = label = option
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
388 elif (isinstance(option, tuple) and len(option) == 2):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
389 value, label = option
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
390 else:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
391 raise NotImplementedError
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
392 if not label:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
393 label = value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
394 instance = super(ListOption, cls).__new__(cls, label)
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
395 instance._value = value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
396 return instance
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
397
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
398 def __eq__(self, other):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
399 # XXX: try to compare values, if other has no value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
400 # (e.g. unicode string) try to compare to other itself
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
401 try:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
402 return self._value == other._value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
403 except AttributeError:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
404 return self._value == other
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
405
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
406 def __ne__(self, other):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
407 # XXX: see __eq__
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
408 try:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
409 return self._value != other._value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
410 except AttributeError:
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
411 return self._value != other
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
412
144
bfab04d0a745 ListOption: add __hash__ method
Goffi <goffi@goffi.org>
parents: 143
diff changeset
413 def __hash__(self):
bfab04d0a745 ListOption: add __hash__ method
Goffi <goffi@goffi.org>
parents: 143
diff changeset
414 return hash(self._value)
bfab04d0a745 ListOption: add __hash__ method
Goffi <goffi@goffi.org>
parents: 143
diff changeset
415
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
416 @property
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
417 def value(self):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
418 """ return option value """
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
419 return self._value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
420
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
421 @value.setter
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
422 def value(self, value):
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
423 self._value = value
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
424
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
425 @staticmethod
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
426 def from_options(options):
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
427 """ convert a list of string/tuple options to a list of listOption
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
428 @param options: list of managed option type (basestring, tuple)
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
429 return: list of ListOption
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
430 """
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
431 return [(ListOption(option)) for option in options]
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
432
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
433
91
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
434 class UnselectableListBox(urwid.ListBox):
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
435 """List box that can be unselectable if all widget are unselectable and visible"""
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
436
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
437 def __init__(self, body):
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
438 super(UnselectableListBox, self).__init__(body)
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
439 self.__size_cache = None
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
440
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
441 def selectable(self):
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
442 """Selectable that return False if everything is visible and nothing is selectable"""
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
443 if self.__size_cache is None:
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
444 return self._selectable
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
445 middle, top, bottom = self.calculate_visible(self.__size_cache, self.__focus_cache)
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
446 if top is None or bottom is None:
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
447 return True
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
448 if top[0] or bottom[0]:
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
449 # if not everything is visible, we can select
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
450 return True
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
451 for wid in self.body:
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
452 # if any widget is selectable, we can select
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
453 if wid.selectable():
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
454 return True
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
455 return False
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
456
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
457 def render(self, size, focus=False):
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
458 """Call ListBox render, but keep size and focus in cache"""
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
459 self.__size_cache = size
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
460 self.__focus_cache = focus
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
461 return super(UnselectableListBox, self).render(size, focus)
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
462
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
463
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
464 class SimpleListWalkerWithCb(urwid.SimpleListWalker):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
465 """a SimpleListWalker which call callbacks on items changes"""
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
466
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
467 def __init__(self, contents, on_new=None, on_delete=None):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
468 """
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
469 @param contents: list to copy into this object
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
470 @param on_new: callback to call when an item is added
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
471 @param on_delete: callback to call when an item is deleted
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
472 """
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
473 # XXX: we can't use modified signal as it doesn't return the modified item
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
474 super(SimpleListWalkerWithCb, self).__init__(contents)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
475 for content in contents:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
476 on_new(content)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
477 self._on_new = on_new
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
478 self._on_delete = on_delete
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
479
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
480 def __cb_single(self, item, cb):
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
481 try:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
482 cb(item)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
483 except TypeError:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
484 pass
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
485
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
486 def __cb_multi(self, items, cb):
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
487 if cb is not None:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
488 for item in items:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
489 cb(item)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
490
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
491 def __add__(self, new_list):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
492 self.__cb_multi(new_list, self._on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
493 return super(SimpleListWalkerWithCb, self).__add__(new_list)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
494
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
495 def __delitem__(self, item):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
496 self.__cb_single(item, self._on_delete)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
497 return super(SimpleListWalkerWithCb, self).__delitem__(item)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
498
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
499 def __delslice__(self, i,j):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
500 items = super(SimpleListWalkerWithCb, self).__getslice__(i,j)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
501 self.__cb_multi(items, self._on_delete)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
502 return super(SimpleListWalkerWithCb, self).__delslice(i,j)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
503
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
504 def __iadd__(self, y):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
505 raise NotImplementedError
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
506
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
507 def __imul__(self, y):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
508 raise NotImplementedError
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
509
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
510 def __mul__(self, n):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
511 raise NotImplementedError
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
512
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
513 def __rmul__(self, n):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
514 raise NotImplementedError
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
515
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
516 def __setitem__(self, i, y):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
517 parent = super(SimpleListWalkerWithCb, self)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
518 self.__cb_single(y, self._on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
519 to_delete = parent.__getitem__(i)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
520 self.__cb_single(to_delete, self._on_delete)
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
521 return parent.__setitem__(i, y)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
522
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
523 def __setslice__(self, i, j, y):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
524 parent = super(SimpleListWalkerWithCb, self)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
525 items_to_delete = parent.__getslice__(i,j)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
526 self.__cb_multi(items_to_delete, self._on_delete)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
527 if hasattr(y, '__iter__'):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
528 self.__cb_multi(y, self._on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
529 else:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
530 self.__cb_single(y, self._on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
531 return parent.__setslice__(i, j, y)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
532
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
533 def append(self, obj):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
534 self.__cb_single(obj, self._on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
535 return super(SimpleListWalkerWithCb, self).append(obj)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
536
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
537 def extend(self, it):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
538 self.__cb_multi(it, self.__on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
539 return super(SimpleListWalkerWithCb, self).extend(it)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
540
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
541 def insert(self, idx, obj):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
542 self.__cb_single(obj, self.__on_new)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
543 return super(SimpleListWalkerWithCb, self).insert(idx, obj)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
544
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
545 def pop(self, idx=None):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
546 if idx is None:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
547 idx=len(self)-1
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
548
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
549 parent = super(SimpleListWalkerWithCb, self)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
550 to_remove = parent.__getitem__(idx)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
551 self.__cb_single(to_remove, self._on_delete)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
552 return parent.pop(idx)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
553
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
554 def remove(self, val):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
555 ret = super(SimpleListWalkerWithCb, self).remove(val)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
556 self.__cb_single(val, self._on_delete)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
557 return ret
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
558
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
559
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
560 class GenericList(urwid.ListBox):
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
561 signals = ['click','change']
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
562
150
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
563 def __init__(self, options, style=None, align='left', option_type=SelectableText, on_click=None, on_change=None, user_data=None):
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
564 """Widget managing list of string and their selection
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
565
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
566 @param options: list of strings used for options
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
567 @param style: list of string:
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
568 - 'single' if only one must be selected
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
569 - 'no_first_select' nothing selected when list is first displayed
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
570 - 'can_select_none' if we can select nothing
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
571 @param align: alignement of text inside the list
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
572 @param option_type: callable (usually a class) which will be called with:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
573 - option as first argument
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
574 - align=align as keyword argument
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
575 @param on_click: method called when click signal is emited
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
576 @param on_change: method called when change signal is emited
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
577 @param user_data: data sent to the callback for click signal
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
578 """
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
579 if style is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
580 style = []
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
581 self.single = 'single' in style
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
582 self.no_first_select = 'no_first_select' in style
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
583 self.can_select_none = 'can_select_none' in style
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
584 self.align = align
8
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
585 self.option_type = option_type
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
586 self.first_display = True
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
587
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
588 if on_click:
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
589 urwid.connect_signal(self, 'click', on_click, user_data)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
590
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
591 if on_change:
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
592 urwid.connect_signal(self, 'change', on_change, user_data)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
593
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
594 self.content = SimpleListWalkerWithCb([], self._add_signals, lambda widget: self._emit('change'))
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
595 super(GenericList, self).__init__(self.content)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
596 self.change_values(options)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
597
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
598 def _add_signals(self, widget):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
599 for signal, callback in (('change', self._on_state_change), ('click', self._on_click)):
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
600 try:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
601 urwid.connect_signal(widget, signal, callback)
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
602 except NameError:
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
603 pass #the widget given doesn't support the signal
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
604
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
605 @property
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
606 def contents(self):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
607 return self.content
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
608
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
609 def _on_state_change(self, widget, selected, *args):
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
610 if self.single:
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
611 if not selected and not self.can_select_none:
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
612 #if in single mode, it's forbidden to unselect a value
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
613 widget.set_state(True, invisible=True)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
614 return
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
615 if selected:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
616 self.unselect_all(invisible=True)
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
617 widget.set_state(True, invisible=True)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
618 self._emit("change", widget, selected, *args)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
619
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
620 def _on_click(self, widget, *args):
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
621 if widget not in self.content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
622 urwid.disconnect_signal(widget, "click", self._on_click)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
623 return
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
624 self._emit("click", widget, *args)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
625
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
626 def unselect_all(self, invisible=False):
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
627 for widget in self.content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
628 if widget.get_state():
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
629 widget.set_state(False, invisible)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
630 widget._invalidate()
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
631
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
632 def delete_value(self, value):
2
07b7dcd314ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 1
diff changeset
633 """Delete the first value equal to the param given"""
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
634 for widget in self.content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
635 if widget.get_value() == value:
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
636 self.content.remove(widget)
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
637 self._emit('change')
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
638 return
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
639 raise ValueError("%s ==> %s" % (str(value),str(self.content)))
2
07b7dcd314ff primitivus: basic contact list, connexion now work \o/
Goffi <goffi@goffi.org>
parents: 1
diff changeset
640
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
641 def get_selected_value(self):
1
a5c9603dac37 primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 0
diff changeset
642 """Convenience method to get the value selected as a string in single mode, or None"""
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
643 values = self.get_selected_values()
1
a5c9603dac37 primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 0
diff changeset
644 return values[0] if values else None
a5c9603dac37 primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 0
diff changeset
645
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
646 def get_all_values(self):
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
647 """Return values of all items"""
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
648 return [widget.get_value() for widget in self.content]
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
649
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
650 def get_selected_values(self):
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
651 """Return values of selected items"""
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
652 result = []
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
653 for widget in self.content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
654 if widget.get_state():
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
655 result.append(widget.get_value())
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
656 return result
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
657
150
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
658 def on_option_change(self, wid, *args, **kwargs):
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
659 if self.single:
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
660 for w in self.content:
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
661 if w is not wid:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
662 w.set_state(False, invisible=True)
150
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
663
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
664 def change_values(self, new_values):
68
5c28bb50ae0d new ListOption class which work like unicode, but make the difference between value and label, so the displayed text can be different from the actual value.
Goffi <goffi@goffi.org>
parents: 67
diff changeset
665 """Change all values in one shot"""
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
666 new_values = ListOption.from_options(new_values)
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
667 old_selected = self.get_selected_values() if not self.first_display else []
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
668 widgets = []
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
669 for option in new_values:
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
670 widget = self.option_type(option, align=self.align)
150
aa8f46b43a71 fix `single` style in GenericList:
Goffi <goffi@goffi.org>
parents: 144
diff changeset
671 urwid.connect_signal(widget, "change", self.on_option_change)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
672 if not self.first_display and option in old_selected:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
673 widget.set_state(True)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
674 widgets.append(widget)
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
675 self.content[:] = widgets
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
676 if self.first_display and self.single and new_values and not self.no_first_select:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
677 self.content[0].set_state(True)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
678 self._emit('change')
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
679 self.first_display = False
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
680
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
681 def select_value(self, value, move_focus=True):
80
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
682 """Select the first item which has the given value.
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
683
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
684 @param value
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
685 @param move_focus (bool):
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
686 - True to move the focus on the selected value,
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
687 - False to leave the focus position unchanged.
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
688
80
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
689 """
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
690 self.unselect_all()
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
691 idx = 0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
692 for widget in self.content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
693 if widget.get_value() == value:
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
694 widget.set_state(True)
80
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
695 if move_focus:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
696 self.focus_position = idx
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
697 return
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
698 idx+=1
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
699
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
700 def select_values(self, values, move_focus=True):
80
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
701 """Select all the given values.
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
702
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
703 @param values [set, list]
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
704 @param move_focus (boolean): True to move the focus on the last selected value,
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
705 False to leave the focus position unchanged.
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
706 """
78
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
707 if self.single:
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
708 if values:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
709 self.select_value(values[-1], move_focus)
78
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
710 return
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
711 self.unselect_all()
78
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
712 for value in values:
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
713 idx = 0
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
714 for widget in self.content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
715 if widget.get_value() == value:
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
716 widget.set_state(True)
80
2d66ac0f4d75 add move_focus parameter to the methods selectValue(s) of list widgets
souliane <souliane@mailoo.org>
parents: 79
diff changeset
717 if move_focus:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
718 self.focus_position = idx
78
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
719 idx += 1
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
720
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
721
67
c270867a80f9 updated deprecated FlowWidget use
Goffi <goffi@goffi.org>
parents: 66
diff changeset
722 class List(urwid.Widget):
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
723 """FlowWidget list, same arguments as GenericList, with an additional one 'max_height'"""
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
724 signals = ['click','change']
67
c270867a80f9 updated deprecated FlowWidget use
Goffi <goffi@goffi.org>
parents: 66
diff changeset
725 _sizing = frozenset(['flow'])
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
726
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
727 def __init__(self, options, style=None, max_height=5, align='left', option_type = SelectableText, on_click=None, on_change=None, user_data=None):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
728 if style is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
729 style = []
8
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
730 self.genericList = GenericList(options, style, align, option_type, on_click, on_change, user_data)
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
731 urwid.connect_signal(self.genericList, 'change', lambda *args: self._emit('change'))
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
732 self.max_height = max_height
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
733
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
734 @property
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
735 def contents(self):
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
736 return self.genericList.content
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
737
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
738 def selectable(self):
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
739 return True
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
740
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
741 def get_cursor_coords(self, size):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
742 return self.genericList.get_cursor_coords((size[0], self._get_height(size, True)))
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
743
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
744 def keypress(self, size, key):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
745 return self.display_widget(size,True).keypress(size, key)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
746
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
747 def unselect_all(self, invisible=False):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
748 return self.genericList.unselect_all(invisible)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
749
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
750 def delete_value(self, value):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
751 return self.genericList.delete_value(value)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
752
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
753 def get_selected_value(self):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
754 return self.genericList.get_selected_value()
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
755
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
756 def get_all_values(self):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
757 return self.genericList.get_all_values()
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
758
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
759 def get_selected_values(self):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
760 return self.genericList.get_selected_values()
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
761
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
762 def change_values(self, new_values):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
763 return self.genericList.change_values(new_values)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
764
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
765 def select_value(self, value, move_focus=True):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
766 return self.genericList.select_value(value, move_focus)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
767
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
768 def select_values(self, values, move_focus=True):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
769 return self.genericList.select_values(values, move_focus)
78
56c02f4731f9 added selectValues for lists widgets that support multi-selection
souliane <souliane@mailoo.org>
parents: 77
diff changeset
770
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
771 def render(self, size, focus=False):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
772 return self.display_widget(size, focus).render(size, focus)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
773
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
774 def rows(self, size, focus=False):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
775 return self.display_widget(size, focus).rows(size, focus)
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
776
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
777 def _get_height(self, size, focus):
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
778 list_size = sum([wid.rows(size, focus) for wid in self.genericList.content])
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
779 height = min(list_size,self.max_height) or 1
110
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
780 return height
436076392538 GenericList + List fixes, better 'change' signal handling, added GenericList.contents property (addind or removing an item there trigger the 'change' signal)
Goffi <goffi@goffi.org>
parents: 109
diff changeset
781
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
782 def display_widget(self, size, focus):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
783 return urwid.BoxAdapter(self.genericList, self._get_height(size, focus))
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
784
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
785
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
786 ## MISC ##
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
787
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
788 class NotificationBar(urwid.WidgetWrap):
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
789 """Bar used to show misc information to user"""
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
790 signals = ['change']
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
791
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
792 def __init__(self):
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
793 self.waitNotifs = urwid.Text('')
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
794 self.message = ClickableText('')
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
795 urwid.connect_signal(self.message, 'click', lambda wid: self.show_next())
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
796 self.progress = ClickableText('')
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
797 self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message,('fixed',4,self.progress)])
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
798 urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs'))
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
799 self.notifs = []
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
800
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
801 def _mod_queue(self):
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
802 """must be called each time the notifications queue is changed"""
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
803 self.waitNotifs.set_text(('notifs',"(%i)" % len(self.notifs) if self.notifs else ''))
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
804 self._emit('change')
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
805
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
806 def set_progress(self,percentage):
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
807 """Define the progression to show on the right side of the bar"""
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
808 if percentage == None:
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
809 self.progress.set_text('')
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
810 else:
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
811 self.progress.set_text(('notifs','%02i%%' % percentage))
97
8f5afab948a0 NotificationBar focus fix
Goffi <goffi@goffi.org>
parents: 96
diff changeset
812 if self.columns.focus != self.progress:
8f5afab948a0 NotificationBar focus fix
Goffi <goffi@goffi.org>
parents: 96
diff changeset
813 self.columns.focus_position = len(self.columns.contents)-1
22
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
814 self._emit('change')
dfabea6f73b5 Primitivus: AdvancedEdit and Notification bar improved
Goffi <goffi@goffi.org>
parents: 21
diff changeset
815
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
816 def add_pop_up(self, pop_up_widget):
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
817 """Add a popup to the waiting queue"""
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
818 self.notifs.append(('popup',pop_up_widget))
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
819 self._mod_queue()
114
bf38c1c0db3b notificationBar: added removePopUp to remove a widget in the queue
Goffi <goffi@goffi.org>
parents: 113
diff changeset
820
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
821 def remove_pop_up(self, pop_up_widget):
114
bf38c1c0db3b notificationBar: added removePopUp to remove a widget in the queue
Goffi <goffi@goffi.org>
parents: 113
diff changeset
822 """Remove a popup from the waiting queue"""
117
0cf705af755c NotificationBar: fixed removePopUp
Goffi <goffi@goffi.org>
parents: 116
diff changeset
823 for idx, (wid_type, widget) in enumerate(self.notifs):
0cf705af755c NotificationBar: fixed removePopUp
Goffi <goffi@goffi.org>
parents: 116
diff changeset
824 if widget == pop_up_widget:
0cf705af755c NotificationBar: fixed removePopUp
Goffi <goffi@goffi.org>
parents: 116
diff changeset
825 del self.notifs[idx]
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
826 self._mod_queue()
117
0cf705af755c NotificationBar: fixed removePopUp
Goffi <goffi@goffi.org>
parents: 116
diff changeset
827 return
0cf705af755c NotificationBar: fixed removePopUp
Goffi <goffi@goffi.org>
parents: 116
diff changeset
828
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
829 raise ValueError("trying to remove an unknown pop_up_widget")
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
830
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
831 def add_message(self, message):
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
832 "Add a message to the notificatio bar"
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
833 if not self.message.get_text():
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
834 self.message.set_text(('notifs',message))
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
835 self._invalidate()
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
836 self._emit('change')
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
837 else:
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
838 self.notifs.append(('message',message))
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
839 self._mod_queue()
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
840
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
841 def show_next(self):
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
842 """Show next message if any, else delete current message"""
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
843 found = None
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
844 for notif in self.notifs:
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
845 if notif[0] == "message":
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
846 found = notif
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
847 break
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
848 if found:
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
849 self.notifs.remove(found)
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
850 self.message.set_text(('notifs',found[1]))
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
851 self._mod_queue()
97
8f5afab948a0 NotificationBar focus fix
Goffi <goffi@goffi.org>
parents: 96
diff changeset
852 self.focus_possition = 1
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
853 else:
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
854 self.message.set_text('')
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
855 self._emit('change')
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
856
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
857 def get_next_popup(self):
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
858 """Return next pop-up and remove it from the queue
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
859 @return: pop-up or None if there is no more in the queue"""
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
860 ret = None
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
861 for notif in self.notifs:
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
862 if notif[0] == 'popup':
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
863 ret = notif[1]
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
864 break
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
865 if ret:
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
866 self.notifs.remove(notif)
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
867 self._mod_queue()
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
868 return ret
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
869
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
870 def is_queue_empty(self):
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
871 return not bool(self.notifs)
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
872
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
873 def can_hide(self):
97
8f5afab948a0 NotificationBar focus fix
Goffi <goffi@goffi.org>
parents: 96
diff changeset
874 """Return True if there is no important information to show"""
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
875 return self.is_queue_empty() and not self.message.get_text() and not self.progress.get_text()
16
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
876
263fe4d067ad Primitivus: notification bar, first draft
Goffi <goffi@goffi.org>
parents: 15
diff changeset
877
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
878 class MenuBox(urwid.WidgetWrap):
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
879 """Show menu items of a category in a box"""
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
880 signals = ['click']
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
881
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
882 def __init__(self,parent,items):
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
883 self.parent = parent
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
884 self.selected = None
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
885 content = urwid.SimpleListWalker([ClickableText(('menuitem',text)) for text in items])
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
886 for wid in content:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
887 urwid.connect_signal(wid, 'click', self.on_click)
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
888
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
889 self.listBox = urwid.ListBox(content)
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
890 menubox = urwid.LineBox(urwid.BoxAdapter(self.listBox,len(items)))
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
891 urwid.WidgetWrap.__init__(self,menubox)
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
892
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
893 def get_value(self):
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
894 return self.selected
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
895
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
896 def keypress(self, size, key):
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
897 if key==a_key['MENU_BOX_UP']:
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
898 if self.listBox.get_focus()[1] == 0:
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
899 self.parent.keypress(size, key)
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
900 elif key in (a_key['MENU_BOX_LEFT'], a_key['MENU_BOX_RIGHT']):
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
901 self.parent.keypress(size,'up')
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
902 self.parent.keypress(size,key)
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
903 return super(MenuBox,self).keypress(size,key)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
904
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
905 def mouse_event(self, size, event, button, x, y, focus):
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
906 if button == 3:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
907 self.parent.keypress(size,'up')
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
908 return True
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
909 return super(MenuBox,self).mouse_event(size, event, button, x, y, focus)
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
910
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
911 def on_click(self, wid):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
912 self.selected = wid.get_value()
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
913 self._emit('click')
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
914
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
915
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
916 class Menu(urwid.WidgetWrap):
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
917
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
918 def __init__(self,loop, x_orig=0):
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
919 """Menu widget
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
920 @param loop: main loop of urwid
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
921 @param x_orig: absolute start of the abscissa
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
922 """
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
923 self.loop = loop
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
924 self.menu_keys = []
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
925 self.menu = {}
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
926 self.x_orig = x_orig
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
927 self.shortcuts = {} #keyboard shortcuts
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
928 self.save_bottom = None
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
929 col_rol = ColumnsRoller()
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
930 urwid.WidgetWrap.__init__(self, urwid.AttrMap(col_rol,'menubar'))
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
931
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
932 def selectable(self):
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
933 return True
13
8cccbaadb9c5 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 12
diff changeset
934
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
935 def get_menu_size(self):
13
8cccbaadb9c5 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 12
diff changeset
936 """return the current number of categories in this menu"""
8cccbaadb9c5 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 12
diff changeset
937 return len(self.menu_keys)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
938
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
939 def set_orig_x(self, orig_x):
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
940 self.x_orig = orig_x
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
941
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
942 def __build_overlay(self, menu_key, columns):
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
943 """Build the overlay menu which show menuitems
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
944 @param menu_key: name of the category
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
945 @param columns: column number where the menubox must be displayed"""
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
946 max_len = 0
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
947 for item in self.menu[menu_key]:
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
948 if len(item[0]) > max_len:
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
949 max_len = len(item[0])
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
950
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
951 self.save_bottom = self.loop.widget
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
952 menu_box = MenuBox(self,[item[0] for item in self.menu[menu_key]])
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
953 urwid.connect_signal(menu_box, 'click', self.on_item_click)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
954
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
955 self.loop.widget = urwid.Overlay(urwid.AttrMap(menu_box,'menubar'),self.save_bottom,('fixed left', columns),max_len+2,('fixed top',1),None)
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
956
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
957 def keypress(self, size, key):
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
958 if key == a_key['MENU_DOWN']:
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
959 key = 'enter'
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
960 elif key == a_key['MENU_UP']:
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
961 if self.save_bottom:
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
962 self.loop.widget = self.save_bottom
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
963 self.save_bottom = None
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
964
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
965 return self._w.base_widget.keypress(size, key)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
966
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
967 def check_shortcuts(self, key):
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
968 for shortcut in list(self.shortcuts.keys()):
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
969 if key == shortcut:
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
970 category, item, callback = self.shortcuts[shortcut]
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
971 callback((category, item))
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
972 return key
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
973
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
974 def add_menu(self, category, item=None, callback=None, shortcut=None):
79
33677d99ebdf addMenu allows to add a new category without adding an item
souliane <souliane@mailoo.org>
parents: 78
diff changeset
975 """Create the category if new and add a menu item (if item is not None).
33677d99ebdf addMenu allows to add a new category without adding an item
souliane <souliane@mailoo.org>
parents: 78
diff changeset
976
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
977 @param category: category of the menu (e.g. File/Edit)
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
978 @param item: menu item (e.g. new/close/about)
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
979 @callback: method to call when item is selected"""
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
980 if not category in list(self.menu.keys()):
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
981 self.menu_keys.append(category)
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
982 self.menu[category] = []
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
983 button = CustomButton(('menubar',category), self.on_category_click,
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
984 left_border = ('menubar',"[ "),
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
985 right_border = ('menubar'," ]"))
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
986 self._w.base_widget.add_widget(button,button.get_size())
79
33677d99ebdf addMenu allows to add a new category without adding an item
souliane <souliane@mailoo.org>
parents: 78
diff changeset
987 if not item:
33677d99ebdf addMenu allows to add a new category without adding an item
souliane <souliane@mailoo.org>
parents: 78
diff changeset
988 return
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
989 self.menu[category].append((item, callback))
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
990 if shortcut:
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
991 assert(shortcut not in list(self.shortcuts.keys()))
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
992 self.shortcuts[shortcut] = (category, item, callback)
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
993
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
994 def on_item_click(self, widget):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
995 category = self._w.base_widget.get_selected().get_label()
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
996 item = widget.get_value()
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
997 callback = None
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
998 for menu_item in self.menu[category]:
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
999 if item == menu_item[0]:
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
1000 callback = menu_item[1]
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
1001 break
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
1002 if callback:
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1003 self.keypress(None, a_key['MENU_UP'])
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
1004 callback((category, item))
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1005
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1006 def on_category_click(self, button):
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1007 self.__build_overlay(button.get_label(),
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1008 self.x_orig + self._w.base_widget.get_start_col(button))
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1009
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1010 MenuItem = collections.namedtuple('MenuItem', ('name', 'widget'))
11
983840425a55 Primitivus: menu is now working
Goffi <goffi@goffi.org>
parents: 10
diff changeset
1011
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1012 class MenuRoller(urwid.WidgetWrap):
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1013
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1014 def __init__(self, menus_list):
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1015 """Create a MenuRoller
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1016
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1017 @param menus_list: list of tuples which can be either:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1018 - (name, Menu instance)
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1019 - (name, Menu instance, id)
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1020 """
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1021 assert menus_list
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1022 self.selected = None
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1023 self.menu_items = collections.OrderedDict()
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1024
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1025 self.columns = urwid.Columns([urwid.Text(''),urwid.Text('')])
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1026 urwid.WidgetWrap.__init__(self, self.columns)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1027
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1028 for menu_tuple in menus_list:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1029 try:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1030 name, menu, id_ = menu_tuple
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1031 except ValueError:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1032 name, menu = menu_tuple
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1033 id_ = None
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1034 self.add_menu(name, menu, id_)
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1035
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1036 def _show_selected(self):
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1037 """show menu selected"""
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1038 if self.selected is None:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1039 self.columns.contents[0] = (urwid.Text(''), ('given', 0, False))
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1040 self.columns.contents[1] = (urwid.Text(''), ('weight', 1, False))
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1041 else:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1042 menu_item = self.menu_items[self.selected]
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1043 name_txt = '\u21c9 ' + menu_item.name + ' \u21c7 '
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1044 current_name = ClickableText(name_txt)
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1045 name_len = len(name_txt)
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1046 current_menu = menu_item.widget
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1047 current_menu.set_orig_x(name_len)
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1048 self.columns.contents[0] = (current_name, ('given', name_len, False))
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1049 self.columns.contents[1] = (current_menu, ('weight', 1, False))
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1050
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1051 def keypress(self, size, key):
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1052 menu_ids = list(self.menu_items.keys())
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1053 try:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1054 idx = menu_ids.index(self.selected)
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1055 except ValueError:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1056 return super(MenuRoller, self).keypress(size, key)
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1057
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1058 if key==a_key['MENU_ROLLER_UP']:
88
c95462c21966 primitivus (menu): MENU_ROLLER_(UP,DOWN) are not transmitted to next widget anymore
Goffi <goffi@goffi.org>
parents: 84
diff changeset
1059 if self.columns.get_focus_column()==0:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1060 if idx > 0:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1061 self.selected = menu_ids[idx-1]
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1062 self._show_selected()
77
e1655ba45fae MenuRoller doesn't propagate key pressed anymore if it manage a 'up' or 'down' event
Goffi <goffi@goffi.org>
parents: 76
diff changeset
1063 return
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1064 elif key==a_key['MENU_ROLLER_DOWN']:
88
c95462c21966 primitivus (menu): MENU_ROLLER_(UP,DOWN) are not transmitted to next widget anymore
Goffi <goffi@goffi.org>
parents: 84
diff changeset
1065 if self.columns.get_focus_column()==0:
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1066 if idx < len(menu_ids)-1:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1067 self.selected = menu_ids[idx+1]
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1068 self._show_selected()
77
e1655ba45fae MenuRoller doesn't propagate key pressed anymore if it manage a 'up' or 'down' event
Goffi <goffi@goffi.org>
parents: 76
diff changeset
1069 return
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1070 elif key==a_key['MENU_ROLLER_RIGHT']:
13
8cccbaadb9c5 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 12
diff changeset
1071 if self.columns.get_focus_column()==0 and \
65
090f3e0754d3 fix for recent urwid versions (> 1.1.0)
Goffi <goffi@goffi.org>
parents: 64
diff changeset
1072 (isinstance(self.columns.contents[1][0], urwid.Text) or \
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1073 self.menu_items[self.selected].widget.get_menu_size()==0):
13
8cccbaadb9c5 Primitivus: menu roller doesn't go anymore on a menu if it's empty
Goffi <goffi@goffi.org>
parents: 12
diff changeset
1074 return #if we have no menu or the menu is empty, we don't go the right column
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1075
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1076 return super(MenuRoller, self).keypress(size, key)
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1077
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1078 def add_menu(self, name, widget, menu_id=None):
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1079 """Add a menu
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1080
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1081 @param name: name of the menu to add, it name already exists, menu is not added
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1082 @param widget: instance of Menu
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1083 @param menu_id: id to use of this menu, or None to generate
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1084 @return: menu_id
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1085 """
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1086 names = {menu_item.name: id_ for id_, menu_item in self.menu_items.items()}
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1087
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1088 if name not in names:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1089 id_ = menu_id or str(uuid.uuid4())
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1090 if id_ in self.menu_items:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1091 raise ValueError('Conflict: the id [{}] is already used'.format(id_))
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1092 self.menu_items[id_] = MenuItem(name, widget)
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1093 else:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1094 id_ = names[name]
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1095 menu_item = self.menu_items[id_]
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1096 if menu_item.widget is not widget:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1097 raise ValueError("The menu with id [{}] exists and doesn't contain the given instance. Use replace_menu if you want to change the menu.".format(id_))
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1098 if self.selected is None:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1099 self.selected = id_
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1100 self._show_selected()
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1101 return id_
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1102
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1103 def replace_menu(self, name, widget, menu_id):
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1104 """Add a menu or replace it if the id already exists
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1105
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1106 @param name: name of the menu to add, it name already exists, menu is not added
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1107 @param widget: instance of Menu
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1108 @param menu_id: id or the menu
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1109 """
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1110 assert menu_id is not None
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1111 if menu_id in self.menu_items:
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1112 del self.menu_items[menu_id]
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1113 self.add_menu(name, widget, menu_id)
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1114 if self.selected == menu_id:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1115 self._show_selected() #if we are on the menu, we update it
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1116
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1117 def remove_menu(self, menu_id):
107
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1118 del self.menu_items[menu_id]
ed2675f92f7c menus management improvment
Goffi <goffi@goffi.org>
parents: 99
diff changeset
1119 if self.selected == menu_id:
113
77ccc1dd2261 MenuRoller: fixed crash when deleting a displayed menu
Goffi <goffi@goffi.org>
parents: 111
diff changeset
1120 try:
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1121 self.selected = next(iter(self.menu_items.keys()))
113
77ccc1dd2261 MenuRoller: fixed crash when deleting a displayed menu
Goffi <goffi@goffi.org>
parents: 111
diff changeset
1122 except StopIteration:
77ccc1dd2261 MenuRoller: fixed crash when deleting a displayed menu
Goffi <goffi@goffi.org>
parents: 111
diff changeset
1123 self.selected = None
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1124 self._show_selected()
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1125
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1126 def check_shortcuts(self, key):
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1127 for menu_item in list(self.menu_items.values()):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1128 key = menu_item.widget.check_shortcuts(key)
12
7e63429cc929 Primitivus: menu are now managed and fully working
Goffi <goffi@goffi.org>
parents: 11
diff changeset
1129 return key
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1130
10
024b79b61a31 Primitivus: misc fixes + menubar first draft
Goffi <goffi@goffi.org>
parents: 9
diff changeset
1131
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1132 ## DIALOGS ##
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1133
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1134 class GenericDialog(urwid.WidgetWrap):
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1135
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1136 def __init__(self, widgets_lst, title, style=None, **kwargs):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1137 if style is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1138 style = []
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1139 frame_header = urwid.AttrMap(urwid.Text(title,'center'),'title')
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1140
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1141 self.buttons = collections.OrderedDict()
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1142
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1143 if "OK/CANCEL" in style:
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1144 self.buttons['cancel'] = urwid.Button(_("Cancel"), kwargs.get('cancel_cb'), kwargs.get('cancel_value'))
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1145 self.buttons['ok'] = urwid.Button(_("Ok"), kwargs.get('ok_cb'), kwargs.get('ok_value'))
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1146 elif "YES/NO" in style:
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1147 self.buttons['yes'] = urwid.Button(_("Yes"), kwargs.get('yes_cb'), kwargs.get('yes_value'))
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1148 self.buttons['no'] = urwid.Button(_("No"), kwargs.get('no_cb'), kwargs.get('no_value'))
1
a5c9603dac37 primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 0
diff changeset
1149 if "OK" in style:
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1150 self.buttons['ok'] = urwid.Button(_("Ok"), kwargs.get('ok_cb'), kwargs.get('ok_value'))
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1151 if self.buttons:
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1152 buttons_flow = urwid.GridFlow(list(self.buttons.values()), max([len(button.get_label()) for button in self.buttons.values()])+4, 1, 1, 'center')
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1153 body_content = urwid.SimpleListWalker(widgets_lst)
91
b447a9c6f1d3 added UnselectableListBox which is a ListBox which can be unselectable when everything is not selectable and visible. Its is used in dialogs
Goffi <goffi@goffi.org>
parents: 90
diff changeset
1154 frame_body = UnselectableListBox(body_content)
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1155 frame = FocusFrame(frame_body, frame_header, buttons_flow if self.buttons else None, 'footer' if self.buttons else 'body')
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1156 decorated_frame = urwid.LineBox(frame)
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1157 urwid.WidgetWrap.__init__(self, decorated_frame)
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1158
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1159 def set_callback(self, name, callback, data=None):
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1160 """Set the callback associated with a button press
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1161
127
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1162 @param name: one of "ok", "cancel", "yes", "no"
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1163 @aram callback(callable): method to call on requested action
116
8e9a6a9c727e minor docstring fix
Goffi <goffi@goffi.org>
parents: 115
diff changeset
1164 @param data: argument to send to the callback (first one will be the button widget)
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1165 @raise KeyError if name is invalid
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1166 """
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1167 button = self.buttons[name]
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1168 urwid.connect_signal(button, 'click', callback, data)
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1169
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1170
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1171 class InputDialog(GenericDialog):
7
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
1172 """Dialog with an edit box"""
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1173
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1174 def __init__(self, title, instrucions, style=None, default_txt = '', **kwargs):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1175 if style is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1176 style = ['OK/CANCEL']
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1177 instr_wid = urwid.Text(instrucions+':')
127
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1178 self.edit = AdvancedEdit(edit_text=default_txt)
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1179 GenericDialog.__init__(self, [instr_wid, self.edit], title, style, ok_value=self.edit, **kwargs)
74
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1180 self._w.base_widget.focusposition = 'body'
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1181
127
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1182 @property
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1183 def text(self):
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1184 return self.edit.text
88722f920b3c added edit to access edit widget in InputDialog, and text property + minor docstring fixes
Goffi <goffi@goffi.org>
parents: 124
diff changeset
1185
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1186
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1187 class ConfirmDialog(GenericDialog):
7
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
1188 """Dialog with buttons for confirm or cancel an action"""
0
cb72fb2bfa0d Primitivus: profile manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1189
82
c456beff1779 ConfirmDialog now manage a body message
Goffi <goffi@goffi.org>
parents: 81
diff changeset
1190 def __init__(self, title, message=None, style=None, **kwargs):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1191 if style is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1192 style = ['YES/NO']
82
c456beff1779 ConfirmDialog now manage a body message
Goffi <goffi@goffi.org>
parents: 81
diff changeset
1193 GenericDialog.__init__(self,
c456beff1779 ConfirmDialog now manage a body message
Goffi <goffi@goffi.org>
parents: 81
diff changeset
1194 [urwid.Text(message, 'center')] if message is not None else [],
c456beff1779 ConfirmDialog now manage a body message
Goffi <goffi@goffi.org>
parents: 81
diff changeset
1195 title,
c456beff1779 ConfirmDialog now manage a body message
Goffi <goffi@goffi.org>
parents: 81
diff changeset
1196 style,
c456beff1779 ConfirmDialog now manage a body message
Goffi <goffi@goffi.org>
parents: 81
diff changeset
1197 **kwargs)
1
a5c9603dac37 primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 0
diff changeset
1198
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1199
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1200 class Alert(GenericDialog):
7
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
1201 """Dialog with just a message and a OK button"""
1
a5c9603dac37 primitivus: added Alert widget
Goffi <goffi@goffi.org>
parents: 0
diff changeset
1202
115
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1203 def __init__(self, title, message, style=None, **kwargs):
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1204 if style is None:
2b7eafea8bdb GenericDialog: buttons callbacks can now be set after widget creation
Goffi <goffi@goffi.org>
parents: 114
diff changeset
1205 style= ['OK']
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1206 GenericDialog.__init__(self, [urwid.Text(message, 'center')], title, style, ok_value=None, **kwargs)
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1207
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1208 ## CONTAINERS ##
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1209
67
c270867a80f9 updated deprecated FlowWidget use
Goffi <goffi@goffi.org>
parents: 66
diff changeset
1210 class ColumnsRoller(urwid.Widget):
c270867a80f9 updated deprecated FlowWidget use
Goffi <goffi@goffi.org>
parents: 66
diff changeset
1211 _sizing = frozenset(['flow'])
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1212
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1213 def __init__(self, widget_list = None, focus_column=0):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1214 self.widget_list = widget_list or []
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1215 self.focus_column = focus_column
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1216 self.__start = 0
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1217 self.__next = False
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1218
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1219 def add_widget(self, widget, width):
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1220 self.widget_list.append((width,widget))
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1221 if len(self.widget_list) == 1:
74
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1222 self.focus_position = 0
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1223
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1224 def get_start_col(self, widget):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1225 """Return the column of the left corner of the widget"""
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1226 start_col = 0
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1227 for wid in self.widget_list[self.__start:]:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1228 if wid[1] == widget:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1229 return start_col
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1230 start_col+=wid[0]
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1231 return None
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1232
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1233 def selectable(self):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1234 try:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1235 return self.widget_list[self.focus_column][1].selectable()
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1236 except IndexError:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1237 return False
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1238
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1239 def keypress(self, size, key):
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1240 if key==a_key['COLUMNS_ROLLER_LEFT']:
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1241 if self.focus_column>0:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1242 self.focus_column-=1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1243 self._invalidate()
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1244 return
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1245 if key==a_key['COLUMNS_ROLLER_RIGHT']:
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1246 if self.focus_column<len(self.widget_list)-1:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1247 self.focus_column+=1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1248 self._invalidate()
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1249 return
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1250 if self.focus_column<len(self.widget_list):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1251 return self.widget_list[self.focus_column][1].keypress(size,key)
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1252 return key
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1253
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1254 def get_selected(self):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1255 """Return selected widget"""
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1256 return self.widget_list[self.focus_column][1]
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1257
74
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1258 @property
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1259 def focus_position(self):
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1260 return self.focus_column
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1261
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1262 @focus_position.setter
0afff3c54b6e replaced deprecated set_focus by focus_position
Goffi <goffi@goffi.org>
parents: 71
diff changeset
1263 def focus_position(self, idx):
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1264 if idx>len(self.widget_list)-1:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1265 idx = len(self.widget_list)-1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1266 self.focus_column = idx
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1267
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1268 def rows(self,size,focus=False):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1269 return 1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1270
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1271 def __calculate_limits(self, size):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1272 (maxcol,) = size
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1273 _prev = _next = False
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1274 start_wid = 0
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1275 end_wid = len(self.widget_list)-1
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1276
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1277 total_wid = sum([w[0] for w in self.widget_list])
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1278 while total_wid > maxcol:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1279 if self.focus_column == end_wid:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1280 if not _prev:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1281 total_wid+=1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1282 _prev = True
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1283 total_wid-=self.widget_list[start_wid][0]
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1284 start_wid+=1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1285 else:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1286 if not _next:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1287 total_wid+=1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1288 _next = True
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1289 total_wid-=self.widget_list[end_wid][0]
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1290 end_wid-=1
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1291
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1292 cols_left = maxcol - total_wid
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1293 self.__start = start_wid #we need to keep it for get_start_col
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1294 return _prev,_next,start_wid,end_wid,cols_left
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1295
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1296
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1297 def mouse_event(self, size, event, button, x, y, focus):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1298 (maxcol,)=size
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1299
53
d34f2b0f68d3 urwid 1.0.0 update: fixed missing is_mouse_press
Goffi <goffi@goffi.org>
parents: 44
diff changeset
1300 if is_mouse_press(event) and button == 1:
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1301 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size)
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1302 if x==0 and _prev:
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1303 self.keypress(size, a_key['COLUMNS_ROLLER_LEFT'])
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1304 return True
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1305 if x==maxcol-1 and _next:
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1306 self.keypress(size, a_key['COLUMNS_ROLLER_RIGHT'])
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1307 return True
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1308
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1309 current_pos = 1 if _prev else 0
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1310 idx = 0
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1311 while current_pos<x and idx<len(self.widget_list):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1312 width,widget = self.widget_list[idx]
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1313 if x<=current_pos+width:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1314 self.focus_column = idx
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1315 self._invalidate()
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1316 if not hasattr(widget,'mouse_event'):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1317 return False
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1318 return widget.mouse_event((width,0), event, button,
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1319 x-current_pos, 0, focus)
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1320
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1321 current_pos+=self.widget_list[idx][0]
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1322 idx+=1
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1323
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1324 return False
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1325
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1326 def render(self, size, focus=False):
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1327 if not self.widget_list:
84
9f683df69a4c shortcut keys are now managed in separate module, with a class checking for conflicts
Goffi <goffi@goffi.org>
parents: 82
diff changeset
1328 return urwid.SolidCanvas(" ", size[0], 1)
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1329
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1330 _prev,_next,start_wid,end_wid,cols_left = self.__calculate_limits(size)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1331
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1332 idx=start_wid
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1333 render = []
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1334
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1335 for width,widget in self.widget_list[start_wid:end_wid+1]:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1336 _focus = idx == self.focus_column and focus
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1337 render.append((widget.render((width,),_focus),False,_focus,width))
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1338 idx+=1
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1339 if _prev:
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1340 render.insert(0,(urwid.Text(["◀"]).render((1,),False),False,False,1))
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1341 if _next:
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1342 render.append((urwid.Text(["▶"],align='right').render((1+cols_left,),False),False,False,1+cols_left))
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1343 else:
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1344 render.append((urwid.SolidCanvas(" "*cols_left, size[0], 1),False,False,cols_left))
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1345
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1346 return urwid.CanvasJoin(render)
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1347
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1348
96
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1349 class FocusPile(urwid.Pile):
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1350 """A Pile Widget which manage SàT Focus keys"""
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1351 _focus_inversed = False
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1352
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1353 def keypress(self, size, key):
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1354 ret = super(FocusPile, self).keypress(size, key)
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1355 if not ret:
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1356 return
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1357
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1358 if key in FOCUS_KEYS:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1359 direction, rotate = get_focus_direction(key, inversed = self._focus_inversed)
96
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1360 max_pos = len(self.contents) - 1
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1361 new_pos = self.focus_position + direction
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1362 if rotate:
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1363 if new_pos > max_pos:
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1364 new_pos = 0
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1365 elif new_pos < 0:
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1366 new_pos = max_pos
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1367 try:
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1368 self.focus_position = new_pos
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1369 except IndexError:
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1370 pass
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1371
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1372 return key
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1373
44fc94b0fe18 new FocusPile widget: a Pile which manage FOCUS_KEYS
Goffi <goffi@goffi.org>
parents: 95
diff changeset
1374
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1375 class FocusFrame(urwid.Frame):
94
66b65ed9baf2 focus direction and rotate boolean are now gotten from getFocusDirection
Goffi <goffi@goffi.org>
parents: 91
diff changeset
1376 """Frame-like which manage SàT Focus Keys"""
98
8bf5a35450f0 Fixed getFocusDirection direction when inversed, and FocusFrame focus order.
Goffi <goffi@goffi.org>
parents: 97
diff changeset
1377 ordered_positions = ('footer', 'body', 'header')
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1378
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1379 def keypress(self, size, key):
75
8ff563825080 FocusFrame is now based on Pile instead of Frame, header, body and footer attributes can be used to change part at any time
Goffi <goffi@goffi.org>
parents: 74
diff changeset
1380 ret = super(FocusFrame, self).keypress(size, key)
19
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1381 if not ret:
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1382 return
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1383
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1384 if key in FOCUS_KEYS:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1385 direction, rotate = get_focus_direction(key)
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1386
98
8bf5a35450f0 Fixed getFocusDirection direction when inversed, and FocusFrame focus order.
Goffi <goffi@goffi.org>
parents: 97
diff changeset
1387 positions = [pos for pos in self.ordered_positions if pos in self]
8bf5a35450f0 Fixed getFocusDirection direction when inversed, and FocusFrame focus order.
Goffi <goffi@goffi.org>
parents: 97
diff changeset
1388 selectables = [pos for pos in positions if self.contents[pos][0].selectable()] # keep positions which exists and have a selectable widget
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1389 if not selectables:
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1390 # no widget is selectable, we just return
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1391 return
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1392 idx = selectables.index(self.focus_position) + direction
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1393 if not rotate and (idx < 0 or idx >= len(selectables)):
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1394 # if we don't rotate, we stay where we are on the first and last position
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1395 return
75
8ff563825080 FocusFrame is now based on Pile instead of Frame, header, body and footer attributes can be used to change part at any time
Goffi <goffi@goffi.org>
parents: 74
diff changeset
1396 try:
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1397 self.focus_position = selectables[idx]
75
8ff563825080 FocusFrame is now based on Pile instead of Frame, header, body and footer attributes can be used to change part at any time
Goffi <goffi@goffi.org>
parents: 74
diff changeset
1398 except IndexError:
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1399 # happen if idx > len(selectables)
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1400 self.focus_position = selectables[0]
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1401 return
5
592cd64933dd Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents: 4
diff changeset
1402
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1403 return ret
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1404
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1405 def get_cursor_coords(self, size):
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1406 """Return the cursor coordinates of the focus widget."""
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1407 if not self.selectable():
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1408 return None
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1409 if not hasattr(self.focus, 'get_cursor_coords'):
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1410 return None
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1411 maxcol, maxrow = size
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1412 try:
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1413 if self.focus_position != 'body':
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1414 # only body is a box widget
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1415 size = (maxcol,)
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1416 col, row = self.focus.get_cursor_coords(size)
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1417 except TypeError:
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1418 return None
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1419 if self.focus_position == 'header':
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1420 return (col, row)
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1421 if self.focus_position == 'body':
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1422 header_rows = self.header.rows((maxcol,))
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1423 return (col, row + header_rows)
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1424 if self.focus_position == 'footer':
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1425 footer_rows = self.footer.rows((maxcol,))
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1426 return (col, row + (maxrow - footer_rows))
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1427 raise Exception('This line should not be reached')
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1428
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1429
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1430 class TabsContainer(urwid.WidgetWrap):
76
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1431 """ Container which can contain multiple box widgets associated to named tabs """
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1432 signals = ['click']
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1433
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1434 def __init__(self):
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1435 self._current_tab = None
18
bdc83e857093 Primitivus: new widget ColumnsRoller which show FlowWidgets on the same row, and can roll between them if there is not enough space
Goffi <goffi@goffi.org>
parents: 17
diff changeset
1436 self._buttons_cont = ColumnsRoller()
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1437 self.tabs = []
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1438 self._frame = FocusFrame(urwid.Filler(urwid.Text('')),urwid.Pile([self._buttons_cont,urwid.Divider("─")]))
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1439 urwid.WidgetWrap.__init__(self, self._frame)
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1440
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1441 def keypress(self, size, key):
19
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1442 return self._w.keypress(size,key)
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1443
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1444 def _button_clicked(self, button, invisible=False):
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1445 """Called when a button on the tab is changed,
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1446 change the page
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1447 @param button: button clicked
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1448 @param invisible: emit signal only if False"""
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1449 tab_name = button.get_label()
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1450 for tab in self.tabs:
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1451 if tab[0] == tab_name:
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1452 break
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1453 if tab[0] != tab_name:
89
2141f07b5fdd primitivus: no more direct error/warning methods for logging
Goffi <goffi@goffi.org>
parents: 88
diff changeset
1454 log.error(_("INTERNAL ERROR: Tab not found"))
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1455 assert(False)
90
f5992b2a0dbf FocusFrame refactoring:
Goffi <goffi@goffi.org>
parents: 89
diff changeset
1456 self._frame.body = tab[1]
26
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1457 button.set_label(('title',button.get_label()))
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1458 if self._current_tab:
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1459 self._current_tab.set_label(self._current_tab.get_label())
fcc20ac7b68a Primitivus: major changes in SelectableText, menu can now be used with mouse, TabsContainer show wich tab is selected
Goffi <goffi@goffi.org>
parents: 25
diff changeset
1460 self._current_tab = button
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1461 if not invisible:
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1462 self._emit('click')
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1463
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1464 def _append_button(self, name, selected=False):
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1465 """Append a button to the frame header, and link it to the page change method.
113
77ccc1dd2261 MenuRoller: fixed crash when deleting a displayed menu
Goffi <goffi@goffi.org>
parents: 111
diff changeset
1466
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1467 @param name (unicode): button name
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1468 @param selected (bool): set to True to select this tab
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1469 """
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1470 button = CustomButton(name, self._button_clicked, left_border = '', right_border=' | ')
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1471 self._buttons_cont.add_widget(button, button.get_size())
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1472 count = len(self._buttons_cont.widget_list)
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1473 if selected or count == 1:
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1474 # first/selected button: we set the focus and the body
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1475 self.select_tab(count - 1)
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1476
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1477 def add_tab(self, name, content=None, selected=False):
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1478 """Add a page to the container
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1479
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1480 @param name: name of the page (what appear on the tab)
76
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1481 @param content: content of the page:
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1482 - if None create and empty Listbox
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1483 - if it is a list instance, create a ListBox with the list in a body
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1484 - else it must be a box widget which will be used instead of the ListBox
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1485 @param selected (bool): set to True to select this tab
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1486 @return: ListBox (content of the page)"""
76
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1487 if content is None or isinstance(content, list):
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1488 tab = urwid.ListBox(urwid.SimpleListWalker(content or []))
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1489 else:
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1490 tab = content
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1491
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1492 self.tabs.append([name, tab])
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1493 self._append_button(name, selected)
76
6c2a1b349416 TabsContainer now accept any box widget as tab content
Goffi <goffi@goffi.org>
parents: 75
diff changeset
1494 return tab
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1495
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1496 def add_footer(self, widget):
19
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1497 """Add a widget on the bottom of the tab (will be displayed on all pages)
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1498 @param widget: FlowWidget"""
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1499 self._w.footer = widget
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1500
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1501 def select_tab(self, index):
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1502 """Select a tab.
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1503
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1504 @param index (int): index of the tab to select
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1505 """
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1506 self._buttons_cont.focus_position = index
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1507 self._button_clicked(self._buttons_cont.widget_list[index][1], True)
111
1cdf4a00b68d TabsContainer: allow to select a tab when adding it
souliane <souliane@mailoo.org>
parents: 110
diff changeset
1508
19
0b83dd2b15d1 Primitivus: misc improvments on TabsContainer/FocusFrame
Goffi <goffi@goffi.org>
parents: 18
diff changeset
1509
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1510 class HighlightColumns(urwid.AttrMap):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1511 """ Decorated columns which highlight all or some columns """
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1512
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1513 def __init__(self, highlight_cols, highlight_attr, *args, **kwargs):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1514 """ Create the HighlightColumns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1515 @param highlight_cols: tuple of columns to highlight, () to highlight to whole row
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1516 @param highlight_attr: name of the attribute to use when focused
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1517 other parameter are passed to urwid Columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1518
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1519 """
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1520 columns = urwid.Columns(*args, **kwargs)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1521 self.highlight_cols = highlight_cols
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1522 self.highlight_attr = highlight_attr
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1523 self.has_focus = False
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1524 if highlight_cols == ():
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1525 super(HighlightColumns, self).__init__(columns, None, highlight_attr)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1526 self.highlight_cols = None
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1527 else:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1528 super(HighlightColumns, self).__init__(columns, None)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1529
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1530 @property
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1531 def options(self):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1532 return self.base_widget.options
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1533
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1534 @property
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1535 def contents(self):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1536 return self.base_widget.contents
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1537
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1538 @property
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1539 def focus_position(self):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1540 return self.base_widget.focus_position
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1541
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1542 @focus_position.setter
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1543 def focus_position(self, value):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1544 self.base_widget.focus_position = value
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1545
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1546 def add_widget(self, wid, options):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1547 """ Add a widget to the columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1548 Widget is wrapped with AttrMap, that's why Columns.contents should not be used directly for appending new widgets
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1549 @param wid: widget to add
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1550 @param options: result of Columns.options(...)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1551
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1552 """
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1553 wrapper = urwid.AttrMap(wid, None)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1554 self.base_widget.contents.append((wrapper, options))
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1555
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1556
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1557 def render(self, size, focus=False):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1558 if self.highlight_cols and focus != self.has_focus:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1559 self.has_focus = focus
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1560 for idx in self.highlight_cols:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1561 wid = self.base_widget.contents[idx][0]
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1562 wid.set_attr_map({None: self.highlight_attr if focus else None})
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1563
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1564 return super(HighlightColumns, self).render(size, focus)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1565
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1566
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1567 class TableContainer(urwid.WidgetWrap):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1568 """ Widgets are disposed in row and columns """
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1569 signals = ['click']
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1570
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1571 def __init__(self, items=None, columns=None, dividechars=1, row_selectable=False, select_key='enter', options=None):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1572 """ Create a TableContainer
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1573 @param items: iterable of widgets to add to this container
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1574 @param columns: nb of columns of this table
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1575 @param dividechars: same as dividechars param for urwid.Columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1576 @param row_selectable: if True, row are always selectable, even if they don't contain any selectable widget
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1577 @param options: dictionnary with the following keys:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1578 - ADAPT: tuple of columns for which the size must be adapted to its contents,
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1579 empty tuple for all columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1580 - HIGHLIGHT: tuple of columns which must be higlighted on focus,
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1581 empty tuple for the whole row
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1582 - FOCUS_ATTR: Attribute name to use when focused (see HIGHLIGHT). Default is "table_selected"
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1583
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1584 """
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1585 pile = urwid.Pile([])
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1586 super(TableContainer, self).__init__(pile)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1587 if items is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1588 items = []
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1589 if columns is None: # if columns is None, we suppose only one row is given in items
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1590 columns = len(items)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1591 assert columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1592 self._columns = columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1593 self._row_selectable = row_selectable
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1594 self.select_key = select_key
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1595 if options is None:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1596 options = {}
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1597 for opt in ['ADAPT', 'HIGHLIGHT']:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1598 if opt in options:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1599 try:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1600 options[opt] = tuple(options[opt])
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1601 except TypeError:
89
2141f07b5fdd primitivus: no more direct error/warning methods for logging
Goffi <goffi@goffi.org>
parents: 88
diff changeset
1602 log.warning('[%s] option is not a tuple' % opt)
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1603 options[opt] = ()
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1604 self._options = options
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1605 self._dividechars = dividechars
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1606 self._idx = 0
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1607 self._longuest = self._columns * [0]
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1608 self._next_row_idx = None
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1609 for item in items:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1610 self.add_widget(item)
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1611
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1612 def _get_ideal_size(self, widget):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1613 """ return preferred size for widget, or 0 if we can't find it """
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1614 try:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1615 return len(widget.text)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1616 except AttributeError:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1617 return 0
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1618
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1619 def keypress(self, size, key):
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1620 if key == self.select_key and self._row_selectable:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1621 self._emit('click')
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1622 else:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1623 return super(TableContainer, self).keypress(size, key)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1624
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1625
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1626 def add_widget(self, widget):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1627 # TODO: use a contents property ?
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1628 pile = self._w
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1629 col_idx = self._idx % self._columns
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1630
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1631 options = None
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1632
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1633 if col_idx == 0:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1634 # we have a new row
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1635 columns = HighlightColumns(self._options.get('HIGHLIGHT'), self._options.get('FOCUS_ATTR', 'table_selected'), [], dividechars=self._dividechars)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1636 columns.row_idx = self._next_row_idx
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1637 pile.contents.append((columns, pile.options()))
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1638 else:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1639 columns = pile.contents[-1][0]
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1640
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1641 if 'ADAPT' in self._options and (col_idx in self._options['ADAPT']
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1642 or self._options['ADAPT'] == ()):
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1643 current_len = self._get_ideal_size(widget)
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1644 longuest = self._longuest[col_idx]
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1645 max_len = max(longuest, current_len)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1646 if max_len > longuest:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1647 self._longuest[col_idx] = max_len
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1648 for wid,_ in pile.contents[:-1]:
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1649 col = wid.base_widget
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1650 col.contents[col_idx] = (col.contents[col_idx][0], col.options('given', max_len))
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1651 options = columns.options('given', max_len) if max_len else columns.options()
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1652
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1653 columns.add_widget(widget, options or columns.options())
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1654
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1655 if self._row_selectable and col_idx == self._columns - 1:
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1656 columns.add_widget(urwid.SelectableIcon(''), columns.options('given', 0))
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1657
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1658 if not columns.selectable() and columns.contents[-1][0].base_widget.selectable():
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1659 columns.focus_position = len(columns.contents)-1
99
b2fee87c1d5a Fixed focus when first row is not selectable in TableContainer
Goffi <goffi@goffi.org>
parents: 98
diff changeset
1660 if not self.selectable() and columns.selectable():
b2fee87c1d5a Fixed focus when first row is not selectable in TableContainer
Goffi <goffi@goffi.org>
parents: 98
diff changeset
1661 pile.focus_position = len(pile.contents) - 1
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1662 self._idx += 1
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1663
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1664 def set_row_index(self, idx):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1665 self._next_row_idx = idx
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1666
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1667 def get_selected_widgets(self):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1668 columns = self._w.focus
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1669 return (wid for wid, _ in columns.contents)
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1670
151
6689aa54b20c refactoring from camelCase -> snake_case:
Goffi <goffi@goffi.org>
parents: 150
diff changeset
1671 def get_selected_index(self):
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1672 columns = self._w.focus
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1673 return columns.row_idx
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1674
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1675 ## DECORATORS ##
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1676 class LabelLine(urwid.LineBox):
7
94868f58850b misc documentation
Goffi <goffi@goffi.org>
parents: 6
diff changeset
1677 """Like LineBox, but with a Label centered in the top line"""
6
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1678
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1679 def __init__(self, original_widget, label_widget):
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1680 urwid.LineBox.__init__(self, original_widget)
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1681 top_columns = self._w.widget_list[0]
d586d06a9d8f primitivus group chat & misc
Goffi <goffi@goffi.org>
parents: 5
diff changeset
1682 top_columns.widget_list[1] = label_widget
8
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1683
70
24d49f1d735f added TableContainer + some bug fixes (bad default parameters)
Goffi <goffi@goffi.org>
parents: 69
diff changeset
1684
8
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1685 class VerticalSeparator(urwid.WidgetDecoration, urwid.WidgetWrap):
143
144bdf877d21 python 3 port (using 2to3)
Goffi <goffi@goffi.org>
parents: 127
diff changeset
1686 def __init__(self, original_widget, left_char = "│", right_char = ''):
15
8241b3157699 Primitivus: custom_widgets imrpovments
Goffi <goffi@goffi.org>
parents: 14
diff changeset
1687 """Draw a separator on left and/or right of original_widget."""
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1688
8
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1689 widgets = [original_widget]
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1690 if left_char:
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1691 widgets.insert(0, ('fixed', 1, urwid.SolidFill(left_char)))
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1692 if right_char:
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1693 widgets.append(('fixed', 1, urwid.SolidFill(right_char)))
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1694 columns = urwid.Columns(widgets, box_columns = [0,2], focus_column = 1)
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1695 urwid.WidgetDecoration.__init__(self, original_widget)
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1696 urwid.WidgetWrap.__init__(self, columns)
ec01505ec109 primitivus chat window
Goffi <goffi@goffi.org>
parents: 7
diff changeset
1697
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 65
diff changeset
1698