removing edges

for/release
dmatetelki 11 years ago
parent fa15a7c30a
commit a9e9fbf940

@ -108,17 +108,43 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
break;
QGraphicsItem* selectedItem = selectedItems.first();
m_graph->removeVertex(float2FromQPointF(selectedItem->pos()));
Node* selectedNode = dynamic_cast<Node*>(selectedItem);
QList<Edge *> edges_of_selected = selectedNode->edges();
for (Edge* edge : edges_of_selected) {
edge->sourceNode()->removeEdge(selectedNode);
edge->destNode()->removeEdge(selectedNode);
scene()->removeItem(edge);
}
scene()->removeItem(selectedItem);
const QPoint global_p = QCursor::pos();
const QPoint widget_p = mapFromGlobal(global_p);
const QPointF scene_p = mapToScene(widget_p);
/// @bug no hit deteced (hover works) on bottom right corner
QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p);
Node* node_under_mouse = dynamic_cast<Node*>(item_under_mouse);
if (node_under_mouse != 0 && node_under_mouse != selectedNode) { // remove edge
const float2 source_pos = float2FromQPointF(selectedItem->pos());
const float2 destination_pos = float2FromQPointF(node_under_mouse->pos());
if (!m_graph->connected(source_pos, destination_pos))
return;
m_graph->removeEdge(source_pos, destination_pos);
QList<Edge *> edges_of_selected = selectedNode->edges();
for (Edge* edge : edges_of_selected) {
if (edge->sourceNode() == node_under_mouse || edge->destNode() == node_under_mouse) {
selectedNode->removeEdge(edge);
node_under_mouse->removeEdge(edge);
scene()->removeItem(edge);
delete edge;
return;
}
}
} else { // remove node
m_graph->removeVertex(float2FromQPointF(selectedItem->pos()));
QList<Edge *> edges_of_selected = selectedNode->edges();
for (Edge* edge : edges_of_selected) {
edge->sourceNode()->removeEdge(edge);
edge->destNode()->removeEdge(edge);
scene()->removeItem(edge);
delete edge;
}
scene()->removeItem(selectedItem);
}
}
case Qt::Key_Insert: {
QList <QGraphicsItem* > selectedItems = scene()->selectedItems();
@ -135,7 +161,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
/// @bug no hit deteced (hover works) on bottom right corner
QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p);
Node* node_under_mouse = dynamic_cast<Node*>(item_under_mouse);
if (node_under_mouse != 0) { // insert Edge
if (node_under_mouse != 0 && node_under_mouse != selectedNode) { // insert Edge
const float2 source_pos = float2FromQPointF(selectedItem->pos());
const float2 destination_pos = float2FromQPointF(node_under_mouse->pos());

@ -25,16 +25,9 @@ void Node::addEdge(Edge *edge)
edge->adjust();
}
void Node::removeEdge(Node* node)
void Node::removeEdge(Edge* edge)
{
// QMutableListIterator<Edge*> i(edgeList);
// while (i.hasNext())
// if (i.next()->sourceNode() == node || i.next()->destNode() == node)
// i.remove();
edgeList.erase(std::remove_if(edgeList.begin(), edgeList.end(),
[node](Edge* e) { return e->sourceNode() == node || e->destNode() == node; }),
edgeList.end());
edgeList.removeAll(edge);
}
QList<Edge *> Node::edges() const

@ -14,7 +14,7 @@ public:
Node(GraphWidget *graphWidget);
void addEdge(Edge *edge);
void removeEdge(Node* node);
void removeEdge(Edge* edge);
QList<Edge *> edges() const;
enum { Type = UserType + 1 };

Loading…
Cancel
Save