#include <qapplication.h>
#include <qsyntaxhighlighter.h>
#include <qtextedit.h>
#include <iostream>
#define NORMAL_STATE 0
#define COMMENT_STATE 1
#define DOXY_STATE 2
public:
keyFont.setBold(true);
keyFont.setItalic(true);
keyFont.setUnderline(true);
keyColor = Qt::blue;
commentColor = Qt::red;
commentDoxyColor = Qt::green;
commentFont.setItalic(true);
}
~SyntaxHighlighter() {
}
void addKeyword
(const QString &str
) { keywordList << str;
}
int highlightParagraph
(const QString & text,
int endStateOfLastPara
) { int index = 0;
int count = 0;
std::cout << "prev par state: " << endStateOfLastPara << std::endl;
std::cout << "text: " << text << std::endl;
// Assume the text to not contain keywords
setFormat(0, text.length(), textEdit()->font(), Qt::black);
int currentState = endStateOfLastPara;
if (endStateOfLastPara == NORMAL_STATE || endStateOfLastPara < 0) {
// keep it simple and assume that comment delimiters appear on a single line
if (text.contains("/**")) {
setFormat(0, text.length(), commentFont, commentDoxyColor);
currentState = DOXY_STATE;
} else if (text.contains("/*")) {
setFormat(0, text.length(), commentFont, commentColor);
currentState = COMMENT_STATE;
} else {
// Look for keywords
while (it != keywordList.end()) {
count = text.contains(*it);
if (count) {
for (int i = 0; i < count; i++) {
index = text.find(*it);
setFormat(index, (*it).length(), keyFont, keyColor);
++index;
}
}
++it;
}
}
} else if (endStateOfLastPara == COMMENT_STATE) {
setFormat(0, text.length(), commentFont, commentColor);
if (text.contains("*/"))
currentState = NORMAL_STATE;
} else if (endStateOfLastPara == DOXY_STATE) {
setFormat(0, text.length(), commentFont, commentDoxyColor);
if (text.contains("*/"))
currentState = NORMAL_STATE;
}
std::cout << "current state: " << currentState << "\n" << std::endl;
return currentState;
}
};
include <qapplication.h>
#include <qtextedit.h>
int main(int argc, char **argv)
{
SyntaxHighlighter* sh = new SyntaxHighlighter(&te);
sh->addKeyword("class");
sh->addKeyword("if");
sh->addKeyword("while");
sh->addKeyword("for");
te.setText("/*\nfoo\n*/\nclass\nwhile");
te.show();
a.setMainWidget(&te);
return a.exec();
}
#include <qapplication.h>
#include <qsyntaxhighlighter.h>
#include <qtextedit.h>
#include <iostream>
#define NORMAL_STATE 0
#define COMMENT_STATE 1
#define DOXY_STATE 2
class SyntaxHighlighter: public QSyntaxHighlighter {
QStringList keywordList;
QColor keyColor;
QFont keyFont;
QColor commentColor;
QFont commentFont;
QColor commentDoxyColor;
public:
SyntaxHighlighter(QTextEdit* textEdit) :
QSyntaxHighlighter(textEdit) {
keyFont.setBold(true);
keyFont.setItalic(true);
keyFont.setUnderline(true);
keyColor = Qt::blue;
commentColor = Qt::red;
commentDoxyColor = Qt::green;
commentFont.setItalic(true);
}
~SyntaxHighlighter() {
}
void addKeyword(const QString &str) {
keywordList << str;
}
int highlightParagraph(const QString & text, int endStateOfLastPara) {
int index = 0;
int count = 0;
QStringList::iterator it = keywordList.begin();
std::cout << "prev par state: " << endStateOfLastPara << std::endl;
std::cout << "text: " << text << std::endl;
// Assume the text to not contain keywords
setFormat(0, text.length(), textEdit()->font(), Qt::black);
int currentState = endStateOfLastPara;
if (endStateOfLastPara == NORMAL_STATE || endStateOfLastPara < 0) {
// keep it simple and assume that comment delimiters appear on a single line
if (text.contains("/**")) {
setFormat(0, text.length(), commentFont, commentDoxyColor);
currentState = DOXY_STATE;
} else if (text.contains("/*")) {
setFormat(0, text.length(), commentFont, commentColor);
currentState = COMMENT_STATE;
} else {
// Look for keywords
while (it != keywordList.end()) {
count = text.contains(*it);
if (count) {
for (int i = 0; i < count; i++) {
index = text.find(*it);
setFormat(index, (*it).length(), keyFont, keyColor);
++index;
}
}
++it;
}
}
} else if (endStateOfLastPara == COMMENT_STATE) {
setFormat(0, text.length(), commentFont, commentColor);
if (text.contains("*/"))
currentState = NORMAL_STATE;
} else if (endStateOfLastPara == DOXY_STATE) {
setFormat(0, text.length(), commentFont, commentDoxyColor);
if (text.contains("*/"))
currentState = NORMAL_STATE;
}
std::cout << "current state: " << currentState << "\n" << std::endl;
return currentState;
}
};
include <qapplication.h>
#include <qtextedit.h>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QTextEdit te;
SyntaxHighlighter* sh = new SyntaxHighlighter(&te);
sh->addKeyword("class");
sh->addKeyword("if");
sh->addKeyword("while");
sh->addKeyword("for");
te.setText("/*\nfoo\n*/\nclass\nwhile");
te.show();
a.setMainWidget(&te);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks