diff options
author | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-07 13:05:23 +0100 |
---|---|---|
committer | Lukáš Lalinský <lalinsky@gmail.com> | 2008-12-07 13:05:23 +0100 |
commit | 9e917b7f88963a7996d5d09c99e4ca41bde60e56 (patch) | |
tree | ac47448a6b4da902380a29c167537f39c0c89521 | |
parent | 752a4031c7dfdc9e2bcf8684818d970c647c6920 (diff) | |
download | dbmodel-9e917b7f88963a7996d5d09c99e4ca41bde60e56.tar.xz |
Make item moves undo-able
-rw-r--r-- | src/commands.cpp | 18 | ||||
-rw-r--r-- | src/commands.h | 13 | ||||
-rw-r--r-- | src/databaserelationship.cpp | 1 | ||||
-rw-r--r-- | src/databasetable.cpp | 7 | ||||
-rw-r--r-- | src/diagramdocument.cpp | 28 | ||||
-rw-r--r-- | src/diagramdocument.h | 9 |
6 files changed, 73 insertions, 3 deletions
diff --git a/src/commands.cpp b/src/commands.cpp index ca23345..4930ac8 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -125,3 +125,21 @@ SwapColumnsCommand::undo() { m_columnList->swapColumns(m_newIndex, m_oldIndex); } + + +MoveItemCommand::MoveItemCommand(DiagramItem *item, const QPointF &oldPos, const QPointF &newPos, QUndoCommand *parent) + : QUndoCommand(parent), m_item(item), m_oldPos(oldPos), m_newPos(newPos) +{ +} + +void +MoveItemCommand::redo() +{ + m_item->setPos(m_newPos); +} + +void +MoveItemCommand::undo() +{ + m_item->setPos(m_oldPos); +} diff --git a/src/commands.h b/src/commands.h index 53fc947..142b62c 100644 --- a/src/commands.h +++ b/src/commands.h @@ -19,6 +19,7 @@ #include <QUndoCommand> #include <QVariant> +#include <QPointer> #include <QLatin1String> class DatabaseTable; @@ -85,4 +86,16 @@ private: int m_oldIndex, m_newIndex; }; +class MoveItemCommand : public QUndoCommand +{ +public: + MoveItemCommand(DiagramItem *item, const QPointF &oldPos, const QPointF &newPos, QUndoCommand *parent = 0); + void undo(); + void redo(); + +private: + QPointer<DiagramItem> m_item; + QPointF m_oldPos, m_newPos; +}; + #endif diff --git a/src/databaserelationship.cpp b/src/databaserelationship.cpp index d7c3dd1..f6555c0 100644 --- a/src/databaserelationship.cpp +++ b/src/databaserelationship.cpp @@ -81,6 +81,7 @@ void DatabaseRelationship::updatePositions() { updateLayout(); + update(); } void diff --git a/src/databasetable.cpp b/src/databasetable.cpp index 968a3f2..3468b8e 100644 --- a/src/databasetable.cpp +++ b/src/databasetable.cpp @@ -117,9 +117,14 @@ DatabaseTable::itemChange(GraphicsItemChange change, const QVariant &value) if (change == ItemPositionChange) { DiagramDocument *model = qobject_cast<DiagramDocument *>(scene()); if (model) { - emit model->tableMoved(this); + emit model->itemMoved(this); } } + if (change == ItemPositionHasChanged) { + DiagramDocument *model = qobject_cast<DiagramDocument *>(scene()); + if (model) + emit model->itemHasMoved(this); + } return QGraphicsItem::itemChange(change, value); } diff --git a/src/diagramdocument.cpp b/src/diagramdocument.cpp index 573d08c..fe8c53f 100644 --- a/src/diagramdocument.cpp +++ b/src/diagramdocument.cpp @@ -18,6 +18,7 @@ #include "databasetable.h" #include "databaserelationship.h" #include "column.h" +#include "commands.h" #include <QGraphicsItem> #include <QDebug> #include <QFile> @@ -27,7 +28,7 @@ DiagramDocument::DiagramDocument(QObject *parent) : QGraphicsScene(parent), m_mode(DiagramDocument::Select), m_line(NULL) { m_undoStack = new QUndoStack(this); - connect(this, SIGNAL(tableMoved(DatabaseTable *)), SLOT(updatePositions(DatabaseTable *))); + //connect(this, SIGNAL(tableMoved(DatabaseTable *)), SLOT(updatePositions(DatabaseTable *))); } DiagramDocument::Mode @@ -52,8 +53,26 @@ DiagramDocument::updatePositions(DatabaseTable *table) } void +DiagramDocument::itemMoved(DiagramItem *item) +{ + if (m_trackingMoves) { + if (!m_movedItems.contains(item)) { + m_movedItems[item] = item->pos(); + } + } +} + +void +DiagramDocument::itemHasMoved(DiagramItem *item) +{ + updatePositions(static_cast<DatabaseTable*>(item)); +} + +void DiagramDocument::mousePressEvent(QGraphicsSceneMouseEvent *event) { + m_movedItems.clear(); + m_trackingMoves = true; if (m_mode == AddTable && event->button() == Qt::LeftButton) { DatabaseTable *table = new DatabaseTable(); table->setPos(event->scenePos()); @@ -112,6 +131,13 @@ DiagramDocument::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) return; } QGraphicsScene::mouseReleaseEvent(event); + m_trackingMoves = false; + + QMapIterator<DiagramItem *, QPointF> i(m_movedItems); + while (i.hasNext()) { + i.next(); + undoStack()->push(new MoveItemCommand(i.key(), i.value(), i.key()->pos())); + } } DatabaseTable * diff --git a/src/diagramdocument.h b/src/diagramdocument.h index b7890f6..c97abcb 100644 --- a/src/diagramdocument.h +++ b/src/diagramdocument.h @@ -22,9 +22,11 @@ #include <QGraphicsSceneMouseEvent> #include <QDomDocument> #include <QGraphicsLineItem> +#include <QSet> class DatabaseTable; class DatabaseRelationship; +class DiagramItem; class DiagramDocument : public QGraphicsScene { @@ -54,9 +56,11 @@ public: QUndoStack *undoStack() const { return m_undoStack; } + void itemMoved(DiagramItem *table); + void itemHasMoved(DiagramItem *table); + signals: void modeChanged(DiagramDocument::Mode mode); - void tableMoved(DatabaseTable *table); friend class DatabaseTable; @@ -69,6 +73,9 @@ protected: void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); private: + bool m_trackingMoves; + QMap<DiagramItem *, QPointF> m_movedItems; + QUndoStack *m_undoStack; Mode m_mode; QString m_fileName; |