From eb19562c6e88110dbad0aa5f4d8b1bcf32709eba Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Mon, 13 Jun 2011 16:06:30 +0200 Subject: [PATCH] cursor is movable with ctrl,cursor keys when editing a node --- graphwidget.cpp | 1 - node.cpp | 63 +++++++++++++++++++++++++++++++++++++------------ node.h | 3 +++ 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index 82d86e4..36c2da4 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -129,7 +129,6 @@ QGraphicsScene *GraphWidget::getScene() void GraphWidget::keyPressEvent(QKeyEvent *event) { qDebug() << __PRETTY_FUNCTION__; - qDebug() << event->key(); // esc leaves node editing mode if (event->key() == Qt::Key_Escape && m_editingNode) diff --git a/node.cpp b/node.cpp index 5920b0f..ad219c7 100644 --- a/node.cpp +++ b/node.cpp @@ -4,6 +4,7 @@ #include #include #include +#include static const double Pi = 3.14159265358979323846264338327950288419717; @@ -15,21 +16,12 @@ Node::Node(GraphWidget *parent) : m_numberIsSpecial(false) { setFlag(ItemIsMovable); - setFlag(ItemSendsGeometryChanges); -// setTextInteractionFlags(Qt::TextBrowserInteraction); -// setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard); - + setCacheMode(DeviceCoordinateCache); -// setZValue(1); // shall I use system colors? setDefaultTextColor(QColor(0,0,0)); - - // shall I set it after some spec key? -// setTextInteractionFlags(Qt::TextEditorInteraction); - - setOpenExternalLinks(true); } Node::~Node() @@ -124,7 +116,6 @@ void Node::setActive(const bool &active) } -/// @note who shall set active: press or release? void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) { m_graph->setActiveNode(this); @@ -224,15 +215,57 @@ void Node::setEditable(const bool &editable) { qDebug() << __PRETTY_FUNCTION__; - //setFocusPolicy(Qt::StrongFocus); - setTextInteractionFlags(editable ? Qt::TextEditable : Qt::NoTextInteraction); + setTextInteractionFlags( + editable ? + Qt::TextEditable : + Qt::NoTextInteraction); } void Node::keyPressEvent(QKeyEvent *event) { qDebug() << __PRETTY_FUNCTION__; - QGraphicsTextItem::keyPressEvent(event); + // cursor movements + switch (event->key()) { - foreach (EdgeElement element, m_edgeList) element.edge->adjust(); + case Qt::Key_Left: + { + QTextCursor c = textCursor(); + c.movePosition( + event->modifiers() == Qt::ControlModifier ? + QTextCursor::PreviousWord : + QTextCursor::PreviousCharacter); + setTextCursor(c); + break; + } + case Qt::Key_Right: + { + QTextCursor c = textCursor(); + c.movePosition( + event->modifiers() == Qt::ControlModifier ? + QTextCursor::NextWord : + QTextCursor::NextCharacter); + setTextCursor(c); + break; + } + case Qt::Key_Up: + { + QTextCursor c = textCursor(); + c.movePosition(QTextCursor::Up); + setTextCursor(c); + break; + } + case Qt::Key_Down: + { + QTextCursor c = textCursor(); + c.movePosition(QTextCursor::Down); + setTextCursor(c); + break; + } + + default: + + QGraphicsTextItem::keyPressEvent(event); + foreach (EdgeElement element, m_edgeList) element.edge->adjust(); + } } diff --git a/node.h b/node.h index 83aadf3..451e9af 100644 --- a/node.h +++ b/node.h @@ -2,6 +2,7 @@ #define NODE_H #include +#include #include "edge.h" #include "graphwidget.h" @@ -55,6 +56,8 @@ private: int m_number; bool m_hasBorder; bool m_numberIsSpecial; +// QTextCursor m_cursor; + }; #endif // NODE_H