From de1c4f2b7cd6aabb99aa3197a5d99047ae69dc65 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Wed, 22 Mar 2023 23:36:12 -0700 Subject: [PATCH] diff: move the search logic into the TextSearchWidget Prepare to reuse this class in the DAG view by moving common functionality over into the TextSearchWidget. Signed-off-by: David Aguilar --- cola/widgets/diff.py | 40 +--------------------------------------- cola/widgets/text.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/cola/widgets/diff.py b/cola/widgets/diff.py index 30326e58..fc4fb4d6 100644 --- a/cola/widgets/diff.py +++ b/cola/widgets/diff.py @@ -481,7 +481,7 @@ class Viewer(QtWidgets.QFrame): self.text = DiffEditor(context, options, self) self.image = imageview.ImageView(parent=self) self.image.setFocusPolicy(Qt.NoFocus) - self.search_widget = TextSearchWidget(self) + self.search_widget = TextSearchWidget(self.text, self) self.search_widget.hide() stack = self.stack = QtWidgets.QStackedWidget(self) @@ -511,8 +511,6 @@ class Viewer(QtWidgets.QFrame): self.setFocusProxy(self.text) - self.search_widget.search_text.connect(self.search_text) - self.search_action = qtutils.add_action( self, N_('Search in Diff'), @@ -541,35 +539,6 @@ class Viewer(QtWidgets.QFrame): self.search_widget.show() self.search_widget.setFocus(True) - def search_text(self, text, backwards): - """Search the diff text for the given text""" - cursor = self.text.textCursor() - if cursor.hasSelection(): - selected_text = cursor.selectedText() - case_sensitive = self.search_widget.is_case_sensitive() - if text_matches(case_sensitive, selected_text, text): - if backwards: - position = cursor.selectionStart() - else: - position = cursor.selectionEnd() - else: - if backwards: - position = cursor.selectionEnd() - else: - position = cursor.selectionStart() - cursor.setPosition(position) - self.text.setTextCursor(cursor) - - flags = self.search_widget.find_flags(backwards) - if not self.text.find(text, flags): - if backwards: - location = QtGui.QTextCursor.End - else: - location = QtGui.QTextCursor.Start - cursor.movePosition(location, QtGui.QTextCursor.MoveAnchor) - self.text.setTextCursor(cursor) - self.text.find(text, flags) - def export_state(self, state): state['show_diff_line_numbers'] = self.options.show_line_numbers.isChecked() state['image_diff_mode'] = self.options.image_mode.currentIndex() @@ -732,13 +701,6 @@ def create_image(width, height): return image -def text_matches(case_sensitive, a, b): - """Compare text with case sensitivity taken into account""" - if case_sensitive: - return a == b - return a.lower() == b.lower() - - def create_painter(image): painter = QtGui.QPainter(image) painter.fillRect(image.rect(), Qt.transparent) diff --git a/cola/widgets/text.py b/cola/widgets/text.py index 1d148983..3aa4703e 100644 --- a/cola/widgets/text.py +++ b/cola/widgets/text.py @@ -379,11 +379,10 @@ class PlainTextEdit(QtWidgets.QPlainTextEdit): class TextSearchWidget(QtWidgets.QWidget): """The search dialog that displays over a text edit field""" - search_text = Signal(object, bool) - - def __init__(self, parent): + def __init__(self, widget, parent): super(TextSearchWidget, self).__init__(parent) self.setAutoFillBackground(True) + self._widget = widget self._parent = parent self.text = HintedDefaultLineEdit(N_('Find in diff'), parent=self) @@ -429,11 +428,11 @@ class TextSearchWidget(QtWidgets.QWidget): def search(self): """Emit a signal with the current search text""" - self.search_text.emit(self.text.get(), False) + self.search_text(backwards=False) def search_backwards(self): """Emit a signal with the current search text for a backwards search""" - self.search_text.emit(self.text.get(), True) + self.search_text(backwards=True) def hide_search(self): """Hide the search window""" @@ -455,6 +454,43 @@ class TextSearchWidget(QtWidgets.QWidget): """Are we searching using a case-insensitive search?""" return self.match_case_checkbox.isChecked() + def search_text(self, backwards=False): + """Search the diff text for the given text""" + text = self.text.get() + cursor = self._widget.textCursor() + if cursor.hasSelection(): + selected_text = cursor.selectedText() + case_sensitive = self.is_case_sensitive() + if text_matches(case_sensitive, selected_text, text): + if backwards: + position = cursor.selectionStart() + else: + position = cursor.selectionEnd() + else: + if backwards: + position = cursor.selectionEnd() + else: + position = cursor.selectionStart() + cursor.setPosition(position) + self._widget.setTextCursor(cursor) + + flags = self.find_flags(backwards) + if not self._widget.find(text, flags): + if backwards: + location = QtGui.QTextCursor.End + else: + location = QtGui.QTextCursor.Start + cursor.movePosition(location, QtGui.QTextCursor.MoveAnchor) + self._widget.setTextCursor(cursor) + self._widget.find(text, flags) + + +def text_matches(case_sensitive, a, b): + """Compare text with case sensitivity taken into account""" + if case_sensitive: + return a == b + return a.lower() == b.lower() + class TextEditExtension(BaseTextEditExtension): def init(self): -- 2.11.4.GIT