Added: #1306 Added undo/redo action for renaming array entry names

This commit is contained in:
sfb 2012-09-27 08:22:50 -05:00
parent 51705fd800
commit 02a9f4d920
6 changed files with 148 additions and 30 deletions

View file

@ -14,4 +14,84 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "actions.h"
// Project includes
#include "actions.h"
#include "formitem.h"
// Qt includes
// NeL includes
#include <nel/misc/debug.h>
#include <nel/misc/file.h>
#include <nel/misc/o_xml.h>
#include <nel/georges/u_form_loader.h>
#include <nel/georges/form.h>
#include <nel/georges/u_form.h>
#include <nel/georges/u_type.h>
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<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)
{
nlinfo("doing array rename");
NLGEORGES::CFormElmArray* array = NLMISC::safe_cast<NLGEORGES::CFormElmArray*> (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<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());
}
}
}

View file

@ -17,4 +17,27 @@
#ifndef ACTIONS_H
#define ACTIONS_H
#include <QtGui/QUndoCommand>
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

View file

@ -14,7 +14,10 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Project includes
#include "formitem.h"
#include "actions.h"
#include "georges_editor_form.h"
// Qt includes
#include <QIcon>
@ -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<NLGEORGES::CForm*>(m_form);
NLGEORGES::CFormElm *elm = static_cast<CFormElm*>(&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<NLGEORGES::CForm*>(m_form);
NLGEORGES::CFormElm *elm = static_cast<CFormElm*>(&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)

View file

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

View file

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

View file

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