From 782425f9dfb80fc26f56a6b201de92ca8a561d39 Mon Sep 17 00:00:00 2001 From: dnk-88 Date: Tue, 16 Aug 2011 13:52:06 +0300 Subject: [PATCH] Changed: #1302 Added save/saveAs action for primitive item. --HG-- branch : gsoc2011-worldeditorqt --- .../plugins/world_editor/primitive_item.cpp | 12 ++++ .../src/plugins/world_editor/primitive_item.h | 4 ++ .../plugins/world_editor/primitives_model.cpp | 10 ++- .../plugins/world_editor/primitives_view.cpp | 63 +++++++++++++++++-- .../plugins/world_editor/primitives_view.h | 2 + .../world_editor/world_editor_actions.cpp | 4 +- .../world_editor/world_editor_constants.h | 2 + 7 files changed, 89 insertions(+), 8 deletions(-) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.cpp index e948f8b0f..e867f1368 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.cpp @@ -235,6 +235,7 @@ Node::NodeType PrimitiveNode::type() const RootPrimitiveNode::RootPrimitiveNode(const QString &name, NLLIGO::CPrimitives *primitives) : PrimitiveNode(primitives->RootNode), + m_fileName(name), m_primitives(primitives) { setData(Qt::DisplayRole, name); @@ -249,6 +250,17 @@ NLLIGO::CPrimitives *RootPrimitiveNode::primitives() const return m_primitives; } +void RootPrimitiveNode::setFileName(const QString &fileName) +{ + setData(Qt::DisplayRole, fileName); + m_fileName = fileName; +} + +QString RootPrimitiveNode::fileName() const +{ + return m_fileName; +} + Node::NodeType RootPrimitiveNode::type() const { return RootPrimitiveNodeType; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.h index 1b2a89d4c..300973c55 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitive_item.h @@ -167,9 +167,13 @@ public: NLLIGO::CPrimitives *primitives() const; + void setFileName(const QString &fileName); + QString fileName() const; virtual NodeType type() const; private: + + QString m_fileName; NLLIGO::CPrimitives *m_primitives; }; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_model.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_model.cpp index f59cb8d67..9e689066a 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_model.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_model.cpp @@ -18,6 +18,7 @@ #include "primitive_item.h" #include "primitives_model.h" #include "world_editor_misc.h" +#include "world_editor_constants.h" // NeL includes #include @@ -218,6 +219,10 @@ Path PrimitivesTreeModel::createRootPrimitiveNode(const QString &fileName, NLLIG if (m_worldEditNode == 0) createWorldEditNode("NewWorldEdit"); + QString name = "NewPrimitive"; + if (!fileName.isEmpty()) + name = fileName; + // Get position int pos = m_worldEditNode->childCount(); @@ -225,10 +230,13 @@ Path PrimitivesTreeModel::createRootPrimitiveNode(const QString &fileName, NLLIG // Add root node in tree model beginInsertRows(parentIndex, pos, pos); - RootPrimitiveNode *newNode = new RootPrimitiveNode(fileName, primitives); + RootPrimitiveNode *newNode = new RootPrimitiveNode(name, primitives); m_worldEditNode->appendChildNode(newNode); endInsertRows(); + newNode->setData(Constants::PRIMITIVE_FILE_IS_CREATED, !fileName.isEmpty()); + newNode->setData(Constants::PRIMITIVE_IS_MODIFIED, false); + QModelIndex rootPrimIndex = index(pos, 0, parentIndex); // Scan childs items and add in the tree model diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.cpp index 896218a54..0a2851e65 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.cpp @@ -18,6 +18,7 @@ #include "primitives_view.h" #include "primitives_model.h" #include "world_editor_actions.h" +#include "world_editor_constants.h" #include "../core/core_constants.h" #include "../landscape_editor/landscape_editor_constants.h" @@ -27,9 +28,11 @@ #include #include #include +#include // Qt includes #include +#include #include #include @@ -49,12 +52,10 @@ PrimitivesView::PrimitivesView(QWidget *parent) m_unloadAction->setEnabled(false); m_saveAction = new QAction("Save", this); - m_saveAction->setEnabled(false); m_saveAction->setIcon(QIcon(Core::Constants::ICON_SAVE)); m_saveAsAction = new QAction("Save As...", this); m_saveAsAction->setIcon(QIcon(Core::Constants::ICON_SAVE_AS)); - m_saveAsAction->setEnabled(false); m_loadLandAction = new QAction("Load landscape file", this); m_loadLandAction->setIcon(QIcon(LandscapeEditor::Constants::ICON_ZONE_ITEM)); @@ -83,6 +84,8 @@ PrimitivesView::PrimitivesView(QWidget *parent) connect(m_newPrimitiveAction, SIGNAL(triggered()), this, SLOT(createRootPrimitive())); connect(m_selectChildrenAction, SIGNAL(triggered()), this, SLOT(selectChildren())); connect(m_deleteAction, SIGNAL(triggered()), this, SLOT(deletePrimitives())); + connect(m_saveAction, SIGNAL(triggered()), this, SLOT(save())); + connect(m_saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs())); #ifdef Q_OS_DARWIN setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); @@ -186,6 +189,54 @@ void PrimitivesView::selectChildren() selectChildren(parentIndex); } +void PrimitivesView::save() +{ + nlassert(m_primitivesTreeModel); + + QModelIndexList indexList = selectionModel()->selectedRows(); + QModelIndex index = indexList.first(); + + RootPrimitiveNode *node = static_cast(index.internalPointer()); + + if (node->data(Constants::PRIMITIVE_FILE_IS_CREATED).toBool()) + { + if (!NLLIGO::saveXmlPrimitiveFile(*node->primitives(), node->fileName().toStdString())) + QMessageBox::warning(this, "World Editor Qt", QString("Error writing output file: %1").arg(node->fileName())); + else + node->setData(Constants::PRIMITIVE_IS_MODIFIED, false); + } + else + saveAs(); +} + +void PrimitivesView::saveAs() +{ + nlassert(m_primitivesTreeModel); + + QString fileName = QFileDialog::getSaveFileName(this, + tr("Save NeL Ligo primitive file"), m_lastDir, + tr("NeL Ligo primitive file (*.primitive)")); + + setCursor(Qt::WaitCursor); + if (!fileName.isEmpty()) + { + QModelIndexList indexList = selectionModel()->selectedRows(); + QModelIndex index = indexList.first(); + + RootPrimitiveNode *node = static_cast(index.internalPointer()); + + if (!NLLIGO::saveXmlPrimitiveFile(*node->primitives(), fileName.toStdString())) + QMessageBox::warning(this, "World Editor Qt", QString("Error writing output file: %1").arg(fileName)); + else + { + node->setFileName(fileName); + node->setData(Constants::PRIMITIVE_FILE_IS_CREATED, true); + node->setData(Constants::PRIMITIVE_IS_MODIFIED, false); + } + } + setCursor(Qt::ArrowCursor); +} + void PrimitivesView::deletePrimitives() { nlassert(m_undoStack); @@ -267,8 +318,8 @@ void PrimitivesView::selectChildren(const QModelIndex &parent) void PrimitivesView::fillMenu_WorldEdit(QMenu *menu) { menu->addAction(m_unloadAction); - menu->addAction(m_saveAction); - menu->addAction(m_saveAsAction); + //menu->addAction(m_saveAction); + //menu->addAction(m_saveAsAction); menu->addSeparator(); menu->addAction(m_loadLandAction); menu->addAction(m_loadPrimitiveAction); @@ -279,7 +330,7 @@ void PrimitivesView::fillMenu_WorldEdit(QMenu *menu) void PrimitivesView::fillMenu_Landscape(QMenu *menu) { - menu->addAction(m_deleteAction); + menu->addAction(m_unloadAction); menu->addSeparator(); menu->addAction(m_showAction); menu->addAction(m_hideAction); @@ -289,7 +340,9 @@ void PrimitivesView::fillMenu_RootPrimitive(QMenu *menu, const QModelIndex &inde { menu->addAction(m_saveAction); menu->addAction(m_saveAsAction); + menu->addAction(m_unloadAction); fillMenu_Primitive(menu, index); + menu->removeAction(m_deleteAction); } void PrimitivesView::fillMenu_Primitive(QMenu *menu, const QModelIndex &index) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.h index eae9187ac..3fce65ae7 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/primitives_view.h @@ -67,6 +67,8 @@ private Q_SLOTS: void createRootPrimitive(); void selectChildren(); + void save(); + void saveAs(); void deletePrimitives(); void addNewPrimitiveByClass(int value); void generatePrimitives(int value); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp index f40e498a1..2eaa2a488 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp @@ -295,7 +295,7 @@ void CreateRootPrimitiveCommand::undo() void CreateRootPrimitiveCommand::redo() { NLLIGO::CPrimitives *newRootPrim = new NLLIGO::CPrimitives(); - m_rootPrimIndex = m_model->createRootPrimitiveNode(m_fileName, newRootPrim); + m_rootPrimIndex = m_model->createRootPrimitiveNode("", newRootPrim); } @@ -337,7 +337,7 @@ void LoadRootPrimitiveCommand::redo() // set the primitive context NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = primitives; - NLLIGO::loadXmlPrimitiveFile(*primitives, m_fileName.toStdString(), *NLLIGO::CPrimitiveContext::instance().CurrentLigoConfig); + NLLIGO::loadXmlPrimitiveFile(*primitives, m_fileName.toStdString(), *Utils::ligoConfig()); // unset the context NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = NULL; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_constants.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_constants.h index e0bdc4705..9a7a834d6 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_constants.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_constants.h @@ -28,6 +28,8 @@ const int NODE_PERISTENT_INDEX = USER_TYPE + 1; const int WORLD_EDITOR_NODE = USER_TYPE + 2; const int GRAPHICS_DATA_QT4_2D = USER_TYPE + 3; const int GRAPHICS_DATA_NEL3D = USER_TYPE + 4; +const int PRIMITIVE_IS_MODIFIED = USER_TYPE + 5; +const int PRIMITIVE_FILE_IS_CREATED = USER_TYPE + 6; //settings const char *const WORLD_EDITOR_SECTION = "WorldEditor";