1
0
Fork 0
forked from len0rd/rockbox

Theme Editor: Made auto-complete functional and enabled it by default. Added a small subset of the available tags to the tagdb file, filling it out is todo

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27625 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Robert Bieber 2010-07-30 08:38:38 +00:00
parent f8dd370ff8
commit ba41fa537a
8 changed files with 174 additions and 27 deletions

View file

@ -107,6 +107,11 @@ void CodeEditor::cursorMoved()
/* Closing the completer if the cursor has moved out of its bounds */
if(completer.isVisible())
{
if(document()->toPlainText().length() > docLength)
tagEnd++;
else if(document()->toPlainText().length() < docLength)
tagEnd--;
if(textCursor().position() < tagBegin
|| textCursor().position() > tagEnd)
{
@ -115,6 +120,24 @@ void CodeEditor::cursorMoved()
}
}
void CodeEditor::insertTag()
{
/* Clearing the typed tag and inserting one from the completer */
QTextCursor at(document());
at.setPosition(tagBegin, QTextCursor::MoveAnchor);
while(document()->characterAt(at.position()) == QChar('%')
|| document()->characterAt(at.position()) == '?')
at.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, 1);
at.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,
tagEnd - at.position());
at.removeSelectedText();
at.insertText(completer.currentItem()->text(0));
completer.hide();
}
//![resizeEvent]
void CodeEditor::resizeEvent(QResizeEvent *e)
@ -131,7 +154,7 @@ void CodeEditor::resizeEvent(QResizeEvent *e)
void CodeEditor::keyPressEvent(QKeyEvent *event)
{
if(!settings.value("completeSyntax", false).toBool())
if(!settings.value("completeSyntax", true).toBool())
{
QPlainTextEdit::keyPressEvent(event);
return;
@ -154,10 +177,22 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
< completer.topLevelItemCount() - 1)
QApplication::sendEvent(&completer, event);
}
else if(event->key() == Qt::Key_Backspace)
else if(event->key() == Qt::Key_Backspace
|| event->key() == Qt::Key_Delete)
{
tagEnd--;
docLength = document()->toPlainText().length();
QPlainTextEdit::keyPressEvent(event);
QString filterText;
for(int i = tagBegin; i < tagEnd; i++)
{
QChar c = document()->characterAt(i);
if(c != '%' && c != '?')
filterText.append(c);
}
completer.filter(filterText);
}
else if(event->key() == Qt::Key_Escape)
{
@ -165,14 +200,15 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
completer.hide();
QPlainTextEdit::keyPressEvent(event);
}
else if(event->key() == Qt::Key_Enter)
else if(event->key() == Qt::Key_Return)
{
/* The enter key inserts the currently selected tag */
insertTag();
}
else if(event->key() == Qt::Key_Question)
{
/* The question mark doesn't filter the list */
tagEnd++;
docLength = document()->toPlainText().length();
QPlainTextEdit::keyPressEvent(event);
}
else if(event->key() == Qt::Key_Left
@ -184,10 +220,19 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
else
{
/* Otherwise, we have to filter the list */
tagEnd++;
docLength = document()->toPlainText().length();
QPlainTextEdit::keyPressEvent(event);
QString filterText = "";
QString filterText;
for(int i = tagBegin; i < tagEnd; i++)
{
QChar c = document()->characterAt(i);
if(c != '%' && c != '?')
filterText.append(c);
}
completer.filter(filterText);
}
}
else

View file

@ -77,6 +77,7 @@ private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void updateLineNumberArea(const QRect &, int);
void cursorMoved();
void insertTag();
private:
QWidget *lineNumberArea;
@ -87,6 +88,7 @@ private:
int tagBegin;
int tagEnd;
int docLength;
};
//![codeeditordefinition]

View file

@ -51,7 +51,7 @@ void PreferencesDialog::loadSettings()
QSettings settings;
settings.beginGroup("CodeEditor");
ui->completionBox->setChecked(settings.value("completeSyntax",
false).toBool());
true).toBool());
settings.endGroup();
}

View file

@ -68,6 +68,9 @@
<property name="text">
<string>Enable Syntax Completion</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>

View file

@ -23,15 +23,20 @@
#include <QTreeWidgetItem>
#include "syntaxcompleter.h"
#include "codeeditor.h"
SyntaxCompleter::SyntaxCompleter(QWidget *parent) :
SyntaxCompleter::SyntaxCompleter(CodeEditor *parent) :
QTreeWidget(parent)
{
setHeaderHidden(true);
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setWordWrap(true);
setColumnCount(2);
QObject::connect(this, SIGNAL(activated(QModelIndex)),
parent, SLOT(insertTag()));
QFile fin(":/resources/tagdb");
fin.open(QFile::ReadOnly | QFile::Text);
@ -45,14 +50,11 @@ SyntaxCompleter::SyntaxCompleter(QWidget *parent) :
QStringList tag;
tag.append(split[0].trimmed());
tag.append(split[1].trimmed());
tags.insert(split[0].trimmed().toLower(), tag);
tags.insertMulti(split[0].trimmed().toLower(), tag);
}
filter("");
resizeColumnToContents(0);
setColumnWidth(0, columnWidth(0) + 10); // Auto-resize is too small
}
void SyntaxCompleter::filter(QString text)
@ -64,13 +66,13 @@ void SyntaxCompleter::filter(QString text)
{
if(text.length() == 1)
{
if(text[0].toLower() != i.key()[0])
if(text[0].toLower() != i.key()[0].toLower())
continue;
}
else if(text.length() == 2)
{
if(text[0].toLower() != i.key()[0] || i.key().length() < 2
|| text[1].toLower() != i.key()[1])
if(text[0].toLower() != i.key()[0].toLower() || i.key().length() < 2
|| text[1].toLower() != i.key()[1].toLower())
continue;
}
else if(text.length() > 2)
@ -80,4 +82,11 @@ void SyntaxCompleter::filter(QString text)
addTopLevelItem(new QTreeWidgetItem(i.value()));
}
if(topLevelItemCount() > 0)
setCurrentIndex(indexFromItem(topLevelItem(0)));
resizeColumnToContents(0);
setColumnWidth(0, columnWidth(0) + 10); // Auto-resize is too small
resizeColumnToContents(1);
}

View file

@ -24,11 +24,13 @@
#include <QTreeWidget>
class CodeEditor;
class SyntaxCompleter : public QTreeWidget
{
Q_OBJECT
public:
SyntaxCompleter(QWidget *parent = 0);
SyntaxCompleter(CodeEditor *parent = 0);
void filter(QString text);
signals:

View file

@ -1,3 +1,23 @@
###########################################################################
# __________ __ ___.
# Open \______ \ ____ ____ | | _\_ |__ _______ ___
# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
# \/ \/ \/ \/ \/
# $Id$
#
# Copyright (C) 2010 Robert Bieber
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
############################################################################/
# This file defines the options for the device configuration panel
# Declare a section with a line containing a string inside brackets, i.e.
# [Some Section]

View file

@ -1,10 +1,76 @@
z : Ending tag
aa : Should come at the beginning of everything
za : Should come after z
y : Should come before z
T : Test tag 1
Ta : Test tag 2
Tb : Test tag 3
U : Another test
Ua : Yet another test
Uc : A sixth test
###########################################################################
# __________ __ ___.
# Open \______ \ ____ ____ | | _\_ |__ _______ ___
# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
# \/ \/ \/ \/ \/
# $Id$
#
# Copyright (C) 2010 Robert Bieber
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
############################################################################/
# Enter tags in the format "tag : Description"
# Empty lines and lines beginning with '#' are ignored
# The descriptions are going to be displayed in a little auto-complete
# pop-up, so keep them as terse as possible
#
# Like the deviceoptions file, this is compiled into the executable and
# could segfault things if improperly formatted, so be careful---treat it
# like part of the source code, because it is.
#
# Viewport tags
V : Viewport declaration
Vl : Viewport preload
Vd : Viewport display
Vi : Custom UI viewport
VI : Pick custom UI viewport
Vf : Foreground color
Vb : Background color
# Fonts
Fl : Load a font
# Status Bar
we : Enable status bar
wd : Disable status bar
wi : Display inbuilt status bar
# ID3 Info
ia : Artist
ic : Composer
iA : Album artist
id : Album Name
iG : Grouping
in : Track #
it : Track Title
iC : Comment
iv : ID3 version
iy : ID3 year
ik : Disc number
# Next track ID3
Ia : Next Artist
Ic : Next Composer
IA : Next Album artist
Id : Next Album Name
IG : Next Grouping
In : Next Track #
It : Next Track Title
IC : Next Comment
Iv : Next ID3 version
Iy : Next ID3 year
Ik : Next Disc number