can add node to selected node with the insert key

for/release
dmatetelki 11 years ago
parent abe35ea720
commit 7f068f8ffd

@ -16,7 +16,8 @@ namespace std {
template <> template <>
struct hash<float2> struct hash<float2>
{ {
std::size_t operator()(const float2& f2) const { std::size_t operator()(const float2& f2) const
{
std::size_t h1 = std::hash<float>()(f2.x); std::size_t h1 = std::hash<float>()(f2.x);
std::size_t h2 = std::hash<float>()(f2.y); std::size_t h2 = std::hash<float>()(f2.y);
return h1 ^ (h2 << 1); return h1 ^ (h2 << 1);
@ -25,10 +26,16 @@ namespace std {
} }
// for the map // for the map
bool operator< (const float2& v1, const float2& v2) { bool operator< (const float2& v1, const float2& v2)
{
return length(v1) < length(v2); return length(v1) < length(v2);
} }
float2 inline float2FromQPointF(const QPointF& p)
{
return float2(p.x(), p.y());
}
GraphWidget::GraphWidget(Graph<float2>* graph, QWidget *p) GraphWidget::GraphWidget(Graph<float2>* graph, QWidget *p)
: QGraphicsView(p) : QGraphicsView(p)
@ -50,8 +57,8 @@ GraphWidget::GraphWidget(Graph<float2>* graph, QWidget *p)
void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos) void GraphWidget::itemMoved(const QPointF oldPos, const QPointF newPos)
{ {
float2 old_v = float2(oldPos.x(), oldPos.y()); float2 old_v = float2FromQPointF(oldPos);
float2 new_v = float2(newPos.x(), newPos.y()); float2 new_v = float2FromQPointF(newPos);
m_graph->modifyVertex(old_v, new_v); m_graph->modifyVertex(old_v, new_v);
} }
@ -84,6 +91,27 @@ void GraphWidget::keyPressEvent(QKeyEvent *e)
case Qt::Key_Minus: case Qt::Key_Minus:
zoomOut(); zoomOut();
break; break;
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);
Node *node = new Node(this);
scene()->addItem(node);
node->setPos(scene_p.x(), scene_p.y());
scene()->addItem(new Edge(selectedNode, node));
const float2 source_pos = float2FromQPointF(selectedItem->pos());
const float2 destination_pos = float2FromQPointF(scene_p);
m_graph->addEdge(source_pos, destination_pos);
}
default: default:
QGraphicsView::keyPressEvent(e); QGraphicsView::keyPressEvent(e);
} }

@ -15,6 +15,7 @@ public:
GraphWidget(Graph<float2>* graph, QWidget *parent = 0); GraphWidget(Graph<float2>* graph, QWidget *parent = 0);
void itemMoved(const QPointF oldPos, const QPointF newPos); void itemMoved(const QPointF oldPos, const QPointF newPos);
void updateFromGraph(); void updateFromGraph();
public slots: public slots:

@ -9,10 +9,11 @@
Node::Node(GraphWidget *graphWidget) Node::Node(GraphWidget *graphWidget)
: graph(graphWidget) : m_graphWidget(graphWidget)
{ {
setFlag(ItemIsMovable); setFlag(ItemIsMovable);
setFlag(ItemSendsGeometryChanges); setFlag(ItemSendsGeometryChanges);
setFlag(ItemIsSelectable);
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setZValue(-1); setZValue(-1);
} }
@ -42,18 +43,18 @@ QPainterPath Node::shape() const
return path; return path;
} }
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget *)
{ {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
painter->setBrush(Qt::darkGray); painter->setBrush(Qt::darkGray);
painter->drawEllipse(-7, -7, 20, 20); painter->drawEllipse(-7, -7, 20, 20);
QRadialGradient gradient(-3, -3, 10); QRadialGradient gradient(-3, -3, 10);
if (option->state & QStyle::State_Sunken) { if (isSelected()) {
gradient.setCenter(3, 3); gradient.setCenter(3, 3);
gradient.setFocalPoint(3, 3); gradient.setFocalPoint(3, 3);
gradient.setColorAt(1, QColor(Qt::yellow).light(120)); gradient.setColorAt(0, Qt::red);
gradient.setColorAt(0, QColor(Qt::darkYellow).light(120)); gradient.setColorAt(1, Qt::darkRed);
} else { } else {
gradient.setColorAt(0, Qt::yellow); gradient.setColorAt(0, Qt::yellow);
gradient.setColorAt(1, Qt::darkYellow); gradient.setColorAt(1, Qt::darkYellow);
@ -73,7 +74,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
const QPointF old_pos = pos(); const QPointF old_pos = pos();
const QPointF new_pos = value.toPointF(); const QPointF new_pos = value.toPointF();
graph->itemMoved(old_pos, new_pos); m_graphWidget->itemMoved(old_pos, new_pos);
break; break;
} }
default: default:

@ -32,7 +32,7 @@ protected:
private: private:
QList<Edge *> edgeList; QList<Edge *> edgeList;
QPointF newPos; QPointF newPos;
GraphWidget *graph; GraphWidget *m_graphWidget;
}; };
#endif #endif

Loading…
Cancel
Save