Example #1
0
 def _cursor_moved(self):
     tc = QTextCursor(self.Editor.textCursor())  # copy of cursor
     self.ColNumber.setText(str(tc.positionInBlock()))
     tb = tc.block()
     if tb == self.last_text_block:
         return  # still on same line, nothing more to do
     # Fill in line-number widget, line #s are origin-1
     self.LineNumber.setText(str(tb.blockNumber() + 1))
     # Fill in the image name and folio widgets
     pn = self.page_model.page_index(tc.position())
     if pn is not None:  # the page model has info on this position
         self.ImageFilename.setText(self.page_model.filename(pn))
         self.Folio.setText(self.page_model.folio_string(pn))
     else:  # no image data, or cursor is above page 1
         self.ImageFilename.setText('')
         self.Folio.setText('')
     # Change the current-line "extra selection" to the new current line.
     # Note: the cursor member of the extra selection may not have a
     # selection. If it does, the current line highlight disappears. The
     # cursor "tc" may or may not have a selection; to make sure, we clone
     # it and remove any selection from it.
     cltc = QTextCursor(tc)
     cltc.setPosition(tc.position(), QTextCursor.MoveAnchor)
     # Set the cloned cursor into the current line extra selection object.
     self.current_line_sel.cursor = cltc
     # Re-assign the list of extra selections to force update of display.
     self.Editor.setExtraSelections(self.extra_sel_list)
Example #2
0
 def _cursor_moved(self):
     tc = QTextCursor(self.Editor.textCursor()) # copy of cursor
     self.ColNumber.setText( str( tc.positionInBlock() ) )
     tb = tc.block()
     if tb == self.last_text_block :
         return # still on same line, nothing more to do
     # Fill in line-number widget, line #s are origin-1
     self.LineNumber.setText( str( tb.blockNumber()+1 ) )
     # Fill in the image name and folio widgets
     pn = self.page_model.page_index(tc.position())
     if pn is not None : # the page model has info on this position
         self.ImageFilename.setText(self.page_model.filename(pn))
         self.Folio.setText(self.page_model.folio_string(pn))
     else: # no image data, or cursor is above page 1
         self.ImageFilename.setText('')
         self.Folio.setText('')
     # Change the current-line "extra selection" to the new current line.
     # Note: the cursor member of the extra selection may not have a
     # selection. If it does, the current line highlight disappears. The
     # cursor "tc" may or may not have a selection; to make sure, we clone
     # it and remove any selection from it.
     cltc = QTextCursor(tc)
     cltc.setPosition(tc.position(),QTextCursor.MoveAnchor)
     # Set the cloned cursor into the current line extra selection object.
     self.current_line_sel.cursor = cltc
     # Re-assign the list of extra selections to force update of display.
     self.Editor.setExtraSelections(self.extra_sel_list)
Example #3
0
    def set_new_cursor_position(self, cursor: QTextCursor) -> None:
        """
        Set new cursor position in main status bar

        Args:
            cursor(QTextCursor): active cursor of code editor in active tab
        """
        self.statusbar_widget.set_line_and_column(
            cursor.block().blockNumber() + 1,
            cursor.positionInBlock() + 1)
Example #4
0
 def select_between(tc: QTextCursor,
                    start_char: str,
                    end_char: str,
                    select_inside: bool = True) -> None:
     pos = tc.positionInBlock()
     text = tc.block().text()
     start_pos = text.rfind(start_char, 0, pos)
     if start_pos == -1:
         return
     end_pos = text.find(end_char, pos)
     if end_pos == -1:
         return
     if select_inside:
         start_pos += len(start_char)
     else:
         end_pos += len(end_char)
         while end_pos < len(text) and text[end_pos].isspace():
             end_pos += 1
     tc.setPosition(tc.block().position() + start_pos)
     tc.setPosition(tc.block().position() + end_pos, QTC.KeepAnchor)
Example #5
0
 def _cursor_moved(self):
     tc = QTextCursor(self.Editor.textCursor()) # copy of cursor
     self.ColNumber.setText( str( tc.positionInBlock() ) )
     tb = tc.block()
     if tb == self.last_text_block :
         return # still on same line, nothing more to do
     # Fill in line-number widget, line #s are origin-1
     self.LineNumber.setText( str( tb.blockNumber()+1 ) )
     # Fill in the image name and folio widgets
     pn = self.page_model.page_index(tc.position())
     if pn is not None : # the page model has info on this position
         self.ImageFilename.setText(self.page_model.filename(pn))
         self.Folio.setText(self.page_model.folio_string(pn))
     else: # no image data, or cursor is above page 1
         self.ImageFilename.setText('')
         self.Folio.setText('')
     # Change the extra selection to the current line. The cursor needs
     # to have no selection. Yes, we are here because the cursor moved,
     # but that doesn't mean no-selection; it might have moved because
     # of a shift-click extending the selection.
     #xtc.clearSelection()
     self.current_line_sel.cursor = tc
     self.Editor.setExtraSelections(self.extra_sel_list)
Example #6
0
    def display_tooltips_for_cursor(self, cursor: QTextCursor,
                                    display_point: QPoint) -> None:
        """Displays a tooltip at current cursor position if a citation tag or an image tag is under cursor. Hides
        the tooltip if no tags are under cursor"""
        def is_inside_tag(tag, cursor_pos) -> bool:
            """Returns True if given cursor position is inside tag boundaries"""
            return tag[0] <= cursor_pos < (tag[1] + tag[0])

        show_citation_tooltips = self.SettingsManager.get_setting_value(
            "Editor/Show citation tooltips")
        show_image_tooltips = self.SettingsManager.get_setting_value(
            "Editor/Show image tooltips")
        hide_tooltip = True
        current_line_text = cursor.block().text()
        current_line_tags = self.highlighter.get_tags(current_line_text)
        cursor_pos = cursor.positionInBlock()

        for tag in current_line_tags:
            tag_text = current_line_text[tag[0]:tag[0] + tag[1]]

            if show_citation_tooltips and tag[
                    2] == "citation" and is_inside_tag(tag, cursor_pos):
                # Remove brackets and @ symbol from the tag text
                citation_identifier = components.extractors.citation_extractor(
                    tag_text)[0]

                # If the citekey is not in self.document_info["citations"] or doesn't have a citation text yet, show
                # placeholder
                if citation_identifier not in self.document_structure[
                        "citations"]:
                    QToolTip.showText(display_point,
                                      "Fetching citation info...", self,
                                      QRect(), 5000)
                    hide_tooltip = False

                elif citation_identifier in self.document_structure[
                        "citations"] and self.document_structure["citations"][
                            citation_identifier]["citation"] == "":
                    QToolTip.showText(display_point,
                                      "Fetching citation info...", self,
                                      QRect(), 5000)
                    hide_tooltip = False

                # If the citekey is in self.document_info["citations"] and citation show citation info
                else:
                    QToolTip.showText(
                        display_point, self.document_structure["citations"]
                        [citation_identifier]["citation"], self, QRect(), 5000)
                    hide_tooltip = False

            elif show_image_tooltips and tag[2] == "image" and is_inside_tag(
                    tag, cursor_pos):
                # Do nothing
                if not self.SettingsManager.get_setting_value(
                        "Editor/Show citation tooltips"):
                    return

                path = tag_text[tag_text.find("(") + 1:tag_text.find(")")]
                width = self.SettingsManager.get_setting_value(
                    "Editor/Image tooltip width")
                height = self.SettingsManager.get_setting_value(
                    "Editor/Image tooltip height")
                QToolTip.showText(
                    display_point,
                    f"<img src='{path}' width='{width}' height='{height}'>",
                    self, QRect(), 5000)
                hide_tooltip = False

        # Hide tooltip if no image or citation is under cursor
        if hide_tooltip:
            QToolTip.hideText()