From c770c7ac78fb20a5e9147cd3a8b6583f22ace769 Mon Sep 17 00:00:00 2001 From: sfb Date: Thu, 27 Sep 2012 08:22:50 -0500 Subject: [PATCH] Added: #1306 Added undo/redo action for renaming array entry names --HG-- branch : branch-features-geqt-plugin --- .../src/plugins/georges_editor/actions.cpp | 82 ++++++++++++++++++- .../src/plugins/georges_editor/actions.h | 23 ++++++ .../src/plugins/georges_editor/formitem.cpp | 58 +++++++------ .../src/plugins/georges_editor/formitem.h | 5 ++ .../georges_editor/georges_editor_form.cpp | 7 +- .../georges_editor/georges_editor_form.h | 3 +- 6 files changed, 148 insertions(+), 30 deletions(-) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.cpp index dba45fb96..2a7780eb4 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.cpp @@ -14,4 +14,84 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -#include "actions.h" \ No newline at end of file +// Project includes +#include "actions.h" +#include "formitem.h" + +// Qt includes + +// NeL includes +#include +#include +#include +#include +#include +#include +#include + +namespace GeorgesQt +{ + + CUndoFormArrayRenameCommand::CUndoFormArrayRenameCommand(CFormItem *item, QString newValue, uint elementId, QUndoCommand *parent) + : QUndoCommand("Rename Form Array", parent), m_item(item), m_newValue(newValue), m_elementId(elementId) + { } + + void CUndoFormArrayRenameCommand::redo() + { + // Get the parent node + const NLGEORGES::CFormDfn *parentDfn; + uint indexDfn; + const NLGEORGES::CFormDfn *nodeDfn; + const NLGEORGES::CType *nodeType; + NLGEORGES::CFormElm *node; + NLGEORGES::UFormDfn::TEntryType type; + bool isArray; + bool vdfnArray; + NLGEORGES::CForm *form=static_cast(m_item->form()); + NLGEORGES::CFormElm *elm = static_cast(&form->getRootNode()); + nlverify ( elm->getNodeByName (m_item->formName().c_str (), &parentDfn, indexDfn, &nodeDfn, &nodeType, &node, type, isArray, vdfnArray, true, NLGEORGES_FIRST_ROUND) ); + if (node) + { + nlinfo("doing array rename"); + NLGEORGES::CFormElmArray* array = NLMISC::safe_cast (node->getParent ()); + if(array->Elements[m_elementId].Name.empty()) + { + m_oldValue.append("#"); + m_oldValue.append(QString("%1").arg(m_elementId)); + } + else + { + m_oldValue = array->Elements[m_elementId].Name.c_str(); + } + + array->Elements[m_elementId].Name = m_newValue.toStdString(); + m_item->setName(m_newValue.toStdString()); + } + + } + + void CUndoFormArrayRenameCommand::undo() + { + // Get the parent node + const NLGEORGES::CFormDfn *parentDfn; + uint indexDfn; + const NLGEORGES::CFormDfn *nodeDfn; + const NLGEORGES::CType *nodeType; + NLGEORGES::CFormElm *node; + NLGEORGES::UFormDfn::TEntryType type; + bool isArray; + bool vdfnArray; + NLGEORGES::CForm *form=static_cast(m_item->form()); + NLGEORGES::CFormElm *elm = static_cast(&form->getRootNode()); + nlverify ( elm->getNodeByName (m_item->formName().c_str (), &parentDfn, indexDfn, &nodeDfn, &nodeType, &node, type, isArray, vdfnArray, true, NLGEORGES_FIRST_ROUND) ); + if (node) + { + NLGEORGES::CFormElmArray* array = NLMISC::safe_cast (node->getParent ()); + //m_oldValue = array->Elements[m_elementId].Name.c_str(); + array->Elements[m_elementId].Name = m_oldValue.toStdString(); + m_item->setName(m_oldValue.toStdString()); + } + + } + +} \ No newline at end of file diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.h index c646adc72..c0e0d3b8e 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/actions.h @@ -17,4 +17,27 @@ #ifndef ACTIONS_H #define ACTIONS_H +#include + +namespace GeorgesQt +{ + class CFormItem; + + class CUndoFormArrayRenameCommand : public QUndoCommand + { + public: + CUndoFormArrayRenameCommand(CFormItem *item, QString newValue, uint elementId, QUndoCommand *parent = 0); + ~CUndoFormArrayRenameCommand() {} + + void redo(); + void undo(); + + protected: + CFormItem *m_item; + QString m_newValue; + QString m_oldValue; + uint m_elementId; + }; +} + #endif // ACTIONS_H \ No newline at end of file diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.cpp index 98bffa7e2..39545afde 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.cpp @@ -14,7 +14,10 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +// Project includes #include "formitem.h" +#include "actions.h" +#include "georges_editor_form.h" // Qt includes #include @@ -81,34 +84,39 @@ namespace GeorgesQt bool CFormItem::setData(int column, const QVariant &value) { - if (column != 0) - return false; - - bool deleteInsert = false; - - - - // Get the parent node - const NLGEORGES::CFormDfn *parentDfn; - uint indexDfn; - const NLGEORGES::CFormDfn *nodeDfn; - const NLGEORGES::CType *nodeType; - NLGEORGES::CFormElm *parentNode; - NLGEORGES::UFormDfn::TEntryType type; - bool array; - bool parentVDfnArray; - NLGEORGES::CForm *form=static_cast(m_form); - NLGEORGES::CFormElm *elm = static_cast(&form->getRootNode()); - nlverify ( elm->getNodeByName (_FormName.c_str(), &parentDfn, indexDfn, &nodeDfn, &nodeType, &parentNode, type, array, parentVDfnArray, true, NLGEORGES_FIRST_ROUND) ); - - if (parentItem && parentItem->nodeType () == CFormItem::Form) + if(isEditable(column)) { - std::string newName = value.toString().toStdString(); - _Name = newName; + nlinfo("form item is editable."); + // Ensure that it is a child. + if (parentItem && parentItem->nodeType () == CFormItem::Form) + { + nlinfo("retrieving node information for data change."); + // Get the parent node + const NLGEORGES::CFormDfn *parentDfn; + uint indexDfn; + const NLGEORGES::CFormDfn *nodeDfn; + const NLGEORGES::CType *nodeType; + NLGEORGES::CFormElm *parentNode; + NLGEORGES::UFormDfn::TEntryType type; + bool isArray; + bool parentVDfnArray; + NLGEORGES::CForm *form=static_cast(m_form); + NLGEORGES::CFormElm *elm = static_cast(&form->getRootNode()); - // Create an action to update the form. + // Lets check the parent first, for arrays. + nlverify ( elm->getNodeByName (parentItem->formName().c_str(), &parentDfn, indexDfn, &nodeDfn, &nodeType, &parentNode, type, isArray, parentVDfnArray, true, NLGEORGES_FIRST_ROUND) ); + + if(isArray && parentNode) + { + nlinfo( "is array and a child, generate rename command"); + CUndoFormArrayRenameCommand *cmd = new CUndoFormArrayRenameCommand(this,value.toString(), _StructId); + GeorgesEditorForm::UndoStack->push(cmd); + return true; + } + } } - return true; + + return false; } bool CFormItem::isEditable(int column) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.h index e08a198bd..2acda42bb 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/formitem.h @@ -58,9 +58,14 @@ namespace GeorgesQt TSub nodeType() { return _Type; } std::string formName() { return _FormName; } + std::string name() { return _Name; } + void setName(std::string name) { _Name = name; } + uint structId() { return _StructId; } + NLGEORGES::UForm *form() { return m_form; } + bool isEditable(int column); QIcon getItemImage(CFormItem *rootItem); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.cpp index a36f411e0..f42d6bc6b 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.cpp @@ -35,6 +35,7 @@ namespace GeorgesQt { + QUndoStack *GeorgesEditorForm::UndoStack = NULL; GeorgesEditorForm::GeorgesEditorForm(QWidget *parent) : QMainWindow(parent), @@ -61,7 +62,7 @@ namespace GeorgesQt m_mainDock->setDockNestingEnabled(true); layout->addWidget(m_mainDock); - m_undoStack = new QUndoStack(this); + UndoStack = new QUndoStack(this); Core::MenuManager *menuManager = Core::ICore::instance()->menuManager(); m_openAction = menuManager->action(Core::Constants::OPEN); @@ -114,7 +115,7 @@ namespace GeorgesQt QUndoStack *GeorgesEditorForm::undoStack() const { - return m_undoStack; + return UndoStack; } void GeorgesEditorForm::open() @@ -211,7 +212,7 @@ namespace GeorgesQt } CGeorgesTreeViewDialog *dock = new CGeorgesTreeViewDialog(m_mainDock); - dock->setUndoStack(m_undoStack); + dock->setUndoStack(UndoStack); m_lastActiveDock = dock; m_dockedWidgets.append(dock); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.h index 77fdd1ec8..c9cef964c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georges_editor_form.h @@ -38,6 +38,8 @@ public: QUndoStack *undoStack() const; + static QUndoStack *UndoStack; + public Q_SLOTS: void open(); void loadFile(const QString fileName); @@ -54,7 +56,6 @@ private: void readSettings(); void writeSettings(); - QUndoStack *m_undoStack; Ui::GeorgesEditorForm m_ui; CGeorgesDirTreeDialog *m_georgesDirTreeDialog;