Commit 0cbe887c authored by Cédric Bellegarde's avatar Cédric Bellegarde

Implement smooth progress in ProgressBar

parent 358a1ed4
Pipeline #37580 passed with stage
in 13 minutes and 32 seconds
......@@ -10,12 +10,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from gi.repository import Gtk
from gi.repository import Gtk, GLib
class ProgressBar(Gtk.ProgressBar):
"""
A smart progress bar
A smart/smooth FIFO progress bar
Many objects can register and must call set_fraction(1) to unregister
"""
def __init__(self):
......@@ -24,6 +25,8 @@ class ProgressBar(Gtk.ProgressBar):
"""
Gtk.ProgressBar.__init__(self)
self.__callers = []
self.__fraction = 0.0
self.__progress_running = False
def add(self, caller):
"""
......@@ -36,13 +39,48 @@ class ProgressBar(Gtk.ProgressBar):
def set_fraction(self, fraction, caller):
"""
Set fraction if caller is on top.
@param fraction as float
@param caller as object
"""
if not self.__callers:
return
if caller == self.__callers[0]:
self.show()
Gtk.ProgressBar.set_fraction(self, fraction)
if fraction == 1:
self.__callers.remove(caller)
self.hide()
Gtk.ProgressBar.set_fraction(self, 0.0)
self.__fraction = fraction
if not self.__progress_running:
self.__progress_running = True
self.__progress_update(caller)
#######################
# 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)
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
if current < self.__fraction:
Gtk.ProgressBar.set_fraction(self, current + progress)
if current < 1.0:
GLib.timeout_add(10, self.__progress_update, caller)
else:
GLib.timeout_add(1000, self.__reset, caller)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment