From 00baf44422c10078b80f5dd0cc2506f9434b2c0e Mon Sep 17 00:00:00 2001 From: hanjinliu Date: Tue, 17 Aug 2021 12:47:42 +0900 Subject: [PATCH] highlighter updated, v1.20.10 --- impy/__init__.py | 2 +- impy/viewer/widgets/textedit.py | 50 ++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/impy/__init__.py b/impy/__init__.py index 0e80670d..5f2fde6c 100644 --- a/impy/__init__.py +++ b/impy/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.20.9" +__version__ = "1.20.10" import logging from ._const import Const, SetConst diff --git a/impy/viewer/widgets/textedit.py b/impy/viewer/widgets/textedit.py index dcad971e..0354fa71 100644 --- a/impy/viewer/widgets/textedit.py +++ b/impy/viewer/widgets/textedit.py @@ -27,13 +27,16 @@ def initText(self, text:str): def setText(self, text:str): self.txtviewer.setPlainText(text) + self.highlighter.setDocument(self.txtviewer.document()) def _add_txt_viewer(self, title): self.txtviewer = TxtViewer(self, title=title) self.txtviewer.setReadOnly(True) self.txtviewer.setFont(QFont("Consolas")) title is None or self.txtviewer.setDocumentTitle(title) - self.highlighter = StringHighlighter(self.txtviewer.document()) + self.highlighter = WordHighlighter(self.txtviewer.document()) + self.highlighter.appendRule(r" [-\+]?\d*(\.\d+)?", fcolor=Qt.yellow) + self.highlighter.appendRule(r"\".*?\"", fcolor=Qt.magenta) self.layout().addWidget(self.txtviewer) def _add_filter_line(self): @@ -49,17 +52,21 @@ def _add_filter_line(self): @self.line.editingFinished.connect def _(): text = self.line.text() - print("called:", text) if text in ("", ".", ".+", ".*"): + if self.highlighter.nrule > 2: + self.highlighter.popRule(-1) self.setText(self.original_text) else: lines = self.original_text.split("\n") if self.checkbox.isChecked(): - reg = re.compile(".*" + self.line.text() + ".*") + reg = re.compile(".*" + text + ".*") matched_lines = filter(lambda l: reg.match(l), lines) else: matched_lines = filter(lambda l: text in l, lines) - + + if self.highlighter.nrule > 2: + self.highlighter.popRule(-1) + self.highlighter.appendRule(text, fcolor=Qt.cyan, underline=True, weight=QFont.Bold) self.setText("\n".join(matched_lines)) # add check box @@ -79,24 +86,39 @@ def __init__(self, parent, title:str=None): self.setFont(QFont("Consolas")) self.createStandardContextMenu() title is None or self.setDocumentTitle(title) - self.highlighter = StringHighlighter(self.document()) -class StringHighlighter(QSyntaxHighlighter): - def highlightBlock(self, text:str): - exp = Regex((" \".*\"")) - exp.defineFormat(fcolor=Qt.magenta) +class WordHighlighter(QSyntaxHighlighter): + def __init__(self, doc) -> None: + super().__init__(doc) + self.exps:list[Regex] = [] + + def appendRule(self, regex:str, **kwargs): + exp = Regex(regex) + exp.defineFormat(**kwargs) + self.exps.append(exp) + + def popRule(self, i:int): + return self.exps.pop(i) + + @property + def nrule(self): + return len(self.exps) - it = exp.globalMatch(text) - while it.hasNext(): - match = it.next() - self.setFormat(match.capturedStart(), match.capturedLength(), exp.format_) + def highlightBlock(self, text:str): + for exp in self.exps: + it = exp.globalMatch(text) + while it.hasNext(): + match = it.next() + self.setFormat(match.capturedStart(), match.capturedLength(), exp.format_) class Regex(QRegularExpression): - def defineFormat(self, fcolor=None, bcolor=None, weight=None): + def defineFormat(self, fcolor=None, bcolor=None, weight=None, underline=False, strikeout=False): format_ = QTextCharFormat() fcolor is None or format_.setForeground(fcolor) bcolor is None or format_.setBackground(bcolor) weight is None or format_.setFontWeight(weight) + underline and format_.setFontUnderline(True) + strikeout and format_.setFontStrikeOut(True) self.format_ = format_ return None \ No newline at end of file