Beispiel #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()
 def __init__(self,
              name_txt,
              file_txt,
              titles,
              normal_params=None,
              ddlf_size=None,
              folders=False,
              file_width=None,
              show_select=True,
              selected=None,
              click_quit=True):
     ddlf_size = style.DDL_SIZE if ddlf_size is None else ddlf_size
     file_width = style.FILE_WIDTH if file_width is None else file_width
     self.show_select = show_select
     if not self.show_select:
         file_txt = ""
     _Launcher.__init__(self, file_txt, normal_params, click_quit)
     self.file_width = file_width
     # to launch
     self.launched_element = DropDownListFast(ddlf_size, titles, folders=folders)
     self.launched_element.finish()
     self._deny_child(self.launched_element)
     self.launched_element._dv.x = 2
     # name
     self._name_element = self._get_name_element(name_txt)
     self._name_element.user_func = self.launch_ddlf
     if not self.show_select:
         self.get_storer_rect = self._name_element.get_storer_rect
     self.add_elements(list([self._name_element]))
     self.selected = selected
    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()
Beispiel #4
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
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
Beispiel #6
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
    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
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
Beispiel #9
0
class DropDownListLauncher(Clickable):

    @staticmethod
    def make(const_text="",
             var_text="",
             titles=None,
             ddlf_size="auto",
             show_select=True,
             click_cancel=True,
             size=None):
        if size is None: size=style.MAKE_SIZE
        ddll = DropDownListLauncher(const_text, var_text, titles, ddlf_size,
                                    show_select, click_cancel,finish=False)
        ddll.finish()
        ddll._make_size(size)
        return ddll

    def __init__(self,
                 const_text="",
                 var_text="",
                 titles=None,
                 ddlf_size="auto",
                 show_select=True,
                 click_cancel=True,
                 finish=True):
        self.recenter = True
        self.const_text = const_text
        self.var_text = var_text
        self.click_cancel = click_cancel
##        self.max_chars = float("inf")
        self.max_chars = 30
        self.cut_text = ".."
        self.unlaunch_func = None
        ddlf_size = style.DDL_SIZE if ddlf_size is None else ddlf_size
        if not isinstance(titles, DropDownListFast):
            titles = [] if titles is None else titles
            self.launched = DropDownListFast(size=ddlf_size, titles=titles, x=2)
        else:
            self.launched = titles
        self.show_select = show_select
        self.launcher = None
        Clickable.__init__(self, self.const_text+self.var_text,finish=False)
        if finish:
            self.finish()

    def finish(self):
        Clickable.finish(self)
        self._set_launcher()

    def get_value(self):
        return self.var_text

    def set_value(self, text):
        old = self.var_text
        if isinstance(text, list):
            self.var_text = text[0]
        else:
            self.var_text = text
        if old != self.var_text:
            self.refresh()

    def _set_launcher(self):
        launcher = launchmod.Launcher(self.launched, launching=self)
        reac_enter = ConstantReaction(constants.THORPY_EVENT,
                             launcher.launch,
                             {"id": constants.EVENT_UNPRESS, "el":self})
##                             reac_name="reac_launch")
        reac_done = ConstantReaction(constants.THORPY_EVENT,
                             self.unlaunch,
                             {"id": constants.EVENT_DDL, "el":self.launched})
##                             reac_name="reac_done")
        if self.click_cancel:
            reac_cancel = Reaction(parameters.MOUSEBUTTONUP,
                                    launchmod.func_click_quit,
                                    params={"launcher":launcher, "what":CLICK_QUIT})
##                                    reac_name="reac_cancel")
            self.launched.add_reaction(reac_cancel)
        self.add_reaction(reac_enter)
        self.launched.add_reaction(reac_done)
        def func_before():
            self.launched.stick_to(self, "bottom", "top")
            self.launched.blit()
            self.launched.update()
        launcher.func_before = func_before
        self.launcher = launcher

    def refresh(self):
        text = self.const_text+self.var_text
        if len(text) > self.max_chars:
            text = text[:self.max_chars-len(self.cut_text)] + self.cut_text
        self.set_text(text)
        self.scale_to_title()
        if self.recenter:
            self.center(element=self.father, axis=(True,False))


    def default_unlaunch(self):
        if self.show_select and self.launched._clicked:
            self.var_text = self.launched._clicked
            self.refresh()
        self.launcher.unlaunch(CANCEL)
        ##            self._file_element.set_text(text,
##                          size=(self.file_width, self.get_fus_rect().h),
##                          cut=True)

    def unlaunch(self):
        ev = pygame.event.Event(constants.THORPY_EVENT, id=constants.EVENT_DDL,
                                el=self, value=self.launched._clicked)
        pygame.event.post(ev)
        if not self.unlaunch_func:
            self.default_unlaunch()
        else:
            self.unlaunch_func()
class DropDownListLauncher(_Launcher):

    def __init__(self,
                 name_txt,
                 file_txt,
                 titles,
                 normal_params=None,
                 ddlf_size=None,
                 folders=False,
                 file_width=None,
                 show_select=True,
                 selected=None,
                 click_quit=True):
        ddlf_size = style.DDL_SIZE if ddlf_size is None else ddlf_size
        file_width = style.FILE_WIDTH if file_width is None else file_width
        self.show_select = show_select
        if not self.show_select:
            file_txt = ""
        _Launcher.__init__(self, file_txt, normal_params, click_quit)
        self.file_width = file_width
        # to launch
        self.launched_element = DropDownListFast(ddlf_size, titles, folders=folders)
        self.launched_element.finish()
        self._deny_child(self.launched_element)
        self.launched_element._dv.x = 2
        # name
        self._name_element = self._get_name_element(name_txt)
        self._name_element.user_func = self.launch_ddlf
        if not self.show_select:
            self.get_storer_rect = self._name_element.get_storer_rect
        self.add_elements(list([self._name_element]))
        self.selected = selected

    def unblit(self, rect=None):
        functions.debug_msg("unblit" + str(self))
        self._name_element.unblit(rect)
        _Launcher.unblit(self, rect)

    def _get_name_element(self, name):
        painter = functions.obtain_valid_painter(
            painterstyle.DEF_PAINTER,
            size=style.SIZE)
        el = Clickable(name)
        el.set_painter(painter)
        el.finish()
        return el

    def launch_ddlf(self):
        self._set_branch_last()
        r = self.get_storer_rect()
        self.launched_element.set_topleft(r.bottomleft)
        self.add_elements([self.launched_element])
        tm = TickedMenu(self.launched_element)
        if self.click_quit:
            self._quit_when_click(self.launched_element)
        self.launched_element._set_selecter(tm)
        tm.refresh()
        tm.play()
        if self.click_quit:
            self.launched_element.deactivate_reaction(constants.REAC_CLICKQUIT)
        if self.launched_element._clicked:
            text = normpath(self.launched_element._clicked)
            text = basename(text)
            self.selected = text
            functions.debug_msg(self.selected)
            if self.show_select:
                size = (self.file_width, self.get_fus_size()[1])
                self.set_text(text, size=size)
        self._deny_child(self.launched_element)
        self._unlaunch()


    def _refresh_pos(self):
        self_left = self.get_fus_topleft()[0]
        (x, y) = self.get_fus_center()
        l = self_left - (self._name_element.get_fus_size()[0] + style.NAME_SPACING)
        self._name_element.set_center((None, y))
        self._name_element.set_topleft((l, None))

    def finish(self):
        _Launcher.finish(self)

    def scale_to_title(self):
        self._name_element.scale_to_title()
##        self._name_element.stick_to(self, "left", "right")
        self._refresh_pos()
 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()
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,
                          finish=False)
        browser.finish()
        return browser

    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()

    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
Beispiel #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
class DropDownListLauncher(_Launcher):
    def __init__(self,
                 name_txt,
                 file_txt,
                 titles,
                 normal_params=None,
                 ddlf_size=None,
                 folders=False,
                 file_width=None,
                 show_select=True,
                 selected=None,
                 click_quit=True):
        ddlf_size = style.DDL_SIZE if ddlf_size is None else ddlf_size
        file_width = style.FILE_WIDTH if file_width is None else file_width
        self.show_select = show_select
        if not self.show_select:
            file_txt = ""
        _Launcher.__init__(self, file_txt, normal_params, click_quit)
        self.file_width = file_width
        # to launch
        self.launched_element = DropDownListFast(ddlf_size,
                                                 titles,
                                                 folders=folders)
        self.launched_element.finish()
        self._deny_child(self.launched_element)
        self.launched_element._dv.x = 2
        # name
        self._name_element = self._get_name_element(name_txt)
        self._name_element.user_func = self.launch_ddlf
        if not self.show_select:
            self.get_storer_rect = self._name_element.get_storer_rect
        self.add_elements(list([self._name_element]))
        self.selected = selected

    def unblit(self, rect=None):
        functions.debug_msg("unblit" + str(self))
        self._name_element.unblit(rect)
        _Launcher.unblit(self, rect)

    def _get_name_element(self, name):
        painter = functions.obtain_valid_painter(painterstyle.DEF_PAINTER,
                                                 size=style.SIZE)
        el = Clickable(name)
        el.set_painter(painter)
        el.finish()
        return el

    def launch_ddlf(self):
        self._set_branch_last()
        r = self.get_storer_rect()
        self.launched_element.set_topleft(r.bottomleft)
        self.add_elements([self.launched_element])
        tm = TickedMenu(self.launched_element)
        if self.click_quit:
            self._quit_when_click(self.launched_element)
        self.launched_element._set_selecter(tm)
        tm.refresh()
        tm.play()
        if self.click_quit:
            self.launched_element.deactivate_reaction(constants.REAC_CLICKQUIT)
        if self.launched_element._clicked:
            text = normpath(self.launched_element._clicked)
            text = basename(text)
            self.selected = text
            functions.debug_msg(self.selected)
            if self.show_select:
                size = (self.file_width, self.get_fus_size()[1])
                self.set_text(text, size=size)
        self._deny_child(self.launched_element)
        self._unlaunch()

    def _refresh_pos(self):
        self_left = self.get_fus_topleft()[0]
        (x, y) = self.get_fus_center()
        l = self_left - (self._name_element.get_fus_size()[0] +
                         style.NAME_SPACING)
        self._name_element.set_center((None, y))
        self._name_element.set_topleft((l, None))

    def finish(self):
        _Launcher.finish(self)

    def scale_to_title(self):
        self._name_element.scale_to_title()
        ##        self._name_element.stick_to(self, "left", "right")
        self._refresh_pos()