From cf876320d87b9d82e7c2cc81a1179f035eb5585e Mon Sep 17 00:00:00 2001 From: parker Date: Fri, 20 Jun 2025 03:42:27 +0100 Subject: [PATCH] feat: add node graphic item --- CMakeLists.txt | 1 + src/gui/network/Network.cpp | 17 ++++++++++++++ src/gui/network/NetworkGraphicsScene.cpp | 2 +- src/gui/network/NetworkGraphicsView.cpp | 10 -------- src/gui/network/NodeGraphic.cpp | 9 ++++++++ src/gui/network/NodeGraphic.h | 29 ++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/gui/network/NodeGraphic.cpp create mode 100644 src/gui/network/NodeGraphic.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ced740e..3f1d01b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/gui/network/Network.cpp b/src/gui/network/Network.cpp index 843a2ec..01bd1ae 100644 --- a/src/gui/network/Network.cpp +++ b/src/gui/network/Network.cpp @@ -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 #include +#include 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); } diff --git a/src/gui/network/NetworkGraphicsScene.cpp b/src/gui/network/NetworkGraphicsScene.cpp index 486d67f..d66f9ba 100644 --- a/src/gui/network/NetworkGraphicsScene.cpp +++ b/src/gui/network/NetworkGraphicsScene.cpp @@ -9,7 +9,7 @@ NetworkGraphicsScene::NetworkGraphicsScene() { sceneWidth_ = 64000; sceneHeight_ = 64000; - gridSize_ = 20; + gridSize_ = 40; setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_); diff --git a/src/gui/network/NetworkGraphicsView.cpp b/src/gui/network/NetworkGraphicsView.cpp index 3bd4d3c..b2e11bc 100644 --- a/src/gui/network/NetworkGraphicsView.cpp +++ b/src/gui/network/NetworkGraphicsView.cpp @@ -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() diff --git a/src/gui/network/NodeGraphic.cpp b/src/gui/network/NodeGraphic.cpp new file mode 100644 index 0000000..97ea34e --- /dev/null +++ b/src/gui/network/NodeGraphic.cpp @@ -0,0 +1,9 @@ +#include "gui/network/NodeGraphic.h" + +NodeGraphic::NodeGraphic(QGraphicsItem *parent) +{ + title_ = new QGraphicsTextItem(this); + title_->setPlainText("hello world!"); + setFlag(QGraphicsItem::ItemIsMovable); +} + diff --git a/src/gui/network/NodeGraphic.h b/src/gui/network/NodeGraphic.h new file mode 100644 index 0000000..cde7fe2 --- /dev/null +++ b/src/gui/network/NodeGraphic.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include + +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_; +}; +