removing edges

for/release
dmatetelki 11 years ago
parent fa15a7c30a
commit a9e9fbf940

@ -108,18 +108,44 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
break; break;
QGraphicsItem* selectedItem = selectedItems.first(); QGraphicsItem* selectedItem = selectedItems.first();
m_graph->removeVertex(float2FromQPointF(selectedItem->pos()));
Node* selectedNode = dynamic_cast<Node*>(selectedItem); Node* selectedNode = dynamic_cast<Node*>(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(); QList<Edge *> edges_of_selected = selectedNode->edges();
for (Edge* edge : edges_of_selected) { for (Edge* edge : edges_of_selected) {
edge->sourceNode()->removeEdge(selectedNode); if (edge->sourceNode() == node_under_mouse || edge->destNode() == node_under_mouse) {
edge->destNode()->removeEdge(selectedNode); selectedNode->removeEdge(edge);
node_under_mouse->removeEdge(edge);
scene()->removeItem(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); scene()->removeItem(selectedItem);
} }
}
case Qt::Key_Insert: { case Qt::Key_Insert: {
QList <QGraphicsItem* > selectedItems = scene()->selectedItems(); QList <QGraphicsItem* > selectedItems = scene()->selectedItems();
if (selectedItems.isEmpty()) if (selectedItems.isEmpty())
@ -135,7 +161,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
/// @bug no hit deteced (hover works) on bottom right corner /// @bug no hit deteced (hover works) on bottom right corner
QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p); QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p);
Node* node_under_mouse = dynamic_cast<Node*>(item_under_mouse); 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 source_pos = float2FromQPointF(selectedItem->pos());
const float2 destination_pos = float2FromQPointF(node_under_mouse->pos()); const float2 destination_pos = float2FromQPointF(node_under_mouse->pos());

@ -25,16 +25,9 @@ void Node::addEdge(Edge *edge)
edge->adjust(); edge->adjust();
} }
void Node::removeEdge(Node* node) void Node::removeEdge(Edge* edge)
{ {
// QMutableListIterator<Edge*> i(edgeList); edgeList.removeAll(edge);
// 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());
} }
QList<Edge *> Node::edges() const QList<Edge *> Node::edges() const

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

Loading…
Cancel
Save