removing edge like adding one, start deletion with 'd'

master
Denes Matetelki 14 years ago
parent 0565dd3c8a
commit 2797afa329

@ -17,7 +17,8 @@ GraphWidget::GraphWidget(QWidget *parent) :
m_showingNodeNumbers(false),
m_hintNode(0),
m_editingNode(false),
m_edgeAdding(false)
m_edgeAdding(false),
m_edgeDeleting(false)
{
m_scene = new QGraphicsScene(this);
m_scene->setItemIndexMethod(QGraphicsScene::NoIndex);
@ -158,6 +159,13 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
tr("Edge adding cancelled"),
5000); // millisec
}
else if (m_edgeDeleting)
{
m_edgeDeleting = false;
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Edge deleting cancelled"),
5000); // millisec
}
else if(m_showingNodeNumbers)
{
m_hintNumber.clear();
@ -284,6 +292,12 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
if (m_edgeAdding)
{
m_scene->addItem(new Edge(m_activeNode, m_hintNode));
m_edgeAdding = false;
}
if (m_edgeDeleting)
{
m_activeNode->removeEdge(m_hintNode);
m_edgeDeleting = false;
}
else // selecting
{
@ -344,7 +358,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
if (m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Add node: select destination node"),
tr("Add edge: select destination node"),
5000); // millisec
m_edgeAdding = true;
@ -358,6 +372,26 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
break;
// add edge to active node
case Qt::Key_D:
if (m_activeNode)
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("Delete edge: select destination node"),
5000); // millisec
m_edgeDeleting = true;
}
else
{
dynamic_cast<MainWindow *>(m_parent)->getStatusBar()->showMessage(
tr("No active node."),
5000); // millisec
}
break;
default:
QGraphicsView::keyPressEvent(event);
}
@ -466,6 +500,11 @@ void GraphWidget::showingNodeNumbersBeginWithNumber(const int &number,
m_scene->addItem(new Edge(m_activeNode, m_hintNode));
m_edgeAdding = false;
}
if (m_edgeDeleting)
{
m_activeNode->removeEdge(m_hintNode);
m_edgeDeleting = false;
}
else // selecting
{
if (m_activeNode)
@ -502,6 +541,11 @@ void GraphWidget::nodeSelected(Node *node)
m_scene->addItem(new Edge(m_activeNode, node));
m_edgeAdding = false;
}
if (m_edgeDeleting)
{
m_activeNode->removeEdge(node);
m_edgeDeleting = false;
}
else
{
setActiveNode(node);

@ -47,6 +47,7 @@ private:
Node *m_hintNode;
bool m_editingNode;
bool m_edgeAdding;
bool m_edgeDeleting;
};
#endif // GRAPHWIDGET_H

@ -48,6 +48,22 @@ void Node::removeEdge(Edge *edge)
}
}
void Node::removeEdge(Node *otherEnd)
{
qDebug() << __PRETTY_FUNCTION__;
for(QList<EdgeElement>::iterator it = m_edgeList.begin();
it != m_edgeList.end(); it++)
{
if ((it->edge->sourceNode() == otherEnd && it->edge->destNode() == this)
|| (it->edge->sourceNode() == this && it->edge->destNode() == otherEnd))
{
delete it->edge;
return;
}
}
}
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change) {

@ -18,6 +18,7 @@ public:
void addEdge(Edge *edge, bool startsFromThisNode);
void removeEdge(Edge *edge);
void removeEdge(Node *otherEnd);
void setBorder(const bool &hasBorder);
void setActive(const bool &active = true);
void showNumber(const int &number, const bool& show = true,

Loading…
Cancel
Save