class QualityCheckMode(BaseMode): """Include units based on quality checks that units fail.""" name = 'QualityCheck' display_name = _("Quality Checks") widgets = [] # INITIALIZERS # def __init__(self, controller): """Constructor. @type controller: virtaal.controllers.ModeController @param controller: The ModeController that managing program modes.""" self.controller = controller self.store_controller = controller.main_controller.store_controller self.checks_controller = controller.main_controller.checks_controller self._checker_set_id = None self.filter_checks = [] self._menuitem_checks = {} self.store_filename = None # METHODS # def selected(self): self.stats = self.store_controller.get_store_stats() self.storecursor = self.store_controller.cursor self.checks_names = {} for check, indices in self.stats.iteritems(): if indices and check not in ('total', 'translated', 'untranslated'): self.checks_names[check] = self.checks_controller.get_check_name(check) self._checker_set_id = self.checks_controller.connect( 'checker-set', self._on_checker_set ) self._add_widgets() self._update_button_label() self.update_indices() def unselected(self): if self._checker_set_id: self.checks_controller.disconnect(self._checker_set_id) self._checker_set_id = None def update_indices(self): if not self.storecursor or not self.storecursor.model: return indices = [] for check in self.filter_checks: indices.extend(self.stats[check]) if not indices: indices = range(len(self.storecursor.model)) indices.sort() self.storecursor.indices = indices def _add_widgets(self): table = self.controller.view.mode_box self.btn_popup = PopupMenuButton(menu_pos=POS_NW_SW) self.btn_popup.set_relief(gtk.RELIEF_NORMAL) self.btn_popup.set_menu(self._create_checks_menu()) self.widgets = [self.btn_popup] xoptions = gtk.FILL table.attach(self.btn_popup, 2, 3, 0, 1, xoptions=xoptions) table.show_all() def _create_checks_menu(self): menu = gtk.Menu() for check_name, display_name in self.checks_names.iteritems(): menuitem = gtk.CheckMenuItem(label=display_name) menuitem.show() self._menuitem_checks[menuitem] = check_name menuitem.connect('toggled', self._on_check_menuitem_toggled) menu.append(menuitem) return menu def _update_button_label(self): check_labels = [mi.child.get_label() for mi in self.btn_popup.menu if mi.get_active()] btn_label = u'' if not check_labels: btn_label = _(u'No Checks') elif len(check_labels) == len(self.checks_names): btn_label = _(u'All Checks') else: btn_label = u', '.join(check_labels[:3]) if len(check_labels) > 3: btn_label += u'...' self.btn_popup.set_label(btn_label) # EVENT HANDLERS # def _on_checker_set(self, checkscontroller, checker): self.unselected() self.selected() def _on_check_menuitem_toggled(self, checkmenuitem): self.filter_checks = [] for menuitem in self.btn_popup.menu: if not isinstance(menuitem, gtk.CheckMenuItem) or not menuitem.get_active(): continue if menuitem in self._menuitem_checks: self.filter_checks.append(self._menuitem_checks[menuitem]) self.update_indices() self._update_button_label()
class WorkflowMode(BaseMode): """Workflow mode - Include units based on its workflow state, as specified by the user.""" name = 'Workflow' display_name = _("Workflow") widgets = [] # INITIALIZERS # def __init__(self, controller): """Constructor. @type controller: virtaal.controllers.ModeController @param controller: The ModeController that managing program modes.""" self.controller = controller self.filter_states = [] self._menuitem_states = {} # METHODS # def selected(self): self.storecursor = self.controller.main_controller.store_controller.cursor self.state_names = self.controller.main_controller.unit_controller.get_unit_state_names() self.state_names = self.state_names.items() self.state_names.sort(key=lambda x: x[0]) self._add_widgets() self._update_button_label() if not self.state_names: self._disable() self.update_indices() def unselected(self): pass def update_indices(self): if not self.storecursor or not self.storecursor.model: return indices = [] for state in self.filter_states: for index, unit in enumerate(self.storecursor.model): if state[0] <= unit.get_state_n() < state[1]: logging.debug('state: %s, unit.state_n: %d' % (state, unit.get_state_n())) indices.append(index) if not indices: indices = range(len(self.storecursor.model)) indices.sort() self.storecursor.indices = indices def _add_widgets(self): table = self.controller.view.mode_box self.btn_popup = PopupMenuButton(menu_pos=POS_NW_SW) self.btn_popup.set_relief(gtk.RELIEF_NORMAL) self.btn_popup.set_menu(self._create_state_menu()) self.widgets = [self.btn_popup] xoptions = gtk.FILL table.attach(self.btn_popup, 2, 3, 0, 1, xoptions=xoptions) table.show_all() def _create_state_menu(self): menu = gtk.Menu() for iid, name in self.state_names: menuitem = gtk.CheckMenuItem(label=name) menuitem.show() self._menuitem_states[menuitem] = iid menuitem.connect('toggled', self._on_state_menuitem_toggled) menu.append(menuitem) return menu def _update_button_label(self): state_labels = [mi.child.get_label() for mi in self.btn_popup.menu if mi.get_active()] btn_label = u'' if not state_labels: btn_label = _(u'No States') elif len(state_labels) == len(self.state_names): btn_label = _(u'All States') else: btn_label = u', '.join(state_labels[:3]) if len(state_labels) > 3: btn_label += u'...' self.btn_popup.set_label(btn_label) def _disable(self): """Disable the widgets (workflow not possible now).""" self.btn_popup.set_sensitive(False) # EVENT HANDLERS # def _on_state_menuitem_toggled(self, checkmenuitem): self.filter_states = [] unit_states = self.controller.main_controller.unit_controller.current_unit.STATE for menuitem in self.btn_popup.menu: if not isinstance(menuitem, gtk.CheckMenuItem) or not menuitem.get_active(): continue if menuitem in self._menuitem_states: self.filter_states.append(unit_states[self._menuitem_states[menuitem]]) self.update_indices() self._update_button_label()
class WorkflowMode(BaseMode): """Workflow mode - Include units based on its workflow state, as specified by the user.""" name = 'Workflow' display_name = _("Workflow") widgets = [] # INITIALIZERS # def __init__(self, controller): """Constructor. @type controller: virtaal.controllers.ModeController @param controller: The ModeController that managing program modes.""" self.controller = controller self.filter_states = [] self._menuitem_states = {} # METHODS # def selected(self): self.storecursor = self.controller.main_controller.store_controller.cursor self.state_names = self.controller.main_controller.unit_controller.get_unit_state_names( ) if self.storecursor and self.storecursor.model and 'extended' in self.storecursor.model.stats: self.state_names = [ i for i in self.state_names.items() if i[0] in self.storecursor.model.stats['extended'] ] else: self.state_names = self.state_names.items() self.state_names.sort(key=lambda x: x[0]) self._add_widgets() self._update_button_label() if not self.state_names: self._disable() self.update_indices() def unselected(self): pass def update_indices(self): if not self.storecursor or not self.storecursor.model: return indices = [] for state in self.filter_states: indices.extend(self.storecursor.model.stats['extended'][state]) if not indices: indices.extend(self.storecursor.model.stats['total']) else: indices.sort() self.storecursor.indices = indices def _add_widgets(self): table = self.controller.view.mode_box self.btn_popup = PopupMenuButton(menu_pos=POS_NW_SW) self.btn_popup.set_relief(gtk.RELIEF_NORMAL) self.btn_popup.set_menu(self._create_state_menu()) self.widgets = [self.btn_popup] xoptions = gtk.FILL table.attach(self.btn_popup, 2, 3, 0, 1, xoptions=xoptions) table.show_all() def _create_state_menu(self): menu = gtk.Menu() for iid, name in self.state_names: menuitem = gtk.CheckMenuItem(label=name) menuitem.show() self._menuitem_states[menuitem] = iid menuitem.connect('toggled', self._on_state_menuitem_toggled) menu.append(menuitem) return menu def _update_button_label(self): state_labels = [ mi.child.get_label() for mi in self.btn_popup.menu if mi.get_active() ] btn_label = u'' if not state_labels: #l10n: This is the button where the user can select units by workflow state btn_label = _(u'Select States') elif len(state_labels) == len(self.state_names): #l10n: This refers to workflow states btn_label = _(u'All States') else: btn_label = u', '.join(state_labels[:3]) if len(state_labels) > 3: btn_label += u'...' self.btn_popup.set_label(btn_label) def _disable(self): """Disable the widgets (workflow not possible now).""" self.btn_popup.set_sensitive(False) # EVENT HANDLERS # def _on_state_menuitem_toggled(self, checkmenuitem): self.filter_states = [] for menuitem in self.btn_popup.menu: if not isinstance(menuitem, gtk.CheckMenuItem) or not menuitem.get_active(): continue if menuitem in self._menuitem_states: self.filter_states.append(self._menuitem_states[menuitem]) self.update_indices() self._update_button_label()
class QualityCheckMode(BaseMode): """Include units based on quality checks that units fail.""" name = 'QualityCheck' display_name = _("Quality Checks") widgets = [] # INITIALIZERS # def __init__(self, controller): """Constructor. @type controller: virtaal.controllers.ModeController @param controller: The ModeController that managing program modes.""" self.controller = controller self.store_controller = controller.main_controller.store_controller self.main_controller = controller.main_controller self._checker_set_id = None self.filter_checks = [] # a way to map menuitems to their check names, and signal ids: self._menuitem_checks = {} self.store_filename = None # METHODS # def _prepare_stats(self): self.store_controller.update_store_checks(checker=self.main_controller.checks_controller.get_checker()) self.stats = self.store_controller.get_store_checks() # A currently selected check might disappear if the style changes: self.filter_checks = [check for check in self.filter_checks if check in self.stats] self.storecursor = self.store_controller.cursor self.checks_names = {} for check, indices in self.stats.iteritems(): if indices and check not in ('total', 'translated', 'untranslated', 'extended'): self.checks_names[check] = self.main_controller.checks_controller.get_check_name(check) def selected(self): self._prepare_stats() self._checker_set_id = self.main_controller.checks_controller.connect('checker-set', self._on_checker_set) # redo stats on save to refresh navigation controls self._store_saved_id = self.store_controller.connect('store-saved', self._on_checker_set) self._add_widgets() self._update_button_label() self.update_indices() def unselected(self): if self._checker_set_id: self.main_controller.checks_controller.disconnect(self._checker_set_id) self._checker_set_id = None self.store_controller.disconnect(self._store_saved_id) self.store_saved_id = None def update_indices(self): if not self.storecursor or not self.storecursor.model: return indices = [] for check in self.filter_checks: indices.extend(self.stats[check]) if not indices: indices = range(len(self.storecursor.model)) indices.sort() self.storecursor.indices = indices def _add_widgets(self): table = self.controller.view.mode_box self.btn_popup = PopupMenuButton(menu_pos=POS_NW_SW) self.btn_popup.set_relief(gtk.RELIEF_NORMAL) self.btn_popup.set_menu(self._create_checks_menu()) self.widgets = [self.btn_popup] xoptions = gtk.FILL table.attach(self.btn_popup, 2, 3, 0, 1, xoptions=xoptions) table.show_all() def _create_checks_menu(self): menu = gtk.Menu() self._create_menu_entries(menu) return menu def _create_menu_entries(self, menu): for mi, (name, signal_id) in self._menuitem_checks.iteritems(): mi.disconnect(signal_id) menu.remove(mi) assert not menu.get_children() self._menuitem_checks = {} for check_name, display_name in sorted(self.checks_names.iteritems(), key=lambda x: x[1], cmp=locale.strcoll): #l10n: %s is the name of the check and must be first. %d is the number of failures menuitem = gtk.CheckMenuItem(label="%s (%d)" % (display_name, len(self.stats[check_name]))) menuitem.set_active(check_name in self.filter_checks) menuitem.show() self._menuitem_checks[menuitem] = (check_name, menuitem.connect('toggled', self._on_check_menuitem_toggled)) menu.append(menuitem) def _update_button_label(self): check_labels = [mi.child.get_label() for mi in self.btn_popup.menu if mi.get_active()] btn_label = u'' if not check_labels: #l10n: This is the button where the user can select units by failing quality checks btn_label = _(u'Select Checks') elif len(check_labels) == len(self.checks_names): #l10n: This refers to quality checks btn_label = _(u'All Checks') else: btn_label = u', '.join(check_labels[:3]) if len(check_labels) > 3: btn_label += u'...' self.btn_popup.set_label(btn_label) # EVENT HANDLERS # def _on_checker_set(self, checkscontroller, checker=None): self._prepare_stats() self._create_menu_entries(self.btn_popup.menu) self._update_button_label() self.update_indices() def _on_check_menuitem_toggled(self, checkmenuitem): self.filter_checks = [] for menuitem in self.btn_popup.menu: if not isinstance(menuitem, gtk.CheckMenuItem) or not menuitem.get_active(): continue if menuitem in self._menuitem_checks: self.filter_checks.append(self._menuitem_checks[menuitem][0]) self.update_indices() self._update_button_label()
class QualityCheckMode(BaseMode): """Include units based on quality checks that units fail.""" name = 'QualityCheck' display_name = _("Quality Checks") widgets = [] # INITIALIZERS # def __init__(self, controller): """Constructor. @type controller: virtaal.controllers.ModeController @param controller: The ModeController that managing program modes.""" self.controller = controller self.store_controller = controller.main_controller.store_controller self.main_controller = controller.main_controller self._checker_set_id = None self.filter_checks = [] # a way to map menuitems to their check names, and signal ids: self._menuitem_checks = {} self.store_filename = None # METHODS # def _prepare_stats(self): self.store_controller.update_store_checks( checker=self.main_controller.checks_controller.get_checker()) self.stats = self.store_controller.get_store_checks() # A currently selected check might disappear if the style changes: self.filter_checks = [ check for check in self.filter_checks if check in self.stats ] self.storecursor = self.store_controller.cursor self.checks_names = {} for check, indices in self.stats.iteritems(): if indices and check not in ('total', 'translated', 'untranslated', 'extended'): self.checks_names[ check] = self.main_controller.checks_controller.get_check_name( check) def selected(self): self._prepare_stats() self._checker_set_id = self.main_controller.checks_controller.connect( 'checker-set', self._on_checker_set) # redo stats on save to refresh navigation controls self._store_saved_id = self.store_controller.connect( 'store-saved', self._on_checker_set) self._add_widgets() self._update_button_label() self.update_indices() def unselected(self): if self._checker_set_id: self.main_controller.checks_controller.disconnect( self._checker_set_id) self._checker_set_id = None self.store_controller.disconnect(self._store_saved_id) self.store_saved_id = None def update_indices(self): if not self.storecursor or not self.storecursor.model: return indices = [] for check in self.filter_checks: indices.extend(self.stats[check]) if not indices: indices = range(len(self.storecursor.model)) indices.sort() self.storecursor.indices = indices def _add_widgets(self): table = self.controller.view.mode_box self.btn_popup = PopupMenuButton(menu_pos=POS_NW_SW) self.btn_popup.set_relief(gtk.RELIEF_NORMAL) self.btn_popup.set_menu(self._create_checks_menu()) self.widgets = [self.btn_popup] xoptions = gtk.FILL table.attach(self.btn_popup, 2, 3, 0, 1, xoptions=xoptions) table.show_all() def _create_checks_menu(self): menu = gtk.Menu() self._create_menu_entries(menu) return menu def _create_menu_entries(self, menu): for mi, (name, signal_id) in self._menuitem_checks.iteritems(): mi.disconnect menu.remove(mi) assert not menu.get_children() for check_name, display_name in sorted(self.checks_names.iteritems(), key=lambda x: x[1], cmp=locale.strcoll): #l10n: %s is the name of the check and must be first. %d is the number of failures menuitem = gtk.CheckMenuItem( label="%s (%d)" % (display_name, len(self.stats[check_name]))) menuitem.set_active(check_name in self.filter_checks) menuitem.show() self._menuitem_checks[menuitem] = ( check_name, menuitem.connect('toggled', self._on_check_menuitem_toggled)) menu.append(menuitem) def _update_button_label(self): check_labels = [ mi.child.get_label() for mi in self.btn_popup.menu if mi.get_active() ] btn_label = u'' if not check_labels: #l10n: This is the button where the user can select units by failing quality checks btn_label = _(u'Select Checks') elif len(check_labels) == len(self.checks_names): #l10n: This refers to quality checks btn_label = _(u'All Checks') else: btn_label = u', '.join(check_labels[:3]) if len(check_labels) > 3: btn_label += u'...' self.btn_popup.set_label(btn_label) # EVENT HANDLERS # def _on_checker_set(self, checkscontroller, checker=None): self._prepare_stats() self._create_menu_entries(self.btn_popup.menu) self._update_button_label() self.update_indices() def _on_check_menuitem_toggled(self, checkmenuitem): self.filter_checks = [] for menuitem in self.btn_popup.menu: if not isinstance(menuitem, gtk.CheckMenuItem) or not menuitem.get_active(): continue if menuitem in self._menuitem_checks: self.filter_checks.append(self._menuitem_checks[menuitem][0]) self.update_indices() self._update_button_label()