Beispiel #1
0
    def compose_html_for_editing(self):
        """ This is used to build the html when quickediting a label. It should reduce the label
        into just one field value that is allowed to be edited, in constituentnode this is
        either label or synobj's label. This can be overridden in syntactic object by having
        'compose_html_for_editing' -method there. The method returns a tuple,
          (field_name, setter, html).
        :return:
        """

        # Allow custom syntactic objects to override this
        if self.syntactic_object and hasattr(self.syntactic_object,
                                             'compose_html_for_editing'):
            return self.syntactic_object.compose_html_for_editing(self)
        label_text_mode = ctrl.settings.get('label_text_mode')
        if label_text_mode == g.NODE_LABELS or label_text_mode == g.NODE_LABELS_FOR_LEAVES:
            if self.label:
                if self.triangle_stack:
                    lower_part = extract_triangle(self.label)
                    return 'node label', as_html(self.label, omit_triangle=True) + \
                           '<br/>' + as_html(lower_part or '')
                else:
                    return 'node label', as_html(self.label)
            elif self.syntactic_object:
                return 'syntactic label', as_html(self.syntactic_object.label)
            else:
                return '', '', ''
        elif label_text_mode == g.SYN_LABELS or label_text_mode == g.SYN_LABELS_FOR_LEAVES:
            if self.syntactic_object:
                return 'syntactic label', as_html(self.syntactic_object.label)
            else:
                return '', ''
Beispiel #2
0
 def update_fields(self):
     """ Update field values on embed form based on template """
     for field_name, field in self.fields.items():
         d = self.editable.get(field_name, {})
         if 'getter' in d:
             value = getattr(self.host, d['getter'])()
         else:
             value = getattr(self.host, field_name, '')
         if 'enabler' in d:
             enabled = getattr(self.host, d['enabler'])()
             field.setEnabled(bool(enabled))
         itype = d.get('input_type', 'text')
         if itype == 'expandingtext':
             value = as_html(value)
             field.setText(value)
         elif itype == 'text' or itype == 'textarea':
             value = as_html(value)
             field.set_original(value)
             field.setText(value)
         elif itype == 'checkbox':
             field.setChecked(bool(value))
         elif itype == 'spinbox':
             if not isinstance(value, int):
                 if not value:
                     value = 0
                 else:
                     try:
                         value = int(value)
                     except TypeError:
                         value = int(d.get('prefill', 0))
             field.setValue(value)
Beispiel #3
0
 def __str__(self):
     label = as_html(self.label)
     syn_label = as_html(self.get_syn_label())
     if label and syn_label:
         return f'{label} ({syn_label})'
     else:
         return label or syn_label or "no label"
    def load_values_from_parsernode(self, parsernode):
        """ Update constituentnode with values from parsernode
        :param parsernode:
        :return:
        """

        def remove_dot_label(inode, row_n):
            for i, part in enumerate(list(inode.parts)):
                if isinstance(part, str):
                    if part.startswith('.'):
                        inode.parts[i] = part[1:]
                    return True
                else:
                    return remove_dot_label(part, row_n)

        extract_triangle(self.label, remove_from_original=True)

        if parsernode.index:
            self.index = parsernode.index
        rows = parsernode.label_rows

        # Remove dotlabel
        for i, row in enumerate(list(rows)):
            if isinstance(row, str):
                if row.startswith('.'):
                    rows[i] = row[1:]
                break
            stop = remove_dot_label(row, i)
            if stop:
                break
        self.label = join_lines(rows)
        if self.index:
            base = as_html(self.label)
            if base.strip().startswith('t<sub>'):
                self.is_trace = True
Beispiel #5
0
    def compose_html_for_viewing(self, peek_into_synobj=True):
        """ This method builds the html to display in label. For convenience, syntactic objects
        can override this (going against the containment logic) by having their own
        'compose_html_for_viewing' -method. This is so that it is easier to create custom
        implementations for constituents without requiring custom constituentnodes.

        Note that synobj's compose_html_for_viewing receives the node object as parameter,
        so you can replicate the behavior below and add your own to it.

        :param peek_into_synobj: allow syntactic object to override this method. If synobj in turn
        needs the result from this implementation (e.g. to append something to it), you have to
        turn this off to avoid infinite loop. See example plugins.
        :return:
        """

        # Allow custom syntactic objects to override this
        if peek_into_synobj and hasattr(self.syntactic_object, 'compose_html_for_viewing'):
            return self.syntactic_object.compose_html_for_viewing(self)

        html = []
        lower_html = []
        syntactic_mode = ctrl.settings.get('syntactic_mode')
        node_labels = ctrl.settings.get('show_node_labels')
        inner_labels = ctrl.settings.get('inner_labels')

        leaf = self.is_leaf(only_similar=True, only_visible=False)
        if self.triangle and not leaf:
            lower_html.append(self.get_triangle_text())
        if inner_labels == 2 and self.syntactic_object:  # use secondary labels
            html.append(as_html(self.syntactic_object.get_secondary_label()))
        elif node_labels and self.label:
            html.append(as_html(self.label))
        else:
            if self.syntactic_object:
                html.append(str(self.syntactic_object.label))
            if self.index and not syntactic_mode:
                html.append('<sub>%s</sub>' % self.index)

        if self.gloss and self.should_show_gloss_in_label():
            if html:
                html.append('<br/>')
            html.append(as_html(self.gloss))
        if html and html[-1] == '<br/>':
            html.pop()
        return ''.join(html), ''.join(lower_html)
Beispiel #6
0
    def compose_html_for_editing(self):
        """ This is used to build the html when quickediting a label. It should reduce the label
        into just one field value that is allowed to be edited, in constituentnode this is
        either label or synobj's label. This can be overridden in syntactic object by having
        'compose_html_for_editing' -method there. The method returns a tuple,
          (field_name, html).
        :return:
        """

        # Allow custom syntactic objects to override this
        if hasattr(self.syntactic_object, 'compose_html_for_editing'):
            return self.syntactic_object.compose_html_for_editing(self)

        node_labels = ctrl.settings.get('show_node_labels')
        if node_labels and self.label:
            return 'label', as_html(self.label)
        elif self.syntactic_object:
            return 'synlabel', as_html(self.syntactic_object.label)
        else:
            return 'label', ''
Beispiel #7
0
 def update_preview(self):
     """ Update preview label in NodeEditEmbed with composite from label, synobj's label and
     index.
     :param edited: the field that triggered the update.
     :return:
     """
     embed = self.sender().parent()
     surefail = 0
     while not hasattr(embed, 'fields') and surefail < 5:
         embed = embed.parent()
         surefail += 1
     index = embed.fields.get('index', None)
     label = embed.fields.get('label', None)
     preview = embed.fields.get('preview', None)
     index_text = as_html(index.text())
     label_text = as_html(label.inode_text())
     parsed = label_text
     if index_text:
         parsed += '<sub>' + index_text + '</sub>'
     preview.setText(parsed)
 def label_as_editable_html(self):
     """ This is used to build the html when quickediting a label. It should reduce the label
     into just one field value that is allowed to be edited, in constituentnode this is
     either label or synobj's label. This can be overridden in syntactic object by having
     'label_as_editable_html' -method there. The method returns a tuple,
       (field_name, setter, html).
     :return:
     """
     label_text_mode = self.label_text_mode
     if (label_text_mode == g.NODE_LABELS or label_text_mode == g.NODE_LABELS_FOR_LEAVES) and self.label:
         return 'node label', as_html(self.label)
     return 'node label', ''
Beispiel #9
0
    def label_as_editable_html(self):
        """ This is used to build the html when quickediting a label. It should reduce the label
        into just one field value that is allowed to be edited, in constituentnode this is
        either label or synobj's label. This can be overridden in syntactic object by having
        'label_as_editable_html' -method there. The method returns a tuple,
          (field_name, setter, html).
        :return:
        """

        # Allow custom syntactic objects to override this
        if hasattr(self.syntactic_object, 'label_as_editable_html'):
            return self.syntactic_object.label_as_editable_html(self)

        return 'label', as_html(self.label)
Beispiel #10
0
 def update_fields(self):
     """ Update field values on embed form based on template """
     ed = self.host.get_editing_template()
     for field_name, field in self.fields.items():
         d = ed.get(field_name, {})
         if 'getter' in d:
             value = getattr(self.host, d['getter'])()
         else:
             value = getattr(self.host, field_name, '')
         if 'enabler' in d:
             enabled = getattr(self.host, d['enabler'])()
             field.setEnabled(bool(enabled))
         itype = d.get('input_type', 'text')
         if itype == 'expandingtext':
             value = as_html(value)
             field.setText(value)
         elif itype == 'text' or itype == 'textarea':
             value = as_html(value)
             field.set_original(value)
             field.setText(value)
         elif itype == 'checkbox':
             field.setChecked(bool(value))
         elif itype == 'multibutton':
             op_func = d.get('option_function')
             op_func = getattr(self.host, op_func, None) or getattr(
                 self.syntactic_object, op_func, None)
             field.update_selections(op_func())
         elif itype == 'spinbox':
             if not isinstance(value, int):
                 if not value:
                     value = 0
                 else:
                     try:
                         value = int(value)
                     except TypeError:
                         value = int(d.get('prefill', 0))
             field.setValue(value)
Beispiel #11
0
 def update_fields(self):
     """ Update field values on embed form based on template """
     ed = self.host.get_editing_template()
     for field_name, field in self.fields.items():
         d = ed.get(field_name, {})
         if 'getter' in d:
             value = getattr(self.host, d['getter'])()
         else:
             value = getattr(self.host, field_name, '')
         itype = d.get('input_type', 'text')
         if itype == 'expandingtext':
             value = as_html(value)
             field.setText(value)
         elif itype == 'text' or itype == 'textarea':
             value = as_html(value)
             field.set_original(value)
             field.setText(value)
         elif itype == 'checkbox':
             field.setChecked(bool(value))
         elif itype == 'multibutton':
             op_func = d.get('option_function')
             op_func = getattr(self.host, op_func, None) or getattr(self.syntactic_object,
                                                                    op_func, None)
             field.update_selections(op_func())
Beispiel #12
0
    def label_as_html(self):
        """ This method builds the html to display in label. For convenience, syntactic objects
        can override this (going against the containment logic) by having their own
        'label_as_html' -method. This is so that it is easier to create custom
        implementations for constituents without requiring custom constituentnodes.

        Note that synobj's label_as_html receives the node object as parameter,
        so you can replicate the behavior below and add your own to it.
        :return:
        """

        # Allow custom syntactic objects to override this
        if hasattr(self.syntactic_object, 'label_as_html'):
            return self.syntactic_object.label_as_html(self)

        return as_html(self.label)
Beispiel #13
0
    def load_values_from_parsernode(self, parsernode):
        """ Update constituentnode with values from parsernode
        :param parsernode:
        :return:
        """
        def remove_dot_label(inode, row_n):
            for i, part in enumerate(list(inode.parts)):
                if isinstance(part, str):
                    if part.startswith('.'):
                        inode.parts[i] = part[1:]
                    return True
                elif isinstance(part,
                                ICommandNode) and part.command == 'qroof':
                    self.triangle_stack = [self]
                    continue
                else:
                    return remove_dot_label(part, row_n)

        if parsernode.index:
            self.index = parsernode.index
        rows = parsernode.label_rows
        # Remove dotlabel

        for i, row in enumerate(list(rows)):
            if isinstance(row, str):
                if row.startswith('.'):
                    rows[i] = row[1:]
                break
            stop = remove_dot_label(row, i)
            if stop:
                break
        # △

        self.label = join_lines(rows)
        # now as rows are in one INode / string, we can extract the triangle part and put it to
        # end. It is different to qtree's way of handling triangles, but much simpler for us in
        # long run.
        triangle_part = extract_triangle(self.label, remove_from_original=True)
        if triangle_part:
            assert isinstance(self.label, ITextNode)

            self.label.parts.append('\n')
            self.label.parts.append(triangle_part)
        if self.index:
            base = as_html(self.label)
            if base.strip().startswith('t<sub>'):
                self.is_trace = True
Beispiel #14
0
    def compose_html_for_viewing(self, peek_into_synobj=True):
        """ This method builds the html to display in label. For convenience, syntactic objects
        can override this (going against the containment logic) by having their own
        'compose_html_for_viewing' -method. This is so that it is easier to create custom
        implementations for constituents without requiring custom constituentnodes.

        Note that synobj's compose_html_for_viewing receives the node object as parameter,
        so you can replicate the behavior below and add your own to it.

        :param peek_into_synobj: allow syntactic object to override this method. If synobj in turn
        needs the result from this implementation (e.g. to append something to it), you have to
        turn this off to avoid infinite loop. See example plugins.
        :return:
        """

        # Allow custom syntactic objects to override this
        if peek_into_synobj and hasattr(self.syntactic_object,
                                        'compose_html_for_viewing'):
            return self.syntactic_object.compose_html_for_viewing(self)

        html = []

        label_text_mode = ctrl.settings.get('label_text_mode')
        l = ''
        if label_text_mode == g.NODE_LABELS:
            if self.label:
                l = self.label
            elif self.syntactic_object:
                l = self.syntactic_object.label
        elif label_text_mode == g.NODE_LABELS_FOR_LEAVES:
            if self.label:
                l = self.label
            elif self.syntactic_object and self.is_leaf(only_similar=True,
                                                        only_visible=False):
                l = self.syntactic_object.label
        elif label_text_mode == g.SYN_LABELS:
            if self.syntactic_object:
                l = self.syntactic_object.label
        elif label_text_mode == g.SYN_LABELS_FOR_LEAVES:
            if self.syntactic_object and self.is_leaf(only_similar=True,
                                                      only_visible=False):
                l = self.syntactic_object.label
        elif label_text_mode == g.SECONDARY_LABELS:
            if self.syntactic_object:
                l = self.syntactic_object.get_secondary_label()
        elif label_text_mode == g.XBAR_LABELS:
            l = self.get_autolabel()
        separate_triangle = bool(self.is_cosmetic_triangle()
                                 and self.triangle_stack[-1] is self)
        l_html = as_html(l,
                         omit_triangle=separate_triangle,
                         include_index=self.index)
        if l_html:
            html.append(l_html)

        if self.gloss and self.should_show_gloss_in_label():
            if html:
                html.append('<br/>')
            html.append(as_html(self.gloss))
        if html and html[-1] == '<br/>':
            html.pop()

        # Lower part
        lower_html = ''
        if separate_triangle:
            qroof_content = extract_triangle(l)
            if qroof_content:
                lower_html = as_html(qroof_content)
        return ''.join(html), lower_html
Beispiel #15
0
 def label_as_html(self) -> str:
     """ Label, reliably a string
     :return:
     """
     return as_html(self.label)