move command is undocommand

master
Denes Matetelki 14 years ago
parent ebbd74147d
commit 3f3968a0e4

@ -15,12 +15,13 @@ struct UndoContext
Node *m_activeNode; Node *m_activeNode;
Node *m_hintNode; Node *m_hintNode;
QList <Node *> *m_nodeList; QList <Node *> *m_nodeList;
QPointF m_pos; QPointF m_pos;
QColor m_color; QColor m_color;
Node *m_source; Node *m_source;
Node *m_destination; Node *m_destination;
bool m_secondary; bool m_secondary;
qreal m_x;
qreal m_y;
UndoContext(GraphLogic *graphLogic = 0, UndoContext(GraphLogic *graphLogic = 0,
Node *activeNode = 0, Node *activeNode = 0,
@ -30,7 +31,9 @@ struct UndoContext
QColor color = QColor(), QColor color = QColor(),
Node *source = 0, Node *source = 0,
Node *destination = 0, Node *destination = 0,
bool secondary = false) bool secondary = false,
qreal x = 0,
qreal y = 0)
: m_graphLogic(graphLogic) : m_graphLogic(graphLogic)
, m_activeNode(activeNode) , m_activeNode(activeNode)
, m_hintNode(hintNode) , m_hintNode(hintNode)
@ -39,7 +42,10 @@ struct UndoContext
, m_color(color) , m_color(color)
, m_source(source) , m_source(source)
, m_destination(destination) , m_destination(destination)
, m_secondary(secondary) {}; , m_secondary(secondary)
, m_x(x)
, m_y(y)
{};
}; };
@ -125,4 +131,22 @@ private:
Edge *m_edge; Edge *m_edge;
}; };
class MoveCommand : public QUndoCommand
{
public:
MoveCommand(UndoContext context);
void undo();
void redo();
private:
bool m_done;
UndoContext m_context;
QList <Node *> m_nodeList;
};
#endif // COMMANDS_H #endif // COMMANDS_H

@ -23,7 +23,6 @@ class GraphLogic : public QObject
public: public:
explicit GraphLogic(GraphWidget *parent = 0); explicit GraphLogic(GraphWidget *parent = 0);
GraphWidget *graphWidget() const; GraphWidget *graphWidget() const;
void setUndoStack(QUndoStack *stack); void setUndoStack(QUndoStack *stack);
@ -40,6 +39,10 @@ public:
void setHintNode(Node *node); void setHintNode(Node *node);
void reShowNumbers(); void reShowNumbers();
void moveNode(qreal x, qreal y);
// void move(const QPointF &oldpos, const QPointF &newpos); /// @todo Rewrite as an undo action
public slots: public slots:
// commands from toolbars: // commands from toolbars:
@ -67,12 +70,12 @@ signals:
private: private:
void moveUp(); void moveNodeUp();
void moveDown(); void moveNodeDown();
void moveLeft(); void moveNodeLeft();
void moveRight(); void moveNodeRight();
void move(const int &x, const int &y); /// @todo Rewrite as an undo action
void setNodeColor(const QColor &color, const bool &subtree = false); /// @todo Rewrite as an undo action void setNodeColor(const QColor &color, const bool &subtree = false); /// @todo Rewrite as an undo action
void setNodeTextColor(const QColor &color, const bool &subtree = false); /// @todo Rewrite as an undo action void setNodeTextColor(const QColor &color, const bool &subtree = false); /// @todo Rewrite as an undo action

@ -47,7 +47,7 @@ public slots:
// toolbars // toolbars
void showMainToolbar(const bool &show = true); void showMainToolbar(const bool &show = true);
void showStatusIconToolbar(const bool &show = true); void showStatusIconToolbar(const bool &show = true);
void showUdoToolbar(const bool &show = true); void showUndoToolbar(const bool &show = true);
// handle changed content at quit // handle changed content at quit
void quit(); void quit();

@ -6,9 +6,11 @@
#include <QGraphicsDropShadowEffect> #include <QGraphicsDropShadowEffect>
#include "edge.h" #include "edge.h"
#include "graphwidget.h" //#include "graphwidget.h"
#include "graphlogic.h"
class GraphWidget; //class GraphWidget;
class GraphLogic;
class Node : public QGraphicsTextItem class Node : public QGraphicsTextItem
{ {
@ -16,7 +18,7 @@ class Node : public QGraphicsTextItem
public: public:
Node(); Node(GraphLogic *graphLogic);
~Node(); ~Node();
// add/remove edges // add/remove edges
@ -94,8 +96,9 @@ private:
EdgeElement(Edge *e, bool s) : edge(e), startsFromThisNode(s) {} EdgeElement(Edge *e, bool s) : edge(e), startsFromThisNode(s) {}
}; };
QList<EdgeElement> m_edgeList; QList<EdgeElement> m_edgeList;
// GraphWidget *m_graph; GraphLogic *m_graphLogic;
int m_number; int m_number;
bool m_hasBorder; bool m_hasBorder;
bool m_numberIsSpecial; bool m_numberIsSpecial;

@ -234,3 +234,36 @@ void RemoveEdgeCommand::redo()
m_context.m_graphLogic->setActiveNode(m_context.m_activeNode); m_context.m_graphLogic->setActiveNode(m_context.m_activeNode);
} }
MoveCommand::MoveCommand(UndoContext context)
: m_context(context)
{
setText(QObject::tr("Node \"").append(
m_context.m_activeNode == m_context.m_nodeList->first() ?
QObject::tr("Base node") :
m_context.m_activeNode->toPlainText()).append("\" moved"));
// move just the active Node or it's subtree too?
if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier)
{
m_nodeList = m_context.m_activeNode->subtree();
setText(text().append(QObject::tr(" with subtree")));
}
else
{
m_nodeList.push_back(m_context.m_activeNode);
}
}
void MoveCommand::undo()
{
foreach(Node *node, m_nodeList)
node->moveBy(-m_context.m_x, -m_context.m_y);
}
void MoveCommand::redo()
{
foreach(Node *node, m_nodeList)
node->moveBy(m_context.m_x, m_context.m_y);
}

@ -38,13 +38,13 @@ GraphLogic::GraphLogic(GraphWidget *parent)
(Qt::Key_F, &GraphLogic::hintMode)); (Qt::Key_F, &GraphLogic::hintMode));
m_memberMap.insert(std::pair<int, void(GraphLogic::*)()> m_memberMap.insert(std::pair<int, void(GraphLogic::*)()>
(Qt::Key_Up, &GraphLogic::moveUp)); (Qt::Key_Up, &GraphLogic::moveNodeUp));
m_memberMap.insert(std::pair<int, void(GraphLogic::*)()> m_memberMap.insert(std::pair<int, void(GraphLogic::*)()>
(Qt::Key_Down, &GraphLogic::moveDown)); (Qt::Key_Down, &GraphLogic::moveNodeDown));
m_memberMap.insert(std::pair<int, void(GraphLogic::*)()> m_memberMap.insert(std::pair<int, void(GraphLogic::*)()>
(Qt::Key_Left, &GraphLogic::moveLeft)); (Qt::Key_Left, &GraphLogic::moveNodeLeft));
m_memberMap.insert(std::pair<int, void(GraphLogic::*)()> m_memberMap.insert(std::pair<int, void(GraphLogic::*)()>
(Qt::Key_Right, &GraphLogic::moveRight)); (Qt::Key_Right, &GraphLogic::moveNodeRight));
m_memberMap.insert(std::pair<int, void(GraphLogic::*)()> m_memberMap.insert(std::pair<int, void(GraphLogic::*)()>
(Qt::Key_Backspace, &GraphLogic::delNumber)); (Qt::Key_Backspace, &GraphLogic::delNumber));
@ -296,10 +296,28 @@ void GraphLogic::writeContentToPngFile(const QString &fileName)
emit notification(tr("MindMap exported as ") + fileName); emit notification(tr("MindMap exported as ") + fileName);
} }
void GraphLogic::reShowNumbers() Node * GraphLogic::nodeFactory()
{ {
if (m_showingNodeNumbers) Node *node = new Node(this);
showNodeNumbers();
connect(node, SIGNAL(nodeChanged()), this, SLOT(nodeChanged()));
connect(node, SIGNAL(nodeSelected()), this, SLOT(nodeSelected()));
connect(node, SIGNAL(nodeEdited()), this, SLOT(nodeEdited()));
connect(node, SIGNAL(nodeMoved(QGraphicsSceneMouseEvent*)),
this, SLOT(nodeMoved(QGraphicsSceneMouseEvent*)));
connect(node, SIGNAL(nodeLostFocus()), this, SLOT(nodeLostFocus()));
return node;
}
void GraphLogic::setActiveNode(Node *node)
{
if (m_activeNode!=0)
m_activeNode->setBorder(false);
m_activeNode = node;
if (m_activeNode)
m_activeNode->setBorder();
} }
void GraphLogic::setHintNode(Node *node) void GraphLogic::setHintNode(Node *node)
@ -307,6 +325,12 @@ void GraphLogic::setHintNode(Node *node)
m_hintNode = node; m_hintNode = node;
} }
void GraphLogic::reShowNumbers()
{
if (m_showingNodeNumbers)
showNodeNumbers();
}
void GraphLogic::insertNode() void GraphLogic::insertNode()
{ {
// checks // checks
@ -571,39 +595,39 @@ void GraphLogic::nodeLostFocus()
} }
} }
void GraphLogic::moveUp() void GraphLogic::moveNodeUp()
{ {
QApplication::keyboardModifiers() & Qt::ControlModifier ? QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(0,-20) : moveNode(qreal(0),qreal(-20)) :
m_graphWidget->verticalScrollBar()->setValue( m_graphWidget->verticalScrollBar()->setValue(
m_graphWidget->verticalScrollBar()->value()-20); m_graphWidget->verticalScrollBar()->value()-20);
} }
void GraphLogic::moveDown() void GraphLogic::moveNodeDown()
{ {
QApplication::keyboardModifiers() & Qt::ControlModifier ? QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(0,20) : moveNode(qreal(0),qreal(20)) :
m_graphWidget->verticalScrollBar()->setValue( m_graphWidget->verticalScrollBar()->setValue(
m_graphWidget->verticalScrollBar()->value()+20); m_graphWidget->verticalScrollBar()->value()+20);
} }
void GraphLogic::moveLeft() void GraphLogic::moveNodeLeft()
{ {
QApplication::keyboardModifiers() & Qt::ControlModifier ? QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(-20,0) : moveNode(qreal(-20),qreal(0)) :
m_graphWidget->horizontalScrollBar()->setValue( m_graphWidget->horizontalScrollBar()->setValue(
m_graphWidget->horizontalScrollBar()->value()-20); m_graphWidget->horizontalScrollBar()->value()-20);
} }
void GraphLogic::moveRight() void GraphLogic::moveNodeRight()
{ {
QApplication::keyboardModifiers() & Qt::ControlModifier ? QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(20,0) : moveNode(qreal(20),qreal(0)) :
m_graphWidget->horizontalScrollBar()->setValue( m_graphWidget->horizontalScrollBar()->setValue(
m_graphWidget->horizontalScrollBar()->value()+20); m_graphWidget->horizontalScrollBar()->value()+20);
} }
void GraphLogic::move(const int &x, const int &y) void GraphLogic::moveNode(qreal x, qreal y)
{ {
if (!m_activeNode) if (!m_activeNode)
{ {
@ -611,20 +635,15 @@ void GraphLogic::move(const int &x, const int &y)
return; return;
} }
if (QApplication::keyboardModifiers() & Qt::ControlModifier && UndoContext context;
QApplication::keyboardModifiers() & Qt::ShiftModifier) context.m_graphLogic = this;
{ context.m_nodeList = &m_nodeList;
QList <Node *> nodeList = m_activeNode->subtree(); context.m_activeNode = m_activeNode;
foreach(Node *node, nodeList) context.m_x = x;
node->moveBy(x, y); context.m_y = y;
emit contentChanged(); QUndoCommand *moveCommand = new MoveCommand(context);
} m_undoStack->push(moveCommand);
else // Move just the active Node.
{
m_activeNode->moveBy(x, y);
emit contentChanged();
}
} }
void GraphLogic::setNodeColor(const QColor &color, const bool &subtree) void GraphLogic::setNodeColor(const QColor &color, const bool &subtree)
@ -686,20 +705,6 @@ void GraphLogic::applyNumber()
selectNode(m_hintNode); selectNode(m_hintNode);
} }
Node * GraphLogic::nodeFactory()
{
Node *node = new Node();
connect(node, SIGNAL(nodeChanged()), this, SLOT(nodeChanged()));
connect(node, SIGNAL(nodeSelected()), this, SLOT(nodeSelected()));
connect(node, SIGNAL(nodeEdited()), this, SLOT(nodeEdited()));
connect(node, SIGNAL(nodeMoved(QGraphicsSceneMouseEvent*)),
this, SLOT(nodeMoved(QGraphicsSceneMouseEvent*)));
connect(node, SIGNAL(nodeLostFocus()), this, SLOT(nodeLostFocus()));
return node;
}
void GraphLogic::selectNode(Node *node) void GraphLogic::selectNode(Node *node)
{ {
// leave hint mode // leave hint mode
@ -722,16 +727,6 @@ void GraphLogic::selectNode(Node *node)
} }
} }
void GraphLogic::setActiveNode(Node *node)
{
if (m_activeNode!=0)
m_activeNode->setBorder(false);
m_activeNode = node;
if (m_activeNode)
m_activeNode->setBorder();
}
QList<Edge *> GraphLogic::allEdges() const QList<Edge *> GraphLogic::allEdges() const
{ {
QList<Edge *> list; QList<Edge *> list;

@ -40,6 +40,7 @@ void GraphWidget::closeScene()
{ {
m_graphlogic->removeAllNodes(); m_graphlogic->removeAllNodes();
this->hide(); this->hide();
} }
GraphLogic *GraphWidget::graphLogic() const GraphLogic *GraphWidget::graphLogic() const

@ -223,7 +223,9 @@ bool MainWindow::closeFile()
m_contentChanged = false; m_contentChanged = false;
setTitle(""); setTitle("");
m_graphicsView->closeScene(); m_graphicsView->closeScene();
m_undoStack->clear();
showMainToolbar(false); showMainToolbar(false);
showUndoToolbar(false);
return true; return true;
} }
@ -275,7 +277,7 @@ void MainWindow::showStatusIconToolbar(const bool &show)
false); false);
} }
void MainWindow::showUdoToolbar(const bool &show) void MainWindow::showUndoToolbar(const bool &show)
{ {
m_ui->undoToolBar->setVisible(show ? m_ui->undoToolBar->setVisible(show ?
!m_ui->undoToolBar->isVisible() : !m_ui->undoToolBar->isVisible() :
@ -484,7 +486,7 @@ void MainWindow::setupEditToolbar()
m_undoToolbar = new QAction(tr("undo toolbar"), this); m_undoToolbar = new QAction(tr("undo toolbar"), this);
m_undoToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U)); m_undoToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
connect(m_undoToolbar, SIGNAL(activated()), connect(m_undoToolbar, SIGNAL(activated()),
this, SLOT (showUdoToolbar())); this, SLOT (showUndoToolbar()));
m_ui->menuEdit->addAction(m_mainToolbar); m_ui->menuEdit->addAction(m_mainToolbar);
m_ui->menuEdit->addAction(m_iconToolbar); m_ui->menuEdit->addAction(m_iconToolbar);

@ -15,8 +15,9 @@ const double Node::m_twoPi = Node::m_pi * 2.0;
const QColor Node::m_gold(255,215,0); const QColor Node::m_gold(255,215,0);
Node::Node() Node::Node(GraphLogic *graphLogic)
: m_number(-1) : m_graphLogic(graphLogic)
, m_number(-1)
, m_hasBorder(false) , m_hasBorder(false)
, m_numberIsSpecial(false) , m_numberIsSpecial(false)
, m_color(m_gold) , m_color(m_gold)
@ -454,6 +455,8 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
return newPos; return newPos;
} }
/// undo call here
break; break;
} }
case ItemPositionHasChanged: case ItemPositionHasChanged:
@ -492,7 +495,8 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
// notify parent so subtree can be moved too if necessary // notify parent so subtree can be moved too if necessary
void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
emit nodeMoved(event); QPointF diff(event->scenePos() - event->lastScenePos());
m_graphLogic->moveNode(diff.x(), diff.y());
} }
QPainterPath Node::shape () const QPainterPath Node::shape () const

Loading…
Cancel
Save