#ifndef SYNTAXHIGHLIGHTER_H #define SYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
QT_BEGIN_NAMESPACE
QT_END_NAMESPACE
{ Q_OBJECT
public:
protected:
void highlightBlock
(const QString &text
);
struct HighlightingRule
{
HighlightingRule() {}
};
QMap<QString,HighlightingRule> highlightingRules;
};
class MultiLineCommentHighlighter : public Highlighter
{ Q_OBJECT
public:
protected:
void highlightBlock
(const QString &text
);
};
class CppHighlighter : public MultiLineCommentHighlighter
{ Q_OBJECT
public:
};
#endif // SYNTAXHIGHLIGHTER_H
#ifndef SYNTAXHIGHLIGHTER_H #define SYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QTextCharFormat>
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
class Highlighter : public QSyntaxHighlighter
{ Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);
void SetRule(QString name,QString pattern,QTextCharFormat format);
protected:
void highlightBlock(const QString &text);
struct HighlightingRule
{
HighlightingRule() {}
HighlightingRule(QRegExp _pattern,QTextCharFormat _format) {pattern = _pattern;format = _format;}
QRegExp pattern;
QTextCharFormat format;
};
QMap<QString,HighlightingRule> highlightingRules;
};
class MultiLineCommentHighlighter : public Highlighter
{ Q_OBJECT
public:
MultiLineCommentHighlighter(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text);
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat multiLineCommentFormat;
};
class CppHighlighter : public MultiLineCommentHighlighter
{ Q_OBJECT
public:
CppHighlighter(QTextDocument *parent = 0);
};
#endif // SYNTAXHIGHLIGHTER_H
To copy to clipboard, switch view to plain text mode
SyntaxHighlighter.cpp:
#include "SyntaxHighlighter.h" #include <QtGui>
/****************************************************************************************
****************************************************************************************
**** Highlighter ***********************************************************************
****************************************************************************************
****************************************************************************************/
{
}
{
if (pattern != "")
highlightingRules.
insert(name,HighlightingRule
(QRegExp(pattern
),format
));
else
highlightingRules.remove(name);
rehighlight();
}
void Highlighter
::highlightBlock(const QString &text
) {
foreach (const HighlightingRule &rule, highlightingRules) {
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}
/****************************************************************************************
****************************************************************************************
**** MultiLineCommentHighlighter *******************************************************
****************************************************************************************
****************************************************************************************/
MultiLineCommentHighlighter
::MultiLineCommentHighlighter(QTextDocument *parent
) : Highlighter
(parent
) {
multiLineCommentFormat.setForeground(Qt::red);
commentStartExpression
= QRegExp("/\\*");
commentEndExpression
= QRegExp("\\*/");
}
void MultiLineCommentHighlighter
::highlightBlock(const QString &text
) {
Highlighter::highlightBlock(text);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}
/****************************************************************************************
****************************************************************************************
**** CppHighlighter ********************************************************************
****************************************************************************************
****************************************************************************************/
CppHighlighter
::CppHighlighter(QTextDocument *parent
) : MultiLineCommentHighlighter
(parent
) {
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.
setFontWeight(QFont::Bold);
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
<< "\\bvoid\\b" << "\\bvolatile\\b";
int i = 0;
foreach
(const QString &pattern, keywordPatterns
) { SetRule
(QString("00_KeyWord_%1").
arg(i
),pattern,keywordFormat
);
++i;
}
classFormat.
setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
SetRule("01_QtClasses","\\bQ[A-Za-z]+\\b",classFormat);
singleLineCommentFormat.setForeground(Qt::red);
SetRule("02_SingleLineComment","//[^\n]*",singleLineCommentFormat);
quotationFormat.setForeground(Qt::darkGreen);
SetRule("03_Quotation","\".*\"",quotationFormat);
functionFormat.setFontItalic(true);
functionFormat.setForeground(Qt::blue);
SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
}
#include "SyntaxHighlighter.h" #include <QtGui>
/****************************************************************************************
****************************************************************************************
**** Highlighter ***********************************************************************
****************************************************************************************
****************************************************************************************/
Highlighter::Highlighter(QTextDocument *parent) : QSyntaxHighlighter(parent)
{
}
void Highlighter::SetRule(QString name,QString pattern,QTextCharFormat format)
{
if (pattern != "")
highlightingRules.insert(name,HighlightingRule(QRegExp(pattern),format));
else
highlightingRules.remove(name);
rehighlight();
}
void Highlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}
setCurrentBlockState(0);
}
/****************************************************************************************
****************************************************************************************
**** MultiLineCommentHighlighter *******************************************************
****************************************************************************************
****************************************************************************************/
MultiLineCommentHighlighter::MultiLineCommentHighlighter(QTextDocument *parent) : Highlighter(parent)
{
multiLineCommentFormat.setForeground(Qt::red);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
}
void MultiLineCommentHighlighter::highlightBlock(const QString &text)
{
Highlighter::highlightBlock(text);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = commentStartExpression.indexIn(text);
while (startIndex >= 0) {
int endIndex = commentEndExpression.indexIn(text, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}
}
/****************************************************************************************
****************************************************************************************
**** CppHighlighter ********************************************************************
****************************************************************************************
****************************************************************************************/
CppHighlighter::CppHighlighter(QTextDocument *parent) : MultiLineCommentHighlighter(parent)
{
QTextCharFormat keywordFormat;
QTextCharFormat classFormat;
QTextCharFormat singleLineCommentFormat;
QTextCharFormat quotationFormat;
QTextCharFormat functionFormat;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
keywordPatterns << "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
<< "\\bvoid\\b" << "\\bvolatile\\b";
int i = 0;
foreach (const QString &pattern, keywordPatterns) {
SetRule(QString("00_KeyWord_%1").arg(i),pattern,keywordFormat);
++i;
}
classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
SetRule("01_QtClasses","\\bQ[A-Za-z]+\\b",classFormat);
singleLineCommentFormat.setForeground(Qt::red);
SetRule("02_SingleLineComment","//[^\n]*",singleLineCommentFormat);
quotationFormat.setForeground(Qt::darkGreen);
SetRule("03_Quotation","\".*\"",quotationFormat);
functionFormat.setFontItalic(true);
functionFormat.setForeground(Qt::blue);
SetRule("04_Functions","\\b[A-Za-z0-9_]+(?=\\()",functionFormat);
}
To copy to clipboard, switch view to plain text mode
HIH
Bookmarks