From aa96e4d514b4147f15be1070af6e326d682a005e Mon Sep 17 00:00:00 2001 From: lipengpeng Date: Tue, 12 Aug 2025 12:23:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=96=E8=A7=82=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- button.cpp | 83 +++++++++++++++++++++++---------- button.h | 6 +++ hmi.cpp | 2 +- hmi.h | 2 +- light.cpp | 124 ++++++++++++++++++++++++++++++++++++++++--------- light.h | 12 +++++ mainwindow.cpp | 2 +- 7 files changed, 182 insertions(+), 49 deletions(-) diff --git a/button.cpp b/button.cpp index 2efd9e9..4cec15e 100644 --- a/button.cpp +++ b/button.cpp @@ -2,6 +2,7 @@ #include #include #include +#include Button::Button(const QString &type) : Item(type) { @@ -15,41 +16,48 @@ QRectF Button::boundingRect() const void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { - painter->setRenderHint(QPainter::Antialiasing); + painter->setRenderHint(QPainter::Antialiasing); - if (type_ == "按钮") { + if (type_ == "按钮") { + if (!imagePath_.isEmpty() && !customPixmap_.isNull()) + { + painter->drawPixmap(0,0,50,50,customPixmap_); + } + else{ painter->drawRect(0,0,50,50); } - if (!registerId_.isEmpty()) { - painter->save(); - painter->setFont(QFont("Arial", 8)); - painter->setPen(Qt::black); - QString text; - if (registerValue_ > 0) - { - text = QString("%1\nON").arg(registerId()); - } - else - { - text = QString("%1\nOFF").arg(registerId()); - } - painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text); - painter->restore(); + } + if (!registerId_.isEmpty()) { + painter->save(); + painter->setFont(QFont("Arial", 8)); + painter->setPen(Qt::red); + QString text; + if (registerValue_ > 0) + { + text = QString("%1\nON").arg(registerId()); } - 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()); + else + { + text = QString("%1\nOFF").arg(registerId()); } + painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text); + painter->restore(); + } + 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()); + } } void Button::addMenuActions(QMenu *menu) { menu->addAction("置ON"); menu->addAction("置OFF"); + menu->addAction("外观设置"); } void Button::handleMenuAction(QAction *action) @@ -72,4 +80,31 @@ void Button::handleMenuAction(QAction *action) if (action->text() == "置OFF"){ emit requestSetON(this, false); } + if (action->text() == "外观设置"){ + QString fileName = QFileDialog::getOpenFileName(nullptr, + "选择图片", + "", + "图片文件(*.png *.jpg *.bmp)"); + if (!fileName.isEmpty()) + { + setCustomImage(fileName); + } + } +} + +void Button::setCustomImage(const QString &imagePath) +{ + imagePath_ = imagePath; + loadImage(); + update(); +} + +void Button::loadImage() +{ + if(!imagePath_.isEmpty()){ + customPixmap_ = QPixmap(imagePath_); + if (!customPixmap_.isNull()){ + customPixmap_ = customPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + } } diff --git a/button.h b/button.h index 76874db..b01017d 100644 --- a/button.h +++ b/button.h @@ -12,6 +12,12 @@ public: QWidget *) override; void addMenuActions(QMenu *menu) override; void handleMenuAction(QAction *action) override; + void setCustomImage(const QString& imagePath); + void loadImage(); + +private: + QString imagePath_; + QPixmap customPixmap_; }; #endif // BUTTON_H diff --git a/hmi.cpp b/hmi.cpp index 9c14f5a..bd7a658 100644 --- a/hmi.cpp +++ b/hmi.cpp @@ -11,7 +11,7 @@ #include #include "creatitem.h" -HMI::HMI(RegisterManager *regManager, QWidget *parent) : +HMI::HMI(QWidget *parent) : QWidget(parent), ui(new Ui::HMI) { diff --git a/hmi.h b/hmi.h index 9ffca74..de6fc7e 100644 --- a/hmi.h +++ b/hmi.h @@ -16,7 +16,7 @@ class HMI : public QWidget Q_OBJECT public: - explicit HMI(RegisterManager* regManager, QWidget *parent = nullptr); + explicit HMI(QWidget *parent = nullptr); ~HMI(); void newPage(); void createComponents(); diff --git a/light.cpp b/light.cpp index 6e25aff..6694073 100644 --- a/light.cpp +++ b/light.cpp @@ -1,6 +1,8 @@ #include "light.h" #include #include +#include +#include Light::Light(const QString &type) : Item(type) { @@ -14,35 +16,113 @@ QRectF Light::boundingRect() const void Light::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) { - painter->setRenderHint(QPainter::Antialiasing); + painter->setRenderHint(QPainter::Antialiasing); - if (state()) { - painter->setBrush(Qt::green); // 激活状态 + if (type_ == "指示灯") { + if (state() && !onPixmap_.isNull()) { + painter->drawPixmap(0, 0, 50, 50, onPixmap_); + } else if (!state() && !offPixmap_.isNull()) { + painter->drawPixmap(0, 0, 50, 50, offPixmap_); } else { - painter->setBrush(Qt::white); // 未激活状态 - } - if (type_ == "指示灯") { + if (state()) { + painter->setBrush(Qt::green); // 激活状态 + } else { + painter->setBrush(Qt::white); // 未激活状态 + } painter->drawEllipse(0,0,50,50); } - if (!registerId_.isEmpty()) { - painter->save(); - painter->setFont(QFont("Arial", 8)); - painter->setPen(Qt::black); - QString text = QString("%1").arg(registerId()); - painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text); - painter->restore(); - } - 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()); - } + } + if (!registerId_.isEmpty()) { + painter->save(); + painter->setFont(QFont("Arial", 8)); + painter->setPen(Qt::black); + QString text = QString("%1").arg(registerId()); + painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text); + painter->restore(); + } + 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()); + } } bool Light::state() const { return registerValue_ > 0; } + +void Light::addMenuActions(QMenu *menu) +{ + menu->addAction("ON外观设置"); + menu->addAction("OFF外观设置"); +} + +void Light::handleMenuAction(QAction *action) +{ + if (action->text() == "复制") { + emit requestCopy(this); + } + if (action->text() == "删除") { + emit requestDelete(this); + } + if (action->text() == "对象"){ + emit requestBindRegister(this); + } + if (action->text() == "重置"){ + emit requestReset(this); + } + if (action->text() == "ON外观设置"){ + QString fileName = QFileDialog::getOpenFileName(nullptr, + "选择图片", + "", + "图片文件(*.png *.jpg *.bmp)"); + if (!fileName.isEmpty()) + { + setOnImage(fileName); + } + } + if (action->text() == "OFF外观设置"){ + QString fileName = QFileDialog::getOpenFileName(nullptr, + "选择图片", + "", + "图片文件(*.png *.jpg *.bmp)"); + if (!fileName.isEmpty()) + { + setOffImage(fileName); + } + } +} + +void Light::setOnImage(const QString& imagePath) +{ + onImagePath_ = imagePath; + loadImage(); + update(); +} + +void Light::setOffImage(const QString &imagePath) +{ + offImagePath_ = imagePath; + loadImage(); + update(); +} + +void Light::loadImage() +{ + if (!onImagePath_.isEmpty()) { + onPixmap_ = QPixmap(onImagePath_); + if (!onPixmap_.isNull()) { + onPixmap_ = onPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + } + if (!offImagePath_.isEmpty()) { + offPixmap_ = QPixmap(offImagePath_); + if (!offPixmap_.isNull()) { + offPixmap_ = offPixmap_.scaled(50, 50, Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + } +} diff --git a/light.h b/light.h index c2f5989..a2a8fc9 100644 --- a/light.h +++ b/light.h @@ -11,6 +11,18 @@ public: const QStyleOptionGraphicsItem *option, QWidget *) override; bool state() const override; + void addMenuActions(QMenu *menu) override; + void handleMenuAction(QAction *action) override; + void setCustomImage(const QString& imagePath); + void setOnImage(const QString& imagePath); + void setOffImage(const QString& imagePath); + void loadImage(); + +private: + QString onImagePath_; // ON状态图片路径 + QString offImagePath_; // OFF状态图片路径 + QPixmap onPixmap_; // ON状态图片 + QPixmap offPixmap_; // OFF状态图片 }; #endif // LIGHT_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 9b7d810..ef470a2 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -448,7 +448,7 @@ void MainWindow::saveProject() void MainWindow::connection() { - modbusManager->connectToDevice("COM1",QSerialPort::BaudRate(9600),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1)); + modbusManager->connectToDevice("COM6",QSerialPort::BaudRate(19200),QSerialPort::DataBits(8),QSerialPort::EvenParity,QSerialPort::StopBits(1)); modbusManager->startSimulation(1000); }