1
0
Fork 0
forked from len0rd/rockbox

Theme Editor: Added syntax highlighting

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26506 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Robert Bieber 2010-06-03 07:38:59 +00:00
parent 1dc7f490c9
commit 6feac4a315
6 changed files with 183 additions and 7 deletions

View file

@ -34,6 +34,11 @@ EditorWindow::EditorWindow(QWidget *parent) :
tree = new ParseTreeModel(ui->code->document()->toPlainText().toAscii());
ui->parseTree->setModel(tree);
/* Setting up the syntax highlighter */
highlighter = new SkinHighlighter(QColor(0,255,0), QColor(255,0,0),
QColor(0,0,255), QColor(150,150,150),
ui->code->document());
/* Connecting the buttons */
QObject::connect(ui->code, SIGNAL(cursorPositionChanged()),
this, SLOT(updateTree()));
@ -50,7 +55,7 @@ void EditorWindow::updateTree()
void EditorWindow::updateCode()
{
tree->genCode();
ui->code->setDocument(new QTextDocument(tree->genCode()));
ui->code->document()->setPlainText(tree->genCode());
}
EditorWindow::~EditorWindow()

View file

@ -25,6 +25,7 @@
#include <QMainWindow>
#include "parsetreemodel.h"
#include "skinhighlighter.h"
namespace Ui {
class EditorWindow;
@ -43,6 +44,7 @@ private slots:
private:
Ui::EditorWindow *ui;
ParseTreeModel* tree;
SkinHighlighter* highlighter;
};

View file

@ -32,7 +32,7 @@
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="code"/>
<widget class="QPlainTextEdit" name="code"/>
</item>
</layout>
</item>
@ -74,8 +74,8 @@
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>65</x>
<y>57</y>
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>299</x>

View file

@ -0,0 +1,132 @@
#include "skinhighlighter.h"
SkinHighlighter::SkinHighlighter(QColor comment, QColor tag, QColor conditional,
QColor escaped, QTextDocument* doc)
:QSyntaxHighlighter(doc),
escaped(escaped), tag(tag),
conditional(conditional), comment(comment)
{
}
SkinHighlighter::~SkinHighlighter()
{
}
void SkinHighlighter::highlightBlock(const QString& text)
{
for(int i = 0; i < text.length(); i++)
{
QChar c = text[i];
/* Checking for delimiters */
if(c == ARGLISTOPENSYM
|| c == ARGLISTCLOSESYM
|| c == ARGLISTSEPERATESYM)
setFormat(i, 1, tag);
if(c == ENUMLISTOPENSYM
|| c == ENUMLISTCLOSESYM
|| c == ENUMLISTSEPERATESYM)
setFormat(i, 1, conditional);
/* Checking for comments */
if(c == COMMENTSYM)
{
setFormat(i, text.length() - i, comment);
return;
}
if(c == TAGSYM)
{
if(text.length() - i < 2)
return;
if(find_escape_character(text[i + 1].toAscii()))
{
/* Checking for escaped characters */
setFormat(i, 2, escaped);
i++;
}
else if(text[i + 1] != CONDITIONSYM)
{
/* Checking for normal tags */
char lookup[3];
struct tag_info* found = 0;
/* First checking for a two-character tag name */
lookup[2] = '\0';
if(text.length() - i >= 3)
{
lookup[0] = text[i + 1].toAscii();
lookup[1] = text[i + 2].toAscii();
found = find_tag(lookup);
}
if(found)
{
setFormat(i, 3, tag);
i += 2;
}
else
{
lookup[1] = '\0';
lookup[0] = text[i + 1].toAscii();
found = find_tag(lookup);
if(found)
{
setFormat(i, 2, tag);
i++;
}
}
}
else if(text[i + 1] == CONDITIONSYM)
{
/* Checking for conditional tags */
if(text.length() - i < 3)
return;
char lookup[3];
struct tag_info* found = 0;
lookup[2] = '\0';
if(text.length() - i >= 4)
{
lookup[0] = text[i + 2].toAscii();
lookup[1] = text[i + 3].toAscii();
found = find_tag(lookup);
}
if(found)
{
setFormat(i, 4, conditional);
i += 3;
}
else
{
lookup[1] = '\0';
lookup[0] = text[i + 2].toAscii();
found = find_tag(lookup);
if(found)
{
setFormat(i, 3, conditional);
i += 2;
}
}
}
}
}
}

View file

@ -0,0 +1,36 @@
#ifndef SKINHIGHLIGHTER_H
#define SKINHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QPlainTextEdit>
#include "tag_table.h"
#include "symbols.h"
class SkinHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
/*
* font - The font used for all text
* normal - The normal text color
* escaped - The color for escaped characters
* tag - The color for tags and their delimiters
* conditional - The color for conditionals and their delimiters
*
*/
SkinHighlighter(QColor comment, QColor tag, QColor conditional,
QColor escaped, QTextDocument* doc);
virtual ~SkinHighlighter();
void highlightBlock(const QString& text);
private:
QColor escaped;
QColor tag;
QColor conditional;
QColor comment;
};
#endif // SKINHIGHLIGHTER_H

View file

@ -4,7 +4,6 @@ OBJECTS_DIR = $$MYBUILDDIR/o
UI_DIR = $$MYBUILDDIR/ui
MOC_DIR = $$MYBUILDDIR/moc
RCC_DIR = $$MYBUILDDIR/rcc
HEADERS += tag_table.h \
symbols.h \
skin_parser.h \
@ -12,7 +11,8 @@ HEADERS += tag_table.h \
skin_debug.h \
parsetreemodel.h \
parsetreenode.h \
editorwindow.h
editorwindow.h \
skinhighlighter.h
SOURCES += tag_table.c \
skin_parser.c \
skin_scan.c \
@ -20,6 +20,7 @@ SOURCES += tag_table.c \
main.cpp \
parsetreemodel.cpp \
parsetreenode.cpp \
editorwindow.cpp
editorwindow.cpp \
skinhighlighter.cpp
OTHER_FILES += README
FORMS += editorwindow.ui