[QScintilla] Using QsciLexerCustom
Chris Packham
judge.packham at gmail.com
Wed Jan 13 21:28:09 GMT 2010
On Wed, Jan 13, 2010 at 1:12 PM, Baz Walter <bazwal at ftml.net> wrote:
> Chris Packham wrote:
>>
>> Hi,
>>
>> Is there an example somewhere using the QsciLexerCustom class?
>
> there's a thread here which may be some help to you:
>
> http://www.riverbankcomputing.com/pipermail/pyqt/2009-July/023655.html
>
Thanks for the link that got me started. For completeness here's my
initial C++ version. Its probably buggy but you get the idea.
> cat mylexer.h
#ifndef MYLEXER_H
#define MYLEXER_H
#include <QObject>
#include <qscilexercustom.h>
class QsciStyle;
class MyLexer : public QsciLexerCustom {
Q_OBJECT
public:
enum {
Default = 0,
Comment = 1,
MaxStyle
};
MyLexer(QObject *parent = 0);
~MyLexer();
const char *language() const;
QString description(int) const;
void styleText(int start, int end);
QColor defaultColor(int);
QFont defaultFont(int);
QColor defaultPaper(int);
private:
QsciStyle getStyle(int);
};
#endif
> cat mylexer.cpp
#include <QDebug>
#include <QColor>
#include <QFont>
#include <qsciscintilla.h>
#include <qscistyle.h>
#include "mylexer.h"
MyLexer::MyLexer(QObject *parent)
: QsciLexerCustom(parent)
{
qDebug() << __FUNCTION__;
}
MyLexer::~MyLexer()
{
qDebug() << __FUNCTION__;
}
const char* MyLexer::language() const
{
return "AsciiDoc";
}
QString MyLexer::description(int style) const
{
switch(style){
case Default:
return "Default";
case Comment:
return "Comment";
}
return QString();
}
void MyLexer::styleText(int start, int end)
{
QString source;
int i;
qDebug() << __FUNCTION__
<< "start =" << start
<< " end =" << end;
if (!editor())
return;
char *chars = (char *) malloc (end - start);
editor()->SendScintilla(QsciScintilla::SCI_GETTEXTRANGE,
start, end, chars);
source = QString(chars);
free(chars);
qDebug() << "source =" << source;
startStyling(start, 0x1f);
QStringList list = source.split("\n");
for (i = 0; i < list.size(); i++) {
QString line = list.at(i);
int len = line.size();
int style;
qDebug() << "line =" << line;
if (line.startsWith("//")) {
style = Comment;
} else {
style = Default;
}
qDebug() << "Styling " << len << "bytes " << description(style);
setStyling(len, getStyle(style));
// newline character was consumed in split so...
setStyling(1, getStyle(Default));
}
}
QColor MyLexer::defaultColor(int style)
{
switch(style){
case Default:
return QColor(0xe0, 0x0, 0x0);
case Comment:
return QColor(0x0, 0xe0, 0x0);
}
return QsciLexer::defaultColor(style);
}
QFont MyLexer::defaultFont(int style)
{
return QFont("Courier New", 10);
}
QColor MyLexer::defaultPaper(int style)
{
return QsciLexer::defaultPaper(style);
}
QsciStyle MyLexer::getStyle(int style)
{
if (style < MaxStyle) {
return QsciStyle(style, description(style), defaultColor(style),
defaultPaper(style), defaultFont(style));
} else {
return QsciStyle(style);
}
}
More information about the QScintilla
mailing list