@@ -1,6 +1,7 @@ | |||||
#include "connection.h" | #include "connection.h" | ||||
#include <QPen> | #include <QPen> | ||||
#include <QPainter> | |||||
Connection::Connection(Item* from, Item::AnchorType fromType, | Connection::Connection(Item* from, Item::AnchorType fromType, | ||||
Item* to, Item::AnchorType toType, QGraphicsItem* parent) | Item* to, Item::AnchorType toType, QGraphicsItem* parent) | ||||
@@ -19,3 +20,14 @@ void Connection::updatePosition() | |||||
{ | { | ||||
setLine(QLineF(from_->anchorPos(fromType_), to_->anchorPos(toType_))); | setLine(QLineF(from_->anchorPos(fromType_), to_->anchorPos(toType_))); | ||||
} | } | ||||
void Connection::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) | |||||
{ | |||||
QPen pen = this->pen(); | |||||
if (isSelected()) { | |||||
pen.setColor(Qt::red); // 选中变红 | |||||
pen.setWidth(3); | |||||
} | |||||
painter->setPen(pen); | |||||
painter->drawLine(line()); | |||||
} |
@@ -11,6 +11,7 @@ public: | |||||
Item* to, Item::AnchorType toType, QGraphicsItem* parent = nullptr); | Item* to, Item::AnchorType toType, QGraphicsItem* parent = nullptr); | ||||
void updatePosition(); | void updatePosition(); | ||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override; | |||||
Item *from_, *to_; | Item *from_, *to_; | ||||
Item::AnchorType fromType_, toType_; | Item::AnchorType fromType_, toType_; | ||||
@@ -35,7 +35,6 @@ protected: | |||||
private: | private: | ||||
QString type_; | QString type_; | ||||
QColor color_; | |||||
QList<Connection*> connections_; | QList<Connection*> connections_; | ||||
}; | }; | ||||
@@ -9,6 +9,7 @@ | |||||
#include <QMouseEvent> | #include <QMouseEvent> | ||||
#include <QDropEvent> | #include <QDropEvent> | ||||
#include <QGraphicsView> | #include <QGraphicsView> | ||||
#include <QMessageBox> | |||||
MainWindow::MainWindow(QWidget *parent) | MainWindow::MainWindow(QWidget *parent) | ||||
: QMainWindow(parent) | : QMainWindow(parent) | ||||
@@ -103,3 +104,55 @@ void MainWindow::createComponents() | |||||
it->setData(Qt::UserRole, c.name); | it->setData(Qt::UserRole, c.name); | ||||
} | } | ||||
} | } | ||||
void MainWindow::on_listWidget_currentTextChanged(const QString &text) | |||||
{ | |||||
selectedComponentType = text; | |||||
} | |||||
void MainWindow::on_pushButton_clicked() | |||||
{ | |||||
// 1. 找场景中被选中的连线 | |||||
QList<QGraphicsItem*> selectedItems = ui->graphicsView->scene()->selectedItems(); | |||||
Connection* selectedConn = nullptr; | |||||
for (QGraphicsItem* item : selectedItems) { | |||||
selectedConn = dynamic_cast<Connection*>(item); | |||||
if (selectedConn) break; | |||||
} | |||||
if (!selectedConn) { | |||||
QMessageBox::warning(this, "提示", "请先选中一条连线"); | |||||
return; | |||||
} | |||||
if (selectedComponentType.isEmpty()) { | |||||
QMessageBox::warning(this, "提示", "请先在列表中选择要插入的组件"); | |||||
return; | |||||
} | |||||
// 2. 计算插入点(中点) | |||||
QLineF lf = selectedConn->line(); | |||||
QPointF insertPos = (lf.p1() + lf.p2()) / 2; | |||||
// 3. 删除原连线 | |||||
Item* from = selectedConn->from_; | |||||
Item* to = selectedConn->to_; | |||||
Item::AnchorType fromType = selectedConn->fromType_; | |||||
Item::AnchorType toType = selectedConn->toType_; | |||||
from->removeConnection(selectedConn); | |||||
to->removeConnection(selectedConn); | |||||
ui->graphicsView->scene()->removeItem(selectedConn); | |||||
delete selectedConn; | |||||
// 4. 插入新元件 | |||||
Item* newItem = new Item(selectedComponentType); | |||||
newItem->setPos(insertPos); | |||||
connect(newItem, &Item::requestCopy, ui->graphicsView, &MyGraphicsView::onItemRequestCopy); | |||||
connect(newItem, &Item::requestDelete, ui->graphicsView, &MyGraphicsView::onItemRequestDelete); | |||||
ui->graphicsView->scene()->addItem(newItem); | |||||
// 5. 新建两条连线 | |||||
Connection* c1 = new Connection(from, fromType, newItem, Item::Left); | |||||
ui->graphicsView->scene()->addItem(c1); | |||||
Connection* c2 = new Connection(newItem, Item::Right, to, toType); | |||||
ui->graphicsView->scene()->addItem(c2); | |||||
} |
@@ -19,11 +19,17 @@ public: | |||||
protected: | protected: | ||||
bool eventFilter(QObject *obj, QEvent *event) override; | bool eventFilter(QObject *obj, QEvent *event) override; | ||||
private slots: | |||||
void on_listWidget_currentTextChanged(const QString ¤tText); | |||||
void on_pushButton_clicked(); | |||||
private: | private: | ||||
void createComponents(); | void createComponents(); | ||||
Ui::MainWindow *ui; | Ui::MainWindow *ui; | ||||
QGraphicsScene *m_scene; | QGraphicsScene *m_scene; | ||||
QString selectedComponentType; | |||||
}; | }; | ||||
#endif // MAINWINDOW_H | #endif // MAINWINDOW_H |
@@ -37,6 +37,19 @@ | |||||
<enum>QGraphicsView::NoDrag</enum> | <enum>QGraphicsView::NoDrag</enum> | ||||
</property> | </property> | ||||
</widget> | </widget> | ||||
<widget class="QPushButton" name="pushButton"> | |||||
<property name="geometry"> | |||||
<rect> | |||||
<x>30</x> | |||||
<y>510</y> | |||||
<width>93</width> | |||||
<height>28</height> | |||||
</rect> | |||||
</property> | |||||
<property name="text"> | |||||
<string>插入</string> | |||||
</property> | |||||
</widget> | |||||
</widget> | </widget> | ||||
<widget class="QMenuBar" name="menubar"> | <widget class="QMenuBar" name="menubar"> | ||||
<property name="geometry"> | <property name="geometry"> | ||||
@@ -40,7 +40,7 @@ private: | |||||
}; | }; | ||||
static ClipInfo clipboard_; | static ClipInfo clipboard_; | ||||
private slots: | |||||
public slots: | |||||
void onItemRequestCopy(Item*); | void onItemRequestCopy(Item*); | ||||
void onItemRequestDelete(Item*); | void onItemRequestDelete(Item*); | ||||
}; | }; | ||||