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
#include "actions.h"
#include "formitem.h"
#include "georgesform_model.h"
// Qt includes
// NeL includes
#include <nel/misc/debug.h>
#include <nel/misc/file.h>
@ -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<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) );
NLGEORGES::CForm *form=static_cast<NLGEORGES::CForm*>(item->form());
if(!form)
{
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)
{
nlinfo("doing array rename");
NLGEORGES::CFormElmArray* array = NLMISC::safe_cast<NLGEORGES::CFormElmArray*> (node->getParent ());
std::string tmpName;
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())
{
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;
}
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<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());
}
array->Elements[m_elementId].Name = value.toStdString();
item->setName(value.toStdString());
m_model->emitDataChanged(index);
}
}
}

View file

@ -18,22 +18,28 @@
#define ACTIONS_H
#include <QtGui/QUndoCommand>
#include <QModelIndex>
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;

View file

@ -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<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;
}
}
}
nlwarning("This should not be called anymore.");
return false;
}

View file

@ -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;
}
/******************************************************************************/

View file

@ -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);