From 4b457d688b9b9c5b4b15babf93c04b8d2db489a9 Mon Sep 17 00:00:00 2001 From: Robert Bieber Date: Wed, 14 Jul 2010 07:38:09 +0000 Subject: Theme Editor: Added optional plaintext editing for config files git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27415 a1c6a512-1295-4272-9138-f99709370657 --- utils/themeeditor/gui/configdocument.cpp | 150 ++++++++++++++++++++++++++++-- utils/themeeditor/gui/configdocument.h | 19 +++- utils/themeeditor/gui/configdocument.ui | 81 ++++++++++++---- utils/themeeditor/models/projectmodel.cpp | 2 + utils/themeeditor/resources.qrc | 2 + utils/themeeditor/resources/cursor.png | Bin 0 -> 235 bytes utils/themeeditor/resources/cursor.xcf | Bin 0 -> 1096 bytes utils/themeeditor/resources/lines.png | Bin 0 -> 231 bytes utils/themeeditor/resources/lines.xcf | Bin 0 -> 918 bytes utils/themeeditor/themeeditor.pro | 6 +- 10 files changed, 232 insertions(+), 28 deletions(-) create mode 100644 utils/themeeditor/resources/cursor.png create mode 100644 utils/themeeditor/resources/cursor.xcf create mode 100644 utils/themeeditor/resources/lines.png create mode 100644 utils/themeeditor/resources/lines.xcf diff --git a/utils/themeeditor/gui/configdocument.cpp b/utils/themeeditor/gui/configdocument.cpp index 0962484ba9..f3bfc7280c 100644 --- a/utils/themeeditor/gui/configdocument.cpp +++ b/utils/themeeditor/gui/configdocument.cpp @@ -27,14 +27,50 @@ #include #include #include +#include ConfigDocument::ConfigDocument(QMap& settings, QString file, QWidget *parent) : TabContent(parent), ui(new Ui::ConfigDocument), - filePath(file) + filePath(file), block(false) { ui->setupUi(this); + editor = new CodeEditor(this); + editor->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); + editor->setLineWrapMode(CodeEditor::NoWrap); + ui->splitter->addWidget(editor); + + QObject::connect(editor, SIGNAL(textChanged()), + this, SLOT(textChanged())); + + /* Loading the splitter status */ + QSettings appSettings; + appSettings.beginGroup("ConfigDocument"); + + if(!appSettings.value("textVisible", true).toBool()) + { + editor->setVisible(false); + ui->textButton->setChecked(false); + } + else + { + ui->textButton->setChecked(true); + } + if(!appSettings.value("linesVisible", false).toBool()) + { + ui->scrollArea->setVisible(false); + ui->linesButton->setChecked(false); + } + else + { + ui->linesButton->setChecked(true); + } + + if(!appSettings.value("splitter", QByteArray()).toByteArray().isNull()) + ui->splitter->restoreState(appSettings.value("splitter").toByteArray()); + + appSettings.endGroup(); /* Populating the known keys list */ QFile fin(":/resources/configkeys"); @@ -50,20 +86,29 @@ ConfigDocument::ConfigDocument(QMap& settings, QString file, container->append(current.trimmed()); } - QMap::iterator i; - for(i = settings.begin(); i != settings.end(); i++) - if(i.key() != "themebase") - addRow(i.key(), i.value()); + /* Loading the text file */ + QFile finT(settings.value("configfile", "")); + if(finT.open(QFile::Text | QFile::ReadOnly)) + { + editor->setPlainText(QString(finT.readAll())); + finT.close(); + } saved = toPlainText(); QObject::connect(ui->addKeyButton, SIGNAL(pressed()), this, SLOT(addClicked())); + + QObject::connect(ui->linesButton, SIGNAL(toggled(bool)), + this, SLOT(buttonChecked())); + QObject::connect(ui->textButton, SIGNAL(toggled(bool)), + this, SLOT(buttonChecked())); } ConfigDocument::~ConfigDocument() { delete ui; + editor->deleteLater(); } void ConfigDocument::changeEvent(QEvent *e) @@ -171,6 +216,15 @@ bool ConfigDocument::requestClose() QString ConfigDocument::toPlainText() const { + return editor->toPlainText(); +} + +void ConfigDocument::syncFromBoxes() +{ + if(block) + return; + blockUpdates(); + QString buffer = ""; for(int i = 0; i < keys.count(); i++) @@ -181,7 +235,61 @@ QString ConfigDocument::toPlainText() const buffer += "\n"; } - return buffer; + editor->setPlainText(buffer); +} + +void ConfigDocument::syncFromText() +{ + if(block) + return; + + blockUpdates(); + + QStringList lines = editor->toPlainText().split("\n"); + QList > splits; + for(int i = 0; i < lines.count(); i++) + { + QString line = lines[i]; + QStringList split = line.split(":"); + if(split.count() != 2) + continue; + + splits.append(QPair(split[0].trimmed(), + split[1].trimmed())); + } + + while(deleteButtons.count() > splits.count()) + { + deleteButtons[0]->deleteLater(); + keys[0]->deleteLater(); + values[0]->deleteLater(); + containers[0]->deleteLater(); + labels[0]->deleteLater(); + + deleteButtons.removeAt(0); + keys.removeAt(0); + values.removeAt(0); + containers.removeAt(0); + labels.removeAt(0); + } + + int initialCount = deleteButtons.count(); + for(int i = 0; i < splits.count(); i++) + { + if(i >= initialCount) + { + addRow(splits[i].first, splits[i].second); + } + else + { + int index = keys[i]->findText(splits[i].first); + if(index != -1) + keys[i]->setCurrentIndex(index); + else + keys[i]->setEditText(splits[i].first); + values[i]->setText(splits[i].second); + } + } } void ConfigDocument::addRow(QString key, QString value) @@ -215,11 +323,11 @@ void ConfigDocument::addRow(QString key, QString value) QObject::connect(delButton, SIGNAL(clicked()), this, SLOT(deleteClicked())); QObject::connect(keyEdit, SIGNAL(currentIndexChanged(QString)), - this, SLOT(textChanged())); + this, SLOT(boxesChanged())); QObject::connect(keyEdit, SIGNAL(textChanged(QString)), - this, SLOT(textChanged())); + this, SLOT(boxesChanged())); QObject::connect(valueEdit, SIGNAL(textChanged(QString)), - this, SLOT(textChanged())); + this, SLOT(boxesChanged())); ui->configBoxes->addLayout(layout); @@ -259,10 +367,34 @@ void ConfigDocument::addClicked() addRow(tr("Key"), tr("Value")); } +void ConfigDocument::boxesChanged() +{ + syncFromBoxes(); + contentsChanged(); +} + void ConfigDocument::textChanged() +{ + syncFromText(); + contentsChanged(); +} + +void ConfigDocument::contentsChanged() { if(toPlainText() != saved) emit titleChanged(title() + "*"); else emit titleChanged(title()); } + +void ConfigDocument::buttonChecked() +{ + editor->setVisible(ui->textButton->isChecked()); + ui->scrollArea->setVisible(ui->linesButton->isChecked()); + + QSettings settings; + settings.beginGroup("ConfigDocument"); + settings.setValue("textVisible", ui->textButton->isChecked()); + settings.setValue("linesVisible", ui->linesButton->isChecked()); + settings.endGroup(); +} diff --git a/utils/themeeditor/gui/configdocument.h b/utils/themeeditor/gui/configdocument.h index e91c5cc357..29278dbd11 100644 --- a/utils/themeeditor/gui/configdocument.h +++ b/utils/themeeditor/gui/configdocument.h @@ -30,8 +30,10 @@ #include #include #include +#include #include "tabcontent.h" +#include "codeeditor.h" namespace Ui { class ConfigDocument; @@ -58,6 +60,8 @@ public: QString title() const; QString toPlainText() const; + void syncFromBoxes(); + void syncFromText(); void save(); void saveAs(); @@ -73,9 +77,20 @@ signals: private slots: void deleteClicked(); void addClicked(); + void boxesChanged(); void textChanged(); + void contentsChanged(); + void blockUpdates() + { + block = true; + QTimer::singleShot(20, this, SLOT(unblockUpdates())); + } + void unblockUpdates(){ block = false; } + void buttonChecked(); private: + void addRow(QString key, QString value); + Ui::ConfigDocument *ui; QList containers; QList keys; @@ -89,7 +104,9 @@ private: QString filePath; QString saved; - void addRow(QString key, QString value); + CodeEditor* editor; + + bool block; }; #endif // CONFIGDOCUMENT_H diff --git a/utils/themeeditor/gui/configdocument.ui b/utils/themeeditor/gui/configdocument.ui index e2f9e7fb21..88d44d2191 100644 --- a/utils/themeeditor/gui/configdocument.ui +++ b/utils/themeeditor/gui/configdocument.ui @@ -15,29 +15,74 @@ - - - true + + + Qt::Vertical - - - - 0 - 0 - 402 - 244 - + + + true - - - - - + + + + 0 + 0 + 402 + 244 + + + + + + + + + + + + + + + + :/resources/resources/cursor.png:/resources/resources/cursor.png + + + true + + + true + + + true + + + + + + + + + + + :/resources/resources/lines.png:/resources/resources/lines.png + + + true + + + false + + + true + + + @@ -74,6 +119,8 @@ - + + + diff --git a/utils/themeeditor/models/projectmodel.cpp b/utils/themeeditor/models/projectmodel.cpp index 632e0aa075..2663e8b426 100644 --- a/utils/themeeditor/models/projectmodel.cpp +++ b/utils/themeeditor/models/projectmodel.cpp @@ -39,6 +39,8 @@ ProjectModel::ProjectModel(QString config, EditorWindow* mainWindow, if(!cfg.isReadable()) return; + settings.insert("configfile", config); + QTextStream fin(&cfg); /* Storing the base directory */ diff --git a/utils/themeeditor/resources.qrc b/utils/themeeditor/resources.qrc index 87211b95e9..bad8edcb20 100644 --- a/utils/themeeditor/resources.qrc +++ b/utils/themeeditor/resources.qrc @@ -13,6 +13,8 @@ resources/ffwd.png resources/pause.png resources/rwnd.png + resources/cursor.png + resources/lines.png resources/render/scenebg.png diff --git a/utils/themeeditor/resources/cursor.png b/utils/themeeditor/resources/cursor.png new file mode 100644 index 0000000000..d1bcef26fb Binary files /dev/null and b/utils/themeeditor/resources/cursor.png differ diff --git a/utils/themeeditor/resources/cursor.xcf b/utils/themeeditor/resources/cursor.xcf new file mode 100644 index 0000000000..97db8743df Binary files /dev/null and b/utils/themeeditor/resources/cursor.xcf differ diff --git a/utils/themeeditor/resources/lines.png b/utils/themeeditor/resources/lines.png new file mode 100644 index 0000000000..fda23beb27 Binary files /dev/null and b/utils/themeeditor/resources/lines.png differ diff --git a/utils/themeeditor/resources/lines.xcf b/utils/themeeditor/resources/lines.xcf new file mode 100644 index 0000000000..2212fb2b3b Binary files /dev/null and b/utils/themeeditor/resources/lines.xcf differ diff --git a/utils/themeeditor/themeeditor.pro b/utils/themeeditor/themeeditor.pro index 0805890db1..5d77d562a6 100644 --- a/utils/themeeditor/themeeditor.pro +++ b/utils/themeeditor/themeeditor.pro @@ -96,7 +96,11 @@ OTHER_FILES += README \ resources/pause.xcf \ resources/pause.png \ resources/ffwd.xcf \ - resources/ffwd.png + resources/ffwd.png \ + resources/lines.xcf \ + resources/lines.png \ + resources/cursor.xcf \ + resources/cursor.png FORMS += gui/editorwindow.ui \ gui/preferencesdialog.ui \ gui/configdocument.ui \ -- cgit v1.2.3