Exemple #1
0
    def __init__(self, **kvargs):
        super(ProgressLoad, self).__init__(**kvargs)
        box = BoxLayout(orientation="vertical", padding=20)

        self.label_already_loaded = Label(
            text="", size_hint=(1, .1), markup=True)
        self.label_total_size = Label(
            text="", size_hint=(1, .1), markup=True)
        self.progress_load = KProgress()
        self.button_cancel = \
            Button(text=self.text_button_cancel, on_press=self.events_callback,
                   background_normal=self.button_cancel_background_normal,
                   background_down=self.button_cancel_background_down,
                   size_hint=(1, .2))

        self.label_already_loaded.bind(
            size=lambda *args: self._update_text_size(args))
        self.label_total_size.bind(
            size=lambda *args: self._update_text_size(args))

        box.add_widget(self.label_already_loaded)
        box.add_widget(self.label_total_size)
        box.add_widget(Widget(size_hint=(None, .02)))
        box.add_widget(SettingSpacer())
        box.add_widget(Widget(size_hint=(None, .02)))
        box.add_widget(self.progress_load)
        box.add_widget(Widget(size_hint=(None, .3)))
        box.add_widget(SettingSpacer())
        box.add_widget(Widget(size_hint=(None, .02)))
        box.add_widget(self.button_cancel)

        self.progress_load.min = 0
        self.progress_load.max = 100
        self.progress_load.bar_value = 0
        self.progress_load.spacing_widget = 2
        self.progress_load.height_widget = 1
        self.progress_load.color = self.progress_line_color
        self.progress_load.background_color = self.progress_background_color
        self.progress_load.border_color = self.progress_border_color

        self.body = Popup(title=self.title, content=box, auto_dismiss=False,
                          size_hint=self.size_hint)
Exemple #2
0
class ProgressLoad(Widget):
    retrieve_callback = ObjectProperty(p)
    events_callback = ObjectProperty(p)
    on_load = ObjectProperty(p)
    """Функция для обработки событий окна."""
    dismiss_callback = ObjectProperty(None)
    """Функция, которая будет вызвана после закрытия окна."""
    size_hint = ListProperty((.7, .5))
    """Размер окна."""
    base_font_size = NumericProperty(18)
    """Размер базового шрифта для текстовой информации."""
    title = StringProperty("Progress Download:")
    """Подпись окна."""
    button_cancel_background_down = StringProperty(
        "atlas://data/images/defaulttheme/button_pressed"
    )
    """background нажатой кнопки добавления ответа в форум."""
    button_cancel_background_normal = StringProperty(
        "atlas://data/images/defaulttheme/button"
    )
    """background не нажатой кнопки добавления ответа в форум."""
    text_already_loaded = StringProperty("Already loaded - {} byte")
    text_total_size = StringProperty("Total size - {} byte")

    progress_line_color = StringProperty("#ff7f32")
    progress_background_color = StringProperty("#2fbfe0")
    progress_border_color = StringProperty("#2fbfe0")

    text_button_cancel = StringProperty("Cancel")

    def __init__(self, **kvargs):
        super(ProgressLoad, self).__init__(**kvargs)
        box = BoxLayout(orientation="vertical", padding=20)

        self.label_already_loaded = Label(
            text="", size_hint=(1, .1), markup=True)
        self.label_total_size = Label(
            text="", size_hint=(1, .1), markup=True)
        self.progress_load = KProgress()
        self.button_cancel = \
            Button(text=self.text_button_cancel, on_press=self.events_callback,
                   background_normal=self.button_cancel_background_normal,
                   background_down=self.button_cancel_background_down,
                   size_hint=(1, .2))

        self.label_already_loaded.bind(
            size=lambda *args: self._update_text_size(args))
        self.label_total_size.bind(
            size=lambda *args: self._update_text_size(args))

        box.add_widget(self.label_already_loaded)
        box.add_widget(self.label_total_size)
        box.add_widget(Widget(size_hint=(None, .02)))
        box.add_widget(SettingSpacer())
        box.add_widget(Widget(size_hint=(None, .02)))
        box.add_widget(self.progress_load)
        box.add_widget(Widget(size_hint=(None, .3)))
        box.add_widget(SettingSpacer())
        box.add_widget(Widget(size_hint=(None, .02)))
        box.add_widget(self.button_cancel)

        self.progress_load.min = 0
        self.progress_load.max = 100
        self.progress_load.bar_value = 0
        self.progress_load.spacing_widget = 2
        self.progress_load.height_widget = 1
        self.progress_load.color = self.progress_line_color
        self.progress_load.background_color = self.progress_background_color
        self.progress_load.border_color = self.progress_border_color

        self.body = Popup(title=self.title, content=box, auto_dismiss=False,
                          size_hint=self.size_hint)

    def show(self, link, path, on_load=None):
        """
        :type link: str;
        :param link: адресс загрузки;

        :type path: str;
        :param path: путь к директории для сохранения загруженного контента;

        :type on_load: function;
        :param on_load: функция, вызываемая после окончания процесса загрузки;

        """

        if callable(self.dismiss_callback):
            self.body.bind(on_dismiss=self.dismiss_callback)
        if callable(on_load):
            self._on_load = on_load

        self.body.open()

        thread = \
            threading.Thread(target=self.retrieve_callback,
                             args=(link, path, self._tick_callback,
                                   self._on_load,))
        thread.start()

    def _on_load(self):
        pass

    def _tick_callback(self, already_loaded, total_size):
        """
        Отрисовка прогресса загрузки.

        :type already_loaded: int;
        :param already_loaded: текущее колличество загруженного контента
                               в байтах;

        :type total_size: int;
        :param total_size: общее колличество загруженного контента в байтах;

        """

        value = (already_loaded * 100) / total_size
        self.progress_load.add_value_percent(value)

        self.label_already_loaded.text = \
            self.text_already_loaded.format(already_loaded)
        self.label_total_size.text = self.text_total_size.format(total_size)

        if value == 100:
            self.body.dismiss()

    def _update_text_size(self, *args):
        label = args[0][0]
        width = args[0][1][0]

        label.text_size = (width - 10, 20)