|
|
@ -102,7 +102,8 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
|
|
|
|
zoomOut();
|
|
|
|
zoomOut();
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case Qt::Key_Delete: {
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
|
|
|
|
case Qt::Key_Insert: {
|
|
|
|
QList <QGraphicsItem* > selectedItems = scene()->selectedItems();
|
|
|
|
QList <QGraphicsItem* > selectedItems = scene()->selectedItems();
|
|
|
|
if (selectedItems.isEmpty())
|
|
|
|
if (selectedItems.isEmpty())
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -114,28 +115,49 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
|
|
|
|
const QPoint widget_p = mapFromGlobal(global_p);
|
|
|
|
const QPoint widget_p = mapFromGlobal(global_p);
|
|
|
|
const QPointF scene_p = mapToScene(widget_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);
|
|
|
|
QGraphicsItem* item_under_mouse = scene()->itemAt(scene_p);
|
|
|
|
Node* node_under_mouse = dynamic_cast<Node*>(item_under_mouse);
|
|
|
|
Node* nodeUnderMouse = dynamic_cast<Node*>(item_under_mouse);
|
|
|
|
if (node_under_mouse != 0 && node_under_mouse != selectedNode) { // remove edge
|
|
|
|
|
|
|
|
const float2 source_pos = float2FromQPointF(selectedItem->pos());
|
|
|
|
if (nodeUnderMouse != 0 && nodeUnderMouse != selectedNode) {
|
|
|
|
const float2 destination_pos = float2FromQPointF(node_under_mouse->pos());
|
|
|
|
if (e->key() == Qt::Key_Delete)
|
|
|
|
|
|
|
|
removeEdge(selectedNode, nodeUnderMouse);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
insertEdge(selectedNode, nodeUnderMouse);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (e->key() == Qt::Key_Delete)
|
|
|
|
|
|
|
|
removeNode(selectedNode);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
insertNode(selectedNode, scene_p);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
QGraphicsView::keyPressEvent(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GraphWidget::removeEdge(Node* selectedNode, Node* nodeUnderMouse)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
const float2 source_pos = float2FromQPointF(selectedNode->pos());
|
|
|
|
|
|
|
|
const float2 destination_pos = float2FromQPointF(nodeUnderMouse->pos());
|
|
|
|
if (!m_graph->connected(source_pos, destination_pos))
|
|
|
|
if (!m_graph->connected(source_pos, destination_pos))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
m_graph->removeEdge(source_pos, destination_pos);
|
|
|
|
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) {
|
|
|
|
if (edge->sourceNode() == node_under_mouse || edge->destNode() == node_under_mouse) {
|
|
|
|
if (edge->sourceNode() == nodeUnderMouse || edge->destNode() == nodeUnderMouse) {
|
|
|
|
selectedNode->removeEdge(edge);
|
|
|
|
selectedNode->removeEdge(edge);
|
|
|
|
node_under_mouse->removeEdge(edge);
|
|
|
|
nodeUnderMouse->removeEdge(edge);
|
|
|
|
scene()->removeItem(edge);
|
|
|
|
scene()->removeItem(edge);
|
|
|
|
delete edge;
|
|
|
|
delete edge;
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // remove node
|
|
|
|
}
|
|
|
|
m_graph->removeVertex(float2FromQPointF(selectedItem->pos()));
|
|
|
|
|
|
|
|
|
|
|
|
void GraphWidget::removeNode(Node* selectedNode)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
m_graph->removeVertex(float2FromQPointF(selectedNode->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(edge);
|
|
|
|
edge->sourceNode()->removeEdge(edge);
|
|
|
@ -143,50 +165,34 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
|
|
|
|
scene()->removeItem(edge);
|
|
|
|
scene()->removeItem(edge);
|
|
|
|
delete edge;
|
|
|
|
delete edge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scene()->removeItem(selectedItem);
|
|
|
|
scene()->removeItem(selectedNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::Key_Insert: {
|
|
|
|
|
|
|
|
QList <QGraphicsItem* > selectedItems = scene()->selectedItems();
|
|
|
|
|
|
|
|
if (selectedItems.isEmpty())
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QGraphicsItem* selectedItem = selectedItems.first();
|
|
|
|
|
|
|
|
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) { // insert Edge
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const float2 source_pos = float2FromQPointF(selectedItem->pos());
|
|
|
|
void GraphWidget::insertEdge(Node* selectedNode, Node* nodeUnderMouse)
|
|
|
|
const float2 destination_pos = float2FromQPointF(node_under_mouse->pos());
|
|
|
|
{
|
|
|
|
|
|
|
|
const float2 source_pos = float2FromQPointF(selectedNode->pos());
|
|
|
|
|
|
|
|
const float2 destination_pos = float2FromQPointF(selectedNode->pos());
|
|
|
|
if (m_graph->connected(source_pos, destination_pos))
|
|
|
|
if (m_graph->connected(source_pos, destination_pos))
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
scene()->addItem(new Edge(selectedNode, node_under_mouse));
|
|
|
|
scene()->addItem(new Edge(selectedNode, nodeUnderMouse));
|
|
|
|
m_graph->addEdge(source_pos, destination_pos);
|
|
|
|
m_graph->addEdge(source_pos, destination_pos);
|
|
|
|
} else { // insert new node
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void GraphWidget::insertNode(Node* selectedNode, QPointF scene_p)
|
|
|
|
|
|
|
|
{
|
|
|
|
Node *node = new Node(this);
|
|
|
|
Node *node = new Node(this);
|
|
|
|
scene()->addItem(node);
|
|
|
|
scene()->addItem(node);
|
|
|
|
node->setPos(scene_p.x(), scene_p.y());
|
|
|
|
node->setPos(scene_p.x(), scene_p.y());
|
|
|
|
scene()->addItem(new Edge(selectedNode, node));
|
|
|
|
scene()->addItem(new Edge(selectedNode, node));
|
|
|
|
|
|
|
|
|
|
|
|
const float2 source_pos = float2FromQPointF(selectedItem->pos());
|
|
|
|
const float2 source_pos = float2FromQPointF(selectedNode->pos());
|
|
|
|
const float2 destination_pos = float2FromQPointF(scene_p);
|
|
|
|
const float2 destination_pos = float2FromQPointF(scene_p);
|
|
|
|
m_graph->addEdge(source_pos, destination_pos);
|
|
|
|
m_graph->addEdge(source_pos, destination_pos);
|
|
|
|
|
|
|
|
|
|
|
|
selectedItem->setSelected(false);
|
|
|
|
selectedNode->setSelected(false);
|
|
|
|
node->setSelected(true);
|
|
|
|
node->setSelected(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
QGraphicsView::keyPressEvent(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GraphWidget::wheelEvent(QWheelEvent *e)
|
|
|
|
void GraphWidget::wheelEvent(QWheelEvent *e)
|
|
|
|