#include "highlighter.h" #include "regex-pcre.h" #include "highlighter.moc" #include #include RuleSetHighlighter::RuleSetHighlighter(QTextDocument * parent, QLineEdit *regexEditor) : QSyntaxHighlighter(parent) { _regexEditor = regexEditor; } void RuleSetHighlighter::highlightBlock(const QString & text) { QTextCharFormat defaultFormat; QTextCharFormat lightClassFormat; lightClassFormat.setBackground(Qt::lightGray); QTextCharFormat myClassFormat; myClassFormat.setFontWeight(QFont::Bold); myClassFormat.setBackground(Qt::green); //myClassFormat.setForeground(Qt::darkMagenta); //setFormat(0, text.length(), defaultFormat); //setFormat(0, 4, myClassFormat); QString regex = _regexEditor->text(); if (regex.length() == 0) return; //QRegExp expression(regex); PCRERegex expr(regex); if (! expr.isValid()) return; int rc = expr.match(text); if (expr.hasMatched()) { setFormat(0, text.length(), lightClassFormat); } if (rc > 1) { //printf("ovector for '%s': %d %d\n",(const char*)text.toAscii(),expr.ovector[0],expr.ovector[1]); for (int i=1; i<=rc-1; i++) { int index = expr.ovector[2*i]; int end = expr.ovector[(2*i)+1]; //printf("\t%d %d\n",index,end); if (index == -1 || end == -1) return; setFormat(index, end-index, myClassFormat); } } //int index = text.indexOf(expression); //while (index >= 0) { // int length = expression.matchedLength(); // setFormat(index, length, myClassFormat); // index = text.indexOf(expression, index + length); //} }