From 36d72cc102dfd08ce714c14902cd941996c2b477 Mon Sep 17 00:00:00 2001 From: lipengpeng Date: Thu, 7 Aug 2025 09:00:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E4=B8=8D=E5=90=8C=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coil.cpp | 34 +++++++++++++ coil.h | 14 ++++++ comparator.cpp | 32 +++++++++++++ comparator.h | 14 ++++++ contact.cpp | 46 ++++++++++++++++++ contact.h | 14 ++++++ creatitem.cpp | 10 ++++ creatitem.h | 10 ++++ editor.pro | 8 ++++ editor.pro.user | 2 +- item.cpp | 116 ++++++++++++++++++++++----------------------- item.h | 8 ++-- mainwindow.cpp | 3 +- mainwindow.ui | 24 ++++++++++ mygraphicsview.cpp | 6 +-- mygraphicsview.h | 1 + 16 files changed, 274 insertions(+), 68 deletions(-) create mode 100644 coil.cpp create mode 100644 coil.h create mode 100644 comparator.cpp create mode 100644 comparator.h create mode 100644 contact.cpp create mode 100644 contact.h create mode 100644 creatitem.cpp create mode 100644 creatitem.h diff --git a/coil.cpp b/coil.cpp new file mode 100644 index 0000000..2ded01a --- /dev/null +++ b/coil.cpp @@ -0,0 +1,34 @@ +#include "coil.h" +#include +#include + +Coil::Coil(const QString &type) : Item(type) +{ + +} + +void Coil::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) +{ + painter->setRenderHint(QPainter::Antialiasing); + + if (type_ == "线圈") { + // 绘制线圈样式: 两边线段+中间椭圆 + painter->drawLine(-12, 0, -5, 0); + painter->drawEllipse(QRectF(-5, -8, 10, 16)); + painter->drawLine(5, 0, 12, 0); + + // 画锚点 + painter->setBrush(Qt::darkGray); + painter->setPen(Qt::NoPen); + painter->drawEllipse(QPointF(-12, 0), 4, 4); // 左锚点 + painter->drawEllipse(QPointF(12, 0), 4, 4); // 右锚点 + } + if (option->state & QStyle::State_Selected) { + QPen pen(Qt::DashLine); + pen.setColor(Qt::blue); + pen.setWidth(2); + painter->setPen(pen); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect()); + } +} diff --git a/coil.h b/coil.h new file mode 100644 index 0000000..58db3b7 --- /dev/null +++ b/coil.h @@ -0,0 +1,14 @@ +#ifndef COIL_H +#define COIL_H +#include "item.h" + +class Coil : public Item +{ +public: + Coil(const QString &type); + void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *) override; +}; + +#endif // COIL_H diff --git a/comparator.cpp b/comparator.cpp new file mode 100644 index 0000000..e1b5929 --- /dev/null +++ b/comparator.cpp @@ -0,0 +1,32 @@ +#include "comparator.h" +#include +#include + +Comparator::Comparator(const QString &type) : Item(type) +{ + +} + +void Comparator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) +{ + painter->setRenderHint(QPainter::Antialiasing); + if (type_ == "比较") { + painter->drawRect(QRectF(-12, -8, 24, 16)); + painter->setFont(QFont("Arial", 8)); + painter->drawText(QRectF(-10, -8, 20, 16), Qt::AlignCenter, "CP"); + + // 锚点 + painter->setBrush(Qt::darkGray); + painter->setPen(Qt::NoPen); + painter->drawEllipse(QPointF(-18, 0), 4, 4); + painter->drawEllipse(QPointF(18, 0), 4, 4); + } + if (option->state & QStyle::State_Selected) { + QPen pen(Qt::DashLine); + pen.setColor(Qt::blue); + pen.setWidth(2); + painter->setPen(pen); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect()); + } +} diff --git a/comparator.h b/comparator.h new file mode 100644 index 0000000..61f75bd --- /dev/null +++ b/comparator.h @@ -0,0 +1,14 @@ +#ifndef COMPARATOR_H +#define COMPARATOR_H +#include "item.h" + +class Comparator : public Item +{ +public: + Comparator(const QString &type); + void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *) override; +}; + +#endif // COMPARATOR_H diff --git a/contact.cpp b/contact.cpp new file mode 100644 index 0000000..bf24eb4 --- /dev/null +++ b/contact.cpp @@ -0,0 +1,46 @@ +#include "contact.h" +#include +#include + +Contact::Contact(const QString &type) : Item(type) +{ + +} + +void Contact::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) +{ + painter->setRenderHint(QPainter::Antialiasing); + if (type_ == "常开") { + painter->drawLine(-12, 0, -4, 0); + painter->drawLine(-4, -8, -4, 8); + painter->drawLine(4, -8, 4, 8); + painter->drawLine(4, 0, 12, 0); + + // 锚点 + painter->setBrush(Qt::darkGray); + painter->setPen(Qt::NoPen); + painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 + painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 + } + else if (type_ == "常闭") { + painter->drawLine(-15, -10, 15, 10); // 对角线 + painter->drawLine(-12, 0, -4, 0); + painter->drawLine(-4, -8, -4, 8); + painter->drawLine(4, -8, 4, 8); + painter->drawLine(4, 0, 12, 0); + + // 锚点 + painter->setBrush(Qt::darkGray); + painter->setPen(Qt::NoPen); + painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 + painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 + } + if (option->state & QStyle::State_Selected) { + QPen pen(Qt::DashLine); + pen.setColor(Qt::blue); + pen.setWidth(2); + painter->setPen(pen); + painter->setBrush(Qt::NoBrush); + painter->drawRect(boundingRect()); + } +} diff --git a/contact.h b/contact.h new file mode 100644 index 0000000..0cb77a6 --- /dev/null +++ b/contact.h @@ -0,0 +1,14 @@ +#ifndef CONTACT_H +#define CONTACT_H +#include "item.h" + +class Contact : public Item +{ +public: + Contact(const QString &type); + void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget *) override; +}; + +#endif // CONTACT_H diff --git a/creatitem.cpp b/creatitem.cpp new file mode 100644 index 0000000..c9725ab --- /dev/null +++ b/creatitem.cpp @@ -0,0 +1,10 @@ +#include "creatitem.h" + +Item *creatItem(const QString &type) +{ + if (type == "常开") return new Contact(type); + if (type == "常闭") return new Contact(type); + if (type == "线圈") return new Coil(type); + if (type == "比较") return new Comparator(type); + return nullptr; +} diff --git a/creatitem.h b/creatitem.h new file mode 100644 index 0000000..d85eaea --- /dev/null +++ b/creatitem.h @@ -0,0 +1,10 @@ +#ifndef CREATITEM_H +#define CREATITEM_H +#include "item.h" +#include "coil.h" +#include "contact.h" +#include "comparator.h" + +Item* creatItem(const QString& type); + +#endif // CREATITEM_H diff --git a/editor.pro b/editor.pro index 40acd73..7793fcc 100644 --- a/editor.pro +++ b/editor.pro @@ -16,14 +16,22 @@ DEFINES += QT_DEPRECATED_WARNINGS #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ + coil.cpp \ + comparator.cpp \ connection.cpp \ + contact.cpp \ + creatitem.cpp \ item.cpp \ main.cpp \ mainwindow.cpp \ mygraphicsview.cpp HEADERS += \ + coil.h \ + comparator.h \ connection.h \ + contact.h \ + creatitem.h \ item.h \ mainwindow.h \ mygraphicsview.h diff --git a/editor.pro.user b/editor.pro.user index 0834988..0791f7a 100644 --- a/editor.pro.user +++ b/editor.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/item.cpp b/item.cpp index 01ca08d..6c493e4 100644 --- a/item.cpp +++ b/item.cpp @@ -18,69 +18,69 @@ QRectF Item::boundingRect() const return QRectF(-22, -15, 44, 30); } -void Item::paint(QPainter *painter, - const QStyleOptionGraphicsItem * option, - QWidget *) -{ - painter->setRenderHint(QPainter::Antialiasing); +//void Item::paint(QPainter *painter, +// const QStyleOptionGraphicsItem * option, +// QWidget *) +//{ +// painter->setRenderHint(QPainter::Antialiasing); - if (type_ == "线圈") { - // 绘制线圈样式: 两边线段+中间椭圆 - painter->drawLine(-12, 0, -5, 0); - painter->drawEllipse(QRectF(-5, -8, 10, 16)); - painter->drawLine(5, 0, 12, 0); +// if (type_ == "线圈") { +// // 绘制线圈样式: 两边线段+中间椭圆 +// painter->drawLine(-12, 0, -5, 0); +// painter->drawEllipse(QRectF(-5, -8, 10, 16)); +// painter->drawLine(5, 0, 12, 0); - // 画锚点 - painter->setBrush(Qt::darkGray); - painter->setPen(Qt::NoPen); - painter->drawEllipse(QPointF(-12, 0), 4, 4); // 左锚点 - painter->drawEllipse(QPointF(12, 0), 4, 4); // 右锚点 - } - else if (type_ == "常开") { - painter->drawLine(-12, 0, -4, 0); - painter->drawLine(-4, -8, -4, 8); - painter->drawLine(4, -8, 4, 8); - painter->drawLine(4, 0, 12, 0); +// // 画锚点 +// painter->setBrush(Qt::darkGray); +// painter->setPen(Qt::NoPen); +// painter->drawEllipse(QPointF(-12, 0), 4, 4); // 左锚点 +// painter->drawEllipse(QPointF(12, 0), 4, 4); // 右锚点 +// } +// else if (type_ == "常开") { +// painter->drawLine(-12, 0, -4, 0); +// painter->drawLine(-4, -8, -4, 8); +// painter->drawLine(4, -8, 4, 8); +// painter->drawLine(4, 0, 12, 0); - // 锚点 - painter->setBrush(Qt::darkGray); - painter->setPen(Qt::NoPen); - painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 - painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 - } - else if (type_ == "常闭") { - painter->drawLine(-15, -10, 15, 10); // 对角线 - painter->drawLine(-12, 0, -4, 0); - painter->drawLine(-4, -8, -4, 8); - painter->drawLine(4, -8, 4, 8); - painter->drawLine(4, 0, 12, 0); +// // 锚点 +// painter->setBrush(Qt::darkGray); +// painter->setPen(Qt::NoPen); +// painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 +// painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 +// } +// else if (type_ == "常闭") { +// painter->drawLine(-15, -10, 15, 10); // 对角线 +// painter->drawLine(-12, 0, -4, 0); +// painter->drawLine(-4, -8, -4, 8); +// painter->drawLine(4, -8, 4, 8); +// painter->drawLine(4, 0, 12, 0); - // 锚点 - painter->setBrush(Qt::darkGray); - painter->setPen(Qt::NoPen); - painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 - painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 - } - else if (type_ == "比较") { - painter->drawRect(QRectF(-12, -8, 24, 16)); - painter->setFont(QFont("Arial", 8)); - painter->drawText(QRectF(-10, -8, 20, 16), Qt::AlignCenter, "CP"); +// // 锚点 +// painter->setBrush(Qt::darkGray); +// painter->setPen(Qt::NoPen); +// painter->drawEllipse(QPointF(-18, 0), 4, 4); // 左 +// painter->drawEllipse(QPointF(18, 0), 4, 4); // 右 +// } +// else if (type_ == "比较") { +// painter->drawRect(QRectF(-12, -8, 24, 16)); +// painter->setFont(QFont("Arial", 8)); +// painter->drawText(QRectF(-10, -8, 20, 16), Qt::AlignCenter, "CP"); - // 锚点 - painter->setBrush(Qt::darkGray); - painter->setPen(Qt::NoPen); - painter->drawEllipse(QPointF(-18, 0), 4, 4); - painter->drawEllipse(QPointF(18, 0), 4, 4); - } - if (option->state & QStyle::State_Selected) { - QPen pen(Qt::DashLine); - pen.setColor(Qt::blue); - pen.setWidth(2); - painter->setPen(pen); - painter->setBrush(Qt::NoBrush); - painter->drawRect(boundingRect()); - } -} +// // 锚点 +// painter->setBrush(Qt::darkGray); +// painter->setPen(Qt::NoPen); +// painter->drawEllipse(QPointF(-18, 0), 4, 4); +// painter->drawEllipse(QPointF(18, 0), 4, 4); +// } +// if (option->state & QStyle::State_Selected) { +// QPen pen(Qt::DashLine); +// pen.setColor(Qt::blue); +// pen.setWidth(2); +// painter->setPen(pen); +// painter->setBrush(Qt::NoBrush); +// painter->drawRect(boundingRect()); +// } +//} QPointF Item::anchorPos(AnchorType type) const { diff --git a/item.h b/item.h index 900a31d..b33acae 100644 --- a/item.h +++ b/item.h @@ -15,9 +15,9 @@ public: explicit Item(const QString &type, QGraphicsItem *parent = nullptr); QRectF boundingRect() const override; - void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, - QWidget *widget) override; +// void paint(QPainter *painter, +// const QStyleOptionGraphicsItem *option, +// QWidget *widget) override; QPointF anchorPos(AnchorType type) const; void addConnection(Connection* conn); @@ -32,8 +32,6 @@ signals: protected: QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override; - -private: QString type_; QList connections_; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index a3993c8..86f7f65 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "creatitem.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) @@ -144,7 +145,7 @@ void MainWindow::on_pushButton_clicked() delete selectedConn; // 4. 插入新元件 - Item* newItem = new Item(selectedComponentType); + Item* newItem = creatItem(selectedComponentType); newItem->setPos(insertPos); connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); diff --git a/mainwindow.ui b/mainwindow.ui index 863f94c..aef3cbb 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -60,8 +60,32 @@ 26 + + + 文件 + + + + + + + + + New + + + + + Open + + + + + Save + + diff --git a/mygraphicsview.cpp b/mygraphicsview.cpp index 86f6499..8b70786 100644 --- a/mygraphicsview.cpp +++ b/mygraphicsview.cpp @@ -42,7 +42,7 @@ void MyGraphicsView::dropEvent(QDropEvent *event) QString type = QString::fromUtf8(event->mimeData()->data("application/x-component")); QPointF scenePos = mapToScene(event->pos()); - Item *item = new Item(type); + Item *item = creatItem(type); item->setPos(scenePos); connect(item, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); connect(item, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); @@ -88,7 +88,7 @@ void MyGraphicsView::keyPressEvent(QKeyEvent *event) // Ctrl+V if (!clipboard_.input.isEmpty()) { QPointF center = mapToScene(viewport()->rect().center()); - Item* newItem = new Item(clipboard_.input); + Item* newItem = creatItem(clipboard_.input); newItem->setPos(center); connect(newItem, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); connect(newItem, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); @@ -108,7 +108,7 @@ void MyGraphicsView::contextMenuEvent(QContextMenuEvent *event) QAction* pasteAct = menu.addAction("粘贴"); QAction* sel = menu.exec(event->globalPos()); if (sel == pasteAct) { - Item* newItem = new Item(clipboard_.input); + Item* newItem = creatItem(clipboard_.input); newItem->setPos(scenePos); connect(newItem, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); connect(newItem, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); diff --git a/mygraphicsview.h b/mygraphicsview.h index 420a07f..614eef3 100644 --- a/mygraphicsview.h +++ b/mygraphicsview.h @@ -9,6 +9,7 @@ #include #include "item.h" #include "connection.h" +#include "creatitem.h" class MyGraphicsView : public QGraphicsView {