Просмотр исходного кода

分离不同组件的创建

master
鹏鹏 李 3 дней назад
Родитель
Сommit
36d72cc102
16 измененных файлов: 274 добавлений и 68 удалений
  1. +34
    -0
      coil.cpp
  2. +14
    -0
      coil.h
  3. +32
    -0
      comparator.cpp
  4. +14
    -0
      comparator.h
  5. +46
    -0
      contact.cpp
  6. +14
    -0
      contact.h
  7. +10
    -0
      creatitem.cpp
  8. +10
    -0
      creatitem.h
  9. +8
    -0
      editor.pro
  10. +1
    -1
      editor.pro.user
  11. +58
    -58
      item.cpp
  12. +3
    -5
      item.h
  13. +2
    -1
      mainwindow.cpp
  14. +24
    -0
      mainwindow.ui
  15. +3
    -3
      mygraphicsview.cpp
  16. +1
    -0
      mygraphicsview.h

+ 34
- 0
coil.cpp Просмотреть файл

@@ -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());
}
}

+ 14
- 0
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

+ 32
- 0
comparator.cpp Просмотреть файл

@@ -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());
}
}

+ 14
- 0
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

+ 46
- 0
contact.cpp Просмотреть файл

@@ -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());
}
}

+ 14
- 0
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

+ 10
- 0
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;
}

+ 10
- 0
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

+ 8
- 0
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


+ 1
- 1
editor.pro.user Просмотреть файл

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<data>
<variable>EnvironmentId</variable>


+ 58
- 58
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
{


+ 3
- 5
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<Connection*> connections_;
};


+ 2
- 1
mainwindow.cpp Просмотреть файл

@@ -10,6 +10,7 @@
#include <QDropEvent>
#include <QGraphicsView>
#include <QMessageBox>
#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);


+ 24
- 0
mainwindow.ui Просмотреть файл

@@ -60,8 +60,32 @@
<height>26</height>
</rect>
</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 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>
<customwidgets>
<customwidget>


+ 3
- 3
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);


+ 1
- 0
mygraphicsview.h Просмотреть файл

@@ -9,6 +9,7 @@
#include <QMouseEvent>
#include "item.h"
#include "connection.h"
#include "creatitem.h"

class MyGraphicsView : public QGraphicsView
{


Загрузка…
Отмена
Сохранить