From 882b31546f758befea7a5ffa10b65565b6504a30 Mon Sep 17 00:00:00 2001 From: sfb Date: Fri, 28 Sep 2012 07:33:51 -0500 Subject: [PATCH] Changed: #1306 Moved creation of undo command for form array renames to the model. --- .../src/plugins/georges_editor/actions.cpp | 100 +++++++++++------- .../src/plugins/georges_editor/actions.h | 10 +- .../src/plugins/georges_editor/formitem.cpp | 33 +----- .../georges_editor/georgesform_model.cpp | 7 +- .../georges_editor/georgesform_model.h | 5 +- 5 files changed, 80 insertions(+), 75 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 2a7780eb4..a55bc389a 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 @@ -17,9 +17,11 @@ // Project includes #include "actions.h" #include "formitem.h" +#include "georgesform_model.h" // Qt includes + // NeL includes #include #include @@ -32,12 +34,30 @@ 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) - { } + CUndoFormArrayRenameCommand::CUndoFormArrayRenameCommand(CGeorgesFormModel *model, const QModelIndex &index, const QVariant &value, uint elementId, QUndoCommand *parent) + : QUndoCommand(parent), m_model(model), m_elementId(elementId) + { + m_row = index.row(); + m_col = index.column(); + + m_newValue = value.toString(); + } void CUndoFormArrayRenameCommand::redo() { + update(true); + } + + void CUndoFormArrayRenameCommand::undo() + { + update(false); + } + + void CUndoFormArrayRenameCommand::update(bool redo) + { + QModelIndex index = m_model->index(m_row, m_col); + CFormItem *item = m_model->getItem(index); + // Get the parent node const NLGEORGES::CFormDfn *parentDfn; uint indexDfn; @@ -47,13 +67,33 @@ namespace GeorgesQt 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) ); + NLGEORGES::CForm *form=static_cast(item->form()); + if(!form) + { + nlinfo("failed to convert form."); + return; + } + + NLGEORGES::CFormElm *elm = static_cast(&form->Elements); + + if(!elm) + nlwarning("Failed to convert elm!"); + + nlverify ( elm->getNodeByName (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 ()); + std::string tmpName; + node->getFormName(tmpName); + nlinfo("doing array rename on '%s'", tmpName.c_str()); + + NLGEORGES::CFormElmArray* array = static_cast (node->getParent ()); + if(!array) + nlwarning("the array is invalid."); + + // In the redo stage save the old value, just in case. + if(redo) + { + // If the name of the element is empty then give it a nice default. if(array->Elements[m_elementId].Name.empty()) { m_oldValue.append("#"); @@ -61,37 +101,21 @@ namespace GeorgesQt } else { - m_oldValue = array->Elements[m_elementId].Name.c_str(); + m_oldValue = QString::fromStdString(array->Elements[m_elementId].Name); } + } - array->Elements[m_elementId].Name = m_newValue.toStdString(); - m_item->setName(m_newValue.toStdString()); - } - + QString value; + if(redo) + value = m_newValue; + else + value = m_oldValue; + + + array->Elements[m_elementId].Name = value.toStdString(); + item->setName(value.toStdString()); + + m_model->emitDataChanged(index); + } } - - 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 c0e0d3b8e..43a19257d 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 @@ -18,22 +18,28 @@ #define ACTIONS_H #include +#include namespace GeorgesQt { class CFormItem; + class CGeorgesFormModel; class CUndoFormArrayRenameCommand : public QUndoCommand { public: - CUndoFormArrayRenameCommand(CFormItem *item, QString newValue, uint elementId, QUndoCommand *parent = 0); + CUndoFormArrayRenameCommand(CGeorgesFormModel *model, const QModelIndex &index, const QVariant &value, uint elementId, QUndoCommand *parent = 0); ~CUndoFormArrayRenameCommand() {} void redo(); void undo(); + void update(bool redo); + protected: - CFormItem *m_item; + int m_row, m_col; + CGeorgesFormModel *m_model; + QString m_newValue; QString m_oldValue; uint m_elementId; 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 39545afde..f053719c2 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 @@ -84,38 +84,7 @@ namespace GeorgesQt bool CFormItem::setData(int column, const QVariant &value) { - if(isEditable(column)) - { - 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()); - - // 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; - } - } - } - + nlwarning("This should not be called anymore."); return false; } diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.cpp index 9a329f396..b9511231b 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.cpp @@ -42,6 +42,8 @@ // project includes #include "formitem.h" +#include "georges_editor_form.h" +#include "actions.h" using namespace NLGEORGES; @@ -118,12 +120,13 @@ namespace GeorgesQt if(!item->isEditable(index.column())) return false; - bool result = item->setData(index.column(), value); + //bool result = item->setData(index.column(), value); + GeorgesEditorForm::UndoStack->push(new CUndoFormArrayRenameCommand(this,index,value,item->structId())); Q_EMIT dataChanged(index, index); //setupModelData(); - return result; + return true; } /******************************************************************************/ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.h index 493f672dd..6c6c2817d 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/georges_editor/georgesform_model.h @@ -40,7 +40,6 @@ namespace GeorgesQt class CGeorgesFormModel : public QAbstractItemModel { - public: CGeorgesFormModel(NLGEORGES::UForm *form, QMap< QString, QStringList> deps, QString comment, QStringList parents, bool* expanded, QObject *parent = 0); @@ -70,6 +69,10 @@ namespace GeorgesQt CFormItem *addArray(CFormItem *parent, NLGEORGES::CFormElmArray *array, NLGEORGES::CFormDfn *rootDfn, const char *name, uint structId, const char *formName, uint slot); + void emitDataChanged(const QModelIndex &index) + { + Q_EMIT dataChanged(index, index); + } private: void setupModelData(); void loadFormData(NLGEORGES::UFormElm *rootElm, CFormItem *parent);