add dialog to manage highlighters

- no support for "no color"
- settings not saved to disk
This commit is contained in:
2021-10-30 16:25:34 +02:00
parent 43e85d2863
commit aee0ff9968
9 changed files with 325 additions and 62 deletions

View File

@@ -14,28 +14,48 @@ import re
class HighlightRegex(Highlight):
def __init__(self, regex: re.Pattern, brush: QBrush = QBrush(), pen: QPen = Qt.PenStyle.NoPen,
brush_full_line=QBrush()):
self.regex = regex
self.brush = brush
self.pen = pen
self.brush_full_line = brush_full_line
def __init__(self, query: str, ignore_case: bool, is_regex: bool, hit_background_color: str = "None",
line_background_color: str = "None"):
self.query = query
self.ignore_case = ignore_case
self.is_regex = is_regex
self.regex = self._get_regex()
self.hit_background_color = hit_background_color
self.line_background_color = line_background_color
def _get_regex(self):
flags = re.IGNORECASE if self.ignore_case else 0
if self.is_regex:
return re.compile(self.query, flags=flags)
else:
return re.compile(re.escape(self.query), flags=flags)
def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]:
result = []
#print("execute regex: %s in %s" % (self.regex, line.line()))
# print("execute regex: %s in %s" % (self.regex, line.line()))
match_iter = re.finditer(self.regex, line.line())
for match in match_iter:
#print("%s" % match)
# print("%s" % match)
start = match.start(0)
end = match.end(0)
result.append(HighlightedRange(
start,
end-start,
end - start,
highlight_full_line=True,
brush=self.brush,
pen=self.pen,
brush_full_line=self.brush_full_line
brush=self.brush(self.hit_background_color),
brush_full_line=self.brush(self.line_background_color)
))
return result
return result
def hit_background_brush(self):
return self.brush(self.hit_background_color)
@staticmethod
def brush(color: str) -> QBrush:
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))
return QBrush()