first commit of scaleUp/Down as an undo-able command

master
Denes Matetelki 14 years ago
parent 24b4d965e0
commit 9195dba565

@ -23,6 +23,7 @@ struct UndoContext
qreal m_x; qreal m_x;
qreal m_y; qreal m_y;
bool m_subtree; bool m_subtree;
qreal m_scale;
UndoContext(GraphLogic *graphLogic = 0, UndoContext(GraphLogic *graphLogic = 0,
Node *activeNode = 0, Node *activeNode = 0,
@ -35,7 +36,8 @@ struct UndoContext
bool secondary = false, bool secondary = false,
qreal x = 0, qreal x = 0,
qreal y = 0, qreal y = 0,
bool subtree = false) bool subtree = false,
qreal scale = 0)
: m_graphLogic(graphLogic) : m_graphLogic(graphLogic)
, m_activeNode(activeNode) , m_activeNode(activeNode)
, m_hintNode(hintNode) , m_hintNode(hintNode)
@ -48,6 +50,7 @@ struct UndoContext
, m_x(x) , m_x(x)
, m_y(y) , m_y(y)
, m_subtree(subtree) , m_subtree(subtree)
, m_scale(scale)
{}; {};
}; };
@ -180,5 +183,20 @@ private:
QMap<Node*, QColor> m_colorMap; QMap<Node*, QColor> m_colorMap;
}; };
class ScaleNodeCommand : public BaseUndoClass
{
public:
ScaleNodeCommand(UndoContext context);
void undo();
void redo();
private:
QMap<Node*, qreal> m_scaleMap;
};
#endif // COMMANDS_H #endif // COMMANDS_H

@ -47,10 +47,10 @@ public slots:
void insertNode(); // undo command void insertNode(); // undo command
void removeNode(); // undo command void removeNode(); // undo command
void nodeEdited(); /// @todo Rewrite as an undo action void nodeEdited(); /// @todo Rewrite as an undo action
void scaleUp(); /// @todo Rewrite as an undo action void scaleUp(); // undo command
void scaleDown(); /// @todo Rewrite as an undo action void scaleDown(); // undo command
void nodeColor(); // undo command void nodeColor(); // undo command
void nodeTextColor(); void nodeTextColor(); // undo command
void addEdge(); void addEdge();
void removeEdge(); void removeEdge();
void hintMode(); void hintMode();

@ -363,3 +363,32 @@ void NodeTextColorCommand::redo()
m_context.m_graphLogic->setActiveNode(m_activeNode); 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);
}

@ -416,17 +416,30 @@ void GraphLogic::scaleUp()
return; return;
} }
if (QApplication::keyboardModifiers() & Qt::ControlModifier && bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier) QApplication::keyboardModifiers() & Qt::ShiftModifier);
{
QList <Node *> nodeList = m_activeNode->subtree(); UndoContext context;
foreach(Node *node, nodeList) context.m_graphLogic = this;
node->setScale(qreal(1.2), m_graphWidget->sceneRect()); context.m_nodeList = &m_nodeList;
} context.m_activeNode = m_activeNode;
else context.m_scale = qreal(1.2);
{ context.m_subtree = subtree;
m_activeNode->setScale(qreal(1.2),m_graphWidget->sceneRect());
} QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context);
m_undoStack->push(scaleNodeCommand);
// if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
// QApplication::keyboardModifiers() & Qt::ShiftModifier)
// {
// QList <Node *> 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() void GraphLogic::scaleDown()
@ -437,17 +450,31 @@ void GraphLogic::scaleDown()
return; return;
} }
if (QApplication::keyboardModifiers() & Qt::ControlModifier && bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier &&
QApplication::keyboardModifiers() & Qt::ShiftModifier) QApplication::keyboardModifiers() & Qt::ShiftModifier);
{
QList <Node *> nodeList = m_activeNode->subtree(); UndoContext context;
foreach(Node *node, nodeList) context.m_graphLogic = this;
node->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect()); context.m_nodeList = &m_nodeList;
} context.m_activeNode = m_activeNode;
else context.m_scale = qreal(1 / 1.2);
{ context.m_subtree = subtree;
m_activeNode->setScale(qreal(1 / 1.2),m_graphWidget->sceneRect());
} QUndoCommand *scaleNodeCommand = new ScaleNodeCommand(context);
m_undoStack->push(scaleNodeCommand);
// if (QApplication::keyboardModifiers() & Qt::ControlModifier &&
// QApplication::keyboardModifiers() & Qt::ShiftModifier)
// {
// QList <Node *> 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() void GraphLogic::nodeColor()

@ -221,9 +221,9 @@ QColor Node::textColor() const
void Node::setScale(const qreal &factor,const QRectF &sceneRect) void Node::setScale(const qreal &factor,const QRectF &sceneRect)
{ {
// limit scale to a reasonable size // limit scale to a reasonable size
if (factor * scale() < 0.4 || // if (factor * scale() < 0.4 ||
factor * scale() > 4 ) // factor * scale() > 4 )
return; // return;
// cannot scale out the Node from the scene // cannot scale out the Node from the scene
if (!sceneRect.contains(pos() + if (!sceneRect.contains(pos() +

Loading…
Cancel
Save