1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *parent);
protected:
void highlightBlock(const QString &text);
private:
struct HighlightingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat multiLineCommentFormat;
};
#endif
Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
QTextCharFormat keywordFormat;
QTextCharFormat singleLineCommentFormat;
HighlightingRule rule;
QStringList keywordPatterns;
highlightingRules.clear();
keywordFormat.setFontWeight(QFont::Bold);
keywordFormat.setForeground(Qt::black);
rule.pattern = QRegExp("\\bTOTO[^\n]*\\b");
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
rule.format = keywordFormat;
highlightingRules.append(rule);
keywordFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\\bTATA[^\n]*\\b");
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
rule.format = keywordFormat;
highlightingRules.append(rule);
keywordFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp("\\bTITI[^\n]*\\b");
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
rule.format = keywordFormat;
highlightingRules.append(rule);
// commentaire avec ';' ou le mot 'END'
singleLineCommentFormat.setForeground(Qt::gray);
rule.pattern = QRegExp(";[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::gray);
commentStartExpression.setCaseSensitivity(Qt::CaseInsensitive);
commentStartExpression = QRegExp("\\bEND\\b");
commentEndExpression = QRegExp("\\bendding\\b");
}
//-----------------------------------------------------------------------------------------------------------------------------
void Highlighter::highlightBlock(const QString &text)
{
QString text2,text2U;
int index,length;
foreach (HighlightingRule rule, highlightingRules) {
QRegExp expression(rule.pattern);
index = text2.indexOf(expression);
while (index >=0) {
QCoreApplication::processEvents();
length = expression.matchedLength();
setFormat(index, length,rule.format);
}
}
setCurrentBlockState(0);
int startIndex = 0;
if (previousBlockState() != 1) startIndex = text2.indexOf(commentStartExpression);
while (startIndex >= 0) {
int endIndex = text2.indexOf(commentEndExpression, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text2.length() - startIndex;
}
else {
commentLength = endIndex - startIndex + commentEndExpression.matchedLength();
}
if (text2U.contains("END")) setFormat(startIndex+3, commentLength, multiLineCommentFormat);
else setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text2.indexOf(commentStartExpression,startIndex + commentLength);
}
} |
Partager