LUIProgressbar.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from LUIObject import LUIObject
  2. from LUISprite import LUISprite
  3. from LUILayouts import LUIHorizontalStretchedLayout
  4. from LUILabel import LUILabel
  5. class LUIProgressbar(LUIObject):
  6. """ A simple progress bar """
  7. def __init__(self, parent=None, width=200, value=50, show_label=True, label_fmt="{percentage} %", **kwargs):
  8. """ Constructs a new progress bar. If show_label is True, a label indicating
  9. the current progress is shown """
  10. LUIObject.__init__(self, **kwargs)
  11. self.set_width(width)
  12. self._bg_layout = LUIHorizontalStretchedLayout(
  13. parent=self, prefix="ProgressbarBg", width="100%")
  14. self._fg_left = LUISprite(self, "ProgressbarFg_Left", "skin")
  15. self._fg_mid = LUISprite(self, "ProgressbarFg", "skin")
  16. self._fg_right = LUISprite(self, "ProgressbarFg_Right", "skin")
  17. self._fg_finish = LUISprite(self, "ProgressbarFg_Finish", "skin")
  18. self._show_label = show_label
  19. self._label_format = label_fmt
  20. self._progress_pixel = 0
  21. self._fg_finish.right = 0
  22. self._max = 100.0
  23. self._value = value
  24. if self._show_label:
  25. self._progress_label = LUILabel(parent=self, text=u"33 %")
  26. self._progress_label.centered = (True, True)
  27. self.set_value(value)
  28. self._update_progress()
  29. if parent is not None:
  30. self.parent = parent
  31. def set_max(self, _max):
  32. self._max = _max
  33. def get_max(self): return self._max
  34. def get_value(self):
  35. """ Returns the current value of the progress bar """
  36. return self._value
  37. def set_value(self, val):
  38. """ Sets the value of the progress bar """
  39. self._value = val
  40. val = max(0, min(self._max, val))
  41. self._progress_pixel = int(val / self._max * self.width)
  42. self._update_progress()
  43. value = property(get_value, set_value)
  44. def _update_progress(self):
  45. """ Internal method to update the progressbar """
  46. self._fg_finish.hide()
  47. if self._progress_pixel <= self._fg_left.width + self._fg_right.width:
  48. self._fg_mid.hide()
  49. self._fg_right.left = self._fg_left.width
  50. else:
  51. self._fg_mid.show()
  52. self._fg_mid.left = self._fg_left.width
  53. self._fg_mid.width = self._progress_pixel - self._fg_right.width - self._fg_left.width
  54. self._fg_right.left = self._fg_mid.left + self._fg_mid.width
  55. if self._progress_pixel >= self.width - self._fg_right.width:
  56. self._fg_finish.show()
  57. self._fg_finish.right = - (self.width - self._progress_pixel)
  58. self._fg_finish.clip_bounds = (0, self.width - self._progress_pixel, 0, 0)
  59. if self._show_label:
  60. string = self._label_format.replace('{value}', "{0}".format(int(self.get_value())))
  61. string = string.replace('{max}', "{0}".format(int(self._max)))
  62. string = string.replace('{percentage}', "{:.2f}".format((self._progress_pixel / self.width) * self._max))
  63. self._progress_label.set_text(string)