progressbar.py 3.99 KB
Newer Older
Cédric Bellegarde's avatar
Cédric Bellegarde committed
1
# Copyright (c) 2014-2019 Cedric Bellegarde <cedric.bellegarde@adishatz.org>
2 3 4 5 6 7 8 9 10 11 12
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

13
from gi.repository import Gtk, GLib
14 15 16 17


class ProgressBar(Gtk.ProgressBar):
    """
18 19
        A smart/smooth FIFO progress bar
        Many objects can register and must call set_fraction(1) to unregister
20 21 22 23 24 25 26 27
    """

    def __init__(self):
        """
            Init progress bar
        """
        Gtk.ProgressBar.__init__(self)
        self.__callers = []
28
        self.__fraction = 0.0
29
        self.__pulse_timeout = None
30
        self.__progress_running = False
31 32 33 34 35 36 37 38 39 40 41 42

    def add(self, caller):
        """
            Add caller
            @param caller as Instance
        """
        if caller not in self.__callers:
            self.__callers.insert(0, caller)

    def set_fraction(self, fraction, caller):
        """
            Set fraction if caller is on top.
43 44
            @param fraction as float
            @param caller as object
45 46 47
        """
        if not self.__callers:
            return
48 49 50
        if self.__pulse_timeout is not None:
            GLib.source_remove(self.__pulse_timeout)
            self.__pulse_timeout = None
51
        if caller == self.__callers[0]:
52
            self.show()
53
            self.__fraction = fraction
54 55 56
            if fraction == 0:
                Gtk.ProgressBar.set_fraction(self, 0.0)
            elif not self.__progress_running:
57 58 59
                self.__progress_running = True
                self.__progress_update(caller)

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    def pulse(self, pulse):
        """
            Make progress bar visible/pulse if pulse is True
            @param pulse as bool
        """
        # Only pulse if nobody is using the progressbar
        if self.__callers:
            return
        if pulse:
            self.show()
            if self.__pulse_timeout is None:
                self.__pulse_timeout = GLib.timeout_add(500, self.__pulse)
        else:
            self.hide()
            if self.__pulse_timeout is not None:
                GLib.source_remove(self.__pulse_timeout)
                self.__pulse_timeout = None

78 79 80 81 82 83 84 85 86 87 88 89 90 91
#######################
# PRIVATE             #
#######################
    def __reset(self, caller):
        """
            Reset and hide progressbar
            @param caller as object
        """
        self.hide()
        self.__fraction = 0.0
        Gtk.ProgressBar.set_fraction(self, 0.0)
        self.__progress_running = False
        self.__callers.remove(caller)

92 93 94 95 96 97 98 99 100 101 102 103 104
    def __pulse(self):
        """
            Make progress bar pulse while visible
            @param pulse as bool
        """
        if self.is_visible():
            Gtk.ProgressBar.pulse(self)
            return True
        else:
            Gtk.ProgressBar.set_fraction(self, 0.0)
            self.__pulse_timeout = None
            return False

105 106 107 108 109 110 111 112 113 114 115 116 117
    def __progress_update(self, caller):
        """
            Update progressbar smoothly
            @param caller as object
        """
        if caller != self.__callers[0]:
            self.__progress_running = False
            return
        current = self.get_fraction()
        if self.__fraction < 1:
            progress = (self.__fraction - current) / 10
        else:
            progress = 0.01
118 119
        if current < self.__fraction:
            Gtk.ProgressBar.set_fraction(self, current + progress)
120 121 122 123
        if current < 1.0:
            GLib.timeout_add(10, self.__progress_update, caller)
        else:
            GLib.timeout_add(1000, self.__reset, caller)