Skip to content

Commit

Permalink
Github Issue #134: Added color updates for highlighting the search re…
Browse files Browse the repository at this point in the history
…sults from the Stylesheet to Highlighter class for MkEdit
  • Loading branch information
AngryFender committed Feb 20, 2024
1 parent 38c8ac1 commit c732c6c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
23 changes: 17 additions & 6 deletions highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ void Highlighter::highlightBlock(const QString &text)
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength);
}

QRegularExpressionMatchIterator searchMatchIterator = searchMatchExpression.globalMatch(text);
while(searchMatchIterator.hasNext()){
QRegularExpressionMatch match = searchMatchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), searchMatchFormat);
}

}

void Highlighter::initColors()
Expand Down Expand Up @@ -105,13 +112,9 @@ void Highlighter::initColors()
commentStartExpression = QRegularExpression(QStringLiteral("/\\*"));
commentEndExpression = QRegularExpression(QStringLiteral("\\*/"));

headingFormat.setFontPointSize(1);
headingFormat.setForeground(Qt::white);

formatBlock.setFontPointSize(1);
formatBlock.setForeground(Qt::white);
regexCodeBlock.setPattern("^```+.*");

searchMatchFormat.setBackground(syntaxColor.searchMatch);
initialised = true;
}

Expand All @@ -122,7 +125,15 @@ void Highlighter::syntaxColorUpdateHandler(HighlightColor &colors)
syntaxColor.method = colors.method;
syntaxColor.comment = colors.comment;
syntaxColor.quote = colors.quote ;
syntaxColor.type = colors.type ;
syntaxColor.type = colors.type;
syntaxColor.searchMatch = colors.searchMatch;

initColors();
}

void Highlighter::updateSearchText(const QString &text)
{
searchMatchExpression.setPattern(text);
searchMatchExpression.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
rehighlight();
}
4 changes: 3 additions & 1 deletion highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Highlighter : public QSyntaxHighlighter

public slots:
void syntaxColorUpdateHandler(HighlightColor& colors);
void updateSearchText(const QString &text);
protected:
void highlightBlock(const QString &text) override;
private:
Expand All @@ -35,6 +36,7 @@ public slots:
QRegularExpression commentStartExpression;
QRegularExpression commentEndExpression;
QRegularExpression regexCodeBlock;
QRegularExpression searchMatchExpression;

QTextCharFormat tickMarkFormat;
QTextCharFormat unTickMarkFormat;
Expand All @@ -44,7 +46,7 @@ public slots:
QTextCharFormat multiLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
QTextCharFormat headingFormat;
QTextCharFormat searchMatchFormat;

QTextCharFormat formatBlock;
QTextCharFormat formatNormal;
Expand Down
10 changes: 10 additions & 0 deletions mkedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,11 @@ QColor MkEdit::getKeywordColor() const
return syntaxColor.keyword;
}

QColor MkEdit::getSearchMatchColor() const
{
return syntaxColor.searchMatch;
}

void MkEdit::setTypeColor(const QColor &color)
{
syntaxColor.type = color;
Expand All @@ -618,6 +623,11 @@ void MkEdit::setQuoteColor(const QColor &color)
syntaxColor.quote = color;
}

void MkEdit::setSearchMatchColor(const QColor &color)
{
syntaxColor.searchMatch = color;
}

void MkEdit::setKeywordColor(const QColor &color)
{
syntaxColor.keyword = color;
Expand Down
4 changes: 4 additions & 0 deletions mkedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MkEdit : public QTextEdit
Q_PROPERTY(QColor commentColor READ getCommentColor WRITE setCommentColor NOTIFY commentColorChanged)
Q_PROPERTY(QColor quoteColor READ getQuoteColor WRITE setQuoteColor NOTIFY quoteColorChanged)
Q_PROPERTY(QColor keywordColor READ getKeywordColor WRITE setKeywordColor NOTIFY keywordColorChanged)
Q_PROPERTY(QColor searchMatchColor READ getSearchMatchColor WRITE setSearchMatchColor NOTIFY searchMatchColorChanged)

public:
explicit MkEdit(QWidget *parent = nullptr);
Expand All @@ -43,12 +44,14 @@ class MkEdit : public QTextEdit
QColor getArgumentColor() const;
QColor getCommentColor() const;
QColor getQuoteColor() const;
QColor getSearchMatchColor() const;
QColor getKeywordColor() const;
void setTypeColor(const QColor& color);
void setMethodColor(const QColor& color);
void setArgumentColor(const QColor& color);
void setCommentColor(const QColor& color);
void setQuoteColor(const QColor& color);
void setSearchMatchColor(const QColor& color);
void setKeywordColor(const QColor& color);

void setDocument(QTextDocument *document);
Expand Down Expand Up @@ -137,6 +140,7 @@ private slots:
void argumentColorChanged(const QColor &color);
void commentColorChanged(const QColor &color);
void quoteColorChanged(const QColor &color);
void searchMatchColorChanged(const QColor &color);
void keywordColorChanged(const QColor &color);

void pushCheckBox(int position);
Expand Down
2 changes: 2 additions & 0 deletions theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct HighlightColor
QColor comment;
QColor quote;
QColor keyword;
QColor searchMatch;
};

const QString lightTheme = QStringLiteral( "QMainWindow {"
Expand Down Expand Up @@ -140,6 +141,7 @@ const QString darkTheme = QStringLiteral( "QMessageBox { background-color: #3535
" qproperty-argumentColor: #fdfd96;"
" qproperty-commentColor: #737373;"
" qproperty-quoteColor: #92DEE4;"
" qproperty-searchMatchColor: #2BB340;"
" qproperty-keywordColor: #FF9991;"
" background-color:#353535;"
" border-style: none;"
Expand Down

0 comments on commit c732c6c

Please sign in to comment.