From 0095e1e59b946c362713321d4d9feaac9212afa3 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 10 Jun 2011 21:05:26 +0200 Subject: [PATCH] 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