calculate biggest angle (yet bugous) algoritm

master
Denes Matetelki 14 years ago
parent bd29b02364
commit 0095e1e59b

@ -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;
}

@ -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

@ -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:
if (!m_activeNode)
{
dynamic_cast<MainWindow *>(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<MainWindow *>(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);

@ -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<Node *> m_nodeList;
QWidget *m_parent;

@ -5,10 +5,11 @@
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
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<EdgeElement>::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<double> tmp;
for(QList<EdgeElement>::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<double>::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 ;
}

@ -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<Edge *> 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<Edge *> m_edgeList;
struct EdgeElement
{
Edge *edge;
bool startsFromThisNode;
EdgeElement(Edge *e, bool s) : edge(e), startsFromThisNode(s) {}
};
// class EdgeList : public QList<EdgeElement>
// {
// public:
// int compareItems(QCollection::Item a, QCollection::Item b)
// {
// return (EdgeElement)a.edge = (EdgeElement)b.edge;
// }
// };
QList<EdgeElement> m_edgeList;
GraphWidget *m_graph;
bool m_isActive;
// Edge *m_activeEdge;

@ -182,7 +182,7 @@
<value key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments" type="QString"></value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.ProFile" type="QString">qtmindmap.pro</value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix" type="bool">false</value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal" type="bool">false</value>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal" type="bool">true</value>
<valuelist key="Qt4ProjectManager.Qt4RunConfiguration.UserEnvironmentChanges" type="QVariantList"/>
<value key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory" type="QString"></value>
<value key="RunConfiguration.QmlDebugServerPort" type="uint">3768</value>

Loading…
Cancel
Save