Bonsoir,
J'ai regardé comment utiliser la classe QSyntaxHighlighter de QT afin de colorier du texte.
Je gère sans aucune difficulté les commentaires sur une ligne et les mots-clés.
Je souhaite utiliser les Regexp (comme pour les commantaires sur une ligne et les mots-clés) pour les commentaires multilignes.
Le problème étant que cela fonctionne que si le commentaire est sur une ligne.
Voici le code de la classe implémentant la coloration :
highlight.cpp :
#include <QtGui>
#include <iostream>
#include "highlight.h"
using namespace std;
Highlighter::Highlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
keywordFormat.setForeground(QColor(0, 153, 255));
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";
foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegExp(pattern);
rule.format = keywordFormat;
highlightingRules.append(rule);
}
commentFormat.setForeground(QColor(255, 132, 0));
rule.pattern = QRegExp("//[^\n]*");
rule.format = commentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(QColor(204, 0, 0));
rule.pattern = QRegExp("/\\*.*\\*/");
rule.format = multiLineCommentFormat;
highlightingRules.append(rule);
}
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);
/*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);
}*/
}
highlight.h :
#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QHash>
#include <QTextCharFormat>
class QTextDocument;
class Highlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
Highlighter(QTextDocument *parent = 0);
protected:
void highlightBlock(const QString &text);
private:
struct HighlightingRule
{
QRegExp pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;
QRegExp commentStartExpression;
QRegExp commentEndExpression;
QTextCharFormat keywordFormat;
QTextCharFormat commentFormat;
QTextCharFormat multiLineCommentFormat;
};
#endif
L'exemple donné par QT est la partie commenté, ils crée deux QTextCharFormat un contenant en regexp le début du type de commentaire, et le second la fin.
Alors que moi je souhaite faire avec un seul est simple regexp.
Merci d'avance pour votre aide.