@@ -2,6 +2,7 @@ | |||
#include <QPainter> | |||
#include <QStyleOptionGraphicsItem> | |||
#include <QMenu> | |||
#include <QFileDialog> | |||
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); | |||
} | |||
} | |||
} |
@@ -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 |
@@ -11,7 +11,7 @@ | |||
#include <QVBoxLayout> | |||
#include "creatitem.h" | |||
HMI::HMI(RegisterManager *regManager, QWidget *parent) : | |||
HMI::HMI(QWidget *parent) : | |||
QWidget(parent), | |||
ui(new Ui::HMI) | |||
{ | |||
@@ -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(); | |||
@@ -1,6 +1,8 @@ | |||
#include "light.h" | |||
#include <QPainter> | |||
#include <QStyleOptionGraphicsItem> | |||
#include <QMenu> | |||
#include <QFileDialog> | |||
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); | |||
} | |||
} | |||
} |
@@ -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 |
@@ -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); | |||
} | |||