From c1e011dc43590ba2d2c9837dad83e3bd107ae4cf Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 11 Jun 2011 21:19:40 +0200 Subject: [PATCH] scroll can be moved with cursor keys, node can be moved with ctrl+cursors keys, node can be edited with F2 key, and leave editing with ECS key --- graphwidget.cpp | 92 +++++++++++++++++++++++++++++++++++++++++-------- graphwidget.h | 1 + node.cpp | 30 +++++++++++++--- node.h | 6 +++- 4 files changed, 108 insertions(+), 21 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 9531364..6ff1132 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -15,7 +15,8 @@ GraphWidget::GraphWidget(QWidget *parent) : m_parent(parent), m_activeNode(0), m_showingNodeNumbers(false), - m_followNode(0) + m_followNode(0), + m_editingNode(false) { m_scene = new QGraphicsScene(this); m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); @@ -83,7 +84,7 @@ GraphWidget::GraphWidget(QWidget *parent) : m_nodeList.append(node9); Node *node10 = new Node(this); - node10->setHtml(QString("no joke")); + node10->setHtml(QString("no joke
anotehr line")); m_scene->addItem(node10); node10->setPos(-160, -10); m_nodeList.append(node10); @@ -99,6 +100,7 @@ GraphWidget::GraphWidget(QWidget *parent) : m_scene->addItem(node12); node12->setPos(170, -10); m_nodeList.append(node12);; + node12->setTextInteractionFlags(Qt::TextEditable); m_scene->addItem(new Edge(node1, node2)); m_scene->addItem(new Edge(node1, node3)); @@ -123,31 +125,65 @@ QGraphicsScene *GraphWidget::getScene() return m_scene; } + void GraphWidget::keyPressEvent(QKeyEvent *event) { + qDebug() << __PRETTY_FUNCTION__; + qDebug() << event->key(); + + // esc leaves node editing mode + if (event->key() == Qt::Key_Escape && m_editingNode) + { + m_activeNode->setEditable(false); + m_editingNode = false; + return; + } + // in node editing mode forward every key (except esc) to node + else if (m_editingNode) + { + m_activeNode->keyPressEvent(event); + return; + } + + switch (event->key()) { + + // move sceve, or move node if modkey is ctrl case Qt::Key_Up: - if (m_activeNode) - m_activeNode->moveBy(0, -20); - break; case Qt::Key_Down: - if (m_activeNode) - m_activeNode->moveBy(0, 20); - break; case Qt::Key_Left: - if (m_activeNode) - m_activeNode->moveBy(-20, 0); - break; case Qt::Key_Right: - if (m_activeNode) - m_activeNode->moveBy(20, 0); + if (event->modifiers() == Qt::ControlModifier) + { + if (m_activeNode) + { + if (event->key() == Qt::Key_Up) m_activeNode->moveBy(0, -20); + else if (event->key() == Qt::Key_Down) m_activeNode->moveBy(0, 20); + else if (event->key() == Qt::Key_Left) m_activeNode->moveBy(-20, 0); + else if (event->key() == Qt::Key_Right) m_activeNode->moveBy(20, 0); + } + else + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("No active node."), + 5000); // millisec + } + } + else + { + QGraphicsView::keyPressEvent(event); + } break; + + // zoom in/out case Qt::Key_Plus: scaleView(qreal(1.2)); break; case Qt::Key_Minus: scaleView(1 / qreal(1.2)); break; + + // follow: select a node vimperator style case Qt::Key_F: m_showingNodeNumbers = !m_showingNodeNumbers; if (m_showingNodeNumbers) @@ -161,6 +197,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) } break; + // insert new node case Qt::Key_Insert: if (!m_activeNode) { @@ -175,6 +212,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) } break; + // used in node selection mode, to select node with numbers/enter case Qt::Key_0: case Qt::Key_1: case Qt::Key_2: @@ -194,6 +232,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) break; + // delete one letter back in node selection case Qt::Key_Backspace: if (!m_showingNodeNumbers) break; @@ -215,19 +254,41 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) } break; + // in node selection select node if nudenum = enterednum case Qt::Key_Return: case Qt::Key_Enter: + if (m_followNode && m_showingNodeNumbers) { showingAllNodeNumbers(false); if (m_activeNode) - m_activeNode->setActive(false); + m_activeNode->setActive(false); m_activeNode = m_followNode; m_activeNode->setActive(); m_showingNodeNumbers = false; } + break; + // edit node + case Qt::Key_F2: + + if (m_activeNode) + { + m_editingNode = true; + m_activeNode->setEditable(); + m_scene->setFocusItem(m_activeNode); + } + else + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("No active node."), + 5000); // millisec + } + + break; + + // delete node case Qt::Key_Delete: if (m_activeNode) @@ -259,6 +320,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) } } + + void GraphWidget::wheelEvent(QWheelEvent *event) { scaleView(pow((double)2, -event->delta() / 240.0)); @@ -302,7 +365,6 @@ void GraphWidget::insertNode() { double angle(m_activeNode->calculateBiggestAngle()); - qDebug() << "got angle: " << angle; qreal length(100); diff --git a/graphwidget.h b/graphwidget.h index 7bc3a2b..e18d07a 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -43,6 +43,7 @@ private: bool m_showingNodeNumbers; QString m_followNumber; Node *m_followNode; + bool m_editingNode; }; #endif // GRAPHWIDGET_H diff --git a/node.cpp b/node.cpp index 67eb4c4..fe5a1f2 100644 --- a/node.cpp +++ b/node.cpp @@ -15,6 +15,7 @@ Node::Node(GraphWidget *parent) : m_numberIsSpecial(false) { setFlag(ItemIsMovable); + setFlag(ItemSendsGeometryChanges); // setTextInteractionFlags(Qt::TextBrowserInteraction); // setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard); @@ -27,6 +28,8 @@ Node::Node(GraphWidget *parent) : // shall I set it after some spec key? // setTextInteractionFlags(Qt::TextEditorInteraction); + + setOpenExternalLinks(true); } Node::~Node() @@ -129,12 +132,12 @@ void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) QGraphicsItem::mousePressEvent(event); } -void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) -{ - m_graph->insertNode(); +//void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +//{ +// m_graph->insertNode(); - QGraphicsItem::mouseDoubleClickEvent(event); -} +// QGraphicsItem::mouseDoubleClickEvent(event); +//} void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { @@ -217,3 +220,20 @@ double Node::doubleModulo(const double &devided, const double &devisor) { return devided - static_cast(devisor * static_cast(devided / devisor)); } + +void Node::setEditable(const bool &editable) +{ + qDebug() << __PRETTY_FUNCTION__; + + //setFocusPolicy(Qt::StrongFocus); + setTextInteractionFlags(editable ? Qt::TextEditable : Qt::NoTextInteraction); +} + +void Node::keyPressEvent(QKeyEvent *event) +{ + qDebug() << __PRETTY_FUNCTION__; + + QGraphicsTextItem::keyPressEvent(event); + + foreach (EdgeElement element, m_edgeList) element.edge->adjust(); +} diff --git a/node.h b/node.h index 13a43c0..b4ceaf5 100644 --- a/node.h +++ b/node.h @@ -22,6 +22,10 @@ public: void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false); double calculateBiggestAngle(); + void setEditable(const bool &editable = true); + + // changing visibility from prot to pub + void keyPressEvent(QKeyEvent *event); protected: @@ -29,7 +33,7 @@ protected: QWidget *widget); QVariant itemChange(GraphicsItemChange change, const QVariant &value); void mousePressEvent(QGraphicsSceneMouseEvent *event); - void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); + //void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void linkActivated(const QString &link);