From 9195dba5651ef68b9359e8720d4ac6126f731405 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 15 Sep 2011 09:11:30 +0200 Subject: [PATCH] first commit of scaleUp/Down as an undo-able command --- include/commands.h | 20 ++++++++++++- include/graphlogic.h | 14 ++++----- src/commands.cpp | 29 ++++++++++++++++++ src/graphlogic.cpp | 71 ++++++++++++++++++++++++++++++-------------- src/node.cpp | 6 ++-- 5 files changed, 107 insertions(+), 33 deletions(-) diff --git a/include/commands.h b/include/commands.h index 2a4a0fe..aa2b8d0 100644 --- a/include/commands.h +++ b/include/commands.h @@ -23,6 +23,7 @@ struct UndoContext qreal m_x; qreal m_y; bool m_subtree; + qreal m_scale; UndoContext(GraphLogic *graphLogic = 0, Node *activeNode = 0, @@ -35,7 +36,8 @@ struct UndoContext bool secondary = false, qreal x = 0, qreal y = 0, - bool subtree = false) + bool subtree = false, + qreal scale = 0) : m_graphLogic(graphLogic) , m_activeNode(activeNode) , m_hintNode(hintNode) @@ -48,6 +50,7 @@ struct UndoContext , m_x(x) , m_y(y) , m_subtree(subtree) + , m_scale(scale) {}; }; @@ -180,5 +183,20 @@ private: QMap m_colorMap; }; +class ScaleNodeCommand : public BaseUndoClass +{ + +public: + + ScaleNodeCommand(UndoContext context); + + void undo(); + void redo(); + +private: + + QMap m_scaleMap; +}; + #endif // COMMANDS_H diff --git a/include/graphlogic.h b/include/graphlogic.h index be55ac6..4e61200 100644 --- a/include/graphlogic.h +++ b/include/graphlogic.h @@ -44,13 +44,13 @@ public: public slots: // commands from toolbars: - void insertNode(); // undo command - void removeNode(); // undo command - void nodeEdited(); /// @todo Rewrite as an undo action - void scaleUp(); /// @todo Rewrite as an undo action - void scaleDown(); /// @todo Rewrite as an undo action - void nodeColor(); // undo command - void nodeTextColor(); + void insertNode(); // undo command + void removeNode(); // undo command + void nodeEdited(); /// @todo Rewrite as an undo action + void scaleUp(); // undo command + void scaleDown(); // undo command + void nodeColor(); // undo command + void nodeTextColor(); // undo command void addEdge(); void removeEdge(); void hintMode(); diff --git a/src/commands.cpp b/src/commands.cpp index d3e20c3..9a3e76b 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -363,3 +363,32 @@ void NodeTextColorCommand::redo() m_context.m_graphLogic->setActiveNode(m_activeNode); } + +ScaleNodeCommand::ScaleNodeCommand(UndoContext context) + : BaseUndoClass(context) +{ + setText(QObject::tr("Changing scale of node: \"").append( + m_context.m_activeNode == m_context.m_nodeList->first() ? + QObject::tr("Base node") : + m_context.m_activeNode->toPlainText()).append("\""). + append(m_subtree ? QObject::tr(" with subtree") : QString(""))); + + foreach(Node *node, m_nodeList) + m_scaleMap[node] = node->scale(); +} + +void ScaleNodeCommand::undo() +{ + foreach(Node *node, m_nodeList) + node->setScale(m_scaleMap[node], m_context.m_graphLogic->graphWidget()->sceneRect()); + + m_context.m_graphLogic->setActiveNode(m_activeNode); +} + +void ScaleNodeCommand::redo() +{ + foreach(Node *node, m_nodeList) + node->setScale(m_context.m_scale, m_context.m_graphLogic->graphWidget()->sceneRect()); + + m_context.m_graphLogic->setActiveNode(m_activeNode); +} diff --git a/src/graphlogic.cpp b/src/graphlogic.cpp index f14b82a..54a2b3a 100644 --- a/src/graphlogic.cpp +++ b/src/graphlogic.cpp @@ -416,17 +416,30 @@ void GraphLogic::scaleUp() return; } - if (QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier) - { - QList nodeList = m_activeNode->subtree(); - foreach(Node *node, nodeList) - node->setScale(qreal(1.2), m_graphWidget->sceneRect()); - } - else - { - m_activeNode->setScale(qreal(1.2),m_graphWidget->sceneRect()); - } + bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier); + + UndoContext context; + context.m_graphLogic = this; + context.m_nodeList = &m_nodeList; + context.m_activeNode = m_activeNode; + context.m_scale = qreal(1.2); + context.m_subtree = subtree; + + QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context); + m_undoStack->push(scaleNodeCommand); + +// if (QApplication::keyboardModifiers() & Qt::ControlModifier && +// QApplication::keyboardModifiers() & Qt::ShiftModifier) +// { +// QList nodeList = m_activeNode->subtree(); +// foreach(Node *node, nodeList) +// node->setScale(qreal(1.2), m_graphWidget->sceneRect()); +// } +// else +// { +// m_activeNode->setScale(qreal(1.2),m_graphWidget->sceneRect()); +// } } void GraphLogic::scaleDown() @@ -437,17 +450,31 @@ void GraphLogic::scaleDown() return; } - if (QApplication::keyboardModifiers() & Qt::ControlModifier && - QApplication::keyboardModifiers() & Qt::ShiftModifier) - { - QList nodeList = m_activeNode->subtree(); - foreach(Node *node, nodeList) - node->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect()); - } - else - { - m_activeNode->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect()); - } + bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier && + QApplication::keyboardModifiers() & Qt::ShiftModifier); + + UndoContext context; + context.m_graphLogic = this; + context.m_nodeList = &m_nodeList; + context.m_activeNode = m_activeNode; + context.m_scale = qreal(1 / 1.2); + context.m_subtree = subtree; + + QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context); + m_undoStack->push(scaleNodeCommand); + + +// if (QApplication::keyboardModifiers() & Qt::ControlModifier && +// QApplication::keyboardModifiers() & Qt::ShiftModifier) +// { +// QList nodeList = m_activeNode->subtree(); +// foreach(Node *node, nodeList) +// node->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect()); +// } +// else +// { +// m_activeNode->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect()); +// } } void GraphLogic::nodeColor() diff --git a/src/node.cpp b/src/node.cpp index 4b3741c..6236444 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -221,9 +221,9 @@ QColor Node::textColor() const void Node::setScale(const qreal &factor,const QRectF &sceneRect) { // limit scale to a reasonable size - if (factor * scale() < 0.4 || - factor * scale() > 4 ) - return; +// if (factor * scale() < 0.4 || +// factor * scale() > 4 ) +// return; // cannot scale out the Node from the scene if (!sceneRect.contains(pos() +