active node can be deleted with DEL. Code is getting messy, time to some refactor

master
Denes Matetelki 14 years ago
parent 6e0524f820
commit 080c5aac6a

@ -22,6 +22,12 @@ Edge::Edge(Node *sourceNode, Node *destNode)
// setZValue(1); // setZValue(1);
} }
Edge::~Edge()
{
m_sourceNode->removeEdge(this);
m_destNode->removeEdge(this);
}
Node *Edge::sourceNode() const Node *Edge::sourceNode() const
{ {
return m_sourceNode; return m_sourceNode;

@ -10,6 +10,7 @@ class Edge : public QGraphicsItem
public: public:
Edge(Node *sourceNode, Node *destNode); Edge(Node *sourceNode, Node *destNode);
~Edge();
Node *sourceNode() const; Node *sourceNode() const;
Node *destNode() const; Node *destNode() const;

@ -8,7 +8,9 @@
GraphWidget::GraphWidget(QWidget *parent) : GraphWidget::GraphWidget(QWidget *parent) :
QGraphicsView(parent), QGraphicsView(parent),
m_showingNodeNumbers(false) m_activeNode(0),
m_showingNodeNumbers(false),
m_followNode(0)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
@ -124,16 +126,20 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
switch (event->key()) { switch (event->key()) {
case Qt::Key_Up: case Qt::Key_Up:
m_activeNode->moveBy(0, -20); if (m_activeNode)
m_activeNode->moveBy(0, -20);
break; break;
case Qt::Key_Down: case Qt::Key_Down:
m_activeNode->moveBy(0, 20); if (m_activeNode)
m_activeNode->moveBy(0, 20);
break; break;
case Qt::Key_Left: case Qt::Key_Left:
m_activeNode->moveBy(-20, 0); if (m_activeNode)
m_activeNode->moveBy(-20, 0);
break; break;
case Qt::Key_Right: case Qt::Key_Right:
m_activeNode->moveBy(20, 0); if (m_activeNode)
m_activeNode->moveBy(20, 0);
break; break;
case Qt::Key_Plus: case Qt::Key_Plus:
scaleView(qreal(1.2)); scaleView(qreal(1.2));
@ -147,6 +153,11 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
m_followNumber.clear(); m_followNumber.clear();
showingAllNodeNumbers(m_showingNodeNumbers); showingAllNodeNumbers(m_showingNodeNumbers);
if (m_showingNodeNumbers)
{
m_nodeList.first()->showNumber(0,true,true);
m_followNode = m_nodeList.first();
}
break; break;
case Qt::Key_Insert: case Qt::Key_Insert:
@ -182,6 +193,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
if (m_followNumber.isEmpty()) if (m_followNumber.isEmpty())
{ {
showingAllNodeNumbers(true); showingAllNodeNumbers(true);
m_nodeList.first()->showNumber(0,true,true);
m_followNode = m_nodeList.first();
} }
else else
{ {
@ -193,16 +206,40 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
case Qt::Key_Return: case Qt::Key_Return:
case Qt::Key_Enter: case Qt::Key_Enter:
if (m_followNode) if (m_followNode && m_showingNodeNumbers)
{ {
qDebug() << m_activeNode;
qDebug() << m_followNode;
showingAllNodeNumbers(false); showingAllNodeNumbers(false);
m_activeNode->setActive(false); if (m_activeNode)
m_activeNode->setActive(false);
m_activeNode = m_followNode; m_activeNode = m_followNode;
m_activeNode->setActive(); m_activeNode->setActive();
m_showingNodeNumbers = false; m_showingNodeNumbers = false;
} }
break; 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: default:
QGraphicsView::keyPressEvent(event); QGraphicsView::keyPressEvent(event);
} }
@ -283,7 +320,9 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number, const boo
{ {
qDebug() << "set active"; qDebug() << "set active";
showingAllNodeNumbers(false); showingAllNodeNumbers(false);
m_activeNode->setActive(false); if (m_activeNode)
m_activeNode->setActive(false);
m_activeNode = m_followNode; m_activeNode = m_followNode;
m_activeNode->setActive(); m_activeNode->setActive();
m_showingNodeNumbers = false; m_showingNodeNumbers = false;

@ -26,6 +26,12 @@ Node::Node(GraphWidget *parent) :
// setTextInteractionFlags(Qt::TextEditorInteraction); // setTextInteractionFlags(Qt::TextEditorInteraction);
} }
Node::~Node()
{
qDebug() << __PRETTY_FUNCTION__;
foreach (Edge *edge, m_edgeList) delete edge;
}
void Node::addEdge(Edge *edge) void Node::addEdge(Edge *edge)
{ {
qDebug() << __PRETTY_FUNCTION__; qDebug() << __PRETTY_FUNCTION__;
@ -34,6 +40,12 @@ void Node::addEdge(Edge *edge)
edge->adjust(); edge->adjust();
} }
void Node::removeEdge(Edge *edge)
{
qDebug() << __PRETTY_FUNCTION__;
m_edgeList.removeAll(edge);
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{ {

@ -13,8 +13,10 @@ class Node : public QGraphicsTextItem
public: public:
Node(GraphWidget *graphWidget = 0); Node(GraphWidget *graphWidget = 0);
~Node();
void addEdge(Edge *edge); void addEdge(Edge *edge);
void removeEdge(Edge *edge);
// QList<Edge *> edges() const; // QList<Edge *> edges() const;
void setActive(const bool &active = true); void setActive(const bool &active = true);
void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false); void showNumber(const int &number, const bool& show = true, const bool &numberIsSpecial = false);

Loading…
Cancel
Save