def build_parameter_widget(self, name, value, description): hbox=gtk.HBox() label=gtk.Label(name) hbox.pack_start(label, expand=False) ra=self.registeredaction if ra: # Get predefined values if callable(ra.predefined): predefined=ra.predefined(self.controller)[name] elif ra.predefined is not None: predefined=ra.predefined[name] else: predefined=None entry=TALESEntry(controller=self.controller, predefined=predefined) entry.set_text(value) entry.set_editable(self.editable) entry.entry.connect('changed', self.on_change_parameter, entry, name) entry.entry.set_tooltip_text(description) hbox.entry=entry hbox.pack_start(entry.widget) hbox.show_all() return hbox
def build_parameter_widget(self, name, value, description): hbox = Gtk.HBox() label = Gtk.Label(label=name) hbox.pack_start(label, False, True, 0) ra = self.registeredaction if ra: # Get predefined values if callable(ra.predefined): predefined = ra.predefined(self.controller)[name] elif ra.predefined is not None: predefined = ra.predefined[name] else: predefined = None entry = TALESEntry(controller=self.controller, predefined=predefined) entry.set_text(value) entry.set_editable(self.editable) entry.entry.connect('changed', self.on_change_parameter, entry, name) entry.entry.set_tooltip_text(description) hbox.entry = entry hbox.pack_start(entry.widget, True, True, 0) hbox.show_all() return hbox
class EditCondition(EditGeneric): def __init__(self, condition, editable=True, controller=None, parent=None): self.model=condition self.current_operator=self.model.operator self.editable=editable self.controller=controller # We check if parent is a EditRuleSet or a EditQuery so that # we can populate the TALES expressions with appropriate values. self.parent=parent # Widgets: self.lhs=None # Entry self.rhs=None # Entry self.operator=None # Selector self.widget=self.build_widget() def invalid_items(self): iv=[] if not self.lhs.is_valid(): iv.append(_("Condition expression: %s") % unicode(self.lhs.get_text())) if (self.current_operator in Condition.binary_operators and not self.rhs.is_valid()): iv.append(_("Condition expression: %s") % unicode(self.lhs.get_text())) return iv def update_value(self): if not self.editable: return False c=self.model c.operator=self.current_operator c.lhs=unicode(self.lhs.get_text()) if c.operator in Condition.binary_operators: c.rhs=unicode(self.rhs.get_text()) return True def update_widget(self): """Update the widget. Invoked when the operator has changed. """ operator=self.current_operator if operator in Condition.binary_operators: self.lhs.show() self.rhs.show() elif operator in Condition.unary_operators: self.lhs.show() self.rhs.hide() else: raise Exception("Undefined operator: %s" % operator) return True def on_change_operator(self, operator): self.current_operator=operator self.update_widget() return True def build_widget(self): hbox=gtk.HBox() if self.parent is None or isinstance(self.parent, EditRule): predefined=[ ('annotation/type/id', _('The annotation type') ), ('annotation/content/data', _('The annotation content') ), ] + [ ('annotation/content/parsed/%s' % p, _("The value of the %s attribute") % p) for p in set(a for at in self.controller.package.annotationTypes for a in getattr(at, '_fieldnames', [])) ] + [ ('annotation/fragment', _('The annotation fragment') ), ('annotation/fragment/begin', _('The annotation begin time') ), ('annotation/fragment/end', _('The annotation end time') ), ('annotation/fragment/duration', _('The annotation duration') ), ('annotation/content/mimetype', _('The annotation MIME-type') ), ('annotation/incomingRelations', _("The annotation's incoming relations") ), ('annotation/outgoingRelations', _("The annotation's outgoing relations") ), ] + [ ('annotation/typedRelatedIn/%s' % rt.id, _("The %s-related incoming annotations") % self.controller.get_title(rt) ) for rt in self.controller.package.relationTypes ] + [ ('annotation/typedRelatedOut/%s' % rt.id, _("The %s-related outgoing annotations") % self.controller.get_title(rt) ) for rt in self.controller.package.relationTypes ] elif isinstance(self.parent, EditQuery): predefined=[ ('element', _('The element')), ('element/content/data', _("The element's content") ), ('element/fragment', _('The element fragment') ), ('element/fragment/begin', _('The element begin time') ), ('element/fragment/end', _('The element end time') ), ('element/fragment/duration', _('The element duration') ), ('element/type/id', _('The element type') ), ('element/incomingRelations', _("The element's incoming relations") ), ('element/outgoingRelations', _("The element's outgoing relations") ), ('here', _('The context')), ('here/fragment', _('The context fragment') ), ('here/annotations', _('The context annotations') ), ('here/type/annotations', _('The annotations of the context type') ), ] self.lhs=TALESEntry(controller=self.controller, predefined=predefined) self.lhs.set_text(self.model.lhs or "") self.lhs.set_editable(self.editable) self.lhs.show() self.lhs.set_no_show_all(True) if self.parent is None or isinstance(self.parent, EditRule): predef=[ ('string:%s' % at.id, _("annotation-type %s") % self.controller.get_title(at) ) for at in self.controller.package.annotationTypes ] + [ ('string:%s' % at.id, _("relation-type %s") % self.controller.get_title(at) ) for at in self.controller.package.relationTypes ] + predefined elif isinstance(self.parent, EditQuery): predef=predefined self.rhs=TALESEntry(controller=self.controller, predefined=predef) self.rhs.set_text(self.model.rhs or "") self.rhs.set_editable(self.editable) self.rhs.hide() self.rhs.set_no_show_all(True) operators={} operators.update(Condition.binary_operators) operators.update(Condition.unary_operators) def description_getter(element): if element in Condition.condition_categories: return Condition.condition_categories[element] else: return operators[element][0] self.selector=dialog.CategorizedSelector(title=_("Select a condition"), elements=operators.keys(), categories=Condition.condition_categories.keys(), current=self.current_operator, description_getter=description_getter, category_getter=lambda e: operators[e][1], callback=self.on_change_operator, editable=self.editable) self.operator=self.selector.get_button() hbox.add(self.lhs.widget) hbox.add(self.operator) hbox.add(self.rhs.widget) hbox.show() self.update_widget() return hbox
class EditQuery(EditGeneric): """Edit form for Query""" def __init__(self, query, controller=None, editable=True): self.model=query self.sourceentry=None self.valueentry=None self.controller=controller self.editconditionlist=[] if query.condition is not None: self.composition=query.condition.composition else: self.composition='and' self.editable=editable self.widget=self.build_widget() def invalid_items(self): iv=[] if not self.sourceentry.is_valid(): iv.append(_("Source expression")) if self.valueentry is not None and not self.valueentry.is_valid(): iv.append(_("Return expression")) for ec in self.editconditionlist: iv.extend(ec.invalid_items()) return iv def update_value(self): if not self.editable: return False self.model.sources=unicode(self.sourceentry.get_text()).split(';') if self.valueentry is None: v='element' else: v=unicode(self.valueentry.get_text()) if v == '' or v == 'element': self.model.rvalue=None else: self.model.rvalue=v for w in self.editconditionlist: w.update_value() if self.editconditionlist: self.model.condition=ConditionList([ e.model for e in self.editconditionlist ]) self.model.condition.composition=self.composition else: self.model.condition=None return True def remove_condition(self, widget, conditionwidget, hbox): if self.editable: self.editconditionlist.remove(conditionwidget) hbox.destroy() return True def add_condition(self, widget, conditionsbox): cond=Condition(lhs="element/content/data", operator="contains", rhs="string:???") self.add_condition_widget(cond, conditionsbox) return True def add_condition_widget(self, cond, conditionsbox): hb=gtk.HBox() conditionsbox.add(hb) w=EditCondition(cond, editable=self.editable, controller=self.controller, parent=self) self.editconditionlist.append(w) hb.add(w.get_widget()) w.get_widget().show() b=gtk.Button(stock=gtk.STOCK_REMOVE) b.set_sensitive(self.editable) b.connect('clicked', self.remove_condition, w, hb) b.show() hb.pack_start(b, expand=False) hb.show() return True def build_widget(self): frame=gtk.Frame() vbox=gtk.VBox() frame.add(vbox) vbox.show() # Event ef=gtk.Frame(_("For all elements in ")) predef=[ ('package/annotations', _("All annotations of the package")), ('package/views', _("All views of the package")), ('here/annotations', _("The context annotations")), ('here/type/annotations', _("The annotations of the context type"))] for at in self.controller.package.annotationTypes: predef.append( ('package/annotationTypes/%s/annotations' % at.id, _("Annotations of type %s") % self.controller.get_title(at) ) ) self.sourceentry=TALESEntry(context=self.model, controller=self.controller, predefined=predef) self.sourceentry.set_text(";".join(self.model.sources)) self.sourceentry.set_editable(self.editable) ef.add(self.sourceentry.widget) ef.show_all() vbox.pack_start(ef, expand=False) if config.data.preferences['expert-mode']: # Return value vf=gtk.Frame(_("Return ")) self.valueentry=TALESEntry(context=self.model, predefined=[ ('element', _("The element")), ('element/content/data', _("The element's content")) ], controller=self.controller) v=self.model.rvalue if v is None or v == '': v='element' self.valueentry.set_text(v) self.valueentry.set_editable(self.editable) vf.add(self.valueentry.widget) vf.show_all() vbox.pack_start(vf, expand=False) else: self.valueentry=None # Conditions if config.data.preferences['expert-mode']: cf=gtk.Frame(_("If the element matches ")) else: cf=gtk.Frame(_("Return the element if it matches ")) conditionsbox=gtk.VBox() cf.add(conditionsbox) # "Add condition" button hb=gtk.HBox() b=gtk.Button(stock=gtk.STOCK_ADD) b.connect('clicked', self.add_condition, conditionsbox) b.set_sensitive(self.editable) hb.pack_start(b, expand=False) hb.set_homogeneous(False) hb.add(gtk.HBox()) def change_composition(combo): self.composition=combo.get_current_element() return True c=dialog.list_selector_widget( [ ('and', _("All conditions must be met") ), ('or', _("Any condition can be met") ) ], preselect=self.composition, callback=change_composition) hb.pack_start(c, expand=False) conditionsbox.pack_start(hb, expand=False, fill=False) cf.show_all() if isinstance(self.model.condition, advene.rules.elements.ConditionList): for c in self.model.condition: self.add_condition_widget(c, conditionsbox) vbox.pack_start(cf, expand=False) frame.show() return frame
class EditCondition(EditGeneric): def __init__(self, condition, editable=True, controller=None, parent=None): self.model = condition self.current_operator = self.model.operator self.editable = editable self.controller = controller # We check if parent is a EditRuleSet or a EditQuery so that # we can populate the TALES expressions with appropriate values. self.parent = parent # Widgets: self.lhs = None # Entry self.rhs = None # Entry self.operator = None # Selector self.widget = self.build_widget() def invalid_items(self): iv = [] if not self.lhs.is_valid(): iv.append(_("Condition expression: %s") % self.lhs.get_text()) if (self.current_operator in Condition.binary_operators and not self.rhs.is_valid()): iv.append(_("Condition expression: %s") % self.lhs.get_text()) return iv def update_value(self): if not self.editable: return False c = self.model c.operator = self.current_operator c.lhs = self.lhs.get_text() if c.operator in Condition.binary_operators: c.rhs = self.rhs.get_text() return True def update_widget(self): """Update the widget. Invoked when the operator has changed. """ operator = self.current_operator if operator in Condition.binary_operators: self.lhs.show() self.rhs.show() elif operator in Condition.unary_operators: self.lhs.show() self.rhs.hide() else: raise Exception("Undefined operator: %s" % operator) return True def on_change_operator(self, operator): self.current_operator = operator self.update_widget() return True def build_widget(self): hbox = Gtk.HBox() if self.parent is None or isinstance(self.parent, EditRule): predefined = [ ('annotation/type/id', _('The annotation type')), ('annotation/content/data', _('The annotation content')), ] + [ ('annotation/content/parsed/%s' % p, _("The value of the %s attribute") % p) for p in set(a for at in self.controller.package.annotationTypes for a in getattr(at, '_fieldnames', [])) ] + [ ('annotation/fragment', _('The annotation fragment')), ('annotation/fragment/begin', _('The annotation begin time')), ('annotation/fragment/end', _('The annotation end time')), ('annotation/fragment/duration', _('The annotation duration')), ('annotation/content/mimetype', _('The annotation MIME-type')), ('annotation/incomingRelations', _("The annotation's incoming relations")), ('annotation/outgoingRelations', _("The annotation's outgoing relations")), ] + [('annotation/typedRelatedIn/%s' % rt.id, _("The %s-related incoming annotations") % self.controller.get_title(rt)) for rt in self.controller.package.relationTypes ] + [('annotation/typedRelatedOut/%s' % rt.id, _("The %s-related outgoing annotations") % self.controller.get_title(rt)) for rt in self.controller.package.relationTypes] elif isinstance(self.parent, EditQuery): predefined = [ ('element', _('The element')), ('element/content/data', _("The element's content")), ('element/fragment', _('The element fragment')), ('element/fragment/begin', _('The element begin time')), ('element/fragment/end', _('The element end time')), ('element/fragment/duration', _('The element duration')), ('element/type/id', _('The element type')), ('element/incomingRelations', _("The element's incoming relations")), ('element/outgoingRelations', _("The element's outgoing relations")), ('here', _('The context')), ('here/fragment', _('The context fragment')), ('here/annotations', _('The context annotations')), ('here/type/annotations', _('The annotations of the context type')), ] self.lhs = TALESEntry(controller=self.controller, predefined=predefined) self.lhs.set_text(self.model.lhs or "") self.lhs.set_editable(self.editable) self.lhs.show() self.lhs.set_no_show_all(True) if self.parent is None or isinstance(self.parent, EditRule): predef = [ ('string:%s' % at.id, _("annotation-type %s") % self.controller.get_title(at)) for at in self.controller.package.annotationTypes ] + [('string:%s' % at.id, _("relation-type %s") % self.controller.get_title(at)) for at in self.controller.package.relationTypes] + predefined elif isinstance(self.parent, EditQuery): predef = predefined self.rhs = TALESEntry(controller=self.controller, predefined=predef) self.rhs.set_text(self.model.rhs or "") self.rhs.set_editable(self.editable) self.rhs.hide() self.rhs.set_no_show_all(True) operators = {} operators.update(Condition.binary_operators) operators.update(Condition.unary_operators) def description_getter(element): if element in Condition.condition_categories: return Condition.condition_categories[element] else: return operators[element][0] self.selector = dialog.CategorizedSelector( title=_("Select a condition"), elements=list(operators), categories=list(Condition.condition_categories), current=self.current_operator, description_getter=description_getter, category_getter=lambda e: operators[e][1], callback=self.on_change_operator, editable=self.editable) self.operator = self.selector.get_button() hbox.add(self.lhs.widget) hbox.add(self.operator) hbox.add(self.rhs.widget) hbox.show() self.update_widget() return hbox
class EditQuery(EditGeneric): """Edit form for Query""" def __init__(self, query, controller=None, editable=True): self.model = query self.sourceentry = None self.valueentry = None self.controller = controller self.editconditionlist = [] if query.condition is not None: self.composition = query.condition.composition else: self.composition = 'and' self.editable = editable self.widget = self.build_widget() def invalid_items(self): iv = [] if not self.sourceentry.is_valid(): iv.append(_("Source expression")) if self.valueentry is not None and not self.valueentry.is_valid(): iv.append(_("Return expression")) for ec in self.editconditionlist: iv.extend(ec.invalid_items()) return iv def update_value(self): if not self.editable: return False self.model.sources = self.sourceentry.get_text().split(';') if self.valueentry is None: v = 'element' else: v = self.valueentry.get_text() if v == '' or v == 'element': self.model.rvalue = None else: self.model.rvalue = v for w in self.editconditionlist: w.update_value() if self.editconditionlist: self.model.condition = ConditionList( [e.model for e in self.editconditionlist]) self.model.condition.composition = self.composition else: self.model.condition = None return True def remove_condition(self, widget, conditionwidget, hbox): if self.editable: self.editconditionlist.remove(conditionwidget) hbox.destroy() return True def add_condition(self, widget, conditionsbox): cond = Condition(lhs="element/content/data", operator="contains", rhs="string:???") self.add_condition_widget(cond, conditionsbox) return True def add_condition_widget(self, cond, conditionsbox): hb = Gtk.HBox() conditionsbox.add(hb) w = EditCondition(cond, editable=self.editable, controller=self.controller, parent=self) self.editconditionlist.append(w) hb.add(w.get_widget()) w.get_widget().show() b = Gtk.Button(stock=Gtk.STOCK_REMOVE) b.set_sensitive(self.editable) b.connect('clicked', self.remove_condition, w, hb) b.show() hb.pack_start(b, False, True, 0) hb.show() return True def build_widget(self): frame = Gtk.Frame() vbox = Gtk.VBox() frame.add(vbox) vbox.show() # Event ef = Gtk.Frame.new(_("For all elements in ")) predef = [('package/annotations', _("All annotations of the package")), ('package/views', _("All views of the package")), ('here/annotations', _("The context annotations")), ('here/type/annotations', _("The annotations of the context type"))] for at in self.controller.package.annotationTypes: predef.append( ('package/annotationTypes/%s/annotations' % at.id, _("Annotations of type %s") % self.controller.get_title(at))) self.sourceentry = TALESEntry(context=self.model, controller=self.controller, predefined=predef) self.sourceentry.set_text(";".join(self.model.sources)) self.sourceentry.set_editable(self.editable) ef.add(self.sourceentry.widget) ef.show_all() vbox.pack_start(ef, False, True, 0) if config.data.preferences['expert-mode']: # Return value vf = Gtk.Frame.new(_("Return ")) self.valueentry = TALESEntry(context=self.model, predefined=[ ('element', _("The element")), ('element/content/data', _("The element's content")) ], controller=self.controller) v = self.model.rvalue if v is None or v == '': v = 'element' self.valueentry.set_text(v) self.valueentry.set_editable(self.editable) vf.add(self.valueentry.widget) vf.show_all() vbox.pack_start(vf, False, True, 0) else: self.valueentry = None # Conditions if config.data.preferences['expert-mode']: cf = Gtk.Frame.new(_("If the element matches ")) else: cf = Gtk.Frame.new(_("Return the element if it matches ")) conditionsbox = Gtk.VBox() cf.add(conditionsbox) # "Add condition" button hb = Gtk.HBox() b = Gtk.Button(stock=Gtk.STOCK_ADD) b.connect('clicked', self.add_condition, conditionsbox) b.set_sensitive(self.editable) hb.pack_start(b, False, True, 0) hb.set_homogeneous(False) hb.add(Gtk.HBox()) def change_composition(combo): self.composition = combo.get_current_element() return True c = dialog.list_selector_widget( [('and', _("All conditions must be met")), ('or', _("Any condition can be met"))], preselect=self.composition, callback=change_composition) hb.pack_start(c, False, True, 0) conditionsbox.pack_start(hb, False, False, 0) cf.show_all() if isinstance(self.model.condition, advene.rules.elements.ConditionList): for c in self.model.condition: self.add_condition_widget(c, conditionsbox) vbox.pack_start(cf, False, True, 0) frame.show() return frame