?? highlighter.cpp
字號:
#include <QtGui>
#include "highlighter.h"
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns;
QString keys="SELECT,FROM,WHERE,GROUP BY,ORDER BY,UNION,UNION ALL ALTER TABLE ADD DROP DELETE UPDATE COLUMN INSERT INTO";
keywordPatterns= keys.toUpper().split(",");
foreach (QString pattern, keywordPatterns) {
pattern=pattern.trimmed();
QString strPattern="\\b"+pattern.toUpper()+"\\b";
rule.pattern = QRegExp(strPattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
strPattern="\\b"+pattern.toLower()+"\\b";
rule.pattern = QRegExp(strPattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
//classFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta);
//rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
rule.pattern = QRegExp("\\b[0-9]+\\b");
rule.format = classFormat;
highlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::red);
rule.pattern = QRegExp("--[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::red);
quotationFormat.setForeground(Qt::magenta);
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
// highlightingRules.append(rule);
rule.pattern = QRegExp("\'.*\'");
rule.format = quotationFormat;
//highlightingRules.append(rule);
//functionFormat.setFontItalic(false);
functionFormat.setForeground(Qt::blue);
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
rule.format = functionFormat;
highlightingRules.append(rule);
commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");
}
void Highlighter::highlightBlock(const QString &text)
{
foreach (HighlightingRule rule, highlightingRules) {
QRegExp expression(rule.pattern);
int index = text.indexOf(expression);
while (index >= 0) {
int length = expression.matchedLength() ;
setFormat(index, length, rule.format);
index = text.indexOf(expression, index + length );
}
}
setCurrentBlockState(0);
int times=0;
int length=0;
int preIndex=0;
int index=text.indexOf("\"");
while (index>=0)
{
times++;
if(times%2==0)
{
length=index-preIndex;
setFormat(preIndex, length+1, quotationFormat);
}
preIndex=index;
index=text.indexOf("\"",index+1);
}
if(index==-1 && times%2)
setFormat(preIndex, text.length()-preIndex, quotationFormat);
times=0;
preIndex=0;
index=text.indexOf("\'");
while (index>=0)
{
times++;
if(times%2==0)
{
length=index-preIndex;
setFormat(preIndex, length+1, quotationFormat);
}
preIndex=index;
index=text.indexOf("\'",index+1);
}
if(index==-1 && times%2)
setFormat(preIndex, text.length()-preIndex, quotationFormat);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression);
while (startIndex >= 0) {
int endIndex = text.indexOf(commentEndExpression, startIndex);
int commentLength;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ commentEndExpression.matchedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression,
startIndex + commentLength);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -