@@ -0,0 +1,34 @@ | |||||
#include "coil.h" | |||||
#include <QPainter> | |||||
#include <QStyleOptionGraphicsItem> | |||||
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()); | |||||
} | |||||
} |
@@ -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 |
@@ -0,0 +1,32 @@ | |||||
#include "comparator.h" | |||||
#include <QPainter> | |||||
#include <QStyleOptionGraphicsItem> | |||||
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()); | |||||
} | |||||
} |
@@ -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 |
@@ -0,0 +1,46 @@ | |||||
#include "contact.h" | |||||
#include <QPainter> | |||||
#include <QStyleOptionGraphicsItem> | |||||
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()); | |||||
} | |||||
} |
@@ -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 |
@@ -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; | |||||
} |
@@ -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 |
@@ -16,14 +16,22 @@ DEFINES += QT_DEPRECATED_WARNINGS | |||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | ||||
SOURCES += \ | SOURCES += \ | ||||
coil.cpp \ | |||||
comparator.cpp \ | |||||
connection.cpp \ | connection.cpp \ | ||||
contact.cpp \ | |||||
creatitem.cpp \ | |||||
item.cpp \ | item.cpp \ | ||||
main.cpp \ | main.cpp \ | ||||
mainwindow.cpp \ | mainwindow.cpp \ | ||||
mygraphicsview.cpp | mygraphicsview.cpp | ||||
HEADERS += \ | HEADERS += \ | ||||
coil.h \ | |||||
comparator.h \ | |||||
connection.h \ | connection.h \ | ||||
contact.h \ | |||||
creatitem.h \ | |||||
item.h \ | item.h \ | ||||
mainwindow.h \ | mainwindow.h \ | ||||
mygraphicsview.h | mygraphicsview.h | ||||
@@ -1,6 +1,6 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||||
<!DOCTYPE QtCreatorProject> | <!DOCTYPE QtCreatorProject> | ||||
<!-- Written by QtCreator 4.11.1, 2025-08-06T13:12:15. --> | |||||
<!-- Written by QtCreator 4.11.1, 2025-08-06T20:12:05. --> | |||||
<qtcreator> | <qtcreator> | ||||
<data> | <data> | ||||
<variable>EnvironmentId</variable> | <variable>EnvironmentId</variable> | ||||
@@ -18,69 +18,69 @@ QRectF Item::boundingRect() const | |||||
return QRectF(-22, -15, 44, 30); | 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 | QPointF Item::anchorPos(AnchorType type) const | ||||
{ | { | ||||
@@ -15,9 +15,9 @@ public: | |||||
explicit Item(const QString &type, QGraphicsItem *parent = nullptr); | explicit Item(const QString &type, QGraphicsItem *parent = nullptr); | ||||
QRectF boundingRect() const override; | 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; | QPointF anchorPos(AnchorType type) const; | ||||
void addConnection(Connection* conn); | void addConnection(Connection* conn); | ||||
@@ -32,8 +32,6 @@ signals: | |||||
protected: | protected: | ||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; | QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; | ||||
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override; | void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override; | ||||
private: | |||||
QString type_; | QString type_; | ||||
QList<Connection*> connections_; | QList<Connection*> connections_; | ||||
}; | }; | ||||
@@ -10,6 +10,7 @@ | |||||
#include <QDropEvent> | #include <QDropEvent> | ||||
#include <QGraphicsView> | #include <QGraphicsView> | ||||
#include <QMessageBox> | #include <QMessageBox> | ||||
#include "creatitem.h" | |||||
MainWindow::MainWindow(QWidget *parent) | MainWindow::MainWindow(QWidget *parent) | ||||
: QMainWindow(parent) | : QMainWindow(parent) | ||||
@@ -144,7 +145,7 @@ void MainWindow::on_pushButton_clicked() | |||||
delete selectedConn; | delete selectedConn; | ||||
// 4. 插入新元件 | // 4. 插入新元件 | ||||
Item* newItem = new Item(selectedComponentType); | |||||
Item* newItem = creatItem(selectedComponentType); | |||||
newItem->setPos(insertPos); | newItem->setPos(insertPos); | ||||
connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | ||||
connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | ||||
@@ -60,8 +60,32 @@ | |||||
<height>26</height> | <height>26</height> | ||||
</rect> | </rect> | ||||
</property> | </property> | ||||
<widget class="QMenu" name="menu"> | |||||
<property name="title"> | |||||
<string>文件</string> | |||||
</property> | |||||
<addaction name="actionnew"/> | |||||
<addaction name="actionOpen"/> | |||||
<addaction name="actionSave"/> | |||||
</widget> | |||||
<addaction name="menu"/> | |||||
</widget> | </widget> | ||||
<widget class="QStatusBar" name="statusbar"/> | <widget class="QStatusBar" name="statusbar"/> | ||||
<action name="actionnew"> | |||||
<property name="text"> | |||||
<string>New</string> | |||||
</property> | |||||
</action> | |||||
<action name="actionOpen"> | |||||
<property name="text"> | |||||
<string>Open</string> | |||||
</property> | |||||
</action> | |||||
<action name="actionSave"> | |||||
<property name="text"> | |||||
<string>Save</string> | |||||
</property> | |||||
</action> | |||||
</widget> | </widget> | ||||
<customwidgets> | <customwidgets> | ||||
<customwidget> | <customwidget> | ||||
@@ -42,7 +42,7 @@ void MyGraphicsView::dropEvent(QDropEvent *event) | |||||
QString type = QString::fromUtf8(event->mimeData()->data("application/x-component")); | QString type = QString::fromUtf8(event->mimeData()->data("application/x-component")); | ||||
QPointF scenePos = mapToScene(event->pos()); | QPointF scenePos = mapToScene(event->pos()); | ||||
Item *item = new Item(type); | |||||
Item *item = creatItem(type); | |||||
item->setPos(scenePos); | item->setPos(scenePos); | ||||
connect(item, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); | connect(item, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); | ||||
connect(item, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); | connect(item, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); | ||||
@@ -88,7 +88,7 @@ void MyGraphicsView::keyPressEvent(QKeyEvent *event) | |||||
// Ctrl+V | // Ctrl+V | ||||
if (!clipboard_.input.isEmpty()) { | if (!clipboard_.input.isEmpty()) { | ||||
QPointF center = mapToScene(viewport()->rect().center()); | QPointF center = mapToScene(viewport()->rect().center()); | ||||
Item* newItem = new Item(clipboard_.input); | |||||
Item* newItem = creatItem(clipboard_.input); | |||||
newItem->setPos(center); | newItem->setPos(center); | ||||
connect(newItem, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); | connect(newItem, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); | ||||
connect(newItem, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); | connect(newItem, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); | ||||
@@ -108,7 +108,7 @@ void MyGraphicsView::contextMenuEvent(QContextMenuEvent *event) | |||||
QAction* pasteAct = menu.addAction("粘贴"); | QAction* pasteAct = menu.addAction("粘贴"); | ||||
QAction* sel = menu.exec(event->globalPos()); | QAction* sel = menu.exec(event->globalPos()); | ||||
if (sel == pasteAct) { | if (sel == pasteAct) { | ||||
Item* newItem = new Item(clipboard_.input); | |||||
Item* newItem = creatItem(clipboard_.input); | |||||
newItem->setPos(scenePos); | newItem->setPos(scenePos); | ||||
connect(newItem, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); | connect(newItem, &Item::requestCopy, this, &MyGraphicsView::onItemRequestCopy); | ||||
connect(newItem, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); | connect(newItem, &Item::requestDelete, this, &MyGraphicsView::onItemRequestDelete); | ||||
@@ -9,6 +9,7 @@ | |||||
#include <QMouseEvent> | #include <QMouseEvent> | ||||
#include "item.h" | #include "item.h" | ||||
#include "connection.h" | #include "connection.h" | ||||
#include "creatitem.h" | |||||
class MyGraphicsView : public QGraphicsView | class MyGraphicsView : public QGraphicsView | ||||
{ | { | ||||