/* editorwindow.h This file is part of the Project::OSiRiON world editor and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "editorwindow.h" #include "entitywidget.h" #include "inistream.h" #include "mapwidget.h" #include "sidebar.h" #include #include #include #include #include #include #include namespace editor { EditorWindow::EditorWindow(QWidget *parent) : QWidget(parent) { editorwindow_splitter = new QSplitter(this); editorwindow_mapwidget = new MapWidget(); editorwindow_sidebar = new SideBar(); editorwindow_splitter->addWidget(editorwindow_sidebar); editorwindow_splitter->addWidget(editorwindow_mapwidget); connect(editorwindow_mapwidget, SIGNAL(propertiesChanged(EntityProperties *)), editorwindow_sidebar, SLOT(setProperties(EntityProperties *))); connect(editorwindow_sidebar, SIGNAL(entityChanged()), editorwindow_mapwidget, SLOT(resizeChildren())); connect(editorwindow_sidebar, SIGNAL(zonePropertiesClicked()), this, SLOT(showZoneProperties())); } QSize EditorWindow::sizeHint () const { return QSize(768, 512); } void EditorWindow::resizeEvent (QResizeEvent *event) { editorwindow_splitter->resize(width(), height()); } bool EditorWindow::loadFile(const QString &filename) { QFile file(filename); if (!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, tr("Open file"), tr("Could not open file %1:\n%2.") .arg(filename) .arg(file.errorString())); return false; } QTextStream textstream(&file); QApplication::setOverrideCursor(Qt::WaitCursor); IniStream ini; EntityWidget *entity = 0; bool in_entity = false; bool in_subsection = false; float x, y, z; float f; QString str; while (ini.getline(textstream)) { if (ini.got_section()) { if (ini.got_section("zone")) { editorwindow_zoneproperties.set_comment(ini.comment()); in_entity = false; } else if (ini.got_section("entity")) { in_entity = true; in_subsection = false; } else if (ini.got_section("jumpgate")) { in_entity = true; in_subsection = false; } else if (ini.got_section("jumppoint")) { in_entity = true; in_subsection = false; } else if (ini.got_section("navpoint")) { in_entity = true; in_subsection = false; } else if (ini.got_section("station")) { in_entity = true; in_subsection = false; } else if (ini.got_section("star")) { in_entity = true; in_subsection = false; } else if (ini.got_section("planet")) { in_entity = true; in_subsection = false; } else if (ini.got_section("racetrack")) { in_entity = true; in_subsection = false; } else if (ini.got_section("checkpoint")) { in_entity = true; in_subsection = false; } else if (ini.got_section("cargo")) { in_entity = false; in_subsection = true; } else if (ini.got_section("ship")) { in_entity = false; in_subsection = true; } else if (ini.got_section("weapon")) { in_entity = false; in_subsection = true; } else if (ini.got_section("platform")) { in_entity = true; in_subsection = false; } else if (ini.got_section("patrol")) { in_entity = true; in_subsection = false; } else if (ini.got_section("waypoint")) { in_entity = false; in_subsection = true; } else if (ini.got_section("npc")) { in_entity = false; in_subsection = true; } else { entity = 0; in_entity = false; in_subsection = false; } if (in_entity) { entity = editorwindow_mapwidget->addEntity(); entity->properties()->set_type(ini.section()); entity->properties()->set_comment(ini.comment()); } if (entity && in_subsection) { entity->properties()->add_subsection_header(ini.section(), ini.comment()); } } else if(ini.got_key()) { if (in_entity) { // 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("template", str)) { entity->properties()->set_comment("template", ini.comment()); entity->properties()->set_template(0, str); } else if (ini.got_key_string("ship", str)) { entity->properties()->set_comment("template", ini.comment()); entity->properties()->set_template(1, 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_vector3f("angles" , x, y, z)) { // the angles key param order is pitch yaw roll // the in-memory order is yaw pitch roll entity->properties()->set_comment(ini.key(), ini.comment()); entity->properties()->set_angles(y, x, z); } else if (ini.got_key_float("yaw", x)) { entity->properties()->set_yaw(x); } else if (ini.got_key_float("pitch", y)) { entity->properties()->set_pitch(y); } else if (ini.got_key_float("roll", z)) { entity->properties()->set_roll(z); } 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()) { entity->properties()->add_value(ini.key(), ini.value(), ini.comment()); } } else if (entity && in_subsection) { entity->properties()->add_subsection_value(ini.key(), ini.value(), ini.comment()); } 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()) { editorwindow_zoneproperties.add_value(ini.key(), ini.value(), ini.comment()); } } } } QApplication::restoreOverrideCursor(); editorwindow_filename = filename; editorwindow_mapwidget->resizeChildren(); editorwindow_mapwidget->update(); return true; } bool EditorWindow::saveFile(const QString &filename) { 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); QApplication::restoreOverrideCursor(); editorwindow_filename = filename; return true; } void EditorWindow::addEntity() { EntityWidget *entitywidget = editorwindow_mapwidget->addEntity(); editorwindow_mapwidget->select(entitywidget); } void EditorWindow::duplicateSelected() { editorwindow_mapwidget->duplicateSelected(); } void EditorWindow::deleteSelected() { editorwindow_mapwidget->deleteSelected(); } void EditorWindow::showZoneProperties() { QMessageBox::information(this, tr("Zone properties"), tr("Needs to be implemented")); } }