From 08ae84ed19505533ea86896d77fcfc1526e26bb7 Mon Sep 17 00:00:00 2001 From: dnk-88 Date: Wed, 17 Aug 2011 02:49:32 +0300 Subject: [PATCH] Changed: #1302 Clean up code for undo commands. --- .../world_editor/world_editor_actions.cpp | 403 ++++++------------ .../world_editor/world_editor_actions.h | 91 ++-- 2 files changed, 193 insertions(+), 301 deletions(-) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp index 9a70cebe6..b0a201980 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.cpp @@ -202,17 +202,6 @@ void removeGraphicsItems(const QModelIndex &primIndex, PrimitivesTreeModel *mode } } -QList graphicsItemsToPaths(const QList &items, PrimitivesTreeModel *model) -{ - QList result; - Q_FOREACH(QGraphicsItem *item, items) - { - Node *node = qvariant_cast(item->data(Constants::WORLD_EDITOR_NODE)); - result.push_back(model->pathFromNode(node)); - } - return result; -} - QList polygonsFromItems(const QList &items) { QList result; @@ -224,49 +213,6 @@ QList polygonsFromItems(const QList &items) return result; } -void updateGraphicsData(AbstractWorldItem *item) -{ - float cellSize = Utils::ligoConfig()->CellSize; - Node *node = qvariant_cast(item->data(Constants::WORLD_EDITOR_NODE)); - PrimitiveNode *primitiveNode = static_cast(node); - if (primitiveNode != 0) - { - NLLIGO::IPrimitive *primitive = primitiveNode->primitive(); - - std::vector vPoints; - QPolygonF polygon = item->polygon(); - polygon.translate(item->pos()); - - for (int i = 0; i < polygon.size(); ++i) - { - NLMISC::CVector vec(polygon.at(i).x(), cellSize - polygon.at(i).y(), 0.0); - vPoints.push_back(NLLIGO::CPrimVector(vec)); - } - - switch (primitiveNode->primitiveClass()->Type) - { - case NLLIGO::CPrimitiveClass::Point: - { - NLLIGO::CPrimPoint *point = static_cast(primitive); - point->Point = vPoints.front(); - break; - } - case NLLIGO::CPrimitiveClass::Path: - { - NLLIGO::CPrimPath *path = static_cast(primitive); - path->VPoints = vPoints; - break; - } - case NLLIGO::CPrimitiveClass::Zone: - { - NLLIGO::CPrimZone *zone = static_cast(primitive); - zone->VPoints = vPoints; - break; - } - } - } -} - CreateWorldCommand::CreateWorldCommand(const QString &fileName, PrimitivesTreeModel *model, QUndoCommand *parent) : QUndoCommand(parent), m_fileName(fileName), @@ -484,14 +430,122 @@ void AddPrimitiveByClassCommand::redo() addNewGraphicsItems(m_model->pathToIndex(m_newPrimIndex), m_model, m_scene); } -MoveWorldItemsCommand::MoveWorldItemsCommand(const QList &items, const QPointF &offset, - WorldEditorScene *scene, PrimitivesTreeModel *model, QUndoCommand *parent) +AbstractWorldItemCommand::AbstractWorldItemCommand(const QList &items, + WorldEditorScene *scene, + PrimitivesTreeModel *model, + QUndoCommand *parent) : QUndoCommand(parent), m_listPaths(graphicsItemsToPaths(items, model)), - m_offset(offset), m_model(model), m_scene(scene), m_firstRun(true) +{ +} + +AbstractWorldItemCommand::~AbstractWorldItemCommand() +{ +} + +void AbstractWorldItemCommand::undo() +{ + bool pointsMode = m_scene->isEnabledEditPoints(); + m_scene->setEnabledEditPoints(false); + for (int i = 0; i < m_listPaths.count(); ++i) + { + Node *node = m_model->pathToNode(m_listPaths.at(i)); + AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); + undoChangeItem(i, item); + updatePrimitiveData(item); + } + m_scene->setEnabledEditPoints(pointsMode); +} + +void AbstractWorldItemCommand::redo() +{ + if (!m_firstRun) + { + bool pointsMode = m_scene->isEnabledEditPoints(); + m_scene->setEnabledEditPoints(false); + for (int i = 0; i < m_listPaths.count(); ++i) + { + Node *node = m_model->pathToNode(m_listPaths.at(i)); + AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); + redoChangeItem(i, item); + updatePrimitiveData(item); + } + m_scene->setEnabledEditPoints(pointsMode); + } + else + { + for (int i = 0; i < m_listPaths.count(); ++i) + { + Node *node = m_model->pathToNode(m_listPaths.at(i)); + AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); + updatePrimitiveData(item); + } + } + + m_firstRun = false; +} + +void AbstractWorldItemCommand::updatePrimitiveData(AbstractWorldItem *item) +{ + float cellSize = Utils::ligoConfig()->CellSize; + Node *node = qvariant_cast(item->data(Constants::WORLD_EDITOR_NODE)); + PrimitiveNode *primitiveNode = static_cast(node); + if (primitiveNode != 0) + { + NLLIGO::IPrimitive *primitive = primitiveNode->primitive(); + + std::vector vPoints; + QPolygonF polygon = item->polygon(); + polygon.translate(item->pos()); + + for (int i = 0; i < polygon.size(); ++i) + { + NLMISC::CVector vec(polygon.at(i).x(), cellSize - polygon.at(i).y(), 0.0); + vPoints.push_back(NLLIGO::CPrimVector(vec)); + } + + switch (primitiveNode->primitiveClass()->Type) + { + case NLLIGO::CPrimitiveClass::Point: + { + NLLIGO::CPrimPoint *point = static_cast(primitive); + point->Point = vPoints.front(); + break; + } + case NLLIGO::CPrimitiveClass::Path: + { + NLLIGO::CPrimPath *path = static_cast(primitive); + path->VPoints = vPoints; + break; + } + case NLLIGO::CPrimitiveClass::Zone: + { + NLLIGO::CPrimZone *zone = static_cast(primitive); + zone->VPoints = vPoints; + break; + } + } + } +} + +QList AbstractWorldItemCommand::graphicsItemsToPaths(const QList &items, PrimitivesTreeModel *model) +{ + QList result; + Q_FOREACH(QGraphicsItem *item, items) + { + Node *node = qvariant_cast(item->data(Constants::WORLD_EDITOR_NODE)); + result.push_back(model->pathFromNode(node)); + } + return result; +} + +MoveWorldItemsCommand::MoveWorldItemsCommand(const QList &items, const QPointF &offset, + WorldEditorScene *scene, PrimitivesTreeModel *model, QUndoCommand *parent) + : AbstractWorldItemCommand(items, scene, model, parent), + m_offset(offset) { setText("Move item(s)"); } @@ -500,57 +554,21 @@ MoveWorldItemsCommand::~MoveWorldItemsCommand() { } -void MoveWorldItemsCommand::undo() +void MoveWorldItemsCommand::undoChangeItem(int i, AbstractWorldItem *item) { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->moveBy(-m_offset.x(), -m_offset.y()); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); + item->moveBy(-m_offset.x(), -m_offset.y()); } -void MoveWorldItemsCommand::redo() +void MoveWorldItemsCommand::redoChangeItem(int i, AbstractWorldItem *item) { - if (!m_firstRun) - { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->moveBy(m_offset.x(), m_offset.y()); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); - } - else - { - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - updateGraphicsData(item); - } - } - - m_firstRun = false; + item->moveBy(m_offset.x(), m_offset.y()); } RotateWorldItemsCommand::RotateWorldItemsCommand(const QList &items, const qreal angle, const QPointF &pivot, WorldEditorScene *scene, PrimitivesTreeModel *model, QUndoCommand *parent) - : QUndoCommand(parent), - m_listPaths(graphicsItemsToPaths(items, model)), + : AbstractWorldItemCommand(items, scene, model, parent), m_angle(angle), - m_pivot(pivot), - m_model(model), - m_scene(scene), - m_firstRun(true) + m_pivot(pivot) { setText("Rotate item(s)"); } @@ -559,57 +577,21 @@ RotateWorldItemsCommand::~RotateWorldItemsCommand() { } -void RotateWorldItemsCommand::undo() +void RotateWorldItemsCommand::undoChangeItem(int i, AbstractWorldItem *item) { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->rotateOn(m_pivot, -m_angle); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); + item->rotateOn(m_pivot, -m_angle); } -void RotateWorldItemsCommand::redo() +void RotateWorldItemsCommand::redoChangeItem(int i, AbstractWorldItem *item) { - if (!m_firstRun) - { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->rotateOn(m_pivot, m_angle); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); - } - else - { - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - updateGraphicsData(item); - } - } - - m_firstRun = false; + item->rotateOn(m_pivot, m_angle); } ScaleWorldItemsCommand::ScaleWorldItemsCommand(const QList &items, const QPointF &factor, const QPointF &pivot, WorldEditorScene *scene, PrimitivesTreeModel *model, QUndoCommand *parent) - : QUndoCommand(parent), - m_listPaths(graphicsItemsToPaths(items, model)), + : AbstractWorldItemCommand(items, scene, model, parent), m_factor(factor), - m_pivot(pivot), - m_model(model), - m_scene(scene), - m_firstRun(true) + m_pivot(pivot) { setText("Scale item(s)"); } @@ -618,57 +600,21 @@ ScaleWorldItemsCommand::~ScaleWorldItemsCommand() { } -void ScaleWorldItemsCommand::undo() +void ScaleWorldItemsCommand::undoChangeItem(int i, AbstractWorldItem *item) { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); QPointF m_invertFactor(1 / m_factor.x(), 1 / m_factor.y()); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->scaleOn(m_pivot, m_invertFactor); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); + item->scaleOn(m_pivot, m_invertFactor); } -void ScaleWorldItemsCommand::redo() +void ScaleWorldItemsCommand::redoChangeItem(int i, AbstractWorldItem *item) { - if (!m_firstRun) - { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->scaleOn(m_pivot, m_factor); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); - } - else - { - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - updateGraphicsData(item); - } - } - - m_firstRun = false; + item->scaleOn(m_pivot, m_factor); } TurnWorldItemsCommand::TurnWorldItemsCommand(const QList &items, const qreal angle, WorldEditorScene *scene, PrimitivesTreeModel *model, QUndoCommand *parent) - : QUndoCommand(parent), - m_listPaths(graphicsItemsToPaths(items, model)), - m_angle(angle), - m_model(model), - m_scene(scene), - m_firstRun(true) + : AbstractWorldItemCommand(items, scene, model, parent), + m_angle(angle) { setText("Turn item(s)"); } @@ -677,58 +623,22 @@ TurnWorldItemsCommand::~TurnWorldItemsCommand() { } -void TurnWorldItemsCommand::undo() +void TurnWorldItemsCommand::undoChangeItem(int i, AbstractWorldItem *item) { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->turnOn(-m_angle); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); + item->turnOn(-m_angle); } -void TurnWorldItemsCommand::redo() +void TurnWorldItemsCommand::redoChangeItem(int i, AbstractWorldItem *item) { - if (!m_firstRun) - { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->turnOn(m_angle); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); - } - else - { - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - updateGraphicsData(item); - } - } - - m_firstRun = false; + item->turnOn(m_angle); } ShapeWorldItemsCommand::ShapeWorldItemsCommand(const QList &items, const QList &polygons, WorldEditorScene *scene, PrimitivesTreeModel *model, QUndoCommand *parent) - : QUndoCommand(parent), - m_listPaths(graphicsItemsToPaths(items, model)), + : AbstractWorldItemCommand(items, scene, model, parent), m_redoPolygons(polygons), - m_undoPolygons(polygonsFromItems(items)), - m_model(model), - m_scene(scene), - m_firstRun(true) + m_undoPolygons(polygonsFromItems(items)) { setText("Change shape"); } @@ -737,49 +647,14 @@ ShapeWorldItemsCommand::~ShapeWorldItemsCommand() { } -void ShapeWorldItemsCommand::undo() +void ShapeWorldItemsCommand::undoChangeItem(int i, AbstractWorldItem *item) { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->setPolygon(m_redoPolygons.at(i)); - updateGraphicsData(item); - } - - m_scene->setEnabledEditPoints(pointsMode); + item->setPolygon(m_redoPolygons.at(i)); } -void ShapeWorldItemsCommand::redo() +void ShapeWorldItemsCommand::redoChangeItem(int i, AbstractWorldItem *item) { - if (!m_firstRun) - { - bool pointsMode = m_scene->isEnabledEditPoints(); - m_scene->setEnabledEditPoints(false); - - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - item->setPolygon(m_undoPolygons.at(i)); - updateGraphicsData(item); - } - m_scene->setEnabledEditPoints(pointsMode); - } - else - { - for (int i = 0; i < m_listPaths.count(); ++i) - { - Node *node = m_model->pathToNode(m_listPaths.at(i)); - AbstractWorldItem *item = qvariant_cast(node->data(Constants::GRAPHICS_DATA_QT4_2D)); - updateGraphicsData(item); - } - } - - m_firstRun = false; + item->setPolygon(m_undoPolygons.at(i)); } } /* namespace WorldEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.h index ad66d0e3d..17f19fbca 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/world_editor/world_editor_actions.h @@ -49,11 +49,9 @@ void addNewGraphicsItems(const QModelIndex &primIndex, PrimitivesTreeModel *mode // Recursive scan primitives model for delete Graphics Items void removeGraphicsItems(const QModelIndex &primIndex, PrimitivesTreeModel *model, WorldEditorScene *scene); -QList graphicsItemsToPaths(const QList &items, PrimitivesTreeModel *model); QList polygonsFromItems(const QList &items); -void updateGraphicsData(AbstractWorldItem *item); /** @class CreateWorldCommand @@ -163,12 +161,41 @@ private: QTreeView *m_view; }; +/** +@class AbstractWorldItemCommand +@brief +@details +*/ +class AbstractWorldItemCommand: public QUndoCommand +{ +public: + AbstractWorldItemCommand(const QList &items, WorldEditorScene *scene, + PrimitivesTreeModel *model, QUndoCommand *parent = 0); + virtual ~AbstractWorldItemCommand(); + + virtual void undo(); + virtual void redo(); + +protected: + virtual void undoChangeItem(int i, AbstractWorldItem *item) = 0; + virtual void redoChangeItem(int i, AbstractWorldItem *item) = 0; + void updatePrimitiveData(AbstractWorldItem *item); + +private: + QList graphicsItemsToPaths(const QList &items, PrimitivesTreeModel *model); + + const QList m_listPaths; + PrimitivesTreeModel *const m_model; + WorldEditorScene *m_scene; + bool m_firstRun; +}; + /** @class MoveWorldItemsCommand @brief @details */ -class MoveWorldItemsCommand: public QUndoCommand +class MoveWorldItemsCommand: public AbstractWorldItemCommand { public: MoveWorldItemsCommand(const QList &items, const QPointF &offset, @@ -176,15 +203,13 @@ public: QUndoCommand *parent = 0); virtual ~MoveWorldItemsCommand(); - virtual void undo(); - virtual void redo(); +protected: + virtual void undoChangeItem(int i, AbstractWorldItem *item); + virtual void redoChangeItem(int i, AbstractWorldItem *item); + private: - const QList m_listPaths; const QPointF m_offset; - PrimitivesTreeModel *const m_model; - WorldEditorScene *m_scene; - bool m_firstRun; }; /** @@ -192,7 +217,7 @@ private: @brief @details */ -class RotateWorldItemsCommand: public QUndoCommand +class RotateWorldItemsCommand: public AbstractWorldItemCommand { public: RotateWorldItemsCommand(const QList &items, const qreal angle, @@ -200,16 +225,14 @@ public: PrimitivesTreeModel *model, QUndoCommand *parent = 0); virtual ~RotateWorldItemsCommand(); - virtual void undo(); - virtual void redo(); +protected: + virtual void undoChangeItem(int i, AbstractWorldItem *item); + virtual void redoChangeItem(int i, AbstractWorldItem *item); + private: - const QList m_listPaths; const qreal m_angle; const QPointF m_pivot; - PrimitivesTreeModel *const m_model; - WorldEditorScene *m_scene; - bool m_firstRun; }; /** @@ -217,7 +240,7 @@ private: @brief @details */ -class ScaleWorldItemsCommand: public QUndoCommand +class ScaleWorldItemsCommand: public AbstractWorldItemCommand { public: ScaleWorldItemsCommand(const QList &items, const QPointF &factor, @@ -225,16 +248,14 @@ public: PrimitivesTreeModel *model, QUndoCommand *parent = 0); virtual ~ScaleWorldItemsCommand(); - virtual void undo(); - virtual void redo(); +protected: + virtual void undoChangeItem(int i, AbstractWorldItem *item); + virtual void redoChangeItem(int i, AbstractWorldItem *item); + private: - const QList m_listPaths; const QPointF m_factor; const QPointF m_pivot; - PrimitivesTreeModel *const m_model; - WorldEditorScene *m_scene; - bool m_firstRun; }; /** @@ -242,7 +263,7 @@ private: @brief @details */ -class TurnWorldItemsCommand: public QUndoCommand +class TurnWorldItemsCommand: public AbstractWorldItemCommand { public: TurnWorldItemsCommand(const QList &items, const qreal angle, @@ -250,15 +271,13 @@ public: QUndoCommand *parent = 0); virtual ~TurnWorldItemsCommand(); - virtual void undo(); - virtual void redo(); +protected: + virtual void undoChangeItem(int i, AbstractWorldItem *item); + virtual void redoChangeItem(int i, AbstractWorldItem *item); + private: - const QList m_listPaths; const qreal m_angle; - PrimitivesTreeModel *const m_model; - WorldEditorScene *m_scene; - bool m_firstRun; }; /** @@ -266,7 +285,7 @@ private: @brief @details */ -class ShapeWorldItemsCommand: public QUndoCommand +class ShapeWorldItemsCommand: public AbstractWorldItemCommand { public: ShapeWorldItemsCommand(const QList &items, const QList &polygons, @@ -274,16 +293,14 @@ public: QUndoCommand *parent = 0); virtual ~ShapeWorldItemsCommand(); - virtual void undo(); - virtual void redo(); +protected: + virtual void undoChangeItem(int i, AbstractWorldItem *item); + virtual void redoChangeItem(int i, AbstractWorldItem *item); + private: - const QList m_listPaths; const QList m_redoPolygons; const QList m_undoPolygons; - PrimitivesTreeModel *const m_model; - WorldEditorScene *m_scene; - bool m_firstRun; }; } /* namespace WorldEditor */