Exemplo n.º 1
0
 def __init__(self,
              path="./",
              ddl_size=None,
              normal_params=None,
              folders=True,
              files=True,
              file_types=None,
              text=""):
     """File and folder browser.
     <path>: the path of the folder in which browser browse files.
     <ddl_size>: if not None, force the size of the dropdown list of files.
     <folders>: if True, displays folders to user.
     <files>: if True, displays file to user.
     <file_types>: if not None, pass a list of files formats that you wand to
         be valid for user choice.
     <text>: title text of the browser.
     """
     ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
     super(BrowserLight, self).__init__(normal_params=normal_params)
     self.path = path
     self._ddl_size = ddl_size
     if not hasattr(file_types, "__iter__") and file_types is not None:
         raise ValueError("Files types must be a sequence.")
     self.file_types = file_types
     # DropDownListFast
     actual_folders, actual_files = self._get_folders_and_files()
     actual_files = self._filter_files(actual_files)
     if not folders:
         actual_folders = None
     if not files:
         actual_files = []
     self._ddlf = DropDownListFast(size=self._ddl_size,
                                   titles=actual_files,
                                   folders=actual_folders)
     # selection button
     inserter_width = 3 * ddl_size[0] // 4
     ##        if inserter_width > style.MAX_INSERTER_WIDTH:
     ##            inserter_width = style.MAX_INSERTER_WIDTH
     self._selected = Inserter("Selected : ", size=(inserter_width, None))
     self._selected.finish()
     if isinstance(text, str):
         self.text_element = OneLineText(text)
         self.text_element.finish()
     else:
         self.text_element = text
     self.add_elements([self.text_element, self._ddlf, self._selected])
     reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                             self._reaction_press,
                             reac_name=constants.REAC_PRESSED)
     self._ddlf.finish()
     self.add_reaction(reac_pressed)
     self._clicked = None
     self._something_selected = False
     painter = functions.obtain_valid_painter(
         painterstyle.BOX_PAINTER,
         pressed=True,
         ##                                                 color=style.DEF_COLOR2,
         radius=style.BOX_RADIUS)
     self.set_painter(painter)
     self._refresh_ddlf_lift()
Exemplo n.º 2
0
    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        """File and folder browser.
        <path>: the path of the folder in which browser browse files.
        <ddl_size>: if not None, force the size of the dropdown list of files.
        <folders>: if True, displays folders to user.
        <files>: if True, displays file to user.
        <file_types>: if not None, pass a list of files formats that you wand to
            be valid for user choice.
        <text>: title text of the browser.
        """
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = path
        self._ddl_size = ddl_size
        if not hasattr(file_types, "__iter__") and file_types is not None:
            raise ValueError("Files types must be a sequence.")
        self.file_types = file_types
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(size=self._ddl_size, titles=actual_files,
                                      folders=actual_folders)
        # selection button
        inserter_width = 3 * ddl_size[0] // 4
##        if inserter_width > style.MAX_INSERTER_WIDTH:
##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self.add_elements([self.text_element, self._ddlf, self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                reac_name=constants.REAC_PRESSED)
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
##                                                 color=style.DEF_COLOR2,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._refresh_ddlf_lift()
Exemplo n.º 3
0
def get_handler_for(variable):
    typ = type(variable.value)
    value = variable.value
    text = variable.text
    limits = variable.limits
    handler_type = variable.handler_type
    handler = None
    if handler_type:
        if handler_type == "color_choice":
            handler = ColorSetter(text=text, value=value)
        elif handler_type == "file_choice":
            limits.setdefault("launcher", False)
            limits.setdefault("light", True)
            limits.setdefault("ddl_size", None)
            limits.setdefault("folders", True)
            limits.setdefault("file_types", None)
            limits.setdefault("files", True)
            if limits["light"]:
                handler = BrowserLight(text=text,
                                       path=value,
                                       ddl_size=limits["ddl_size"],
                                       folders=limits["folders"],
                                       files=limits["files"],
                                       file_types=limits["file_types"])
            else:
                handler = Browser(text=text,
                                  path=value,
                                  ddl_size=limits["ddl_size"],
                                  folders=limits["folders"],
                                  files=limits["files"],
                                  file_types=limits["file_types"])
        else:
            handler = obtain_valid_object(handler_type,
                                          value=value,
                                          typ=typ,
                                          text=text,
                                          limits=limits)
    elif typ is tuple:
        if len(value) == 3:
            handler = ColorSetter(text=text, value=value)
    elif (typ is str) or not (limits):
        handler = Inserter(text, value=str(value), value_type=typ)
    elif (typ is int) or (typ is float):
        handler = SliderXSetter(100,
                                limits,
                                text,
                                typ=typ,
                                initial_value=value)
    elif typ is bool:
        handler = Checker(variable.text,
                          value=variable.value,
                          checked=variable.value)
    if handler:
        handler.rank = variable.rank
        return handler
    else:
        raise Exception("Variable doesn't have default handler: " +
                        str(varname))
def get_user_text(title, value, size=(None, None)):
    from thorpy.miscgui.launchers.launcher import make_ok_box, auto_ok, launch_blocking
    ins = Inserter.make(title, value=value, size=size)
    box = make_ok_box([ins])
    auto_ok(box)
    box.center()
    ins.enter()
    launch_blocking(box)
    return ins.get_value()
Exemplo n.º 5
0
def launch_inserter(title_text, initial_value="", parent=None, title_fontsize=None,
                    title_fontcolor=None, main_color=None, blocking=True):
    from thorpy.miscgui.launchers.launcher import make_ok_box, auto_ok, launch_blocking
    ins = Inserter.make(title_text, value=initial_value)
    ins.center()
    ins.enter()
    launch_blocking(ins)
    parent.unblit_and_reblit()
    return ins.get_value()
Exemplo n.º 6
0
    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = path
        self._ddl_size = ddl_size
        self.file_types = file_types
        self.folders = folders
        self.files = files
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(self._ddl_size, titles=actual_files,
                                      folders=actual_folders, has_lift=True)
##        self._ddlf.finish()
        # selection button
        inserter_width = 3*ddl_size[0]//4
##        if inserter_width > style.MAX_INSERTER_WIDTH:
##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self._path_element = PathElement(self, True)
        self._path_element.finish()
        self.add_elements([self.text_element, self._path_element, self._ddlf,
                           self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                name=constants.REAC_PRESSED)
##        self._ddlf._force_lift = True
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._last_click = -2 * parameters.DOUBLE_CLICK_DELAY
Exemplo n.º 7
0
class BrowserLight(Element):
    """File and folder browser for a given directory."""

    @staticmethod
    def make(path="./", ddl_size=None, folders=True, files=True, file_types=None, text=""):
        browser = BrowserLight(path, ddl_size, folders=folders, files=files,
                            file_types=file_types, text=text)
        browser.finish()
        return browser

    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        """File and folder browser.
        <path>: the path of the folder in which browser browse files.
        <ddl_size>: if not None, force the size of the dropdown list of files.
        <folders>: if True, displays folders to user.
        <files>: if True, displays file to user.
        <file_types>: if not None, pass a list of files formats that you wand to
            be valid for user choice.
        <text>: title text of the browser.
        """
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = path
        self._ddl_size = ddl_size
        if not hasattr(file_types, "__iter__") and file_types is not None:
            raise ValueError("Files types must be a sequence.")
        self.file_types = file_types
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(size=self._ddl_size, titles=actual_files,
                                      folders=actual_folders)
        # selection button
        inserter_width = 3 * ddl_size[0] // 4
##        if inserter_width > style.MAX_INSERTER_WIDTH:
##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self.add_elements([self.text_element, self._ddlf, self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                reac_name=constants.REAC_PRESSED)
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
##                                                 color=style.DEF_COLOR2,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._refresh_ddlf_lift()

    def finish(self):
        Element.finish(self)
        self.store()
        self.text_element.set_center((self.get_fus_rect().centerx, None))
        self.set_prison()

    def _refresh_ddlf_lift(self):
        if self._ddlf._lift:
            functions.remove_element(self._ddlf._lift)
        if self._ddlf.get_family_rect().height > self._ddlf.get_fus_rect().height:
            self._ddlf.add_lift()
        functions.refresh_current_menu()

    def _refresh_select(self, inserted=None):
        self._selected._value = self.path + self._clicked + "/"
        inserted = self._clicked if inserted is None else inserted
        _iwriter = self._selected._iwriter
        writer = _iwriter.current_state.fusionner.title._writer
        size = _iwriter.get_zone().width - 2*_iwriter.margin
        self._selected._inserted = writer.get_line_sized(inserted, size)
        txt_refreshed = self._selected._urbu()
        self._something_selected = True

    def _reaction_press(self, event):
        x, y = self._ddlf._get_dirviewer_coords(event.pos)
        if not self._ddlf._lift or x < self._ddlf._dv.size[0] - self._ddlf._lift.get_fus_size()[0]:
            self._clicked = self._ddlf._dv.get_txt_at_pix(x, y)
            if self._clicked:
                self._refresh_select()

    def get_value(self):
        return self._selected._value

    def _get_folders_and_files(self):
        try:
            titles = listdir(self.path)
        except WindowsError:
            functions.debug_msg("Access denied to this folder/file. Try running\
                                 the script as administrator.")
            return [], []
        folders = []
        files = []
        for title in titles:
            if isdir(self.path + title + "/"):
                folders.append(title)
            else:
                files.append(title)
        return folders, files

    def list_folder_and_files(self):
        return self._ddlf._dv.folders, self._ddlf._dv.files

    def _filter_files(self, files):
        if self.file_types is not None:
            new_files = []
            for title in files:
                if not isdir(title):
                    for extension in self.file_types:
                        if title.endswith(extension):
                            new_files.append(title)
            return new_files
        else:
            return files

    def get_help_rect(self):
        return self._selected.get_help_rect()

    def get_dirviewer(self):
        return self._ddlf
Exemplo n.º 8
0
class BrowserLight(Element):
    """File and folder browser for a given directory."""
    @staticmethod
    def make(path="./",
             ddl_size=None,
             folders=True,
             files=True,
             file_types=None,
             text=""):
        browser = BrowserLight(path,
                               ddl_size,
                               folders=folders,
                               files=files,
                               file_types=file_types,
                               text=text)
        browser.finish()
        return browser

    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        """File and folder browser.
        <path>: the path of the folder in which browser browse files.
        <ddl_size>: if not None, force the size of the dropdown list of files.
        <folders>: if True, displays folders to user.
        <files>: if True, displays file to user.
        <file_types>: if not None, pass a list of files formats that you wand to
            be valid for user choice.
        <text>: title text of the browser.
        """
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = path
        self._ddl_size = ddl_size
        if not hasattr(file_types, "__iter__") and file_types is not None:
            raise ValueError("Files types must be a sequence.")
        self.file_types = file_types
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(size=self._ddl_size,
                                      titles=actual_files,
                                      folders=actual_folders)
        # selection button
        inserter_width = 3 * ddl_size[0] // 4
        ##        if inserter_width > style.MAX_INSERTER_WIDTH:
        ##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self.add_elements([self.text_element, self._ddlf, self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                reac_name=constants.REAC_PRESSED)
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(
            painterstyle.BOX_PAINTER,
            pressed=True,
            ##                                                 color=style.DEF_COLOR2,
            radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._refresh_ddlf_lift()

    def finish(self):
        Element.finish(self)
        self.store()
        self.text_element.set_center((self.get_fus_rect().centerx, None))
        self.set_prison()

    def _refresh_ddlf_lift(self):
        if self._ddlf._lift:
            functions.remove_element(self._ddlf._lift)
        if self._ddlf.get_family_rect().height > self._ddlf.get_fus_rect(
        ).height:
            self._ddlf.add_lift()
        functions.refresh_current_menu()

    def _refresh_select(self, inserted=None):
        self._selected._value = self.path + self._clicked + "/"
        inserted = self._clicked if inserted is None else inserted
        _iwriter = self._selected._iwriter
        writer = _iwriter.current_state.fusionner.title._writer
        size = _iwriter.get_zone().width - 2 * _iwriter.margin
        self._selected._inserted = writer.get_line_sized(inserted, size)
        txt_refreshed = self._selected._urbu()
        self._something_selected = True

    def _reaction_press(self, event):
        x, y = self._ddlf._get_dirviewer_coords(event.pos)
        if not self._ddlf._lift or x < self._ddlf._dv.size[
                0] - self._ddlf._lift.get_fus_size()[0]:
            self._clicked = self._ddlf._dv.get_txt_at_pix(x, y)
            if self._clicked:
                self._refresh_select()

    def get_value(self):
        return self._selected._value

    def _get_folders_and_files(self):
        try:
            titles = listdir(self.path)
        except:
            print("Access denied to this folder/file. Try running\
                                 the script as administrator.")
            return [], []
        folders = []
        files = []
        for title in titles:
            if isdir(self.path + title + "/"):
                folders.append(title)
            else:
                files.append(title)
        return folders, files

    def list_folder_and_files(self):
        return self._ddlf._dv.folders, self._ddlf._dv.files

    def _filter_files(self, files):
        if self.file_types is not None:
            new_files = []
            for title in files:
                if not isdir(title):
                    for extension in self.file_types:
                        if title.endswith(extension):
                            new_files.append(title)
            return new_files
        else:
            return files

    def get_help_rect(self):
        return self._selected.get_help_rect()

    def get_dirviewer(self):
        return self._ddlf
Exemplo n.º 9
0
def get_handler_for(variable):
    type_ = type(variable.value)
    value = variable.value
    text = variable.text
    limits = variable.limits
    handler_type = variable.handler_type
    more = variable.more
    handler = None
    if handler_type:
        if handler_type == "color choice":
##            cs = ColorSetter(text=text, value=value)
            handler = ColorSetterLauncher(value, text)
        elif handler_type == "file choice":
            limits.setdefault("launcher", False)
            limits.setdefault("light", True)
            limits.setdefault("ddl_size", None)
            limits.setdefault("folders", True)
            limits.setdefault("file_types", None)
            limits.setdefault("files", True)
            if limits["light"]:
                handler = BrowserLight(text=text, path=value,
                                     ddl_size=limits["ddl_size"],
                                     folders=limits["folders"],
                                     files=limits["files"],
                                     file_types=limits["file_types"])
            else:
                handler = Browser(text=text, path=value,
                                 ddl_size=limits["ddl_size"],
                                 folders=limits["folders"],
                                 files=limits["files"],
                                 file_types=limits["file_types"])
        else:
            handler = obtain_valid_object(handler_type, value=value, type_=type_,
                                          text=text, limits=limits)
    elif type_ is tuple:
        if len(value) == 3:
##            handler = ColorSetter(text=text, value=value)
            handler = ColorSetterLauncher(value, text, finish=False)
    elif type_ is list:
        var_text = more.get("var_text", value[0])
        ddlf_size = more.get("ddlf_size", "auto")
        handler = DropDownListLauncher(const_text=text, var_text=var_text,
                                        titles=value, ddlf_size=ddlf_size,
                                        finish=False)
    elif ((type_ is str) or not(limits)) and not(type_ is bool):
        if not(limits) or isinstance(limits,str):
            ilimits = (None, None)
        else:
            ilimits = limits
        handler = Inserter(text, value=str(value), value_type=type_,
                            size=ilimits, finish=False)
        if limits == "float":
            handler.numeric_only = True
            handler.int_only = False
            handler._value_type = float
        elif limits == "int":
            handler.numeric_only = True
            handler.int_only = True
            handler._value_type = int
    elif (type_ is int) or (type_ is float):
        length = more.get("length", 100)
        handler = SliderXSetter(length,
                                limits,
                                text,
                                type_=type_,
                                initial_value=value,
                                finish=False)
    elif type_ is bool:
        handler = Checker(variable.text, value=variable.value, finish=False)
    if handler:
        handler.rank = variable.rank
        return handler
    else:
        raise Exception(
            "Variable doesn't have default handler: " +
            str(text))
Exemplo n.º 10
0
    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        """File and folder browser for a.
        <path>: the path of the folder in which browser browse files.
        <ddl_size>: if not None, force the size of the dropdown list of files.
        <folders>: if True, displays folders to user.
        <files>: if True, displays file to user.
        <file_types>: if not None, pass a list of files formats that you wand to
            be valid for user choice.
        <text>: title text of the browser.
        """
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = self.set_path(path)
        self.last_done_path = str(self.path)
        self._ddl_size = ddl_size
        self.file_types = file_types
        self.folders = folders
        self.files = files
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(size=self._ddl_size, titles=actual_files,
                                      folders=actual_folders, has_lift=True)
##        self._ddlf.finish()
        # selection button
        inserter_width = 3*ddl_size[0]//4
##        if inserter_width > style.MAX_INSERTER_WIDTH:
##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self._path_element = PathElement(self, True)
        self._path_element.finish()
        self.add_elements([self.text_element, self._path_element, self._ddlf,
                           self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                {"button":1},
                                reac_name=constants.REAC_PRESSED)
##        self._ddlf._force_lift = True
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._last_click = -2 * parameters.DOUBLE_CLICK_DELAY
Exemplo n.º 11
0
class Browser(BrowserLight):
    """File and folder browser."""

    @staticmethod
    def make(path="./", ddl_size=None, folders=True, files=True, file_types=None, text=""):
        browser = Browser(path, ddl_size, folders=folders, files=files,
                            file_types=file_types, text=text)
        browser.finish()
        return browser

    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        """File and folder browser for a.
        <path>: the path of the folder in which browser browse files.
        <ddl_size>: if not None, force the size of the dropdown list of files.
        <folders>: if True, displays folders to user.
        <files>: if True, displays file to user.
        <file_types>: if not None, pass a list of files formats that you wand to
            be valid for user choice.
        <text>: title text of the browser.
        """
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = self.set_path(path)
        self.last_done_path = str(self.path)
        self._ddl_size = ddl_size
        self.file_types = file_types
        self.folders = folders
        self.files = files
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(size=self._ddl_size, titles=actual_files,
                                      folders=actual_folders, has_lift=True)
##        self._ddlf.finish()
        # selection button
        inserter_width = 3*ddl_size[0]//4
##        if inserter_width > style.MAX_INSERTER_WIDTH:
##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self._path_element = PathElement(self, True)
        self._path_element.finish()
        self.add_elements([self.text_element, self._path_element, self._ddlf,
                           self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                {"button":1},
                                reac_name=constants.REAC_PRESSED)
##        self._ddlf._force_lift = True
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._last_click = -2 * parameters.DOUBLE_CLICK_DELAY

    def finish(self):
        self._path_element._set_path_elements()
        Element.finish(self)
        self.store()
        centerx = self.get_fus_rect().centerx
        self.text_element.set_center((centerx, None))
        ycoord = self._path_element._elements[0].get_storer_rect().centery
        self._path_element._set_path_elements(ycoord)
        self.set_prison()

    def set_path(self, path):
        path = os.path.normpath(path)
        if path[-1] == os.path.sep:
            return path
        else:
            return path + os.path.sep

##    def store(self):
####        r = self.get_family_rect()
####        self.set_size((r.width, r.height))
##        storer = Storer(self, margins=style.BROWSERLIGHT_STORE_MARGINS,
##                        gaps=style.BROWSERLIGHT_STORE_GAPS)
##        storer.autoset_framesize()

##    def _refresh_ddlf_lift(self):
##        if self._ddlf._lift:
##            functions.remove_element(self._ddlf._lift)
##        if self._ddlf.get_family_rect().height > self._ddlf.get_fus_rect().height:
##            self._ddlf.add_lift()
##        functions.refresh_current_menu()

    def _refresh_ddlf(self):
        self.path = self._path_element._path
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not self.folders:
            actual_folders = None
        if not self.files:
            actual_files = []
        self._ddlf._dv = self._ddlf._get_dirviewer(titles=actual_files,
                                                   size=self._ddl_size,
                                                   folders=actual_folders)
        self._refresh_ddlf_lift()

    def _go_to_dir(self, selected):
        self._path_element._path = selected
        self._path_element._path_list = self._path_element._get_strs()
        ycoord = self._path_element._elements[0].get_storer_rect().centery
        self._path_element._set_path_elements(ycoord)
        functions.refresh_current_menu()
        self._refresh_ddlf()
        self.unblit()
        self.blit()
        self.update()

    def _reaction_press(self, event):
        hit_lift = False
        if self._ddlf._lift:
            if self._ddlf._lift.get_fus_rect().collidepoint(event.pos):
                hit_lift = True
        if not hit_lift:
            BrowserLight._reaction_press(self, event)
            selected = self.get_value()
            tick = pygame.time.get_ticks()
            if os.path.isdir(selected):
                if tick - self._last_click < parameters.DOUBLE_CLICK_DELAY:
                    self._go_to_dir(selected)
            self._last_click = tick
 def __init__(self,
              path="./",
              ddl_size=None,
              normal_params=None,
              folders=True,
              files=True,
              file_types=None,
              text="",
              finish=True):
     """File and folder browser for a.
     <path>: the path of the folder in which browser browse files.
     <ddl_size>: if not None, force the size of the dropdown list of files.
     <folders>: if True, displays folders to user.
     <files>: if True, displays file to user.
     <file_types>: if not None, pass a list of files formats that you wand to
         be valid for user choice.
     <text>: title text of the browser.
     """
     ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
     super(BrowserLight, self).__init__(normal_params=normal_params,
                                        finish=False)
     self.path = self.set_path(path)
     self.last_done_path = str(self.path)
     self._ddl_size = ddl_size
     self.file_types = file_types
     self.folders = folders
     self.files = files
     # DropDownListFast
     actual_folders, actual_files = self._get_folders_and_files()
     actual_files = self._filter_files(actual_files)
     if not folders:
         actual_folders = None
     if not files:
         actual_files = []
     self._ddlf = DropDownListFast(size=self._ddl_size,
                                   titles=actual_files,
                                   folders=actual_folders,
                                   has_lift=True,
                                   finish=False)
     ##        self._ddlf.finish()
     # selection button
     inserter_width = 3 * ddl_size[0] // 4
     ##        if inserter_width > style.MAX_INSERTER_WIDTH:
     ##            inserter_width = style.MAX_INSERTER_WIDTH
     self._selected = Inserter("Selected : ", size=(inserter_width, None))
     if isinstance(text, str):
         self.text_element = OneLineText(text)
     else:
         self.text_element = text
     self._path_element = PathElement(father=self, abspath=True)
     self.add_elements([
         self.text_element, self._path_element, self._ddlf, self._selected
     ])
     reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                             self._reaction_press, {"button": 1},
                             reac_name=constants.REAC_PRESSED)
     ##        self._ddlf._force_lift = True
     self._ddlf.finish()
     self.add_reaction(reac_pressed)
     self._clicked = None
     self._something_selected = False
     painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                              pressed=True,
                                              radius=style.BOX_RADIUS)
     self.set_painter(painter)
     self._last_click = -2 * parameters.DOUBLE_CLICK_DELAY
     if finish:
         self.finish()
Exemplo n.º 13
0
class Browser(BrowserLight):
    """Either use it dynamically (create a menu based on an explorer that is
    created "on fly", or statically adding an explorer to the main element"""

    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = path
        self._ddl_size = ddl_size
        self.file_types = file_types
        self.folders = folders
        self.files = files
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(self._ddl_size, titles=actual_files,
                                      folders=actual_folders, has_lift=True)
##        self._ddlf.finish()
        # selection button
        inserter_width = 3*ddl_size[0]//4
##        if inserter_width > style.MAX_INSERTER_WIDTH:
##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self._path_element = PathElement(self, True)
        self._path_element.finish()
        self.add_elements([self.text_element, self._path_element, self._ddlf,
                           self._selected])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press,
                                name=constants.REAC_PRESSED)
##        self._ddlf._force_lift = True
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._last_click = -2 * parameters.DOUBLE_CLICK_DELAY

    def finish(self):
        self._path_element._set_path_elements()
        Element.finish(self)
        self.store()
        centerx = self.get_fus_rect().centerx
        self.text_element.set_center((centerx, None))
        ycoord = self._path_element._elements[0].get_storer_rect().centery
        self._path_element._set_path_elements(ycoord)
        self.set_prison()

##    def store(self):
####        r = self.get_family_rect()
####        self.set_size((r.width, r.height))
##        storer = Storer(self, margins=style.BROWSERLIGHT_STORE_MARGINS,
##                        gaps=style.BROWSERLIGHT_STORE_GAPS)
##        storer.autoset_framesize()

##    def _refresh_ddlf_lift(self):
##        if self._ddlf._lift:
##            functions.remove_element(self._ddlf._lift)
##        if self._ddlf.get_family_rect().height > self._ddlf.get_fus_rect().height:
##            self._ddlf.add_lift()
##        functions.refresh_current_menu()

    def _refresh_ddlf(self):
        self.path = self._path_element._path
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not self.folders:
            actual_folders = None
        if not self.files:
            actual_files = []
        self._ddlf._dv = self._ddlf._get_dirviewer(titles=actual_files,
                                                   size=self._ddl_size,
                                                   folders=actual_folders)
        self._refresh_ddlf_lift()

    def _go_to_dir(self, selected):
        self._path_element._path = selected
        self._path_element._path_list = self._path_element._get_strs()
        ycoord = self._path_element._elements[0].get_storer_rect().centery
        self._path_element._set_path_elements(ycoord)
        functions.refresh_current_menu()
        self._refresh_ddlf()
        self.unblit()
        self.blit()
        self.update()

    def _reaction_press(self, event):
        hit_lift = False
        if self._ddlf._lift:
            if self._ddlf._lift.get_fus_rect().collidepoint(event.pos):
                hit_lift = True
        if not hit_lift:
            BrowserLight._reaction_press(self, event)
            selected = self.get_value()
            tick = pygame.time.get_ticks()
            if os.path.isdir(selected):
                if tick - self._last_click < parameters.DOUBLE_CLICK_DELAY:
                    self._go_to_dir(selected)
            self._last_click = tick
Exemplo n.º 14
0
class Browser(BrowserLight):
    """File and folder browser."""
    @staticmethod
    def make(path="./",
             ddl_size=None,
             folders=True,
             files=True,
             file_types=None,
             text=""):
        browser = Browser(path,
                          ddl_size,
                          folders=folders,
                          files=files,
                          file_types=file_types,
                          text=text)
        browser.finish()
        return browser

    def __init__(self,
                 path="./",
                 ddl_size=None,
                 normal_params=None,
                 folders=True,
                 files=True,
                 file_types=None,
                 text=""):
        """File and folder browser for a.
        <path>: the path of the folder in which browser browse files.
        <ddl_size>: if not None, force the size of the dropdown list of files.
        <folders>: if True, displays folders to user.
        <files>: if True, displays file to user.
        <file_types>: if not None, pass a list of files formats that you wand to
            be valid for user choice.
        <text>: title text of the browser.
        """
        ddl_size = style.BROWSERLIGHT_DDL_SIZE if ddl_size is None else ddl_size
        super(BrowserLight, self).__init__(normal_params=normal_params)
        self.path = self.set_path(path)
        self.last_done_path = str(self.path)
        self._ddl_size = ddl_size
        self.file_types = file_types
        self.folders = folders
        self.files = files
        # DropDownListFast
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not folders:
            actual_folders = None
        if not files:
            actual_files = []
        self._ddlf = DropDownListFast(size=self._ddl_size,
                                      titles=actual_files,
                                      folders=actual_folders,
                                      has_lift=True)
        ##        self._ddlf.finish()
        # selection button
        inserter_width = 3 * ddl_size[0] // 4
        ##        if inserter_width > style.MAX_INSERTER_WIDTH:
        ##            inserter_width = style.MAX_INSERTER_WIDTH
        self._selected = Inserter("Selected : ", size=(inserter_width, None))
        self._selected.finish()
        if isinstance(text, str):
            self.text_element = OneLineText(text)
            self.text_element.finish()
        else:
            self.text_element = text
        self._path_element = PathElement(self, True)
        self._path_element.finish()
        self.add_elements([
            self.text_element, self._path_element, self._ddlf, self._selected
        ])
        reac_pressed = Reaction(parameters.BUTTON_UNPRESS_EVENT,
                                self._reaction_press, {"button": 1},
                                reac_name=constants.REAC_PRESSED)
        ##        self._ddlf._force_lift = True
        self._ddlf.finish()
        self.add_reaction(reac_pressed)
        self._clicked = None
        self._something_selected = False
        painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                                 pressed=True,
                                                 radius=style.BOX_RADIUS)
        self.set_painter(painter)
        self._last_click = -2 * parameters.DOUBLE_CLICK_DELAY

    def finish(self):
        self._path_element._set_path_elements()
        Element.finish(self)
        self.store()
        centerx = self.get_fus_rect().centerx
        self.text_element.set_center((centerx, None))
        ycoord = self._path_element._elements[0].get_storer_rect().centery
        self._path_element._set_path_elements(ycoord)
        self.set_prison()

    def set_path(self, path):
        path = os.path.normpath(path)
        if path[-1] == os.path.sep:
            return path
        else:
            return path + os.path.sep

##    def store(self):
####        r = self.get_family_rect()
####        self.set_size((r.width, r.height))
##        storer = Storer(self, margins=style.BROWSERLIGHT_STORE_MARGINS,
##                        gaps=style.BROWSERLIGHT_STORE_GAPS)
##        storer.autoset_framesize()

##    def _refresh_ddlf_lift(self):
##        if self._ddlf._lift:
##            functions.remove_element(self._ddlf._lift)
##        if self._ddlf.get_family_rect().height > self._ddlf.get_fus_rect().height:
##            self._ddlf.add_lift()
##        functions.refresh_current_menu()

    def _refresh_ddlf(self):
        self.path = self._path_element._path
        actual_folders, actual_files = self._get_folders_and_files()
        actual_files = self._filter_files(actual_files)
        if not self.folders:
            actual_folders = None
        if not self.files:
            actual_files = []
        self._ddlf._dv = self._ddlf._get_dirviewer(titles=actual_files,
                                                   size=self._ddl_size,
                                                   folders=actual_folders)
        self._refresh_ddlf_lift()

    def _go_to_dir(self, selected):
        self._path_element._path = selected
        self._path_element._path_list = self._path_element._get_strs()
        ycoord = self._path_element._elements[0].get_storer_rect().centery
        self._path_element._set_path_elements(ycoord)
        functions.refresh_current_menu()
        self._refresh_ddlf()
        self.unblit()
        self.blit()
        self.update()

    def _reaction_press(self, event):
        hit_lift = False
        if self._ddlf._lift:
            if self._ddlf._lift.get_fus_rect().collidepoint(event.pos):
                hit_lift = True
        if not hit_lift:
            BrowserLight._reaction_press(self, event)
            selected = self.get_value()
            tick = pygame.time.get_ticks()
            if os.path.isdir(selected):
                if tick - self._last_click < parameters.DOUBLE_CLICK_DELAY:
                    self._go_to_dir(selected)
            self._last_click = tick