From 7ee57e3be9be6520c609a81add02115740e49cb9 Mon Sep 17 00:00:00 2001 From: Jesse Qu Date: Fri, 26 Dec 2025 20:20:52 +0800 Subject: [PATCH] Added BusSection component support by AI --- CMakeLists.txt | 2 + include/global.h | 3 +- include/graphicsItem/graphicsBusSectionItem.h | 29 +++++ source/graphicElementsPanel.cpp | 2 + .../graphicsItem/graphicsBusSectionItem.cpp | 113 ++++++++++++++++++ source/util/creatingSelector.cpp | 7 ++ ui/graphicElementsPanel.ui | 13 ++ 7 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 include/graphicsItem/graphicsBusSectionItem.h create mode 100644 source/graphicsItem/graphicsBusSectionItem.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6624577..cd7102d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ set(H_HEADER_FILES include/graphicsItem/graphicsRectItem.h include/graphicsItem/graphicsPolygonItem.h include/graphicsItem/graphicsItemGroup.h + include/graphicsItem/graphicsBusSectionItem.h ) set(CPP_SOURCE_FILES source/main.cpp @@ -79,6 +80,7 @@ set(CPP_SOURCE_FILES source/graphicsItem/graphicsRectItem.cpp source/graphicsItem/graphicsPolygonItem.cpp source/graphicsItem/graphicsItemGroup.cpp + source/graphicsItem/graphicsBusSectionItem.cpp ) set(UI_FILES ui/mainwindow.ui diff --git a/include/global.h b/include/global.h index 41eee44..77b47c5 100644 --- a/include/global.h +++ b/include/global.h @@ -13,7 +13,8 @@ enum GraphicsItemType GIT_rect = QGraphicsItem::UserType + 3, GIT_roundRect = QGraphicsItem::UserType + 4, GIT_ellipse = QGraphicsItem::UserType + 5, - GIT_polygon = QGraphicsItem::UserType + 6 + GIT_polygon = QGraphicsItem::UserType + 6, + GIT_busSection = QGraphicsItem::UserType + 7 }; #endif diff --git a/include/graphicsItem/graphicsBusSectionItem.h b/include/graphicsItem/graphicsBusSectionItem.h new file mode 100644 index 0000000..a34c45f --- /dev/null +++ b/include/graphicsItem/graphicsBusSectionItem.h @@ -0,0 +1,29 @@ +#ifndef GRAPHICSBUSSECTIONITEM_H +#define GRAPHICSBUSSECTIONITEM_H + +#include "graphicsItem/graphicsBaseItem.h" + +class GraphicsBusSectionItem : public GraphicsBaseItem +{ +public: + explicit GraphicsBusSectionItem(QGraphicsItem *parent = nullptr); + virtual ~GraphicsBusSectionItem(); + + void resize(int, double, double, const QPointF&) override; + void updateCoordinate() override; + void move(const QPointF&) override; + void editShape(int, const QPointF&) override; + +protected: + virtual QPainterPath shape() override; + virtual void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*) override; + +private: + virtual void updateHandles() override; + + QRectF m_lastBoudingRect; + double m_dRatioX; + double m_dRatioY; +}; + +#endif // GRAPHICSBUSSECTIONITEM_H diff --git a/source/graphicElementsPanel.cpp b/source/graphicElementsPanel.cpp index 2213ca5..9a56d92 100644 --- a/source/graphicElementsPanel.cpp +++ b/source/graphicElementsPanel.cpp @@ -13,6 +13,8 @@ GraphicElementsPanel::GraphicElementsPanel(QWidget *parent) connect(ui->pushBtn_roundRect, SIGNAL(clicked()), this, SLOT(onBtnClicked_GraphicsItem())); ui->pushBtn_polygon->setProperty("shap",GIT_polygon); connect(ui->pushBtn_polygon, SIGNAL(clicked()), this, SLOT(onBtnClicked_GraphicsItem())); + ui->pushBtn_busSection->setProperty("shap",GIT_busSection); + connect(ui->pushBtn_busSection, SIGNAL(clicked()), this, SLOT(onBtnClicked_GraphicsItem())); } GraphicElementsPanel::~GraphicElementsPanel() diff --git a/source/graphicsItem/graphicsBusSectionItem.cpp b/source/graphicsItem/graphicsBusSectionItem.cpp new file mode 100644 index 0000000..0220339 --- /dev/null +++ b/source/graphicsItem/graphicsBusSectionItem.cpp @@ -0,0 +1,113 @@ +#include "graphicsItem/graphicsBusSectionItem.h" +#include "graphicsItem/itemControlHandle.h" +#include +#include + +GraphicsBusSectionItem::GraphicsBusSectionItem(QGraphicsItem *parent) + : GraphicsBaseItem(parent), m_dRatioX(1 / 10.0), m_dRatioY(1 / 10.0) +{ + m_pen = QPen(Qt::blue, 2); + m_brush = QBrush(QColor(100, 150, 255, 50)); + m_lastBoudingRect = QRectF(-10, -10, 20, 20); + m_boundingRect = QRectF(-10, -10, 20, 20); + m_dWidth = 20; + m_dHeight = 20; +} + +GraphicsBusSectionItem::~GraphicsBusSectionItem() +{ +} + +QPainterPath GraphicsBusSectionItem::shape() +{ + QPainterPath path; + path.addRect(m_boundingRect); + return path; +} + +void GraphicsBusSectionItem::updateHandles() +{ + GraphicsBaseItem::updateHandles(); +} + +void GraphicsBusSectionItem::updateCoordinate() +{ + if (!parentItem()) + { + QPointF pt1, pt2, delta; + pt1 = mapToScene(QPointF(0, 0)); + pt2 = mapToScene(m_boundingRect.center()); + delta = pt1 - pt2; + + prepareGeometryChange(); + m_boundingRect = QRectF(-m_dWidth / 2, -m_dHeight / 2, m_dWidth, m_dHeight); + moveBy(-delta.x(), -delta.y()); + updateHandles(); + } + + m_lastBoudingRect = m_boundingRect; +} + +void GraphicsBusSectionItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +{ + painter->setPen(m_pen); + painter->setBrush(m_brush); + painter->drawRect(m_boundingRect); + + // 绘制Bus Section标签 + painter->setPen(Qt::blue); + painter->setFont(QFont("Arial", 10, QFont::Bold)); + painter->drawText(m_boundingRect, Qt::AlignCenter, "BUS"); + + if (option->state & QStyle::State_Selected) + { + int nPenWidth = 1; + painter->setPen(QPen(QColor(70, 70, 70), nPenWidth, Qt::DashLine)); + painter->setBrush(Qt::NoBrush); + painter->drawRect(m_boundingRect_selected); + + // 绘制变换原点 + QPointF originPoint = transformOriginPoint(); + painter->setBrush(Qt::red); + painter->drawEllipse(originPoint, 4, 4); + } +} + +void GraphicsBusSectionItem::resize(int nHandle, double dSX, double dSY, const QPointF& basePoint) +{ + switch (nHandle) + { + case H_left: + case H_right: + dSY = 1; + break; + case H_top: + case H_bottom: + dSX = 1; + break; + default: + break; + } + + QTransform trans; + trans.translate(basePoint.x(), basePoint.y()); + trans.scale(dSX, dSY); + trans.translate(-basePoint.x(), -basePoint.y()); + + prepareGeometryChange(); + m_boundingRect = trans.mapRect(m_lastBoudingRect); + m_dWidth = m_boundingRect.width(); + m_dHeight = m_boundingRect.height(); + update(); +} + +void GraphicsBusSectionItem::move(const QPointF& delta) +{ + moveBy(delta.x(), delta.y()); +} + +void GraphicsBusSectionItem::editShape(int nHandle, const QPointF& point) +{ + // Bus Section没有特殊的编辑点 +} + diff --git a/source/util/creatingSelector.cpp b/source/util/creatingSelector.cpp index eb70a4f..ce6dca5 100644 --- a/source/util/creatingSelector.cpp +++ b/source/util/creatingSelector.cpp @@ -1,6 +1,7 @@ #include "util/creatingSelector.h" #include "graphicsItem/graphicsRectItem.h" #include "graphicsItem/graphicsPolygonItem.h" +#include "graphicsItem/graphicsBusSectionItem.h" #include #include @@ -49,6 +50,12 @@ void CreatingSelector::mousePressEvent(QGraphicsSceneMouseEvent* event, Designer m_pCreatingItem = new GraphicPolygonItem(); } break; + case GIT_busSection: + { + m_creatingMethod = CM_drag; + m_pCreatingItem = new GraphicsBusSectionItem(); + } + break; default: break; } diff --git a/ui/graphicElementsPanel.ui b/ui/graphicElementsPanel.ui index d18d984..d0ffce7 100644 --- a/ui/graphicElementsPanel.ui +++ b/ui/graphicElementsPanel.ui @@ -70,6 +70,19 @@ 电力图元 + + + + 30 + 20 + 81 + 51 + + + + Bus Section + +