From 47181b8b9b0e3e914243a463ad02a2eceab61c6e Mon Sep 17 00:00:00 2001 From: Robert Bieber Date: Sat, 5 Jun 2010 19:47:49 +0000 Subject: Theme Editor: Got save/save-as functionality working and added Tango icons to the toolbar git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26593 a1c6a512-1295-4272-9138-f99709370657 --- utils/themeeditor/editorwindow.cpp | 61 +++++++++++++++++- utils/themeeditor/editorwindow.h | 5 +- utils/themeeditor/editorwindow.ui | 89 ++++++++++++++++++++++++-- utils/themeeditor/resources.qrc | 3 + utils/themeeditor/resources/COPYING | 6 ++ utils/themeeditor/resources/document-new.png | Bin 0 -> 1008 bytes utils/themeeditor/resources/document-open.png | Bin 0 -> 1550 bytes utils/themeeditor/resources/document-save.png | Bin 0 -> 1971 bytes utils/themeeditor/skindocument.cpp | 87 ++++++++++++++++++++----- utils/themeeditor/skindocument.h | 12 ++-- utils/themeeditor/themeeditor.pro | 6 +- 11 files changed, 240 insertions(+), 29 deletions(-) create mode 100644 utils/themeeditor/resources/COPYING create mode 100644 utils/themeeditor/resources/document-new.png create mode 100644 utils/themeeditor/resources/document-open.png create mode 100644 utils/themeeditor/resources/document-save.png diff --git a/utils/themeeditor/editorwindow.cpp b/utils/themeeditor/editorwindow.cpp index cadc6313a9..4d2ed87169 100644 --- a/utils/themeeditor/editorwindow.cpp +++ b/utils/themeeditor/editorwindow.cpp @@ -96,9 +96,22 @@ void EditorWindow::setupMenus() QObject::connect(ui->actionPreview_Panel, SIGNAL(triggered()), this, SLOT(showPanel())); - /* Connecting the document opening/closing actions */ + /* Connecting the document management actions */ QObject::connect(ui->actionNew_Document, SIGNAL(triggered()), this, SLOT(newTab())); + QObject::connect(ui->actionToolbarNew, SIGNAL(triggered()), + this, SLOT(newTab())); + + QObject::connect(ui->actionClose_Document, SIGNAL(triggered()), + this, SLOT(closeCurrent())); + + QObject::connect(ui->actionSave_Document, SIGNAL(triggered()), + this, SLOT(saveCurrent())); + QObject::connect(ui->actionSave_Document_As, SIGNAL(triggered()), + this, SLOT(saveCurrentAs())); + QObject::connect(ui->actionToolbarSave, SIGNAL(triggered()), + this, SLOT(saveCurrent())); + } @@ -115,13 +128,23 @@ void EditorWindow::newTab() void EditorWindow::shiftTab(int index) { if(index < 0) + { ui->parseTree->setModel(0); + ui->actionSave_Document->setEnabled(false); + ui->actionSave_Document_As->setEnabled(false); + ui->actionClose_Document->setEnabled(false); + } else + { ui->parseTree->setModel(dynamic_cast (ui->editorTabs->currentWidget())->getModel()); + ui->actionSave_Document->setEnabled(true); + ui->actionSave_Document_As->setEnabled(true); + ui->actionClose_Document->setEnabled(true); + } } -void EditorWindow::closeTab(int index) +bool EditorWindow::closeTab(int index) { SkinDocument* widget = dynamic_cast (ui->editorTabs->widget(index)); @@ -129,9 +152,30 @@ void EditorWindow::closeTab(int index) { ui->editorTabs->removeTab(index); widget->deleteLater(); + return true; } + + return false; +} + +void EditorWindow::closeCurrent() +{ + closeTab(ui->editorTabs->currentIndex()); } +void EditorWindow::saveCurrent() +{ + if(ui->editorTabs->currentIndex() >= 0) + dynamic_cast(ui->editorTabs->currentWidget())->save(); +} + +void EditorWindow::saveCurrentAs() +{ + if(ui->editorTabs->currentIndex() >= 0) + dynamic_cast(ui->editorTabs->currentWidget())->saveAs(); +} + + void EditorWindow::tabTitleChanged(QString title) { SkinDocument* sender = dynamic_cast(QObject::sender()); @@ -150,7 +194,20 @@ void EditorWindow::showPanel() void EditorWindow::closeEvent(QCloseEvent* event) { + saveSettings(); + + /* Closing all the tabs */ + for(int i = 0; i < ui->editorTabs->count(); i++) + { + if(!dynamic_cast + (ui->editorTabs->widget(i))->requestClose()) + { + event->ignore(); + return; + } + } + event->accept(); } diff --git a/utils/themeeditor/editorwindow.h b/utils/themeeditor/editorwindow.h index 52076b61a2..0febe54021 100644 --- a/utils/themeeditor/editorwindow.h +++ b/utils/themeeditor/editorwindow.h @@ -45,7 +45,10 @@ private slots: void showPanel(); void newTab(); void shiftTab(int index); - void closeTab(int index); + bool closeTab(int index); + void closeCurrent(); + void saveCurrent(); + void saveCurrentAs(); void tabTitleChanged(QString title); private: diff --git a/utils/themeeditor/editorwindow.ui b/utils/themeeditor/editorwindow.ui index b990f6eabd..21152dad75 100644 --- a/utils/themeeditor/editorwindow.ui +++ b/utils/themeeditor/editorwindow.ui @@ -14,7 +14,7 @@ Rockbox Theme Editor - + :/resources/resources/windowicon.png:/resources/resources/windowicon.png @@ -50,6 +50,11 @@ + + + + + @@ -91,6 +96,9 @@ false + + + @@ -147,14 +155,14 @@ Parse &Tree Panel - - Ctrl+D - &Preferences + + Ctrl+P + @@ -194,8 +202,79 @@ Ctrl+O + + + false + + + &Save Document + + + Ctrl+S + + + + + false + + + &Close Document + + + Ctrl+W + + + + + false + + + Save Document &As + + + Ctrl+Shift+S + + + + + + :/resources/resources/document-new.png:/resources/resources/document-new.png + + + ToolbarNew + + + New + + + + + + :/resources/resources/document-open.png:/resources/resources/document-open.png + + + ToolbarOpen + + + Open + + + + + + :/resources/resources/document-save.png:/resources/resources/document-save.png + + + ToolbarSave + + + Save + + - + + + actionQuit diff --git a/utils/themeeditor/resources.qrc b/utils/themeeditor/resources.qrc index fbe5cfbb01..b882e23a1d 100644 --- a/utils/themeeditor/resources.qrc +++ b/utils/themeeditor/resources.qrc @@ -1,5 +1,8 @@ resources/windowicon.png + resources/document-new.png + resources/document-open.png + resources/document-save.png diff --git a/utils/themeeditor/resources/COPYING b/utils/themeeditor/resources/COPYING new file mode 100644 index 0000000000..02389762b0 --- /dev/null +++ b/utils/themeeditor/resources/COPYING @@ -0,0 +1,6 @@ +The files appicon.xcf and windowicon.png are authored by Robert Bieber, and +made available in the public domain. + +The files document-new.png, document-open.png, and document-save.png came from +the Tango Desktop Project (http://www.tango.freedesktop.org) and are also in +the public domain. diff --git a/utils/themeeditor/resources/document-new.png b/utils/themeeditor/resources/document-new.png new file mode 100644 index 0000000000..e6d64bb90b Binary files /dev/null and b/utils/themeeditor/resources/document-new.png differ diff --git a/utils/themeeditor/resources/document-open.png b/utils/themeeditor/resources/document-open.png new file mode 100644 index 0000000000..f35f258354 Binary files /dev/null and b/utils/themeeditor/resources/document-open.png differ diff --git a/utils/themeeditor/resources/document-save.png b/utils/themeeditor/resources/document-save.png new file mode 100644 index 0000000000..db5c52b769 Binary files /dev/null and b/utils/themeeditor/resources/document-save.png differ diff --git a/utils/themeeditor/skindocument.cpp b/utils/themeeditor/skindocument.cpp index 8617030180..3fb7d4871b 100644 --- a/utils/themeeditor/skindocument.cpp +++ b/utils/themeeditor/skindocument.cpp @@ -28,13 +28,18 @@ #include SkinDocument::SkinDocument(QWidget *parent) : - QWidget(parent) + QWidget(parent), fileFilter(tr("WPS Files (*.wps *.rwps);;" + "SBS Files (*.sbs *.rsbs);;" + "FMS Files (*.fms *.rfms);;" + "All Skin Files (*.wps *.rwps *.sbs " + "*.rsbs *.fms *.rfms);;" + "All Files (*.*)")) { setupUI(); title = "Untitled"; fileName = ""; - saved = true; + saved = ""; } SkinDocument::~SkinDocument() @@ -45,7 +50,35 @@ SkinDocument::~SkinDocument() bool SkinDocument::requestClose() { - saveAs(); + if(editor->document()->toPlainText() != saved) + { + /* Spawning the "Are you sure?" dialog */ + QMessageBox confirm(this); + confirm.setText(title + tr(" has been modified.")); + confirm.setInformativeText(tr("Do you want to save your changes?")); + confirm.setStandardButtons(QMessageBox::Save | QMessageBox::Discard + | QMessageBox::Cancel); + confirm.setDefaultButton(QMessageBox::Save); + int confirmation = confirm.exec(); + + switch(confirmation) + { + case QMessageBox::Save: + save(); + /* After calling save, make sure the user actually went through */ + if(editor->document()->toPlainText() != saved) + return false; + else + return true; + + case QMessageBox::Discard: + return true; + + case QMessageBox::Cancel: + return false; + } + } + return true; } @@ -74,11 +107,22 @@ void SkinDocument::setupUI() void SkinDocument::codeChanged() { model->changeTree(editor->document()->toPlainText().toAscii()); - if(saved == true) + + if(editor->document()->toPlainText() != saved) { - saved = false; - title.append(tr("*")); - emit titleChanged(title); + if(title.length() > 0 && title[title.length() - 1] != '*') + { + title.append('*'); + emit titleChanged(title); + } + } + else + { + if(title.length() > 0 && title[title.length() - 1] == '*') + { + title.chop(1); + emit titleChanged(title); + } } } @@ -88,7 +132,7 @@ void SkinDocument::save() if(!fout.exists()) { - QTimer::singleShot(0, this, SLOT(saveAs())); + saveAs(); return; } @@ -96,22 +140,31 @@ void SkinDocument::save() fout.write(editor->document()->toPlainText().toAscii()); fout.close(); - saved = true; + saved = editor->document()->toPlainText(); + QStringList decompose = fileName.split('/'); + title = decompose[decompose.count() - 1]; + emit titleChanged(title); + } void SkinDocument::saveAs() { /* Determining the directory to open */ - QSettings settings; + QString directory = fileName; + QSettings settings; settings.beginGroup("SkinDocument"); - QString openDir = settings.value("defaultDirectory", "").toString(); + if(directory == "") + directory = settings.value("defaultDirectory", "").toString(); + + fileName = QFileDialog::getSaveFileName(this, tr("Save Document"), + directory, fileFilter); + directory = fileName; + if(fileName == "") + return; - fileName = QFileDialog::getSaveFileName(this, tr("Save File"), openDir,""); - QString directory = fileName; directory.chop(fileName.length() - fileName.lastIndexOf('/') - 1); settings.setValue("defaultDirectory", directory); - settings.endGroup(); QFile fout(fileName); @@ -119,5 +172,9 @@ void SkinDocument::saveAs() fout.write(editor->document()->toPlainText().toAscii()); fout.close(); - saved = true; + saved = editor->document()->toPlainText(); + QStringList decompose = fileName.split('/'); + title = decompose[decompose.count() - 1]; + emit titleChanged(title); + } diff --git a/utils/themeeditor/skindocument.h b/utils/themeeditor/skindocument.h index 37f1443ece..d00c81f36b 100644 --- a/utils/themeeditor/skindocument.h +++ b/utils/themeeditor/skindocument.h @@ -33,21 +33,23 @@ class SkinDocument : public QWidget { Q_OBJECT public: + const QString fileFilter; + + SkinDocument(QWidget *parent = 0); virtual ~SkinDocument(); ParseTreeModel* getModel(){ return model; } QString getTitle(){ return title; } + void save(); + void saveAs(); + bool requestClose(); signals: void titleChanged(QString); -public slots: - void save(); - void saveAs(); - private slots: void codeChanged(); @@ -56,7 +58,7 @@ private: QString title; QString fileName; - bool saved; + QString saved; QLayout* layout; QPlainTextEdit* editor; diff --git a/utils/themeeditor/themeeditor.pro b/utils/themeeditor/themeeditor.pro index 19d9205d7b..ef32a3e56e 100644 --- a/utils/themeeditor/themeeditor.pro +++ b/utils/themeeditor/themeeditor.pro @@ -26,6 +26,10 @@ SOURCES += tag_table.c \ skindocument.cpp OTHER_FILES += README \ resources/windowicon.png \ - resources/appicon.xcf + resources/appicon.xcf \ + resources/COPYING \ + resources/document-save.png \ + resources/document-open.png \ + resources/document-new.png FORMS += editorwindow.ui RESOURCES += resources.qrc -- cgit v1.2.3