From f443cd577e87a4be97edee25bff320feae5ced68 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 16 Sep 2011 15:57:54 +0200 Subject: [PATCH] scale command is now undo-able command --- include/commands.h | 8 ++++---- include/graphlogic.h | 2 +- include/graphwidget.h | 6 +++++- qtmindmap.pro | 2 +- qtmindmap_test.pro | 2 ++ src/commands.cpp | 42 ++++++++++++++++++++++++++++++++++------- src/graphlogic.cpp | 41 ++++++++++++++-------------------------- src/graphwidget.cpp | 17 +++++++++-------- src/mainwindow.cpp | 4 ++++ src/node.cpp | 9 ++------- test/algorithmtests.cpp | 10 ++++++---- 11 files changed, 83 insertions(+), 60 deletions(-) diff --git a/include/commands.h b/include/commands.h index aa2b8d0..2bb80fc 100644 --- a/include/commands.h +++ b/include/commands.h @@ -60,7 +60,8 @@ public: enum MergeableCommandId { - MoveCommandId = 0 + MoveCommandId = 0, + ScaleCommandId }; BaseUndoClass(UndoContext context); @@ -193,9 +194,8 @@ public: void undo(); void redo(); -private: - - QMap m_scaleMap; + bool mergeWith(const QUndoCommand *command); + int id() const; }; diff --git a/include/graphlogic.h b/include/graphlogic.h index 4e61200..9a5703b 100644 --- a/include/graphlogic.h +++ b/include/graphlogic.h @@ -64,7 +64,7 @@ public slots: signals: void contentChanged(const bool& changed = true); - void notification(const QString &msg); + void notification(const QString &msg); private: diff --git a/include/graphwidget.h b/include/graphwidget.h index 037ac29..b1182c1 100644 --- a/include/graphwidget.h +++ b/include/graphwidget.h @@ -36,6 +36,10 @@ public slots: void zoomIn(); void zoomOut(); +signals: + + void notification(const QString &msg); + protected: void keyPressEvent(QKeyEvent *event); @@ -44,7 +48,7 @@ protected: private: - void scaleView(qreal scaleFactor); + void scaleView(qreal factor); MainWindow *m_parent; diff --git a/qtmindmap.pro b/qtmindmap.pro index 28832ae..d4e0fea 100644 --- a/qtmindmap.pro +++ b/qtmindmap.pro @@ -37,7 +37,7 @@ HEADERS += include/mainwindow.h \ FORMS += ui/mainwindow.ui -RESOURCES += images/qtmindmap.qrc +RESOURCES += images/qtmindmap.qrc # the translation hack diff --git a/qtmindmap_test.pro b/qtmindmap_test.pro index fa9e04f..c85f14e 100644 --- a/qtmindmap_test.pro +++ b/qtmindmap_test.pro @@ -6,6 +6,7 @@ TARGET = qtmindmap_test SOURCES += src/mainwindow.cpp \ src/graphwidget.cpp \ + src/graphlogic.cpp \ src/node.cpp \ src/edge.cpp \ src/systemtray.cpp \ @@ -14,6 +15,7 @@ SOURCES += src/mainwindow.cpp \ HEADERS += include/mainwindow.h \ include/graphwidget.h \ + include/graphlogic.hpp \ include/node.h \ include/edge.h \ include/systemtray.h \ diff --git a/src/commands.cpp b/src/commands.cpp index 9a3e76b..6e91be9 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -367,20 +367,18 @@ void NodeTextColorCommand::redo() ScaleNodeCommand::ScaleNodeCommand(UndoContext context) : BaseUndoClass(context) { - setText(QObject::tr("Changing scale of node: \"").append( + setText(QObject::tr("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(); + m_context.m_activeNode->toPlainText()). + append("\" scaled (%1%)").arg(int((1+m_context.m_scale)*100)). + append(m_subtree ? QObject::tr(" with subtree") : QString(""))); } void ScaleNodeCommand::undo() { foreach(Node *node, m_nodeList) - node->setScale(m_scaleMap[node], m_context.m_graphLogic->graphWidget()->sceneRect()); + node->setScale(qreal(-m_context.m_scale), m_context.m_graphLogic->graphWidget()->sceneRect()); m_context.m_graphLogic->setActiveNode(m_activeNode); } @@ -392,3 +390,33 @@ void ScaleNodeCommand::redo() m_context.m_graphLogic->setActiveNode(m_activeNode); } + +bool ScaleNodeCommand::mergeWith(const QUndoCommand *command) +{ + if (command->id() != id()) + return false; + + const ScaleNodeCommand *scaleNodeCommand = static_cast(command); + + if (m_context.m_activeNode != scaleNodeCommand->m_context.m_activeNode) + return false; + + if (m_subtree != scaleNodeCommand->m_subtree) + return false; + + m_context.m_scale += scaleNodeCommand->m_context.m_scale; + + setText(QObject::tr("Node \"").append( + m_context.m_activeNode == m_context.m_nodeList->first() ? + QObject::tr("Base node") : + m_context.m_activeNode->toPlainText()). + append("\" scaled (%1%)").arg(int((1+m_context.m_scale)*100)). + append(m_subtree ? QObject::tr(" with subtree") : QString(""))); + + return true; +} + +int ScaleNodeCommand::id() const +{ + return ScaleCommandId; +} diff --git a/src/graphlogic.cpp b/src/graphlogic.cpp index 54a2b3a..cae167e 100644 --- a/src/graphlogic.cpp +++ b/src/graphlogic.cpp @@ -416,6 +416,12 @@ void GraphLogic::scaleUp() return; } + if (m_activeNode->scale()+qreal(0.2) > qreal(4)) + { + emit notification(tr("Too much scaling.")); + return; + } + bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier && QApplication::keyboardModifiers() & Qt::ShiftModifier); @@ -423,23 +429,11 @@ void GraphLogic::scaleUp() context.m_graphLogic = this; context.m_nodeList = &m_nodeList; context.m_activeNode = m_activeNode; - context.m_scale = qreal(1.2); + context.m_scale = qreal(0.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() @@ -450,6 +444,12 @@ void GraphLogic::scaleDown() return; } + if (m_activeNode->scale()-qreal(0.2) < qreal(0.1)) + { + emit notification(tr("Too much scaling.")); + return; + } + bool subtree(QApplication::keyboardModifiers() & Qt::ControlModifier && QApplication::keyboardModifiers() & Qt::ShiftModifier); @@ -457,24 +457,11 @@ void GraphLogic::scaleDown() context.m_graphLogic = this; context.m_nodeList = &m_nodeList; context.m_activeNode = m_activeNode; - context.m_scale = qreal(1 / 1.2); + context.m_scale = qreal(-0.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/graphwidget.cpp b/src/graphwidget.cpp index 3baf870..68dff00 100644 --- a/src/graphwidget.cpp +++ b/src/graphwidget.cpp @@ -50,12 +50,12 @@ GraphLogic *GraphWidget::graphLogic() const void GraphWidget::zoomIn() { - scaleView(qreal(1.2)); + scaleView(qreal(0.2)); } void GraphWidget::zoomOut() { - scaleView(qreal(1 / 1.2)); + scaleView(qreal(-0.2)); } // MainWindow::keyPressEvent passes all keyevent to here, except @@ -101,14 +101,15 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect) painter->drawRect(m_scene->sceneRect()); } -void GraphWidget::scaleView(qreal scaleFactor) +void GraphWidget::scaleView(qreal factor) { - qreal factor = transform().scale(scaleFactor, scaleFactor). - mapRect(QRectF(0, 0, 1, 1)).width(); - // don't allow to scale up/down too much - if (factor < 0.2 || factor > 10) + qreal viewScale = transform().m11() + 1 + factor; + if (viewScale < qreal(1) || viewScale > qreal(10)) + { + emit notification(tr("Too much zooming.")); return; + } - scale(scaleFactor, scaleFactor); + scale(1 + factor, 1 + factor); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 77d09a6..300c5b3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -30,9 +30,13 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_graphicsView->graphLogic(), SIGNAL(contentChanged(const bool&)), this, SLOT(contentChanged(const bool&))); + connect(m_graphicsView, SIGNAL(notification(QString)), + this, SLOT(statusBarMsg(QString))); + connect(m_graphicsView->graphLogic(), SIGNAL(notification(QString)), this, SLOT(statusBarMsg(QString))); + // setup toolbars, don't show them setupMainToolbar(); m_ui->mainToolBar->hide(); diff --git a/src/node.cpp b/src/node.cpp index 6236444..2d104bb 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -220,24 +220,19 @@ 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; - // cannot scale out the Node from the scene if (!sceneRect.contains(pos() + boundingRect().bottomRight() * scale() * factor)) return; prepareGeometryChange(); - QGraphicsTextItem::setScale(factor * scale()); + QGraphicsTextItem::setScale(factor + scale()); // scale edges to this Node too foreach(EdgeElement element, m_edgeList) { if (!element.startsFromThisNode) - element.edge->setWidth(element.edge->width() * factor ); + element.edge->setWidth(element.edge->width() + factor ); element.edge->adjust(); } diff --git a/test/algorithmtests.cpp b/test/algorithmtests.cpp index 4ae4014..b79daeb 100644 --- a/test/algorithmtests.cpp +++ b/test/algorithmtests.cpp @@ -33,16 +33,17 @@ void AlgorithmTests::calculateBiggestAngle() { MainWindow *mainWindow = new MainWindow; GraphWidget *graphWidget = new GraphWidget(mainWindow); + GraphLogic *graphLogic = new GraphLogic(graphWidget); // no edges - Node *node1 = new Node(graphWidget); + Node *node1 = new Node(graphLogic); node1->setPos(0,0); QCOMPARE(node1->calculateBiggestAngle(), Pi * 1.5); // one egde // 1 - Node *node2 = new Node(graphWidget); + Node *node2 = new Node(graphLogic); node2->setPos(30,0); Edge *edge1 = new Edge(node1, node2); @@ -65,12 +66,12 @@ void AlgorithmTests::calculateBiggestAngle() node2->setPos(30,0); edge1->m_angle = angleOfPoints(node1->pos(), node2->pos()); - Node *node3 = new Node(graphWidget); + Node *node3 = new Node(graphLogic); node3->setPos(-30,0); Edge *edge2 = new Edge(node1, node3); edge2->m_angle = angleOfPoints(node1->pos(), node3->pos()); - Node *node4 = new Node(graphWidget); + Node *node4 = new Node(graphLogic); node4->setPos(0, -30); Edge *edge3 = new Edge(node1, node4); edge3->m_angle = angleOfPoints(node1->pos(), node4->pos()); @@ -85,6 +86,7 @@ void AlgorithmTests::calculateBiggestAngle() delete node3; delete node4; + delete graphLogic; delete graphWidget; delete mainWindow; }