comparison urwid_satext/files_management.py @ 58:7155b81ffdd5

new 'dir' style in FileDialog (get a dir path instead of a file
author Goffi <goffi@goffi.org>
date Wed, 14 Nov 2012 20:27:11 +0100
parents 875ff127b2dd
children 5cef69971f23
comparison
equal deleted inserted replaced
57:d493f95724a7 58:7155b81ffdd5
49 return super(PathEdit, self).keypress(size, key) 49 return super(PathEdit, self).keypress(size, key)
50 50
51 class FilesViewer(urwid.WidgetWrap): 51 class FilesViewer(urwid.WidgetWrap):
52 """List specialised for files""" 52 """List specialised for files"""
53 53
54 def __init__(self, onPreviousDir, onDirClick, onFileClick): 54 def __init__(self, onPreviousDir, onDirClick, onFileClick = None):
55 self.path='' 55 self.path=''
56 self.key_cache = '' 56 self.key_cache = ''
57 self.key_time = time() 57 self.key_time = time()
58 self.onPreviousDir = onPreviousDir 58 self.onPreviousDir = onPreviousDir
59 self.onDirClick = onDirClick 59 self.onDirClick = onDirClick
120 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator')) 120 self.files_list.append(urwid.AttrMap(urwid.Divider('-'),'separator'))
121 for filename in files: 121 for filename in files:
122 if filename.startswith('.') and not self.show_hidden: 122 if filename.startswith('.') and not self.show_hidden:
123 continue 123 continue
124 file_wid = sat_widgets.ClickableText(filename) 124 file_wid = sat_widgets.ClickableText(filename)
125 urwid.connect_signal(file_wid,'click',self.onFileClick) 125 if self.onFileClick:
126 urwid.connect_signal(file_wid,'click',self.onFileClick)
126 self.files_list.append(file_wid) 127 self.files_list.append(file_wid)
127 128
128 129
129 130
130 class FileDialog(urwid.WidgetWrap): 131 class FileDialog(urwid.WidgetWrap):
131 132
132 def __init__(self, ok_cb, cancel_cb, title=_("Please select a file"), style=[]): 133 def __init__(self, ok_cb, cancel_cb, title=_("Please select a file"), style=[]):
133 """Create file dialog 134 """Create file dialog
134 @param title: title of the window/popup 135 @param title: title of the window/popup
135 @param style: NOT USED YET #FIXME 136 @param style: list of string:
137 - 'dir' if a dir path must be selected
136 """ 138 """
137 self.ok_cb = ok_cb 139 self.ok_cb = ok_cb
140 self._type = 'dir' if 'dir' in style else 'normal'
138 self.__home_path = os.path.expanduser('~') 141 self.__home_path = os.path.expanduser('~')
139 self.path_wid = PathEdit(_('Path: ')) 142 self.path_wid = PathEdit(_('Path: '))
140 self.path_wid.setCompletionMethod(self._directory_completion) 143 self.path_wid.setCompletionMethod(self._directory_completion)
141 urwid.connect_signal(self.path_wid, 'change', self.onPathChange) 144 urwid.connect_signal(self.path_wid, 'change', self.onPathChange)
142 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')]) 145 header = urwid.Pile([self.path_wid, urwid.Divider(u'─')])
148 bookmark="~"+bookmark[len(self.__home_path):] 151 bookmark="~"+bookmark[len(self.__home_path):]
149 book_wid = sat_widgets.ClickableText(bookmark) 152 book_wid = sat_widgets.ClickableText(bookmark)
150 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected) 153 urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected)
151 bookm_list.append(book_wid) 154 bookm_list.append(book_wid)
152 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title')) 155 bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title'))
153 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick) 156 self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick if self._type == 'normal' else None)
154 center_row = urwid.Columns([('weight',2,bookm_wid), 157 center_row = urwid.Columns([('weight',2,bookm_wid),
155 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))]) 158 ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))])
156 159
157 buttons = [] 160 buttons = []
161 if self._type == 'dir':
162 buttons.append(sat_widgets.CustomButton(_('Ok'), self._validateDir))
158 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb)) 163 buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb))
159 max_len = max([button.getSize() for button in buttons]) 164 max_len = max([button.getSize() for button in buttons])
160 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center') 165 buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
161 main_frame = sat_widgets.FocusFrame(center_row, header, buttons_wid) 166 main_frame = sat_widgets.FocusFrame(center_row, header, buttons_wid)
162 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title)) 167 decorated = sat_widgets.LabelLine(main_frame, sat_widgets.SurroundedText(title))
163 urwid.WidgetWrap.__init__(self, decorated) 168 urwid.WidgetWrap.__init__(self, decorated)
164 self.path_wid.set_edit_text(os.getcwdu()) 169 self.path_wid.set_edit_text(os.getcwdu())
170
171 def _validateDir(self, wid):
172 """ call ok callback if current path is a dir """
173 path = os.path.abspath(self.path_wid.get_edit_text())
174 if os.path.isdir(path):
175 self.ok_cb(path)
165 176
166 def _directory_completion(self, path, completion_data): 177 def _directory_completion(self, path, completion_data):
167 path=os.path.abspath(path) 178 path=os.path.abspath(path)
168 if not os.path.isdir(path): 179 if not os.path.isdir(path):
169 head,dir_start = os.path.split(path) 180 head,dir_start = os.path.split(path)