feat: add node graphic item

This commit is contained in:
parker
2025-06-20 03:42:27 +01:00
parent 7394f72f51
commit cf876320d8
6 changed files with 57 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ qt_add_executable(${AppExec}
src/gui/network/NetworkGraphicsView.cpp
src/gui/network/NetworkGraphicsScene.cpp
src/gui/network/Network.cpp
src/gui/network/NodeGraphic.cpp
)
target_link_libraries(${AppExec} PRIVATE Qt6::Core Qt6::Widgets)

View File

@@ -1,8 +1,10 @@
#include "gui/network/Network.h"
#include "gui/network/NetworkGraphicsView.h"
#include "gui/network/NetworkGraphicsScene.h"
#include "gui/network/NodeGraphic.h"
#include <qboxlayout.h>
#include <QPushButton>
#include <QGraphicsItem>
Network::Network(QWidget* parent)
{
@@ -15,6 +17,21 @@ Network::Network(QWidget* parent)
NetworkGraphicsScene* scene = new NetworkGraphicsScene();
NetworkGraphicsView* view = new NetworkGraphicsView(parent, scene);
QPen greenPen = QPen(Qt::green);
greenPen.setWidth(6);
auto* rect1 = scene->addRect(50, 50, 100, 100, greenPen);
rect1->setFlag(QGraphicsItem::ItemIsMovable);
auto* rect2 = scene->addRect(80, 120, 100, 100, greenPen);
rect2->setFlag(QGraphicsItem::ItemIsMovable);
auto* rect3 = scene->addRect(80, -120, 100, 100, greenPen);
rect3->setFlag(QGraphicsItem::ItemIsMovable);
NodeGraphic* node1 = new NodeGraphic();
scene->addItem(node1);
mainLayout_->addWidget(view);
}

View File

@@ -9,7 +9,7 @@ NetworkGraphicsScene::NetworkGraphicsScene()
{
sceneWidth_ = 64000;
sceneHeight_ = 64000;
gridSize_ = 20;
gridSize_ = 40;
setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_);

View File

@@ -12,17 +12,7 @@ NetworkGraphicsView::NetworkGraphicsView(QWidget *parent, QGraphicsScene* scene)
initUI();
QPen greenPen = QPen(Qt::green);
greenPen.setWidth(6);
auto* rect1 = scene->addRect(50, 50, 100, 100, greenPen);
rect1->setFlag(QGraphicsItem::ItemIsMovable);
auto* rect2 = scene->addRect(80, 120, 100, 100, greenPen);
rect2->setFlag(QGraphicsItem::ItemIsMovable);
auto* rect3 = scene->addRect(80, -120, 100, 100, greenPen);
rect3->setFlag(QGraphicsItem::ItemIsMovable);
}
void NetworkGraphicsView::initUI()

View File

@@ -0,0 +1,9 @@
#include "gui/network/NodeGraphic.h"
NodeGraphic::NodeGraphic(QGraphicsItem *parent)
{
title_ = new QGraphicsTextItem(this);
title_->setPlainText("hello world!");
setFlag(QGraphicsItem::ItemIsMovable);
}

View File

@@ -0,0 +1,29 @@
#pragma once
#include <QGraphicsItem>
#include <QPainter>
class NodeGraphic
: public QGraphicsItem
{
public:
NodeGraphic(QGraphicsItem *parent = nullptr);
QRectF boundingRect() const override
{
qreal penWidth = 6;
return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
20 + penWidth, 20 + penWidth);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
{
QPen greenPen = QPen(Qt::green);
greenPen.setWidth(6);
painter->setPen(greenPen);
painter->drawRoundedRect(-10, -10, 20, 20, 5, 5);
}
private:
QGraphicsTextItem* title_;
};