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_hintNode;
QList <Node *> *m_nodeList;
QPointF m_pos;
QColor m_color;
Node *m_source;
Node *m_destination;
bool m_secondary;
qreal m_x;
qreal m_y;
UndoContext(GraphLogic *graphLogic = 0,
Node *activeNode = 0,
@ -30,7 +31,9 @@ struct UndoContext
QColor color = QColor(),
Node *source = 0,
Node *destination = 0,
bool secondary = false)
bool secondary = false,
qreal x = 0,
qreal y = 0)
: m_graphLogic(graphLogic)
, m_activeNode(activeNode)
, m_hintNode(hintNode)
@ -39,7 +42,10 @@ struct UndoContext
, m_color(color)
, m_source(source)
, m_destination(destination)
, m_secondary(secondary) {};
, m_secondary(secondary)
, m_x(x)
, m_y(y)
{};
};
@ -125,4 +131,22 @@ private:
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

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

@ -47,7 +47,7 @@ public slots:
// toolbars
void showMainToolbar(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
void quit();

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

@ -234,3 +234,36 @@ void RemoveEdgeCommand::redo()
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));
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::*)()>
(Qt::Key_Down, &GraphLogic::moveDown));
(Qt::Key_Down, &GraphLogic::moveNodeDown));
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::*)()>
(Qt::Key_Right, &GraphLogic::moveRight));
(Qt::Key_Right, &GraphLogic::moveNodeRight));
m_memberMap.insert(std::pair<int, void(GraphLogic::*)()>
(Qt::Key_Backspace, &GraphLogic::delNumber));
@ -296,10 +296,28 @@ void GraphLogic::writeContentToPngFile(const QString &fileName)
emit notification(tr("MindMap exported as ") + fileName);
}
void GraphLogic::reShowNumbers()
Node * GraphLogic::nodeFactory()
{
if (m_showingNodeNumbers)
showNodeNumbers();
Node *node = new Node(this);
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)
@ -307,6 +325,12 @@ void GraphLogic::setHintNode(Node *node)
m_hintNode = node;
}
void GraphLogic::reShowNumbers()
{
if (m_showingNodeNumbers)
showNodeNumbers();
}
void GraphLogic::insertNode()
{
// checks
@ -571,39 +595,39 @@ void GraphLogic::nodeLostFocus()
}
}
void GraphLogic::moveUp()
void GraphLogic::moveNodeUp()
{
QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(0,-20) :
moveNode(qreal(0),qreal(-20)) :
m_graphWidget->verticalScrollBar()->setValue(
m_graphWidget->verticalScrollBar()->value()-20);
}
void GraphLogic::moveDown()
void GraphLogic::moveNodeDown()
{
QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(0,20) :
moveNode(qreal(0),qreal(20)) :
m_graphWidget->verticalScrollBar()->setValue(
m_graphWidget->verticalScrollBar()->value()+20);
}
void GraphLogic::moveLeft()
void GraphLogic::moveNodeLeft()
{
QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(-20,0) :
moveNode(qreal(-20),qreal(0)) :
m_graphWidget->horizontalScrollBar()->setValue(
m_graphWidget->horizontalScrollBar()->value()-20);
}
void GraphLogic::moveRight()
void GraphLogic::moveNodeRight()
{
QApplication::keyboardModifiers() & Qt::ControlModifier ?
move(20,0) :
moveNode(qreal(20),qreal(0)) :
m_graphWidget->horizontalScrollBar()->setValue(
m_graphWidget->horizontalScrollBar()->value()+20);
}
void GraphLogic::move(const int &x, const int &y)
void GraphLogic::moveNode(qreal x, qreal y)
{
if (!m_activeNode)
{
@ -611,20 +635,15 @@ void GraphLogic::move(const int &x, const int &y)
return;
}
if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier)
{
QList <Node *> nodeList = m_activeNode->subtree();
foreach(Node *node, nodeList)
node->moveBy(x, y);
UndoContext context;
context.m_graphLogic = this;
context.m_nodeList = &m_nodeList;
context.m_activeNode = m_activeNode;
context.m_x = x;
context.m_y = y;
emit contentChanged();
}
else // Move just the active Node.
{
m_activeNode->moveBy(x, y);
emit contentChanged();
}
QUndoCommand *moveCommand = new MoveCommand(context);
m_undoStack->push(moveCommand);
}
void GraphLogic::setNodeColor(const QColor &color, const bool &subtree)
@ -686,20 +705,6 @@ void GraphLogic::applyNumber()
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)
{
// 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 *> list;

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

@ -223,7 +223,9 @@ bool MainWindow::closeFile()
m_contentChanged = false;
setTitle("");
m_graphicsView->closeScene();
m_undoStack->clear();
showMainToolbar(false);
showUndoToolbar(false);
return true;
}
@ -275,7 +277,7 @@ void MainWindow::showStatusIconToolbar(const bool &show)
false);
}
void MainWindow::showUdoToolbar(const bool &show)
void MainWindow::showUndoToolbar(const bool &show)
{
m_ui->undoToolBar->setVisible(show ?
!m_ui->undoToolBar->isVisible() :
@ -484,7 +486,7 @@ void MainWindow::setupEditToolbar()
m_undoToolbar = new QAction(tr("undo toolbar"), this);
m_undoToolbar->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_U));
connect(m_undoToolbar, SIGNAL(activated()),
this, SLOT (showUdoToolbar()));
this, SLOT (showUndoToolbar()));
m_ui->menuEdit->addAction(m_mainToolbar);
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);
Node::Node()
: m_number(-1)
Node::Node(GraphLogic *graphLogic)
: m_graphLogic(graphLogic)
, m_number(-1)
, m_hasBorder(false)
, m_numberIsSpecial(false)
, m_color(m_gold)
@ -454,6 +455,8 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
return newPos;
}
/// undo call here
break;
}
case ItemPositionHasChanged:
@ -492,7 +495,8 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
// notify parent so subtree can be moved too if necessary
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

Loading…
Cancel
Save