From 02e34cd7f8e2a8667ba46472b2f3bcf69aff8f2a Mon Sep 17 00:00:00 2001 From: dnk-88 Date: Tue, 14 Jun 2011 12:26:41 +0300 Subject: [PATCH] Changed: #1301 Experiments with undo\redo and QGraphicsScene. --HG-- branch : gsoc2011-worldeditorqt --- .../plugins/landscape_editor/builder_zone.h | 29 +++++++++++ .../landscape_editor/landscape_actions.cpp | 31 ++++++++++++ .../landscape_editor/landscape_actions.h | 22 ++++++++ .../landscape_editor_window.cpp | 6 +-- .../landscape_editor_window.ui | 8 +++ .../landscape_editor/landscape_scene.cpp | 50 ++++++++++++++++++- .../landscape_editor/landscape_scene.h | 19 ++++++- .../landscape_editor/landscape_view.cpp | 43 ++++++++++++---- .../plugins/landscape_editor/landscape_view.h | 6 ++- .../landscape_editor/list_zones_widget.cpp | 12 ++++- .../landscape_editor/list_zones_widget.h | 3 +- 11 files changed, 209 insertions(+), 20 deletions(-) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/builder_zone.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/builder_zone.h index f43bb7d7e..0ccbf4e23 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/builder_zone.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/builder_zone.h @@ -36,6 +36,35 @@ namespace LandscapeEditor { +// Data +struct LigoData +{ + uint32 PosX; + uint32 PosY; + qreal Scale; + uint8 Rot; + uint8 Flip; + std::string ZoneName; + std::string SharingMatNames[4]; + uint8 SharingCutEdges[4]; + bool operator!= (const LigoData& other) const + { + return (PosX != other.PosX) || + (PosY != other.PosY) || + (Rot != other.Rot) || + (Flip != other.Flip) || + (ZoneName != other.ZoneName) || + (SharingMatNames[0] != other.SharingMatNames[0]) || + (SharingMatNames[1] != other.SharingMatNames[1]) || + (SharingMatNames[2] != other.SharingMatNames[2]) || + (SharingMatNames[3] != other.SharingMatNames[3]) || + (SharingCutEdges[0] != other.SharingCutEdges[0]) || + (SharingCutEdges[1] != other.SharingCutEdges[1]) || + (SharingCutEdges[2] != other.SharingCutEdges[2]) || + (SharingCutEdges[3] != other.SharingCutEdges[3]); + } +}; + /** @class PixmapDatabase @brief PixmapDatabase contains the image database diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.cpp index f0b86dd8e..14cdb4f4b 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.cpp @@ -17,6 +17,7 @@ // Project includes #include "landscape_actions.h" +#include "builder_zone.h" // NeL includes #include @@ -25,4 +26,34 @@ namespace LandscapeEditor { + +ActionLigoTile::ActionLigoTile(const LigoData &data, ZoneBuilder *zoneBuilder, QGraphicsScene *scene, QUndoCommand *parent) + : QUndoCommand(parent), + m_item(0), + m_zoneBuilder(zoneBuilder), + m_scene(scene) +{ + m_ligoData = data; +} + +ActionLigoTile::~ActionLigoTile() +{ +} + +void ActionLigoTile::undo() +{ + m_scene->removeItem(m_item); + delete m_item; + m_item = 0; +} + +void ActionLigoTile::redo() +{ + QPixmap *pixmap = m_zoneBuilder->pixmapDatabase()->pixmap(QString(m_ligoData.ZoneName.c_str())); + m_item = new QGraphicsPixmapItem(*pixmap, 0, m_scene); + m_item->setPos(m_ligoData.PosX, m_ligoData.PosY); + m_item->setScale(m_ligoData.Scale); + setText(QObject::tr("Add tile(%1, %2)").arg(m_ligoData.PosX).arg(m_ligoData.PosY)); +} + } /* namespace LandscapeEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.h index fdcda5451..76cb92762 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_actions.h @@ -19,13 +19,35 @@ #define LANDSCAPE_ACTIONS_H // Project includes +#include "builder_zone.h" // NeL includes // Qt includes +#include +#include +#include namespace LandscapeEditor { +class ZoneBuilder; + +class ActionLigoTile : public QUndoCommand +{ +public: + ActionLigoTile(const LigoData &data, ZoneBuilder *zoneBuilder, QGraphicsScene *scene, QUndoCommand *parent = 0); + ~ActionLigoTile(); + + virtual void undo(); + virtual void redo(); + +private: + + LigoData m_ligoData; + QGraphicsPixmapItem *m_item; + ZoneBuilder *m_zoneBuilder; + QGraphicsScene *m_scene; +}; } /* namespace LandscapeEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.cpp index c17de1f43..90c712e6c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.cpp @@ -33,7 +33,6 @@ #include #include #include -#include namespace LandscapeEditor { @@ -50,10 +49,9 @@ LandscapeEditorWindow::LandscapeEditorWindow(QWidget *parent) m_ui.zoneListWidget->setZoneBuilder(m_zoneBuilder); m_ui.zoneListWidget->updateUi(); - m_landscapeScene = new LandscapeScene(this); + m_landscapeScene = new LandscapeScene(m_undoStack, m_ui.zoneListWidget, m_zoneBuilder, this); m_ui.graphicsView->setScene(m_landscapeScene); - m_ui.graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::SampleBuffers))); - + //m_ui.graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::DoubleBuffer | QGL::SampleBuffers))); createMenus(); createToolBars(); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.ui b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.ui index 1361cd2bc..bb14576a2 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.ui +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_editor_window.ui @@ -27,6 +27,14 @@ + + + 0.000000000000000 + 0.000000000000000 + 99999.000000000000000 + 99999.000000000000000 + + QGraphicsView::NoDrag diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.cpp index f491dfa2b..993b6ec9c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.cpp @@ -17,22 +17,68 @@ // Project includes #include "landscape_scene.h" +#include "builder_zone.h" +#include "landscape_actions.h" +#include "list_zones_widget.h" // NeL includes #include // Qt includes +#include +#include namespace LandscapeEditor { -LandscapeScene::LandscapeScene(QObject *parent) - : QGraphicsScene(parent) +LandscapeScene::LandscapeScene(QUndoStack *undoStack, ListZonesWidget *listZonesWidget, ZoneBuilder *zoneBuilder, QObject *parent) + : QGraphicsScene(parent), + m_undoStack(undoStack), + m_listZonesWidget(listZonesWidget), + m_zoneBuilder(zoneBuilder) { + m_cellSize = 160; + createBackgroundPixmap(); } LandscapeScene::~LandscapeScene() { } +void LandscapeScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) +{ + if (mouseEvent->button() != Qt::LeftButton) + return; + + qreal x = mouseEvent->scenePos().rx(); + qreal y = mouseEvent->scenePos().ry(); + if ((x < 0) || (y < 0)) + return; + + LigoData ligoData = m_listZonesWidget->currentLigoData(); + if (ligoData.ZoneName == "") + return; + + ligoData.PosX = m_cellSize * int(x / m_cellSize);; + ligoData.PosY = m_cellSize * int(y / m_cellSize); + ligoData.Scale = m_cellSize / 256.0; + + ActionLigoTile *action = new ActionLigoTile(ligoData, m_zoneBuilder, this); + m_undoStack->push(action); + + QGraphicsScene::mousePressEvent(mouseEvent); +} + +void LandscapeScene::createBackgroundPixmap() +{ + QPixmap pixmap(QSize(m_cellSize, m_cellSize)); + QPainter painter(&pixmap); + //painter.setRenderHint(QPainter::Antialiasing, true); + painter.setBrush(QBrush(Qt::lightGray)); + painter.setPen(QPen(Qt::black, 3, Qt::DotLine)); + painter.drawRect(0, 0, pixmap.width(), pixmap.height()); + + setBackgroundBrush(pixmap); +} + } /* namespace LandscapeEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.h index d4fb91c78..b4a7a68de 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_scene.h @@ -23,18 +23,33 @@ // NeL includes // Qt includes -#include +#include +#include +#include namespace LandscapeEditor { +class ZoneBuilder; +class ListZonesWidget; class LandscapeScene : public QGraphicsScene { Q_OBJECT public: - LandscapeScene(QObject *parent = 0); + LandscapeScene(QUndoStack *undoStack, ListZonesWidget *listZonesWidget, ZoneBuilder *zoneBuilder, QObject *parent = 0); virtual ~LandscapeScene(); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent); + +private: + void createBackgroundPixmap(); + + int m_cellSize; + ListZonesWidget *m_listZonesWidget; + QUndoStack *m_undoStack; + ZoneBuilder *m_zoneBuilder; }; } /* namespace LandscapeEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.cpp index 79b33599e..4bd202b7c 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.cpp @@ -26,27 +26,52 @@ #include // Qt includes - +#include namespace LandscapeEditor { LandscapeView::LandscapeView(QWidget *parent) - : QGraphicsView(parent) + : QGraphicsView(parent), + m_moveMouse(false) { - setDragMode(ScrollHandDrag); + setDragMode(ScrollHandDrag); + setTransformationAnchor(AnchorUnderMouse); } LandscapeView::~LandscapeView() { } -void LandscapeView::wheelEvent(QWheelEvent *event) -{ - double numDegrees = event->delta() / 8.0; - double numSteps = numDegrees / 15.0; - double factor = std::pow(1.125, numSteps); - scale(factor, factor); +void LandscapeView::wheelEvent(QWheelEvent *event) +{ + double numDegrees = event->delta() / 8.0; + double numSteps = numDegrees / 15.0; + double factor = std::pow(1.125, numSteps); + scale(factor, factor); +} + +void LandscapeView::mousePressEvent(QMouseEvent *event) +{ + QGraphicsView::mousePressEvent(event); + if (event->button() != Qt::MiddleButton) + return; + m_moveMouse = true; + QApplication::setOverrideCursor(Qt::ClosedHandCursor); +} + +void LandscapeView::mouseMoveEvent(QMouseEvent *event) +{ + if (m_moveMouse) + translate(0.001, 0.001); + QGraphicsView::mouseMoveEvent(event); +} + +void LandscapeView::mouseReleaseEvent(QMouseEvent *event) +{ + QApplication::restoreOverrideCursor(); + m_moveMouse = false; + QGraphicsView::mouseReleaseEvent(event); } } /* namespace LandscapeEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.h index db79b79ff..4ac090af6 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/landscape_view.h @@ -36,12 +36,16 @@ public: ~LandscapeView(); protected: - void wheelEvent(QWheelEvent *event); + virtual void wheelEvent(QWheelEvent *event); + virtual void mousePressEvent(QMouseEvent *event); + virtual void mouseMoveEvent(QMouseEvent *event); + virtual void mouseReleaseEvent(QMouseEvent *event); private Q_SLOTS: private: + bool m_moveMouse; }; /* class LandscapeView */ } /* namespace LandscapeEditor */ diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.cpp index e4e3fae0d..56e362181 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.cpp @@ -17,7 +17,6 @@ // Project includes #include "list_zones_widget.h" -#include "builder_zone.h" #include "list_zones_model.h" // NeL includes @@ -31,6 +30,7 @@ // Qt includes #include +#include namespace LandscapeEditor { @@ -100,6 +100,16 @@ void ListZonesWidget::updateUi() m_listZonesModel->rebuildModel(m_zoneBuilder->pixmapDatabase()); } +LigoData ListZonesWidget::currentLigoData() const +{ + LigoData ligoData; + ligoData.ZoneName = ""; + QModelIndex index = m_ui.listView->currentIndex(); + if (index.isValid()) + ligoData.ZoneName = index.data().toString().toStdString(); + return ligoData; +} + void ListZonesWidget::setZoneBuilder(ZoneBuilder *zoneBuilder) { m_zoneBuilder = zoneBuilder; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.h index 049f0ebcb..b1a6525fc 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/landscape_editor/list_zones_widget.h @@ -20,6 +20,7 @@ // Project includes #include "ui_list_zones_widget.h" +#include "builder_zone.h" // NeL includes @@ -27,7 +28,6 @@ namespace LandscapeEditor { -class ZoneBuilder; class ListZonesModel; /** @@ -45,6 +45,7 @@ public: void updateUi(); void setZoneBuilder(ZoneBuilder *zoneBuilder); + LigoData currentLigoData() const; Q_SIGNALS: private Q_SLOTS: