Changed: #1306 Moved creation of undo command for form array renames to the model.

This commit is contained in:
sfb 2012-09-28 07:33:51 -05:00
parent 02a9f4d920
commit e918468ef8
5 changed files with 80 additions and 75 deletions

View file

@ -17,9 +17,11 @@
// Project includes // Project includes
#include "actions.h" #include "actions.h"
#include "formitem.h" #include "formitem.h"
#include "georgesform_model.h"
// Qt includes // Qt includes
// NeL includes // NeL includes
#include <nel/misc/debug.h> #include <nel/misc/debug.h>
#include <nel/misc/file.h> #include <nel/misc/file.h>
@ -32,12 +34,30 @@
namespace GeorgesQt namespace GeorgesQt
{ {
CUndoFormArrayRenameCommand::CUndoFormArrayRenameCommand(CFormItem *item, QString newValue, uint elementId, QUndoCommand *parent) CUndoFormArrayRenameCommand::CUndoFormArrayRenameCommand(CGeorgesFormModel *model, const QModelIndex &index, const QVariant &value, uint elementId, QUndoCommand *parent)
: QUndoCommand("Rename Form Array", parent), m_item(item), m_newValue(newValue), m_elementId(elementId) : QUndoCommand(parent), m_model(model), m_elementId(elementId)
{ } {
m_row = index.row();
m_col = index.column();
m_newValue = value.toString();
}
void CUndoFormArrayRenameCommand::redo() 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 // Get the parent node
const NLGEORGES::CFormDfn *parentDfn; const NLGEORGES::CFormDfn *parentDfn;
uint indexDfn; uint indexDfn;
@ -47,13 +67,33 @@ namespace GeorgesQt
NLGEORGES::UFormDfn::TEntryType type; NLGEORGES::UFormDfn::TEntryType type;
bool isArray; bool isArray;
bool vdfnArray; bool vdfnArray;
NLGEORGES::CForm *form=static_cast<NLGEORGES::CForm*>(m_item->form()); NLGEORGES::CForm *form=static_cast<NLGEORGES::CForm*>(item->form());
NLGEORGES::CFormElm *elm = static_cast<NLGEORGES::CFormElm*>(&form->getRootNode()); if(!form)
nlverify ( elm->getNodeByName (m_item->formName().c_str (), &parentDfn, indexDfn, &nodeDfn, &nodeType, &node, type, isArray, vdfnArray, true, NLGEORGES_FIRST_ROUND) ); {
nlinfo("failed to convert form.");
return;
}
NLGEORGES::CFormElm *elm = static_cast<NLGEORGES::CFormElm*>(&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) if (node)
{ {
nlinfo("doing array rename"); std::string tmpName;
NLGEORGES::CFormElmArray* array = NLMISC::safe_cast<NLGEORGES::CFormElmArray*> (node->getParent ()); node->getFormName(tmpName);
nlinfo("doing array rename on '%s'", tmpName.c_str());
NLGEORGES::CFormElmArray* array = static_cast<NLGEORGES::CFormElmArray*> (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()) if(array->Elements[m_elementId].Name.empty())
{ {
m_oldValue.append("#"); m_oldValue.append("#");
@ -61,37 +101,21 @@ namespace GeorgesQt
} }
else 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(); QString value;
m_item->setName(m_newValue.toStdString()); if(redo)
} value = m_newValue;
else
value = m_oldValue;
}
void CUndoFormArrayRenameCommand::undo() array->Elements[m_elementId].Name = value.toStdString();
{ item->setName(value.toStdString());
// 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<NLGEORGES::CForm*>(m_item->form());
NLGEORGES::CFormElm *elm = static_cast<NLGEORGES::CFormElm*>(&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<NLGEORGES::CFormElmArray*> (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());
}
m_model->emitDataChanged(index);
}
} }
} }

View file

@ -18,22 +18,28 @@
#define ACTIONS_H #define ACTIONS_H
#include <QtGui/QUndoCommand> #include <QtGui/QUndoCommand>
#include <QModelIndex>
namespace GeorgesQt namespace GeorgesQt
{ {
class CFormItem; class CFormItem;
class CGeorgesFormModel;
class CUndoFormArrayRenameCommand : public QUndoCommand class CUndoFormArrayRenameCommand : public QUndoCommand
{ {
public: 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() {} ~CUndoFormArrayRenameCommand() {}
void redo(); void redo();
void undo(); void undo();
void update(bool redo);
protected: protected:
CFormItem *m_item; int m_row, m_col;
CGeorgesFormModel *m_model;
QString m_newValue; QString m_newValue;
QString m_oldValue; QString m_oldValue;
uint m_elementId; uint m_elementId;

View file

@ -84,38 +84,7 @@ namespace GeorgesQt
bool CFormItem::setData(int column, const QVariant &value) bool CFormItem::setData(int column, const QVariant &value)
{ {
if(isEditable(column)) nlwarning("This should not be called anymore.");
{
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<NLGEORGES::CForm*>(m_form);
NLGEORGES::CFormElm *elm = static_cast<CFormElm*>(&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;
}
}
}
return false; return false;
} }

View file

@ -42,6 +42,8 @@
// project includes // project includes
#include "formitem.h" #include "formitem.h"
#include "georges_editor_form.h"
#include "actions.h"
using namespace NLGEORGES; using namespace NLGEORGES;
@ -118,12 +120,13 @@ namespace GeorgesQt
if(!item->isEditable(index.column())) if(!item->isEditable(index.column()))
return false; 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); Q_EMIT dataChanged(index, index);
//setupModelData(); //setupModelData();
return result; return true;
} }
/******************************************************************************/ /******************************************************************************/

View file

@ -40,7 +40,6 @@ namespace GeorgesQt
class CGeorgesFormModel : public QAbstractItemModel class CGeorgesFormModel : public QAbstractItemModel
{ {
public: public:
CGeorgesFormModel(NLGEORGES::UForm *form, QMap< QString, QStringList> deps, CGeorgesFormModel(NLGEORGES::UForm *form, QMap< QString, QStringList> deps,
QString comment, QStringList parents, bool* expanded, QObject *parent = 0); 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, CFormItem *addArray(CFormItem *parent, NLGEORGES::CFormElmArray *array, NLGEORGES::CFormDfn *rootDfn,
const char *name, uint structId, const char *formName, uint slot); const char *name, uint structId, const char *formName, uint slot);
void emitDataChanged(const QModelIndex &index)
{
Q_EMIT dataChanged(index, index);
}
private: private:
void setupModelData(); void setupModelData();
void loadFormData(NLGEORGES::UFormElm *rootElm, CFormItem *parent); void loadFormData(NLGEORGES::UFormElm *rootElm, CFormItem *parent);