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

master
Denes Matetelki 14 years ago
parent 6cab47bfae
commit c1e011dc43

@ -15,7 +15,8 @@ GraphWidget::GraphWidget(QWidget *parent) :
m_parent(parent), m_parent(parent),
m_activeNode(0), m_activeNode(0),
m_showingNodeNumbers(false), m_showingNodeNumbers(false),
m_followNode(0) m_followNode(0),
m_editingNode(false)
{ {
m_scene = new QGraphicsScene(this); m_scene = new QGraphicsScene(this);
m_scene->setItemIndexMethod(QGraphicsScene::NoIndex); m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
@ -83,7 +84,7 @@ GraphWidget::GraphWidget(QWidget *parent) :
m_nodeList.append(node9); m_nodeList.append(node9);
Node *node10 = new Node(this); Node *node10 = new Node(this);
node10->setHtml(QString("no joke")); node10->setHtml(QString("no joke <br> anotehr line"));
m_scene->addItem(node10); m_scene->addItem(node10);
node10->setPos(-160, -10); node10->setPos(-160, -10);
m_nodeList.append(node10); m_nodeList.append(node10);
@ -99,6 +100,7 @@ GraphWidget::GraphWidget(QWidget *parent) :
m_scene->addItem(node12); m_scene->addItem(node12);
node12->setPos(170, -10); node12->setPos(170, -10);
m_nodeList.append(node12);; m_nodeList.append(node12);;
node12->setTextInteractionFlags(Qt::TextEditable);
m_scene->addItem(new Edge(node1, node2)); m_scene->addItem(new Edge(node1, node2));
m_scene->addItem(new Edge(node1, node3)); m_scene->addItem(new Edge(node1, node3));
@ -123,31 +125,65 @@ QGraphicsScene *GraphWidget::getScene()
return m_scene; return m_scene;
} }
void GraphWidget::keyPressEvent(QKeyEvent *event) 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()) { switch (event->key()) {
// move sceve, or move node if modkey is ctrl
case Qt::Key_Up: case Qt::Key_Up:
if (m_activeNode)
m_activeNode->moveBy(0, -20);
break;
case Qt::Key_Down: case Qt::Key_Down:
if (m_activeNode)
m_activeNode->moveBy(0, 20);
break;
case Qt::Key_Left: case Qt::Key_Left:
if (m_activeNode)
m_activeNode->moveBy(-20, 0);
break;
case Qt::Key_Right: case Qt::Key_Right:
if (event->modifiers() == Qt::ControlModifier)
{
if (m_activeNode) if (m_activeNode)
m_activeNode->moveBy(20, 0); {
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<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
}
else
{
QGraphicsView::keyPressEvent(event);
}
break; break;
// zoom in/out
case Qt::Key_Plus: case Qt::Key_Plus:
scaleView(qreal(1.2)); scaleView(qreal(1.2));
break; break;
case Qt::Key_Minus: case Qt::Key_Minus:
scaleView(1 / qreal(1.2)); scaleView(1 / qreal(1.2));
break; break;
// follow: select a node vimperator style
case Qt::Key_F: case Qt::Key_F:
m_showingNodeNumbers = !m_showingNodeNumbers; m_showingNodeNumbers = !m_showingNodeNumbers;
if (m_showingNodeNumbers) if (m_showingNodeNumbers)
@ -161,6 +197,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
} }
break; break;
// insert new node
case Qt::Key_Insert: case Qt::Key_Insert:
if (!m_activeNode) if (!m_activeNode)
{ {
@ -175,6 +212,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
} }
break; break;
// used in node selection mode, to select node with numbers/enter
case Qt::Key_0: case Qt::Key_0:
case Qt::Key_1: case Qt::Key_1:
case Qt::Key_2: case Qt::Key_2:
@ -194,6 +232,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
break; break;
// delete one letter back in node selection
case Qt::Key_Backspace: case Qt::Key_Backspace:
if (!m_showingNodeNumbers) if (!m_showingNodeNumbers)
break; break;
@ -215,8 +254,10 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
} }
break; break;
// in node selection select node if nudenum = enterednum
case Qt::Key_Return: case Qt::Key_Return:
case Qt::Key_Enter: case Qt::Key_Enter:
if (m_followNode && m_showingNodeNumbers) if (m_followNode && m_showingNodeNumbers)
{ {
showingAllNodeNumbers(false); showingAllNodeNumbers(false);
@ -226,8 +267,28 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_activeNode->setActive(); m_activeNode->setActive();
m_showingNodeNumbers = false; 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<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break; break;
// delete node
case Qt::Key_Delete: case Qt::Key_Delete:
if (m_activeNode) if (m_activeNode)
@ -259,6 +320,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
} }
} }
void GraphWidget::wheelEvent(QWheelEvent *event) void GraphWidget::wheelEvent(QWheelEvent *event)
{ {
scaleView(pow((double)2, -event->delta() / 240.0)); scaleView(pow((double)2, -event->delta() / 240.0));
@ -302,7 +365,6 @@ void GraphWidget::insertNode()
{ {
double angle(m_activeNode->calculateBiggestAngle()); double angle(m_activeNode->calculateBiggestAngle());
qDebug() << "got angle: " << angle;
qreal length(100); qreal length(100);

@ -43,6 +43,7 @@ private:
bool m_showingNodeNumbers; bool m_showingNodeNumbers;
QString m_followNumber; QString m_followNumber;
Node *m_followNode; Node *m_followNode;
bool m_editingNode;
}; };
#endif // GRAPHWIDGET_H #endif // GRAPHWIDGET_H

@ -15,6 +15,7 @@ Node::Node(GraphWidget *parent) :
m_numberIsSpecial(false) m_numberIsSpecial(false)
{ {
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges); setFlag(ItemSendsGeometryChanges);
// setTextInteractionFlags(Qt::TextBrowserInteraction); // setTextInteractionFlags(Qt::TextBrowserInteraction);
// setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard); // setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard);
@ -27,6 +28,8 @@ Node::Node(GraphWidget *parent) :
// shall I set it after some spec key? // shall I set it after some spec key?
// setTextInteractionFlags(Qt::TextEditorInteraction); // setTextInteractionFlags(Qt::TextEditorInteraction);
setOpenExternalLinks(true);
} }
Node::~Node() Node::~Node()
@ -129,12 +132,12 @@ void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
QGraphicsItem::mousePressEvent(event); QGraphicsItem::mousePressEvent(event);
} }
void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) //void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{ //{
m_graph->insertNode(); // m_graph->insertNode();
QGraphicsItem::mouseDoubleClickEvent(event); // QGraphicsItem::mouseDoubleClickEvent(event);
} //}
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{ {
@ -217,3 +220,20 @@ double Node::doubleModulo(const double &devided, const double &devisor)
{ {
return devided - static_cast<double>(devisor * static_cast<int>(devided / devisor)); return devided - static_cast<double>(devisor * static_cast<int>(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();
}

@ -22,6 +22,10 @@ public:
void showNumber(const int &number, const bool& show = true, void showNumber(const int &number, const bool& show = true,
const bool &numberIsSpecial = false); const bool &numberIsSpecial = false);
double calculateBiggestAngle(); double calculateBiggestAngle();
void setEditable(const bool &editable = true);
// changing visibility from prot to pub
void keyPressEvent(QKeyEvent *event);
protected: protected:
@ -29,7 +33,7 @@ protected:
QWidget *widget); QWidget *widget);
QVariant itemChange(GraphicsItemChange change, const QVariant &value); QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); //void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void linkActivated(const QString &link); void linkActivated(const QString &link);

Loading…
Cancel
Save