PDA

View Full Version : Using QtDesigner highlighter in my own application



elcuco
4th May 2011, 23:12
I see that this question raises a lot, and quite frankly besides scintilla is no reliable way to have in my application a simple widget which displays a text editor with syntax highlighter. Writing one is a very tedious job, and then maintaining the language definitions is a huge problem.

I tried writing a QSyntaxHighlighter that knows how to load the KatePart definitions but it seemed like a lot of work, which as a part time hobbyist I can not spare. Then I found out that QtCreator 2.2 and above have this code. But I have not seen anyone who managed to rip this off that hugs ass project.

I started working on this, slowly about two weeks ago and now I managed to load a simple langues (I started with SQL which does not include another definition). The code I use for the demo is quite simple, and not connected to the QtCreator application:



#include <QApplication>
#include <QMainWindow>
#include <QPlainTextEdit>
#include <QFile>
#include <QIODevice>

#include "formats.h"
#include "highlighter.h"
#include "highlightdefinition.h"
#include "highlighterexception.h"
#include "qate/highlightdefinitionhandler-v2.h"

void load_text(QString fe, QPlainTextEdit *te );
QSharedPointer<TextEditor::Internal::HighlightDefinition> get_highlighter_definition(QString definitionFileName);

int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
QString definitionFileName = "/usr/share/kde4/apps/katepart/syntax/sql.xml";
QSharedPointer<TextEditor::Internal::HighlightDefinition> def = get_highlighter_definition(definitionFileName);

QMainWindow *mw = new QMainWindow;
QPlainTextEdit *te = new QPlainTextEdit(mw);
TextEditor::Internal::Highlighter *hl = new TextEditor::Internal::Highlighter;

hl->configureFormat(TextEditor::Internal::Highlighter: :Normal, Formats::instance().charFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter: :Keyword, Formats::instance().keywordFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter: :Decimal, Formats::instance().decimalFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter: :Comment, Formats::instance().commentFormat() );
hl->configureFormat(TextEditor::Internal::Highlighter: :Function, Formats::instance().functionFormat() );

hl->setParent(te);
hl->setDocument(te->document());
hl->setDefaultContext(def->initialContext());
hl->rehighlight();

load_text("/home/elcuco/test.sql", te);
mw->setWindowTitle("Kate syntax highter test");
mw->setCentralWidget(te);
mw->show();
return app.exec();

Q_UNUSED(hl);
}

void load_text(QString fe, QPlainTextEdit *te )
{
QFile f(fe);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QString s = f.readAll();
te->setPlainText(s);
te->setLineWrapMode(QPlainTextEdit::NoWrap);
}

// stollen from QSharedPointer<HighlightDefinition> Manager::definition(const QString &id)
QSharedPointer<TextEditor::Internal::HighlightDefinition> get_highlighter_definition(QString definitionFileName)
{
QFile definitionFile(definitionFileName);
if (!definitionFile.open(QIODevice::ReadOnly | QIODevice::Text))
return QSharedPointer<TextEditor::Internal::HighlightDefinition>();

QSharedPointer<TextEditor::Internal::HighlightDefinition> definition(new TextEditor::Internal::HighlightDefinition);
TextEditor::Internal::HighlightDefinitionHandlerV2 handler(definition);

QXmlInputSource source(&definitionFile);
QXmlSimpleReader reader;
reader.setContentHandler(&handler);
//m_isBuilding.insert(id);
try {
reader.parse(source);
} catch (TextEditor::Internal::HighlighterException &) {
definition.clear();
}
//m_isBuilding.remove(id);
definitionFile.close();

// m_definitions.insert(id, definition);
return definition;
}


I also attach here a demo of this small application running. Is anyone else working on such thing? Did anyone manage to get the syntax highlighter used outside QtCreator on a simple QPlainTextEdit?

elcuco
12th May 2011, 23:15
Now the code is working, and I can load simple syntax definitions. Next step is to use the language manager to nest syntax definitions, as KatePart really does. Code is available from SVN:



svn co https://qtedit4.googlecode.com/svn/tools/qtsourceview


The code depends on the full QtCreator source. Just follow instructions to configure it (look inside the *.pro file).

Feel free to use and abuse.

elcuco
21st May 2011, 12:02
Good news:

I managed to get the full syntax highlighter working, and I started testing it. I loaded the HTML syntax, and was able to see the colors for HTML/CSS/JavaScript - which means the main engine is working. I also "rebased" the code to the latest code from QtCreator (see the SVN history).

The code should be usable for 3rd parties now.

pinkieiknip
21st May 2011, 18:54
Very interesting to know that there is a syntax highlighter ready for use. Will keep this in mind for future applications!

elcuco
7th July 2011, 18:22
I finally forked the code. The google code SVN now contains a small demo that loads definitions. Tested under Win7 and Linux, using Qt4.7. This should build on Qt4.2 and above, but I did not test it fully.

If you are on windows, the example will not load as expected. This is because QtCreator does not use the Kate highlighter for C++ files, and thus it does not contain cpp.xml. Just download the corresponding XML from the QtCreator GUI:
Tools->Options->Text Editor->Generic Highlighter->Download definitions.

The XML will be saved in %HOME%\AppData\Roaming\Nokia\qtcreator\generic-highlighter, the code currently does load files from that dir, so don't worry.

Check the SVN and send patches if you like this project.

elcuco
30th July 2011, 19:59
I just released QtSourceView 0.0.3, with qate support: http://www.qtcentre.org/threads/3105-qtsourceview.

Home page + download links: http://code.google.com/p/qtedit4/wiki/QtSourceView