From e6347f3747a746151f1f67c7a508c5e2fc9091a8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 22 Feb 2012 21:36:54 +0000 Subject: Preserve ini file comments. preserve racetrack and checkpoint entities. --- src/editorwindow.cc | 50 ++++++++++++++++++++++++++++++++++++------------- src/entityproperties.cc | 8 ++++++-- src/entityproperties.h | 2 -- src/mainwindow.cc | 47 ++++++++++++++++++++++++++++++++++++---------- src/mainwindow.h | 2 ++ src/properties.cc | 8 ++++++++ src/properties.h | 33 +++++++++++++++++++++++++++++--- src/zoneproperties.cc | 4 +++- src/zoneproperties.h | 2 -- 9 files changed, 123 insertions(+), 33 deletions(-) diff --git a/src/editorwindow.cc b/src/editorwindow.cc index a15ed43..ae7d024 100644 --- a/src/editorwindow.cc +++ b/src/editorwindow.cc @@ -50,7 +50,7 @@ bool EditorWindow::loadFile(const QString &filename) { QFile file(filename); if (!file.open(QFile::ReadOnly | QFile::Text)) { - QMessageBox::warning(this, tr("Open Zone"), + QMessageBox::warning(this, tr("Open file"), tr("Could not open file %1:\n%2.") .arg(filename) .arg(file.errorString())); @@ -101,6 +101,12 @@ bool EditorWindow::loadFile(const QString &filename) } else if (ini.got_section("planet")) { in_entity = true; + } else if (ini.got_section("racetrack")) { + in_entity = true; + + } else if (ini.got_section("checkpoint")) { + in_entity = true; + } else if (ini.got_section("cargo")) { in_entity = false; in_subsection = true; @@ -129,19 +135,26 @@ bool EditorWindow::loadFile(const QString &filename) // read entity properties into the MapWidget instance if (ini.got_key_vector3f("location" , x, y, z)) { + entity->properties()->set_comment("location", ini.comment()); entity->properties()->set_location(x, y, z); //qDebug() << "got location " << x << " " << y << " " << z; } else if (ini.got_key_string("label", str)) { + entity->properties()->set_comment("label", ini.comment()); entity->properties()->set_label(str); } else if (ini.got_key_string("name", str)) { + entity->properties()->set_comment("name", ini.comment()); entity->properties()->set_name(str); } else if (ini.got_key_float("radius", f)) { + entity->properties()->set_comment("radius", ini.comment()); entity->properties()->set_radius(f); } else if (ini.got_key_string("info", str)) { + QString comment = entity->properties()->comment("info"); + comment += ini.comment(); + entity->properties()->set_comment("info", comment); entity->properties()->add_info(str); } else if (ini.got_key()) { @@ -154,10 +167,14 @@ bool EditorWindow::loadFile(const QString &filename) } else if (ini.in_section("zone")) { if (ini.got_key_string("name", str)) { + editorwindow_zoneproperties.set_comment("name", ini.comment()); editorwindow_zoneproperties.set_name(str); editorwindow_sidebar->setZoneName(str); } else if (ini.got_key_string("info", str)) { + QString comment = editorwindow_zoneproperties.comment("info"); + comment += ini.comment(); + editorwindow_zoneproperties.set_comment("info", comment); editorwindow_zoneproperties.add_info(str); } else if (ini.got_key()) { @@ -169,27 +186,34 @@ bool EditorWindow::loadFile(const QString &filename) } QApplication::restoreOverrideCursor(); - editorwindow_filename = QFileInfo(filename).canonicalFilePath(); + editorwindow_filename = filename; return true; } bool EditorWindow::saveFile(const QString &filename) { - QString buffer; - QTextStream textstream(&buffer); - + QFile file(filename); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Save file"), + tr("Cannot write file %1:\n%2.") + .arg(filename) + .arg(file.errorString())); + return false; + } + + QTextStream textstream(&file); + + QApplication::setOverrideCursor(Qt::WaitCursor); + editorwindow_zoneproperties.save(textstream); editorwindow_mapwidget->save(textstream); - - // debug output - QTextStream out(&buffer); - QString line; - while (!out.atEnd()) { - line = out.readLine(1024); - qDebug() << line; - } + QApplication::restoreOverrideCursor(); + + editorwindow_filename = filename; + return true; + } diff --git a/src/entityproperties.cc b/src/entityproperties.cc index e9ed1d0..429b759 100644 --- a/src/entityproperties.cc +++ b/src/entityproperties.cc @@ -62,15 +62,19 @@ void EntityProperties::save(QTextStream &textstream) } textstream << "[" << type() << "]" << '\n'; + save_comment(textstream, "label"); if (label().size()) textstream << "label=" << label() << '\n'; + save_comment(textstream, "name"); if (name().size()) textstream << "name=" << name() << '\n'; // location + save_comment(textstream, "location"); textstream << "location=" << location().x() << " " << location().y() << " " << location().z() << '\n'; // radius + save_comment(textstream, "radius"); if (radius()) { textstream << "radius=" << radius() << '\n'; } @@ -83,7 +87,7 @@ void EntityProperties::save(QTextStream &textstream) // info string if (info().size()) { textstream << '\n'; - + save_comment(textstream, "info"); // QTextStream operates on QString, not on QString const QString infobuffer = info(); QTextStream infostream(&infobuffer); @@ -107,9 +111,9 @@ void EntityProperties::save(QTextStream &textstream) else textstream << '\n'; } - } textstream << '\n'; } + } // namespace editor diff --git a/src/entityproperties.h b/src/entityproperties.h index edf7f38..b80a82e 100644 --- a/src/entityproperties.h +++ b/src/entityproperties.h @@ -10,8 +10,6 @@ #include "properties.h" -#include - namespace editor { diff --git a/src/mainwindow.cc b/src/mainwindow.cc index a92fb8b..4c79fd7 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -45,14 +45,19 @@ void MainWindow::init_actions() // File -> Open action_open = new QAction( tr("&Open..."), this); action_open->setShortcuts(QKeySequence::Open); - action_open->setStatusTip(tr("Open an existing zone")); + action_open->setStatusTip(tr("Open an existing zone ini file")); connect(action_open, SIGNAL(triggered()), this, SLOT(slot_open())); - action_save = new QAction( tr("&Save..."), this); + action_save = new QAction( tr("&Save"), this); action_save->setShortcuts(QKeySequence::Save); - action_save->setStatusTip(tr("Save File")); + action_save->setStatusTip(tr("Save zone")); connect(action_save, SIGNAL(triggered()), this, SLOT(slot_save())); + action_save_as = new QAction( tr("Save &As..."), this); + action_save_as->setShortcuts(QKeySequence::SaveAs); + action_save_as->setStatusTip(tr("Save zone as ini file")); + connect(action_save_as, SIGNAL(triggered()), this, SLOT(slot_save_as())); + // File -> Quit action_quit = new QAction(tr("&Quit"), this); action_quit->setShortcuts(QKeySequence::Quit); @@ -66,6 +71,7 @@ void MainWindow::init_menu() mainwindow_filemenu->addAction(action_new); mainwindow_filemenu->addAction(action_open); mainwindow_filemenu->addAction(action_save); + mainwindow_filemenu->addAction(action_save_as); mainwindow_filemenu->addSeparator(); mainwindow_filemenu->addAction(action_quit); @@ -88,7 +94,7 @@ QMdiSubWindow *MainWindow::find_child(const QString &filename) foreach (QMdiSubWindow *subwindow, mainwindow_mdiarea->subWindowList()) { EditorWindow *editorwindow = qobject_cast(subwindow->widget()); - if (editorwindow->filename() == canonical) + if (canonical == QFileInfo(editorwindow->filename()).canonicalFilePath()) return subwindow; } return 0; @@ -105,11 +111,10 @@ EditorWindow *MainWindow::add_child() QMdiSubWindow *subwindow = mainwindow_mdiarea->addSubWindow(child_widget); child_widget->show(); - mainwindow_mdiarea->setActiveSubWindow(subwindow); - // FIXME check if maximized subwindow->resize(768, 512); + mainwindow_mdiarea->setActiveSubWindow(subwindow); return child_widget; } @@ -120,7 +125,7 @@ void MainWindow::slot_new() void MainWindow::slot_open() { - QString filename = QFileDialog::getOpenFileName(this, tr("Open file")); + QString filename = QFileDialog::getOpenFileName(this, tr("Open...")); if (!filename.isEmpty()) { QMdiSubWindow *subwindow = find_child(filename); @@ -139,13 +144,35 @@ void MainWindow::slot_open() } } +void MainWindow::slot_save_as() +{ + EditorWindow *editorwindow = active_child(); + if (editorwindow) { + QString filename = QFileDialog::getSaveFileName(this, tr("Save as...")); + + if (!filename.isEmpty()) { + editorwindow->saveFile(filename); + + QMdiSubWindow *subwindow = find_child(filename); + if (subwindow) { + subwindow->setWindowTitle(editorwindow->filename()); + } + } + } +} + void MainWindow::slot_save() { - QString filename; - + /* if (active_child()) { - active_child()->saveFile(filename); + if (!active_child()->filename().size()) { + slot_save_as(); + return; + } else { + active_child()->saveFile(active_child()->filename()); + } } + */ } } diff --git a/src/mainwindow.h b/src/mainwindow.h index b2943a5..b76eaa8 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -32,6 +32,7 @@ private slots: void slot_new(); void slot_open(); void slot_save(); + void slot_save_as(); private: void init_actions(); @@ -53,6 +54,7 @@ private: QAction *action_new; QAction *action_open; QAction *action_save; + QAction *action_save_as; QAction *action_quit; }; diff --git a/src/properties.cc b/src/properties.cc index 87633f7..c9263cb 100644 --- a/src/properties.cc +++ b/src/properties.cc @@ -35,4 +35,12 @@ void Properties::add_info(const QString &text) properties_info += '\n'; } +void Properties::save_comment(QTextStream &textstream, const QString &attribute) +{ + QString text = comment(attribute); + if (text.size()) { + textstream << text; + } +} + } // namespace editor diff --git a/src/properties.h b/src/properties.h index c6147e4..efde85b 100644 --- a/src/properties.h +++ b/src/properties.h @@ -11,7 +11,9 @@ #include "vector3f.h" #include +#include #include +#include namespace editor { @@ -63,17 +65,24 @@ public: return properties_comment; } + /** + * @brief returns the comment string for a specified attribute + * */ + inline const QString comment(const QString &attribute) const { + return properties_comments[attribute]; + } + /** * @brief returns the info string of this object * */ - inline const QString & info() const { + inline const QString &info() const { return properties_info; } /** * @brief returns the values string of this object * */ - inline const QString & values() const { + inline const QString &values() const { return properties_values; } @@ -86,6 +95,13 @@ public: properties_comment = text; } + /** + * @brief set the comments string for a specified attribute + * */ + inline void set_comment(const QString attribute, const QString &text) { + properties_comments[attribute] = text; + } + /** * @brief set the info string of this object * */ @@ -162,12 +178,23 @@ public: properties_color.setRgb(r, g, b); } +protected: + /** + * @brief save attribute comments to a textstream + * */ + void save_comment(QTextStream &textstream, const QString &attribute); private: + typedef QMap Comments; + + /// comments for this objects QString properties_comment; + /// comments for the individual attributes; + Comments properties_comments; + QString properties_label; QString properties_name; - + Vector3f properties_location; QColor properties_color; diff --git a/src/zoneproperties.cc b/src/zoneproperties.cc index 43e4804..4e5736b 100644 --- a/src/zoneproperties.cc +++ b/src/zoneproperties.cc @@ -34,7 +34,8 @@ void ZoneProperties::save(QTextStream & textstream) } textstream << "[zone]" << '\n'; - + + save_comment(textstream, "name"); if (name().size()) textstream << "name=" << name() << '\n'; @@ -46,6 +47,7 @@ void ZoneProperties::save(QTextStream & textstream) // info string if (info().size()) { textstream << '\n'; + save_comment(textstream, "info"); // QTextStream operates on QString, not on QString const QString infobuffer = info(); diff --git a/src/zoneproperties.h b/src/zoneproperties.h index 9b79569..9e6ac80 100644 --- a/src/zoneproperties.h +++ b/src/zoneproperties.h @@ -10,8 +10,6 @@ #include "properties.h" -#include - namespace editor { -- cgit v1.2.3