def __init__(self): super(RatingBox, self).__init__(self) self.thumb_ups = 1 self.thumb_downs = 1 self.title = Gtk.Label("") self.title.set_line_wrap(True) self.title.set_lines(2) hbox = Gtk.HBox() self.upvote = ToggleButton("👍") self.downvote = ToggleButton("👎") self.upvote.connect("toggled", self.__thumb_toggled) self.downvote.connect("toggled", self.__thumb_toggled) self.score_label = Gtk.Label("----") self.upvote.set_property("height-request", 50) self.downvote.set_property("height-request", 50) hbox.pack_start(self.upvote, True, True, 5) hbox.pack_start(self.downvote, True, True, 5) self.hbox = hbox self.pack_start(self.title, False, False, 10) self.pack_start(self.score_label, True, True, 5) self.pack_start(self.hbox, False, False, 5)
def __init__(self, count=0, text="", initial={}, limit=3): """count: the total amount of items expected, 0 for unknown/indefinite text: text to display in the label; may contain % formats initial: initial values for % formats (text % initial) limit: count must be greater than limit (or 0) for pause/stop to appear The current iteration of the counter can be gotten as self.current. count can be gotten as self.count. """ super().__init__() self._label = Gtk.Label() self._label.set_use_markup(True) self._progress = Gtk.ProgressBar() self._progress.set_pulse_step(0.08) self.pulse = self._progress.pulse self.set_fraction = self._progress.set_fraction self.set_text = self._label.set_markup self.setup(count, text, initial) if self.count > limit or self.count == 0: # Add stop/pause buttons. count = 0 means an indefinite # number of steps. self._cancel_button = Button(_("_Stop"), Icons.PROCESS_STOP) self._pause_button = ToggleButton(_("P_ause"), Icons.MEDIA_PLAYBACK_PAUSE) self._cancel_button.connect('clicked', self.__cancel_clicked) self._pause_button.connect('clicked', self.__pause_clicked) else: self._cancel_button = None self._pause_button = None
def __init__(self, count=0, text="", initial={}, limit=3): """count: the total amount of items expected, 0 for unknown/indefinite text: text to display in the label; may contain % formats initial: initial values for % formats (text % initial) limit: count must be greater than limit (or 0) for pause/stop to appear The current iteration of the counter can be gotten as self.current. count can be gotten as self.count. """ super(WaitLoadBase, self).__init__() self._label = Gtk.Label() self._label.set_use_markup(True) self._progress = Gtk.ProgressBar() self._progress.set_pulse_step(0.08) self.pulse = self._progress.pulse self.set_fraction = self._progress.set_fraction self.set_text = self._label.set_markup self.setup(count, text, initial) if self.count > limit or self.count == 0: # Add stop/pause buttons. count = 0 means an indefinite # number of steps. self._cancel_button = Button(_("_Stop"), Icons.PROCESS_STOP) self._pause_button = ToggleButton(_("P_ause"), Icons.MEDIA_PLAYBACK_PAUSE) self._cancel_button.connect('clicked', self.__cancel_clicked) self._pause_button.connect('clicked', self.__pause_clicked) else: self._cancel_button = None self._pause_button = None
class RatingBox(Gtk.VBox): def __init__(self): super(RatingBox, self).__init__(self) self.thumb_ups = 1 self.thumb_downs = 1 self.title = Gtk.Label("") self.title.set_line_wrap(True) self.title.set_lines(2) hbox = Gtk.HBox() self.upvote = ToggleButton("👍") self.downvote = ToggleButton("👎") self.upvote.connect("toggled", self.__thumb_toggled) self.downvote.connect("toggled", self.__thumb_toggled) self.score_label = Gtk.Label("----") self.upvote.set_property("height-request", 50) self.downvote.set_property("height-request", 50) hbox.pack_start(self.upvote, True, True, 5) hbox.pack_start(self.downvote, True, True, 5) self.hbox = hbox self.pack_start(self.title, False, False, 10) self.pack_start(self.score_label, True, True, 5) self.pack_start(self.hbox, False, False, 5) def set_current_title(self, title): self.title.set_text(title) def set_current_score(self, cth_up, cth_down): self.thumb_ups = cth_up self.thumb_downs = cth_down self.__set_pending_score_value(self.thumb_ups - self.thumb_downs) def poll_vote(self, reset=True): upward = 1 if self.upvote.get_active() else 0 downward = 1 if self.downvote.get_active() else 0 vote = (upward, downward) if reset: self.downvote.set_active(False) self.upvote.set_active(False) return vote def __set_pending_score_value(self, score): existing_score = self.thumb_ups - self.thumb_downs if score == existing_score: self.score_label.set_markup("<b>" + str(int(score)) + "</b>") elif score > existing_score: self.score_label.set_markup("<b><span foreground=\"green\">" + str(int(score)) + "</span></b>") else: self.score_label.set_markup("<b><span foreground=\"red\">" + str(int(score)) + "</span></b>") def __thumb_toggled(self, button): if button.get_active(): if button == self.upvote: self.downvote.set_active(False) elif button == self.downvote: self.upvote.set_active(False) vote = self.poll_vote(False) self.__set_pending_score_value(self.thumb_ups + vote[0] - self.thumb_downs - vote[1])
class WaitLoadBase: """Abstract class providing a label, a progressbar, pause/stop buttons, and the stepping logic.""" def __init__(self, count=0, text="", initial={}, limit=3): """count: the total amount of items expected, 0 for unknown/indefinite text: text to display in the label; may contain % formats initial: initial values for % formats (text % initial) limit: count must be greater than limit (or 0) for pause/stop to appear The current iteration of the counter can be gotten as self.current. count can be gotten as self.count. """ super().__init__() self._label = Gtk.Label() self._label.set_use_markup(True) self._progress = Gtk.ProgressBar() self._progress.set_pulse_step(0.08) self.pulse = self._progress.pulse self.set_fraction = self._progress.set_fraction self.set_text = self._label.set_markup self.setup(count, text, initial) if self.count > limit or self.count == 0: # Add stop/pause buttons. count = 0 means an indefinite # number of steps. self._cancel_button = Button(_("_Stop"), Icons.PROCESS_STOP) self._pause_button = ToggleButton(_("P_ause"), Icons.MEDIA_PLAYBACK_PAUSE) self._cancel_button.connect('clicked', self.__cancel_clicked) self._pause_button.connect('clicked', self.__pause_clicked) else: self._cancel_button = None self._pause_button = None def setup(self, count=0, text="", initial=None): self.current = 0 self.count = count self._text = text self.paused = False self.quit = False self._start_time = time.time() initial = initial or {} initial.setdefault("total", self.count) initial.setdefault("current", self.current) initial.setdefault("remaining", _("Unknown")) def localeify(k, v): foo = '%(' + k + ')d' if foo in self._text: self._text = self._text.replace(foo, '%(' + k + ')s') return k, format_int_locale(int(v)) return k, v localed = dict([localeify(k, v) for k, v in initial.items()]) self._label.set_markup(self._text % localed) self._progress.set_fraction(0.0) def __pause_clicked(self, button): self.paused = button.get_active() def __cancel_clicked(self, button): self.quit = True def step(self, **values): """Advance the counter by one. Arguments are applied to the originally-supplied text as a format string. This function doesn't return if the dialog is paused (though the GTK main loop will still run), and returns True if stop was pressed. """ if self.count: self.current += 1 self._progress.set_fraction( max(0, min(1, self.current / float(self.count)))) else: self._progress.pulse() values.setdefault("total", format_int_locale(self.count)) values.setdefault("current", format_int_locale(self.current)) if self.count: t = (time.time() - self._start_time) / self.current remaining = math.ceil((self.count - self.current) * t) values.setdefault("remaining", format_time_display(remaining)) self._label.set_markup(self._text % values) while not self.quit and (self.paused or Gtk.events_pending()): Gtk.main_iteration() return self.quit
class WaitLoadBase(object): """Abstract class providing a label, a progressbar, pause/stop buttons, and the stepping logic.""" def __init__(self, count=0, text="", initial={}, limit=3): """count: the total amount of items expected, 0 for unknown/indefinite text: text to display in the label; may contain % formats initial: initial values for % formats (text % initial) limit: count must be greater than limit (or 0) for pause/stop to appear The current iteration of the counter can be gotten as self.current. count can be gotten as self.count. """ super(WaitLoadBase, self).__init__() self._label = Gtk.Label() self._label.set_use_markup(True) self._progress = Gtk.ProgressBar() self._progress.set_pulse_step(0.08) self.pulse = self._progress.pulse self.set_fraction = self._progress.set_fraction self.set_text = self._label.set_markup self.setup(count, text, initial) if self.count > limit or self.count == 0: # Add stop/pause buttons. count = 0 means an indefinite # number of steps. self._cancel_button = Button(_("_Stop"), Icons.PROCESS_STOP) self._pause_button = ToggleButton(_("P_ause"), Icons.MEDIA_PLAYBACK_PAUSE) self._cancel_button.connect('clicked', self.__cancel_clicked) self._pause_button.connect('clicked', self.__pause_clicked) else: self._cancel_button = None self._pause_button = None def setup(self, count=0, text="", initial=None): self.current = 0 self.count = count self._text = text self.paused = False self.quit = False self._start_time = time.time() initial = initial or {} initial.setdefault("total", self.count) initial.setdefault("current", self.current) initial.setdefault("remaining", _("Unknown")) def localeify(k, v): foo = '%(' + k + ')d' if foo in self._text: self._text = self._text.replace(foo, '%(' + k + ')s') return k, format_int_locale(int(v)) return k, v localed = dict([localeify(k, v) for k, v in iteritems(initial)]) self._label.set_markup(self._text % localed) self._progress.set_fraction(0.0) def __pause_clicked(self, button): self.paused = button.get_active() def __cancel_clicked(self, button): self.quit = True def step(self, **values): """Advance the counter by one. Arguments are applied to the originally-supplied text as a format string. This function doesn't return if the dialog is paused (though the GTK main loop will still run), and returns True if stop was pressed. """ if self.count: self.current += 1 self._progress.set_fraction( max(0, min(1, self.current / float(self.count)))) else: self._progress.pulse() values.setdefault("total", format_int_locale(self.count)) values.setdefault("current", format_int_locale(self.current)) if self.count: t = (time.time() - self._start_time) / self.current remaining = math.ceil((self.count - self.current) * t) values.setdefault("remaining", format_time_display(remaining)) self._label.set_markup(self._text % values) while not self.quit and (self.paused or Gtk.events_pending()): Gtk.main_iteration() return self.quit