From 6e0524f820fa51bc28c57f544708eaf35827e7a6 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 9 Jun 2011 14:11:35 +0200 Subject: [PATCH 1/5] nodes are selectable with vimperator-like follow keys --- edge.cpp | 5 +- graphwidget.cpp | 158 ++++++++++++++++++++++++++++++++++++++++++++---- graphwidget.h | 8 ++- mainwindow.ui | 12 ++++ node.cpp | 38 ++++++++---- node.h | 8 ++- 6 files changed, 200 insertions(+), 29 deletions(-) diff --git a/edge.cpp b/edge.cpp index e602999..4b8535e 100644 --- a/edge.cpp +++ b/edge.cpp @@ -32,7 +32,10 @@ Node *Edge::destNode() const return m_destNode; } -/// @note This is brute force. Isn't there a simple fv for this? +/** @note This is brute force. Isn't there a simple fv for this? + * The precision is not the best either + */ + QPointF firstNotContainedPoint(const QLineF &line, const QRectF &rect, bool reverse = false) diff --git a/graphwidget.cpp b/graphwidget.cpp index 6f65cf7..985f15e 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -8,7 +8,6 @@ GraphWidget::GraphWidget(QWidget *parent) : QGraphicsView(parent), - m_activeNode(0), m_showingNodeNumbers(false) { qDebug() << __PRETTY_FUNCTION__; @@ -59,14 +58,58 @@ GraphWidget::GraphWidget(QWidget *parent) : node6->setPos(-10, 100); m_nodeList.append(node6); + Node *node7 = new Node(this); + node7->setHtml(QString("node

important

shit")); + m_scene->addItem(node7); + node7->setPos(-200, 50); + m_nodeList.append(node7); + + Node *node8 = new Node(this); + node8->setHtml(QString("more than\none lins")); + m_scene->addItem(node8); + node8->setPos(50, -80); + m_nodeList.append(node8); + + Node *node9 = new Node(this); + node9->setHtml(QString("iam a bald and italian guy")); + m_scene->addItem(node9); + node9->setPos(50, 40); + m_nodeList.append(node9); + + Node *node10 = new Node(this); + node10->setHtml(QString("no joke")); + m_scene->addItem(node10); + node10->setPos(-160, -10); + m_nodeList.append(node10); + + Node *node11 = new Node(this); + node11->setHtml(QString("salalal")); + m_scene->addItem(node11); + node11->setPos(-120, -120); + m_nodeList.append(node11); + + Node *node12 = new Node(this); + node12->setHtml(QString("lalalala")); + m_scene->addItem(node12); + node12->setPos(130, -10); + m_nodeList.append(node12);; + m_scene->addItem(new Edge(node1, node2)); m_scene->addItem(new Edge(node1, node3)); m_scene->addItem(new Edge(node3, node4)); m_scene->addItem(new Edge(node1, node5)); m_scene->addItem(new Edge(node5, node6)); + m_scene->addItem(new Edge(node4, node11)); + m_scene->addItem(new Edge(node2, node12)); + m_scene->addItem(new Edge(node3, node10)); + m_scene->addItem(new Edge(node10, node7)); + m_scene->addItem(new Edge(node5, node7)); + m_scene->addItem(new Edge(node2, node8)); + m_scene->addItem(new Edge(node2, node9)); + m_activeNode = m_nodeList.first(); - m_activeNode->setActive(true); + m_activeNode->setActive(); } QGraphicsScene *GraphWidget::getScene() @@ -77,7 +120,7 @@ QGraphicsScene *GraphWidget::getScene() void GraphWidget::keyPressEvent(QKeyEvent *event) { qDebug() << __PRETTY_FUNCTION__; -// qDebug() << event->key(); + qDebug() << event->key(); switch (event->key()) { case Qt::Key_Up: @@ -99,12 +142,15 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) scaleView(1 / qreal(1.2)); break; case Qt::Key_F: - { // need brackets because of local variable m_showingNodeNumbers = !m_showingNodeNumbers; - int i =0; - for (QList::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++) - dynamic_cast(*it)->showNumber(i,m_showingNodeNumbers); - } + if (m_showingNodeNumbers) + m_followNumber.clear(); + + showingAllNodeNumbers(m_showingNodeNumbers); + break; + + case Qt::Key_Insert: + insertNode(); break; case Qt::Key_0: @@ -117,11 +163,44 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) case Qt::Key_7: case Qt::Key_8: case Qt::Key_9: + if (!m_showingNodeNumbers) + break; + + m_followNumber.append(QString::number(event->key()-48)); + showingAllNodeNumbers(false); + showingNodeNumbersBeginWithNumber(m_followNumber.toInt(), true); + + break; - m_activeNode->setActive(false); - m_activeNode = m_nodeList[3]; - m_activeNode->setActive(true); + case Qt::Key_Backspace: + if (!m_showingNodeNumbers) + break; + if (!m_followNumber.isEmpty()) + { + m_followNumber.remove(m_followNumber.length()-1,1); + if (m_followNumber.isEmpty()) + { + showingAllNodeNumbers(true); + } + else + { + showingAllNodeNumbers(false); + showingNodeNumbersBeginWithNumber(m_followNumber.toInt(), true); + } + } + break; + + case Qt::Key_Return: + case Qt::Key_Enter: + if (m_followNode) + { + showingAllNodeNumbers(false); + m_activeNode->setActive(false); + m_activeNode = m_followNode; + m_activeNode->setActive(); + m_showingNodeNumbers = false; + } break; default: @@ -158,7 +237,6 @@ void GraphWidget::scaleView(qreal scaleFactor) scale(scaleFactor, scaleFactor); } -/// @note this == 0? how is it possible? void GraphWidget::setActiveNode(Node *node) { qDebug() << __PRETTY_FUNCTION__; @@ -167,5 +245,59 @@ void GraphWidget::setActiveNode(Node *node) m_activeNode->setActive(false); m_activeNode = node; - m_activeNode->setActive(true); + m_activeNode->setActive(); +} + +void GraphWidget::insertNode() +{ + qDebug() << __PRETTY_FUNCTION__; +} + +void GraphWidget::showingAllNodeNumbers(const bool &show) +{ + int i =0; + for (QList::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++) + dynamic_cast(*it)->showNumber(i,show); +} + +void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const bool &show) +{ + int i(0); + int hit(0); + for (QList::const_iterator it = m_nodeList.begin(); it != m_nodeList.end(); it++, i++) + { + if (i == number) + { + hit++; + dynamic_cast(*it)->showNumber(i,show,true); + m_followNode = dynamic_cast(*it); + continue; + } + if (numberStartsWithNumber(i, number)) + { + hit++; + dynamic_cast(*it)->showNumber(i,show); + } + } + if (hit==1) + { + qDebug() << "set active"; + showingAllNodeNumbers(false); + m_activeNode->setActive(false); + m_activeNode = m_followNode; + m_activeNode->setActive(); + m_showingNodeNumbers = false; + } + + /// @bug handle this case +// else if (hit == 0) +// { + + +// } +} + +bool GraphWidget::numberStartsWithNumber(const int &number, const int &prefix) +{ + return (QString::number(number)).startsWith(QString::number(prefix)); } diff --git a/graphwidget.h b/graphwidget.h index f9cb66e..b420aed 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -17,6 +17,7 @@ public: GraphWidget(QWidget *parent = 0); QGraphicsScene *getScene(); void setActiveNode(Node *node); + void insertNode(); protected: @@ -28,11 +29,16 @@ protected: private: + void showingAllNodeNumbers(const bool &show = true); + void showingNodeNumbersBeginWithNumber(const int &number, const bool &show = true); + bool numberStartsWithNumber(const int &number, const int &prefix); + QList m_nodeList; Node *m_activeNode; QGraphicsScene *m_scene; bool m_showingNodeNumbers; - + QString m_followNumber; + Node *m_followNode; }; diff --git a/mainwindow.ui b/mainwindow.ui index 4485505..029f7d3 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -55,6 +55,9 @@ + + false + &New @@ -63,6 +66,9 @@ + + false + &Open @@ -71,6 +77,9 @@ + + false + &Save @@ -92,6 +101,9 @@ + + false + &Close diff --git a/node.cpp b/node.cpp index 779e00a..a121c27 100644 --- a/node.cpp +++ b/node.cpp @@ -8,8 +8,9 @@ Node::Node(GraphWidget *parent) : m_graph(parent), m_isActive(false), - m_activeEdge(0), - m_number(-1) +// m_activeEdge(0), + m_number(-1), + m_numberIsSpecial(false) { qDebug() << __PRETTY_FUNCTION__; @@ -72,7 +73,13 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *w) { - qDebug() << __PRETTY_FUNCTION__; +// qDebug() << __PRETTY_FUNCTION__; + + if (m_number != -1) + { + painter->setBackground(m_numberIsSpecial ? Qt::green : Qt::yellow); + painter->setBackgroundMode(Qt::OpaqueMode); + } QGraphicsTextItem::paint(painter, option, w); @@ -82,12 +89,10 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid boundingRect().bottomRight().toPoint() - QPoint(1,1))); - qDebug() << m_number; - if (m_number != -1) { - painter->setPen(Qt::yellow); - painter->setBackground(Qt::black); + painter->setPen(Qt::white); + painter->setBackground(Qt::red); painter->setBackgroundMode(Qt::OpaqueMode); painter->drawText(boundingRect().topLeft()+QPointF(0,11), QString("%1").arg(m_number)); } @@ -102,18 +107,28 @@ void Node::setActive(const bool &active) } +/// @note who shall set active: press or release? void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug() << __PRETTY_FUNCTION__; + m_graph->setActiveNode(this); + QGraphicsItem::mousePressEvent(event); } -void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { qDebug() << __PRETTY_FUNCTION__; - m_graph->setActiveNode(this); + m_graph->insertNode(); + + QGraphicsItem::mouseDoubleClickEvent(event); +} + +void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + qDebug() << __PRETTY_FUNCTION__; QGraphicsItem::mouseReleaseEvent(event); } @@ -125,10 +140,11 @@ void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) QGraphicsItem::mouseMoveEvent(event); } -void Node::showNumber(const int &number, const bool& show) +void Node::showNumber(const int &number, const bool& show, const bool &numberIsSpecial) { - qDebug() << __PRETTY_FUNCTION__; +// qDebug() << __PRETTY_FUNCTION__; m_number = show ? number : -1; + m_numberIsSpecial = numberIsSpecial; update(); } diff --git a/node.h b/node.h index 113cfe2..6068fad 100644 --- a/node.h +++ b/node.h @@ -16,8 +16,8 @@ public: void addEdge(Edge *edge); // QList edges() const; - void setActive(const bool &active); - void showNumber(const int &number, const bool& show); + void setActive(const bool &active = true); + void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false); protected: @@ -25,6 +25,7 @@ protected: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QVariant itemChange(GraphicsItemChange change, const QVariant &value); void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); @@ -33,8 +34,9 @@ private: QList m_edgeList; GraphWidget *m_graph; bool m_isActive; - Edge *m_activeEdge; +// Edge *m_activeEdge; int m_number; + bool m_numberIsSpecial; }; #endif // NODE_H From 080c5aac6aa8e9425198e118b8294e250dff20b4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 9 Jun 2011 14:55:16 +0200 Subject: [PATCH 2/5] active node can be deleted with DEL. Code is getting messy, time to some refactor --- edge.cpp | 6 ++++++ edge.h | 1 + graphwidget.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++------- node.cpp | 12 +++++++++++ node.h | 2 ++ 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/edge.cpp b/edge.cpp index 4b8535e..69ff306 100644 --- a/edge.cpp +++ b/edge.cpp @@ -22,6 +22,12 @@ Edge::Edge(Node *sourceNode, Node *destNode) // setZValue(1); } +Edge::~Edge() +{ + m_sourceNode->removeEdge(this); + m_destNode->removeEdge(this); +} + Node *Edge::sourceNode() const { return m_sourceNode; diff --git a/edge.h b/edge.h index 3d4c4b0..7e94c8f 100644 --- a/edge.h +++ b/edge.h @@ -10,6 +10,7 @@ class Edge : public QGraphicsItem public: Edge(Node *sourceNode, Node *destNode); + ~Edge(); Node *sourceNode() const; Node *destNode() const; diff --git a/graphwidget.cpp b/graphwidget.cpp index 985f15e..ca3fdc3 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -8,7 +8,9 @@ GraphWidget::GraphWidget(QWidget *parent) : QGraphicsView(parent), - m_showingNodeNumbers(false) + m_activeNode(0), + m_showingNodeNumbers(false), + m_followNode(0) { qDebug() << __PRETTY_FUNCTION__; @@ -124,16 +126,20 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) switch (event->key()) { case Qt::Key_Up: - m_activeNode->moveBy(0, -20); + if (m_activeNode) + m_activeNode->moveBy(0, -20); break; case Qt::Key_Down: - m_activeNode->moveBy(0, 20); + if (m_activeNode) + m_activeNode->moveBy(0, 20); break; case Qt::Key_Left: - m_activeNode->moveBy(-20, 0); + if (m_activeNode) + m_activeNode->moveBy(-20, 0); break; case Qt::Key_Right: - m_activeNode->moveBy(20, 0); + if (m_activeNode) + m_activeNode->moveBy(20, 0); break; case Qt::Key_Plus: scaleView(qreal(1.2)); @@ -147,6 +153,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) m_followNumber.clear(); showingAllNodeNumbers(m_showingNodeNumbers); + if (m_showingNodeNumbers) + { + m_nodeList.first()->showNumber(0,true,true); + m_followNode = m_nodeList.first(); + } break; case Qt::Key_Insert: @@ -182,6 +193,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) if (m_followNumber.isEmpty()) { showingAllNodeNumbers(true); + m_nodeList.first()->showNumber(0,true,true); + m_followNode = m_nodeList.first(); } else { @@ -193,16 +206,40 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) case Qt::Key_Return: case Qt::Key_Enter: - if (m_followNode) + if (m_followNode && m_showingNodeNumbers) { + qDebug() << m_activeNode; + qDebug() << m_followNode; showingAllNodeNumbers(false); - m_activeNode->setActive(false); + if (m_activeNode) + m_activeNode->setActive(false); m_activeNode = m_followNode; m_activeNode->setActive(); m_showingNodeNumbers = false; } break; + case Qt::Key_Delete: + + if (m_activeNode) + { + if (m_followNode==m_activeNode) + m_followNode=0; + + m_nodeList.removeAll(m_activeNode); + delete m_activeNode; + m_activeNode = 0; + + /// @bug + if (m_showingNodeNumbers) + { + m_showingNodeNumbers = false; + showingAllNodeNumbers(false); + } + } + + break; + default: QGraphicsView::keyPressEvent(event); } @@ -283,7 +320,9 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const boo { qDebug() << "set active"; showingAllNodeNumbers(false); - m_activeNode->setActive(false); + if (m_activeNode) + m_activeNode->setActive(false); + m_activeNode = m_followNode; m_activeNode->setActive(); m_showingNodeNumbers = false; diff --git a/node.cpp b/node.cpp index a121c27..3687acb 100644 --- a/node.cpp +++ b/node.cpp @@ -26,6 +26,12 @@ Node::Node(GraphWidget *parent) : // setTextInteractionFlags(Qt::TextEditorInteraction); } +Node::~Node() +{ + qDebug() << __PRETTY_FUNCTION__; + foreach (Edge *edge, m_edgeList) delete edge; +} + void Node::addEdge(Edge *edge) { qDebug() << __PRETTY_FUNCTION__; @@ -34,6 +40,12 @@ void Node::addEdge(Edge *edge) edge->adjust(); } +void Node::removeEdge(Edge *edge) +{ + qDebug() << __PRETTY_FUNCTION__; + + m_edgeList.removeAll(edge); +} QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { diff --git a/node.h b/node.h index 6068fad..a6270d1 100644 --- a/node.h +++ b/node.h @@ -13,8 +13,10 @@ class Node : public QGraphicsTextItem public: Node(GraphWidget *graphWidget = 0); + ~Node(); void addEdge(Edge *edge); + void removeEdge(Edge *edge); // QList edges() const; void setActive(const bool &active = true); void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false); From 4233f7e00dda8cff6f22bf61357c22ad670b89e3 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 10 Jun 2011 13:20:51 +0200 Subject: [PATCH 3/5] 'No active node' message is printed to statusbar upon attempt to delete when there is no active node --- .gitignore | 3 +++ graphwidget.cpp | 14 ++++++++++++-- graphwidget.h | 1 + mainwindow.cpp | 7 ++++++- mainwindow.h | 1 + 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 8df47d5..041ce7d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.qm +*.o +Makefile +ui_* diff --git a/graphwidget.cpp b/graphwidget.cpp index ca3fdc3..d1d2dcf 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -1,13 +1,17 @@ #include "graphwidget.h" + #include -#include "node.h" +#include + -//#include +#include "node.h" #include "edge.h" #include "math.h" +#include "mainwindow.h" GraphWidget::GraphWidget(QWidget *parent) : QGraphicsView(parent), + m_parent(parent), m_activeNode(0), m_showingNodeNumbers(false), m_followNode(0) @@ -237,6 +241,12 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) showingAllNodeNumbers(false); } } + else + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("No active node "), + 5000); // millisec + } break; diff --git a/graphwidget.h b/graphwidget.h index b420aed..261ed3a 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -34,6 +34,7 @@ private: bool numberStartsWithNumber(const int &number, const int &prefix); QList m_nodeList; + QWidget *m_parent; Node *m_activeNode; QGraphicsScene *m_scene; bool m_showingNodeNumbers; diff --git a/mainwindow.cpp b/mainwindow.cpp index 5140074..45a29c3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -47,7 +47,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_ui->actionAbout_QtMindMap, SIGNAL(activated()), this, SLOT(about())); - m_graphicsView = new GraphWidget(m_ui->centralWidget); + m_graphicsView = new GraphWidget(this); setCentralWidget(m_graphicsView); } @@ -120,3 +120,8 @@ void MainWindow::aboutDestroyed() setEnabled(true); } + +QStatusBar * MainWindow::getStatusBar() +{ + return m_ui->statusBar; +} diff --git a/mainwindow.h b/mainwindow.h index 483bf36..cd54171 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -18,6 +18,7 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + QStatusBar * getStatusBar(); public slots: void klakk(); From bd29b02364ad9f57cef7ac103e5f6465523e0e15 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 10 Jun 2011 13:24:46 +0200 Subject: [PATCH 4/5] hide all numbers when the typed number does not exists --- graphwidget.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/graphwidget.cpp b/graphwidget.cpp index d1d2dcf..0a61096 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -234,7 +234,6 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) delete m_activeNode; m_activeNode = 0; - /// @bug if (m_showingNodeNumbers) { m_showingNodeNumbers = false; @@ -337,13 +336,11 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const boo m_activeNode->setActive(); m_showingNodeNumbers = false; } - - /// @bug handle this case -// else if (hit == 0) -// { - - -// } + else if (hit == 0) + { + m_showingNodeNumbers = false; + showingAllNodeNumbers(false); + } } bool GraphWidget::numberStartsWithNumber(const int &number, const int &prefix) From 0095e1e59b946c362713321d4d9feaac9212afa3 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 10 Jun 2011 21:05:26 +0200 Subject: [PATCH 5/5] calculate biggest angle (yet bugous) algoritm --- edge.cpp | 27 +++++++++++----- edge.h | 3 +- graphwidget.cpp | 39 +++++++++++++++++++++-- graphwidget.h | 1 + node.cpp | 79 +++++++++++++++++++++++++++++++++++++++++----- node.h | 21 ++++++++++-- qtmindmap.pro.user | 2 +- 7 files changed, 148 insertions(+), 24 deletions(-) diff --git a/edge.cpp b/edge.cpp index 69ff306..6ee60c1 100644 --- a/edge.cpp +++ b/edge.cpp @@ -16,8 +16,8 @@ Edge::Edge(Node *sourceNode, Node *destNode) setAcceptedMouseButtons(0); m_sourceNode = sourceNode; m_destNode = destNode; - m_sourceNode->addEdge(this); - m_destNode->addEdge(this); + m_sourceNode->addEdge(this,true); + m_destNode->addEdge(this,false); adjust(); // setZValue(1); } @@ -113,15 +113,26 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) return; // Draw the arrows - double angle = ::acos(line.dx() / line.length()); + m_angle = ::acos(line.dx() / line.length()); if (line.dy() >= 0) - angle = TwoPi - angle; + m_angle = TwoPi - m_angle; - QPointF destArrowP1 = m_destPoint + QPointF(sin(angle - Pi / 3) * m_arrowSize, - cos(angle - Pi / 3) * m_arrowSize); - QPointF destArrowP2 = m_destPoint + QPointF(sin(angle - Pi + Pi / 3) * m_arrowSize, - cos(angle - Pi + Pi / 3) * m_arrowSize); + qDebug() << m_angle; + + QPointF destArrowP1 = m_destPoint + QPointF(sin(m_angle - Pi / 3) * m_arrowSize, + cos(m_angle - Pi / 3) * m_arrowSize); + QPointF destArrowP2 = m_destPoint + QPointF(sin(m_angle - Pi + Pi / 3) * m_arrowSize, + cos(m_angle - Pi + Pi / 3) * m_arrowSize); painter->setBrush(Qt::black); painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); } + +double Edge::getAngle() const +{ + qDebug() << __PRETTY_FUNCTION__; + + qDebug() << m_angle; + + return m_angle; +} diff --git a/edge.h b/edge.h index 7e94c8f..8d5de87 100644 --- a/edge.h +++ b/edge.h @@ -14,6 +14,7 @@ public: Node *sourceNode() const; Node *destNode() const; + double getAngle() const; void adjust(); @@ -30,7 +31,7 @@ private: QPointF m_sourcePoint; QPointF m_destPoint; qreal m_arrowSize; - + double m_angle; }; #endif diff --git a/graphwidget.cpp b/graphwidget.cpp index 0a61096..cef7d3d 100644 --- a/graphwidget.cpp +++ b/graphwidget.cpp @@ -9,6 +9,8 @@ #include "math.h" #include "mainwindow.h" + + GraphWidget::GraphWidget(QWidget *parent) : QGraphicsView(parent), m_parent(parent), @@ -165,7 +167,17 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) break; case Qt::Key_Insert: - insertNode(); + if (!m_activeNode) + { + dynamic_cast(m_parent)->getStatusBar()->showMessage( + tr("No active node."), + 5000); // millisec + + } + else + { + insertNode(); + } break; case Qt::Key_0: @@ -243,7 +255,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event) else { dynamic_cast(m_parent)->getStatusBar()->showMessage( - tr("No active node "), + tr("No active node."), 5000); // millisec } @@ -297,6 +309,28 @@ void GraphWidget::setActiveNode(Node *node) void GraphWidget::insertNode() { qDebug() << __PRETTY_FUNCTION__; + + double angle(m_activeNode->calculateBiggestAngle()); + + qreal length(100); + + qDebug() << "angle: "; + qDebug() << angle; + + QPointF pos(length * cos(angle), length * sin(angle)); + + + qDebug() << pos; + + Node *node = new Node(this); + node->setHtml(QString("new node")); + m_scene->addItem(node); + node->setPos(m_activeNode->pos() + +// m_activeNode->boundingRect().center() + + pos); + m_nodeList.append(node); + + m_scene->addItem(new Edge(m_activeNode, node)); } void GraphWidget::showingAllNodeNumbers(const bool &show) @@ -327,7 +361,6 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const boo } if (hit==1) { - qDebug() << "set active"; showingAllNodeNumbers(false); if (m_activeNode) m_activeNode->setActive(false); diff --git a/graphwidget.h b/graphwidget.h index 261ed3a..7cc17a5 100644 --- a/graphwidget.h +++ b/graphwidget.h @@ -32,6 +32,7 @@ private: void showingAllNodeNumbers(const bool &show = true); void showingNodeNumbersBeginWithNumber(const int &number, const bool &show = true); bool numberStartsWithNumber(const int &number, const int &prefix); + qreal calculateBiggestAngle(Node *node); QList m_nodeList; QWidget *m_parent; diff --git a/node.cpp b/node.cpp index 3687acb..a9bf5cd 100644 --- a/node.cpp +++ b/node.cpp @@ -5,10 +5,11 @@ #include #include +static const double Pi = 3.14159265358979323846264338327950288419717; + Node::Node(GraphWidget *parent) : m_graph(parent), m_isActive(false), -// m_activeEdge(0), m_number(-1), m_numberIsSpecial(false) { @@ -29,14 +30,14 @@ Node::Node(GraphWidget *parent) : Node::~Node() { qDebug() << __PRETTY_FUNCTION__; - foreach (Edge *edge, m_edgeList) delete edge; + foreach (EdgeElement element, m_edgeList) delete element.edge; } -void Node::addEdge(Edge *edge) +void Node::addEdge(Edge *edge, bool startsFromThisNode) { qDebug() << __PRETTY_FUNCTION__; - m_edgeList << edge; + m_edgeList.push_back(EdgeElement(edge, startsFromThisNode)); edge->adjust(); } @@ -44,12 +45,17 @@ void Node::removeEdge(Edge *edge) { qDebug() << __PRETTY_FUNCTION__; - m_edgeList.removeAll(edge); + for(QList::iterator it = m_edgeList.begin(); it != m_edgeList.end(); it++) + if (it->edge == edge) + { + m_edgeList.erase(it); + return; + } } QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) { - qDebug() << __PRETTY_FUNCTION__; +// qDebug() << __PRETTY_FUNCTION__; switch (change) { @@ -74,7 +80,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) case ItemPositionHasChanged: - foreach (Edge *edge, m_edgeList) edge->adjust(); + foreach (EdgeElement element, m_edgeList) element.edge->adjust(); break; default: break; @@ -147,7 +153,7 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) void Node::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - qDebug() << __PRETTY_FUNCTION__; +// qDebug() << __PRETTY_FUNCTION__; QGraphicsItem::mouseMoveEvent(event); } @@ -160,3 +166,60 @@ void Node::showNumber(const int &number, const bool& show, const bool &numberIsS m_numberIsSpecial = numberIsSpecial; update(); } + +double Node::calculateBiggestAngle() +{ + qDebug() << __PRETTY_FUNCTION__; + + if (m_edgeList.empty()) + return 1.5 * Pi; + + if (m_edgeList.size()==1) + { + if (m_edgeList.first().startsFromThisNode) + { + return Pi - m_edgeList.first().edge->getAngle(); + } + else + { + return 2 * Pi - m_edgeList.first().edge->getAngle(); + } + } + + QList tmp; + for(QList::iterator it = m_edgeList.begin(); it != m_edgeList.end(); it++) + { + if (it->startsFromThisNode) + { + tmp.push_back(Pi - it->edge->getAngle()); + } + else + { + tmp.push_back(2 * Pi - it->edge->getAngle()); + } + } + + qSort(tmp.begin(), tmp.end()); + + qDebug() << tmp; + + double prev(tmp.last()); + double max_prev(tmp.last()); + double max(0); + + /// @bug algorith is baaad + for(QList::const_iterator it = tmp.begin(); it!=tmp.end(); it++) + { + if (abs(*it - prev) > abs(max) ) + { + max = *it - prev; + max_prev = prev; + } + prev = *it; + } + + qDebug() << max; + qDebug() << max_prev; + + return max_prev + max / 2 ; +} diff --git a/node.h b/node.h index a6270d1..d12226d 100644 --- a/node.h +++ b/node.h @@ -15,12 +15,12 @@ public: Node(GraphWidget *graphWidget = 0); ~Node(); - void addEdge(Edge *edge); + void addEdge(Edge *edge, bool startsFromThisNode); void removeEdge(Edge *edge); // QList edges() const; void setActive(const bool &active = true); void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false); - + double calculateBiggestAngle(); protected: @@ -33,7 +33,22 @@ protected: private: - QList m_edgeList; + struct EdgeElement + { + Edge *edge; + bool startsFromThisNode; + EdgeElement(Edge *e, bool s) : edge(e), startsFromThisNode(s) {} + }; +// class EdgeList : public QList +// { +// public: +// int compareItems(QCollection::Item a, QCollection::Item b) +// { +// return (EdgeElement)a.edge = (EdgeElement)b.edge; +// } +// }; + + QList m_edgeList; GraphWidget *m_graph; bool m_isActive; // Edge *m_activeEdge; diff --git a/qtmindmap.pro.user b/qtmindmap.pro.user index 675c486..a2bd180 100644 --- a/qtmindmap.pro.user +++ b/qtmindmap.pro.user @@ -182,7 +182,7 @@ qtmindmap.pro false - false + true 3768