def _start_stopper(self, activity_type): """Leaves the Gtk thread, creates a Stopper object there that is referenced in this object and starts it.""" try: self._stopper = Stopper(self, activity_type) self._stopper.start() except threading.ThreadError: print("Threading problem in Tracking sub-menu.")
class TimeManagerSubMenu(BaseSubMenu): _stopper = None _display_label = '' def __init__(self): super(TimeManagerSubMenu, self).__init__(self.__class__.__name__, "Time manager") #from itc_kit.gui.icons.build_in_icons import get_productivity_icons #pro_icon, neu_icon, cou_icon = get_productivity_icons() menu_items = [Gtk.ImageMenuItem(None), Gtk.ImageMenuItem(None), Gtk.ImageMenuItem(None), Gtk.MenuItem("Display"), Gtk.MenuItem("Stop"), Gtk.MenuItem("Undo")] self.productive_widget = menu_items[0] self.productive_widget.set_label("Productive") self.neutral_widget = menu_items[1] self.neutral_widget.set_label("Neutral") self.counter_productive_widget = menu_items[2] self.counter_productive_widget.set_label("Counterproductive") self.display_widget = menu_items[3] self.stop_widget = menu_items[4] self.undo_widget = menu_items[5] self.productive_widget.set_always_show_image(True) self.neutral_widget.set_always_show_image(True) self.counter_productive_widget.set_always_show_image(True) [(self.append(item)) for item in menu_items] self.productive_widget.connect("activate", self.on_productivity_choice_clicked) self.neutral_widget.connect("activate", self.on_productivity_choice_clicked) self.counter_productive_widget.connect("activate", self.on_productivity_choice_clicked) self.stop_widget.connect("activate", self.on_stop_clicked) self.undo_widget.connect("activate", self.on_undo_clicked) self.set_menu_state2("activated") GLib.timeout_add(10, self.handler_timeout) def handler_timeout(self): self.display_widget.set_label(self._display_label) return True def on_productivity_choice_clicked(self, widget): """Event handler for all three productivity type choices. Independent of choice sets sub-menu state to tracking. :param widget: the widget that triggered this event handler :type widget: Gtk.ImageMenuItem """ self.set_menu_state2("tracking") self._start_stopper(widget.get_label()) def on_undo_clicked(self): #ToDo Implement tracking undo. raise NotImplementedError def on_stop_clicked(self, widget): """Event handler for stopping the stopper. :param widget: the widget that triggered this event handler :type widget: Gtk.MenuItem """ self._stopper.stop_tracking() self._stopper = None self.set_menu_state2("activated") def _start_stopper(self, activity_type): """Leaves the Gtk thread, creates a Stopper object there that is referenced in this object and starts it.""" try: self._stopper = Stopper(self, activity_type) self._stopper.start() except threading.ThreadError: print("Threading problem in Tracking sub-menu.") def set_menu_state2(self, state): """Sets the sub-menu to the appropriate state. :param state: The current state :type state: str """ if state == "tracking": self.productive_widget.hide() self.neutral_widget.hide() self.counter_productive_widget.hide() self.stop_widget.show() self.display_widget.show() elif state == "activated": self.productive_widget.show() self.neutral_widget.show() self.counter_productive_widget.show() self.stop_widget.hide() self.display_widget.hide() #self.undo_widget.hide() else: print("state parameter need to be either tracking or activated.") raise RuntimeError