Example #1
0
    def search(self, text, flags):
        """Search for text in the current page.

        Args:
            text: The text to search for.
            flags: The QWebPage::FindFlags.
        """
        log.webview.debug("Searching with text '{}' and flags "
                          "0x{:04x}.".format(text, int(flags)))
        old_scroll_pos = self.scroll_pos
        flags = QWebPage.FindFlags(flags)
        found = self.findText(text, flags)
        if not found and not flags & QWebPage.HighlightAllOccurrences and text:
            message.error(self.win_id, "Text '{}' not found on "
                          "page!".format(text), immediately=True)
        else:
            backward = int(flags) & QWebPage.FindBackward

            def check_scroll_pos():
                """Check if the scroll position got smaller and show info."""
                if not backward and self.scroll_pos < old_scroll_pos:
                    message.info(self.win_id, "Search hit BOTTOM, continuing "
                                 "at TOP", immediately=True)
                elif backward and self.scroll_pos > old_scroll_pos:
                    message.info(self.win_id, "Search hit TOP, continuing at "
                                 "BOTTOM", immediately=True)
            # We first want QWebPage to refresh.
            QTimer.singleShot(0, check_scroll_pos)
Example #2
0
    def find(self, forwards=True):
        text = str(self.search_text.text()).strip()
        flags = QWebPage.FindFlags(0) if forwards else QWebPage.FindBackward
        d = self.dest_list
        if d.count() == 1:
            flags |= QWebPage.FindWrapsAroundDocument
        if not self.view.findText(text, flags) and text:
            if d.count() == 1:
                return error_dialog(self,
                                    _('No match found'),
                                    _('No match found for: %s') % text,
                                    show=True)

            delta = 1 if forwards else -1
            current = str(d.currentItem().data(Qt.DisplayRole) or '')
            next_index = (d.currentRow() + delta) % d.count()
            next = str(d.item(next_index).data(Qt.DisplayRole) or '')
            msg = '<p>' + _(
                'No matches for %(text)s found in the current file [%(current)s].'
                ' Do you want to search in the %(which)s file [%(next)s]?')
            msg = msg % dict(text=text,
                             current=current,
                             next=next,
                             which=_('next') if forwards else _('previous'))
            if question_dialog(self, _('No match found'), msg):
                self.pending_search = self.find_next if forwards else self.find_previous
                d.setCurrentRow(next_index)
Example #3
0
    def search(self,
               text,
               *,
               ignore_case=usertypes.IgnoreCase.never,
               reverse=False,
               wrap=True,
               result_cb=None):
        # Don't go to next entry on duplicate search
        if self.text == text and self.search_displayed:
            log.webview.debug("Ignoring duplicate search request"
                              " for {}".format(text))
            return

        # Clear old search results, this is done automatically on QtWebEngine.
        self.clear()

        self.text = text
        self.search_displayed = True
        self._flags = QWebPage.FindFlags(0)  # type: ignore
        if self._is_case_sensitive(ignore_case):
            self._flags |= QWebPage.FindCaseSensitively
        if reverse:
            self._flags |= QWebPage.FindBackward
        if wrap:
            self._flags |= QWebPage.FindWrapsAroundDocument
        # We actually search *twice* - once to highlight everything, then again
        # to get a mark so we can navigate.
        found = self._widget.findText(text, self._flags)
        self._widget.findText(text,
                              self._flags | QWebPage.HighlightAllOccurrences)
        self._call_cb(result_cb, found, text, self._flags, 'search')
Example #4
0
 def prev_result(self, *, result_cb=None):
     # The int() here makes sure we get a copy of the flags.
     flags = QWebPage.FindFlags(int(self._flags))
     if flags & QWebPage.FindBackward:
         flags &= ~QWebPage.FindBackward
     else:
         flags |= QWebPage.FindBackward
     found = self._widget.findText(self.text, flags)
     self._call_cb(result_cb, found)
Example #5
0
 def prev_result(self, *, result_cb=None):
     self.search_displayed = True
     # The int() here makes sure we get a copy of the flags.
     flags = QWebPage.FindFlags(int(self._flags))  # type: ignore
     if flags & QWebPage.FindBackward:
         flags &= ~QWebPage.FindBackward
     else:
         flags |= QWebPage.FindBackward
     found = self._widget.findText(self.text, flags)
     self._call_cb(result_cb, found, self.text, flags, 'prev_result')
Example #6
0
    def next_result(self, *, wrap=False, callback=None):
        self.search_displayed = True
        # The int() here makes sure we get a copy of the flags.
        flags = QWebPage.FindFlags(int(
            self._flags))  # type: ignore[call-overload]

        if wrap:
            flags |= QWebPage.FindWrapsAroundDocument

        found = self._widget.findText(self.text,
                                      flags)  # type: ignore[arg-type]
        self._call_cb(callback, found, self.text, flags, 'next_result')
Example #7
0
    def search(self, text, flags):
        """Search for text in the current page.

        Args:
            text: The text to search for.
            flags: The QWebPage::FindFlags.
        """
        log.webview.debug("Searching with text '{}' and flags "
                          "0x{:04x}.".format(text, int(flags)))
        old_scroll_pos = self.scroll_pos
        flags = QWebPage.FindFlags(flags)
        found = self.findText(text, flags)
        backward = flags & QWebPage.FindBackward

        if not found and not flags & QWebPage.HighlightAllOccurrences and text:
            # User disabled wrapping; but findText() just returns False. If we
            # have a selection, we know there's a match *somewhere* on the page
            if (not flags & QWebPage.FindWrapsAroundDocument
                    and self.hasSelection()):
                if not backward:
                    message.warning(self.win_id,
                                    "Search hit BOTTOM without "
                                    "match for: {}".format(text),
                                    immediately=True)
                else:
                    message.warning(self.win_id,
                                    "Search hit TOP without "
                                    "match for: {}".format(text),
                                    immediately=True)
            else:
                message.error(self.win_id,
                              "Text '{}' not found on "
                              "page!".format(text),
                              immediately=True)
        else:

            def check_scroll_pos():
                """Check if the scroll position got smaller and show info."""
                if not backward and self.scroll_pos < old_scroll_pos:
                    message.info(self.win_id, "Search hit BOTTOM, continuing "
                                 "at TOP",
                                 immediately=True)
                elif backward and self.scroll_pos > old_scroll_pos:
                    message.info(self.win_id, "Search hit TOP, continuing at "
                                 "BOTTOM",
                                 immediately=True)

            # We first want QWebPage to refresh.
            QTimer.singleShot(0, check_scroll_pos)
Example #8
0
 def __init__(self, parent=None):
     super().__init__(parent)
     self._flags = QWebPage.FindFlags(0)
Example #9
0
 def find(self, direction):
     text = unicode_type(self.search.text())
     self.view.findText(
         text, QWebPage.FindWrapsAroundDocument |
         (QWebPage.FindBackward
          if direction == 'prev' else QWebPage.FindFlags(0)))
Example #10
0
 def _empty_flags(self):
     return QWebPage.FindFlags(0)  # type: ignore[call-overload]
Example #11
0
 def __init__(self, tab, parent=None):
     super().__init__(tab, parent)
     self._flags = QWebPage.FindFlags(0)  # type: ignore