highlight other matches of selected text
This commit is contained in:
@@ -10,6 +10,7 @@ from PySide6.QtGui import QMouseEvent
|
||||
from PySide6.QtWidgets import *
|
||||
|
||||
from src.ui.ScaledScrollBar import ScaledScrollBar
|
||||
from src.ui.bigtext.highlight_regex import HighlightRegex
|
||||
from src.ui.bigtext.highlight_selection import HighlightSelection
|
||||
from src.ui.bigtext.highlighted_range import HighlightedRange
|
||||
from src.ui.bigtext.highlightingdialog import HighlightingDialog
|
||||
@@ -132,6 +133,12 @@ class InnerBigText(QWidget):
|
||||
self._last_double_click_time = 0
|
||||
self._last_double_click_line_number = -1
|
||||
|
||||
self.highlight_selected_text = HighlightRegex(
|
||||
"",
|
||||
is_regex=False,
|
||||
ignore_case=True,
|
||||
hit_background_color="d7efffc0") # same blue as the selection hightlight, but with lower saturation
|
||||
|
||||
self.line_click_listeners: [Callable[[int], None]] = []
|
||||
|
||||
def keyPressEvent(self, e: QKeyEvent) -> None:
|
||||
@@ -208,6 +215,7 @@ class InnerBigText(QWidget):
|
||||
if e.buttons() == Qt.MouseButton.LeftButton and e.modifiers() == Qt.KeyboardModifier.ShiftModifier:
|
||||
offset = self.to_byte_offset(e)
|
||||
self.selection_highlight.set_end_byte(offset)
|
||||
self._update_highlight_selected_text()
|
||||
self.update()
|
||||
return
|
||||
|
||||
@@ -218,6 +226,7 @@ class InnerBigText(QWidget):
|
||||
line: Line = self.lines[line_number]
|
||||
self.selection_highlight.set_start(line.byte_offset())
|
||||
self.selection_highlight.set_end_byte(line.byte_end())
|
||||
self._update_highlight_selected_text()
|
||||
self.update()
|
||||
return
|
||||
|
||||
@@ -225,6 +234,7 @@ class InnerBigText(QWidget):
|
||||
offset = self.to_byte_offset(e)
|
||||
self.selection_highlight.set_start(offset)
|
||||
self.selection_highlight.set_end_byte(offset)
|
||||
self._update_highlight_selected_text()
|
||||
self.update()
|
||||
|
||||
line_number = self.y_pos_to_line(e.pos().y())
|
||||
@@ -246,6 +256,8 @@ class InnerBigText(QWidget):
|
||||
else:
|
||||
self.selection_highlight.set_start(offset)
|
||||
self.selection_highlight.set_end_byte(offset)
|
||||
|
||||
self._update_highlight_selected_text()
|
||||
self.update()
|
||||
|
||||
def mouseMoveEvent(self, e: QMouseEvent):
|
||||
@@ -257,8 +269,10 @@ class InnerBigText(QWidget):
|
||||
|
||||
if self.selection_highlight.end_byte != current_byte:
|
||||
self.selection_highlight.set_end_byte(current_byte)
|
||||
self._update_highlight_selected_text()
|
||||
self.update()
|
||||
# print("-> %s,%s" %(self._selection_start_byte, self._selection_end_byte))
|
||||
|
||||
line_number = self.y_pos_to_line(e.pos().y())
|
||||
column_in_line = self.x_pos_to_column(e.pos().x())
|
||||
if line_number < 0:
|
||||
@@ -372,8 +386,20 @@ class InnerBigText(QWidget):
|
||||
def _select_all(self):
|
||||
self.selection_highlight.start_byte = 0
|
||||
self.selection_highlight.end_byte = self.model.byte_count()
|
||||
self._update_highlight_selected_text()
|
||||
self.update()
|
||||
|
||||
def _update_highlight_selected_text(self):
|
||||
start_byte=self.selection_highlight.start_byte
|
||||
end_byte=self.selection_highlight.end_byte
|
||||
if abs(start_byte - end_byte) < 1024:
|
||||
query = self.model.read_range(start_byte, end_byte)
|
||||
if query.find("\n") < 0:
|
||||
self.highlight_selected_text.set_query(query)
|
||||
return
|
||||
|
||||
self.highlight_selected_text.set_query("")
|
||||
|
||||
def paintEvent(self, event: QPaintEvent) -> None:
|
||||
painter = QPainter(self)
|
||||
# font = "Courier New" if sys.platform == 'win32' or sys.platform == 'cygwin' else "Monospace"
|
||||
@@ -398,6 +424,7 @@ class InnerBigText(QWidget):
|
||||
highlighters = self.model.highlighters()
|
||||
if self.model.get_query_highlight():
|
||||
highlighters = highlighters + [self.model.get_query_highlight()]
|
||||
highlighters = highlighters + [self.highlight_selected_text]
|
||||
highlighters = highlighters + [self.selection_highlight] # selection highlight should be last
|
||||
|
||||
# draw highlights first - some characters may overlap to the next line
|
||||
|
||||
@@ -29,6 +29,10 @@ class HighlightRegex(Highlight):
|
||||
else:
|
||||
return re.compile(re.escape(self.query), flags=flags)
|
||||
|
||||
def set_query(self, query: str) -> None:
|
||||
self.query = query
|
||||
self.regex = self._get_regex()
|
||||
|
||||
def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]:
|
||||
result = []
|
||||
# print("execute regex: %s in %s" % (self.regex, line.line()))
|
||||
@@ -56,9 +60,15 @@ class HighlightRegex(Highlight):
|
||||
|
||||
@staticmethod
|
||||
def brush(color: str) -> QBrush:
|
||||
if re.match("[0-9a-f]{6}", color, flags=re.IGNORECASE):
|
||||
if re.match("^[0-9a-f]{6}$", color, flags=re.IGNORECASE):
|
||||
red = int(color[0:2], 16)
|
||||
green = int(color[2:4], 16)
|
||||
blue = int(color[4:6], 16)
|
||||
return QBrush(QColor(red, green, blue))
|
||||
if re.match("^[0-9a-f]{8}$", color, flags=re.IGNORECASE):
|
||||
red = int(color[0:2], 16)
|
||||
green = int(color[2:4], 16)
|
||||
blue = int(color[4:6], 16)
|
||||
alpha = int(color[6:8], 16)
|
||||
return QBrush(QColor(red, green, blue, alpha))
|
||||
return QBrush()
|
||||
|
||||
Reference in New Issue
Block a user