From fe8983fce9ac6c4241db255d13c1271b39652ba0 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Wed, 22 Jun 2011 10:27:15 +0200 Subject: [PATCH] move subtree with shift --- graphwidget.cpp | 13 +++++++++++++ node.cpp | 32 ++++++++++++++++++++++++++++++++ node.h | 1 + 3 files changed, 46 insertions(+) diff --git a/graphwidget.cpp b/graphwidget.cpp index cba3ebf..45e8bf9 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -281,6 +281,19 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) else if (event->key() == Qt::Key_Right) m_activeNode->moveBy(20, 0); contentChanged(); } + else if (event->modifiers() & Qt::ControlModifier && + event->modifiers() & Qt::ShiftModifier) + { + QList nodeList = m_activeNode->subtree(); + foreach(Node *node, nodeList) + { + if (event->key() == Qt::Key_Up) node->moveBy(0, -20); + else if (event->key() == Qt::Key_Down) node->moveBy(0, 20); + else if (event->key() == Qt::Key_Left) node->moveBy(-20, 0); + else if (event->key() == Qt::Key_Right) node->moveBy(20, 0); + contentChanged(); + } + } else // move scene { QGraphicsView::keyPressEvent(event); diff --git a/node.cpp b/node.cpp index b6e6498..d212b6f 100644 --- a/node.cpp +++ b/node.cpp @@ -308,6 +308,38 @@ Edge * Node::edgeTo(const Node *node) const return 0; } + +QList Node::subtree() const +{ + /** @note QList crashes if modified while traversal, + * QMutableListIterator lacks push_back + */ + + std::list list; + list.push_back(const_cast(this)); + + // inorder + for(std::list::const_iterator it = list.begin(); + it != list.end(); + it++) + { + QList edges = (*it)->edgesFrom(); + if (!edges.empty()) + { + foreach(Edge *edge, edges) + { + if (!edge->secondary()) + { + list.push_back( edge->destNode() != this ? + edge->destNode(): + edge->sourceNode()); + } + } + } + } + return QList::fromStdList(list); +} + void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) diff --git a/node.h b/node.h index 67274eb..c1fe133 100644 --- a/node.h +++ b/node.h @@ -42,6 +42,7 @@ public: QList edgesFrom(const bool &excludeSecondaries = true) const; QList edgesToThis(const bool &excludeSecondaries = true) const; Edge * edgeTo(const Node* node) const; + QList subtree() const; protected: