Browse Source

PLC模块的编辑界面

master
鹏鹏 李 3 days ago
parent
commit
448f077125
7 changed files with 86 additions and 2 deletions
  1. +12
    -0
      connection.cpp
  2. +1
    -0
      connection.h
  3. +0
    -1
      item.h
  4. +53
    -0
      mainwindow.cpp
  5. +6
    -0
      mainwindow.h
  6. +13
    -0
      mainwindow.ui
  7. +1
    -1
      mygraphicsview.h

+ 12
- 0
connection.cpp View File

@@ -1,6 +1,7 @@
#include "connection.h"

#include <QPen>
#include <QPainter>

Connection::Connection(Item* from, Item::AnchorType fromType,
Item* to, Item::AnchorType toType, QGraphicsItem* parent)
@@ -19,3 +20,14 @@ void Connection::updatePosition()
{
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());
}

+ 1
- 0
connection.h View File

@@ -11,6 +11,7 @@ public:
Item* to, Item::AnchorType toType, QGraphicsItem* parent = nullptr);

void updatePosition();
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;

Item *from_, *to_;
Item::AnchorType fromType_, toType_;


+ 0
- 1
item.h View File

@@ -35,7 +35,6 @@ protected:

private:
QString type_;
QColor color_;
QList<Connection*> connections_;
};



+ 53
- 0
mainwindow.cpp View File

@@ -9,6 +9,7 @@
#include <QMouseEvent>
#include <QDropEvent>
#include <QGraphicsView>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
@@ -103,3 +104,55 @@ void MainWindow::createComponents()
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);
}

+ 6
- 0
mainwindow.h View File

@@ -19,11 +19,17 @@ public:
protected:
bool eventFilter(QObject *obj, QEvent *event) override;

private slots:
void on_listWidget_currentTextChanged(const QString &currentText);

void on_pushButton_clicked();

private:
void createComponents();

Ui::MainWindow *ui;
QGraphicsScene *m_scene;
QString selectedComponentType;
};

#endif // MAINWINDOW_H

+ 13
- 0
mainwindow.ui View File

@@ -37,6 +37,19 @@
<enum>QGraphicsView::NoDrag</enum>
</property>
</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 class="QMenuBar" name="menubar">
<property name="geometry">


+ 1
- 1
mygraphicsview.h View File

@@ -40,7 +40,7 @@ private:
};
static ClipInfo clipboard_;

private slots:
public slots:
void onItemRequestCopy(Item*);
void onItemRequestDelete(Item*);
};


Loading…
Cancel
Save