Ejemplo n.º 1
0
class FileDialog(Dialog):
    box_width = 250
    default_prompt = None
    up_button_text = ThemeProperty("up_button_text")

    def __init__(self, prompt=None, suffixes=None, **kwds):
        Dialog.__init__(self, **kwds)
        label = None
        d = self.margin
        self.suffixes = suffixes or ("", )
        up_button = Button(self.up_button_text, action=self.go_up)
        dir_box = DirPathView(self.box_width - up_button.width - 10, self)
        self.dir_box = dir_box
        top_row = Row([dir_box, up_button])
        list_box = FileListView(self.box_width - 16, self)
        self.list_box = list_box
        ctrls = [top_row, list_box]
        prompt = prompt or self.default_prompt
        if prompt:
            label = Label(prompt)
        if self.saving:
            filename_box = TextField(self.box_width)
            filename_box.change_action = self.update
            filename_box._enter_action = filename_box.enter_action
            filename_box.enter_action = self.enter_action
            self.filename_box = filename_box
            ctrls.append(Column([label, filename_box], align='l', spacing=0))
        else:
            if label:
                ctrls.insert(0, label)
        ok_button = Button(self.ok_label,
                           action=self.ok,
                           enable=self.ok_enable)
        self.ok_button = ok_button
        cancel_button = Button("Cancel", action=self.cancel)
        vbox = Column(ctrls, align='l', spacing=d)
        vbox.topleft = (d, d)
        y = vbox.bottom + d
        ok_button.topleft = (vbox.left, y)
        cancel_button.topright = (vbox.right, y)
        self.add(vbox)
        self.add(ok_button)
        self.add(cancel_button)
        self.shrink_wrap()
        self._directory = None
        self.directory = os.getcwdu()
        #print "FileDialog: cwd =", repr(self.directory) ###
        if self.saving:
            filename_box.focus()

    def get_directory(self):
        return self._directory

    def set_directory(self, x):
        x = os.path.abspath(x)
        while not os.path.exists(x):
            y = os.path.dirname(x)
            if y == x:
                x = os.getcwdu()
                break
            x = y
        if self._directory != x:
            self._directory = x
            self.list_box.update()
            self.update()

    directory = property(get_directory, set_directory)

    def filter(self, path):
        suffixes = self.suffixes
        if not suffixes or os.path.isdir(path):
            #return os.path.isfile(path)
            return True
        for suffix in suffixes:
            if path.endswith(suffix.lower()):
                return True

    def update(self):
        pass

    def go_up(self):
        self.directory = os.path.dirname(self.directory)
        self.list_box.scroll_to_item(0)

    def dir_box_click(self, double):
        if double:
            name = self.list_box.get_selected_name()
            path = os.path.join(self.directory, name)
            suffix = os.path.splitext(name)[1]
            if suffix not in self.suffixes and os.path.isdir(path):
                self.directory = path
            else:
                self.double_click_file(name)
        self.update()

    def enter_action(self):
        self.filename_box._enter_action()
        self.ok()

    def ok(self):
        self.dir_box_click(True)
        #self.dismiss(True)

    def cancel(self):
        self.dismiss(False)

    def key_down(self, evt):
        k = evt.key
        if k == K_RETURN or k == K_KP_ENTER:
            self.dir_box_click(True)
        if k == K_ESCAPE:
            self.cancel()
Ejemplo n.º 2
0
class FileDialog(Dialog):
    box_width = 450
    default_prompt = None
    up_button_text = ThemeProperty("up_button_text")

    def __init__(self,
                 prompt=None,
                 suffixes=None,
                 default_suffix=None,
                 **kwds):
        Dialog.__init__(self, **kwds)
        label = None
        d = self.margin
        self.suffixes = suffixes or ("", )
        self.file_type = self.suffixes[0]  # To be removed
        self.compute_file_types()
        self.default_suffix = default_suffix  # The default file extension. Will be searched in 'suffixes'.
        up_button = Button(self.up_button_text, action=self.go_up)
        dir_box = DirPathView(self.box_width + 250, self)
        self.dir_box = dir_box
        top_row = Row([dir_box, up_button])
        list_box = FileListView(self.box_width - 16, self)
        self.list_box = list_box
        tree = FSTree(self, inner_width=250, directory='/')
        self.tree = tree
        row = Row((tree, list_box), margin=0)
        ctrls = [top_row, row]
        prompt = prompt or self.default_prompt
        if prompt:
            label = Label(prompt)
        if suffixes:
            filetype_label = Label("File type", width=250)

            def set_file_type():
                self.file_type = self.filetype_button.get_value(
                )  # To be removed
                self.compute_file_types(self.filetype_button.get_value())
                self.list_box.update()

            filetype_button = ChoiceButton(choices=self.suffixes,
                                           width=250,
                                           choose=set_file_type)
            if default_suffix:
                v = next((s for s in self.suffixes
                          if ("*.%s;" % default_suffix in s or "*.%s)" %
                              default_suffix in s)), None)
                if v:
                    filetype_button.selectedChoice = v
                    self.compute_file_types(v)
            self.filetype_button = filetype_button
        if self.saving:
            filename_box = TextFieldWrapped(self.box_width)
            filename_box.change_action = self.update_filename
            filename_box._enter_action = filename_box.enter_action
            filename_box.enter_action = self.enter_action
            self.filename_box = filename_box
            if suffixes:
                ctrls.append(
                    Row([
                        Column([label, filename_box], align='l', spacing=0),
                        Column([filetype_label, filetype_button],
                               align='l',
                               spacing=0)
                    ], ))
            else:
                ctrls.append(
                    Column([label, filename_box], align='l', spacing=0))
        else:
            if label:
                ctrls.insert(0, label)
            if suffixes:
                ctrls.append(
                    Column([filetype_label, filetype_button],
                           align='l',
                           spacing=0))
        ok_button = Button(self.ok_label,
                           action=self.ok,
                           enable=self.ok_enable)
        self.ok_button = ok_button
        cancel_button = Button("Cancel", action=self.cancel)
        vbox = Column(ctrls, align='l', spacing=d)
        vbox.topleft = (d, d)
        y = vbox.bottom + d
        ok_button.topleft = (vbox.left, y)
        cancel_button.topright = (vbox.right, y)
        self.add(vbox)
        self.add(ok_button)
        self.add(cancel_button)
        self.shrink_wrap()
        self._directory = None
        self.directory = os.getcwd()
        # print "FileDialog: cwd =", repr(self.directory) ###
        if self.saving:
            filename_box.focus()

    def compute_file_types(self, suffix=None):
        if suffix is None:
            suffix = self.suffixes[0]
        if suffix:
            self.file_types = [
                a.replace('*.', '.')
                for a in suffix.split('(')[-1].split(')')[0].split(';')
            ]
        else:
            self.file_types = [".*"]

    def get_directory(self):
        return self._directory

    def set_directory(self, x):
        x = os.path.abspath(x)
        while not os.path.exists(x):
            y = os.path.dirname(x)
            if y == x:
                x = os.getcwd()
                break
            x = y
        if os.path.isfile(x):
            x = os.path.dirname(x)
        if self._directory != x:
            self._directory = x
            self.list_box.update()
            self.update()

    directory = property(get_directory, set_directory)

    def filter(self, path):
        if os.path.isdir(path) or os.path.splitext(
                path)[1] in self.file_types or self.file_types == ['.*']:
            return True
        if os.path.isdir(path) or path.endswith(
                self.file_type.lower()) or self.file_type == '.*':
            return True

    def update(self):
        self.tree.set_directory(self.directory)

    def update_filename(self):
        if self.filename_box.text in self.list_box.names:
            self.directory = os.path.join(self.directory,
                                          self.filename_box.text)

    def go_up(self):
        self.directory = os.path.dirname(self.directory)
        self.list_box.scroll_to_item(0)

    def dir_box_click(self, double):
        if double:
            name = self.list_box.get_selected_name()
            path = os.path.join(self.directory, name)
            suffix = os.path.splitext(name)[1]
            if suffix not in self.suffixes and os.path.isdir(path):
                self.directory = path
            else:
                self.double_click_file(name)
        self.update()

    def enter_action(self):
        self.filename_box._enter_action()
        self.ok()

    def ok(self):
        self.dir_box_click(True)
        # self.dismiss(True)

    def cancel(self):
        self.dismiss(False)

    def key_down(self, evt):
        k = evt.key
        if k == K_RETURN or k == K_KP_ENTER:
            self.dir_box_click(True)
        if k == K_ESCAPE:
            self.cancel()