diff 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
line wrap: on
line diff
--- a/urwid_satext/files_management.py	Sun Oct 21 16:36:59 2012 +0200
+++ b/urwid_satext/files_management.py	Wed Nov 14 20:27:11 2012 +0100
@@ -51,7 +51,7 @@
 class FilesViewer(urwid.WidgetWrap):
     """List specialised for files"""
 
-    def __init__(self, onPreviousDir, onDirClick, onFileClick):
+    def __init__(self, onPreviousDir, onDirClick, onFileClick = None):
         self.path=''
         self.key_cache = ''
         self.key_time = time()
@@ -122,7 +122,8 @@
             if filename.startswith('.') and not self.show_hidden:
                 continue
             file_wid = sat_widgets.ClickableText(filename)
-            urwid.connect_signal(file_wid,'click',self.onFileClick)
+            if self.onFileClick:
+                urwid.connect_signal(file_wid,'click',self.onFileClick)
             self.files_list.append(file_wid)
 
 
@@ -132,9 +133,11 @@
     def __init__(self, ok_cb, cancel_cb, title=_("Please select a file"), style=[]):
         """Create file dialog
         @param title: title of the window/popup
-        @param style: NOT USED YET #FIXME
+        @param style: list of string:
+            - 'dir' if a dir path must be selected
         """
         self.ok_cb = ok_cb
+        self._type = 'dir' if 'dir' in style else 'normal'
         self.__home_path = os.path.expanduser('~')
         self.path_wid = PathEdit(_('Path: '))
         self.path_wid.setCompletionMethod(self._directory_completion)
@@ -150,11 +153,13 @@
             urwid.connect_signal(book_wid, 'click', self.onBookmarkSelected)
             bookm_list.append(book_wid)
         bookm_wid = urwid.Frame(urwid.ListBox(bookm_list), urwid.AttrMap(urwid.Text(_('Bookmarks'),'center'),'title'))
-        self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick)
+        self.files_wid = FilesViewer(self.onPreviousDir, self.onDirClick, self.onFileClick if self._type == 'normal' else None)
         center_row = urwid.Columns([('weight',2,bookm_wid),
                      ('weight',8,sat_widgets.VerticalSeparator(self.files_wid))])
 
         buttons = []
+        if self._type == 'dir':
+            buttons.append(sat_widgets.CustomButton(_('Ok'), self._validateDir))
         buttons.append(sat_widgets.CustomButton(_('Cancel'),cancel_cb))
         max_len = max([button.getSize() for button in buttons])
         buttons_wid = urwid.GridFlow(buttons,max_len,1,0,'center')
@@ -163,6 +168,12 @@
         urwid.WidgetWrap.__init__(self, decorated)
         self.path_wid.set_edit_text(os.getcwdu())
 
+    def _validateDir(self, wid):
+        """ call ok callback if current path is a dir """
+        path = os.path.abspath(self.path_wid.get_edit_text())
+        if os.path.isdir(path):
+            self.ok_cb(path)
+
     def _directory_completion(self, path, completion_data):
         path=os.path.abspath(path)
         if not os.path.isdir(path):