495 lines
15 KiB
C++
495 lines
15 KiB
C++
#include "QtSendPLC.h"
|
|
#include "qmath.h"
|
|
#include "qmessagebox.h"
|
|
#include "QScreen"
|
|
|
|
QtSendPLC::QtSendPLC(QWidget *parent)
|
|
: QMainWindow(parent)
|
|
{
|
|
ui.setupUi(this);
|
|
//追踪鼠标,写在构造函数中
|
|
this->setMouseTracking(true);
|
|
ui.centralWidget->setMouseTracking(true);
|
|
ui.title->setMouseTracking(true);
|
|
//setWindowIcon(QIcon("./icon/logo_Dongsheng.png"));
|
|
|
|
//ui.pushButton_min->setStyleSheet("QToolTip{color: rgb(32, 36, 40);}"); //设置边框, 边框色, 背景色, 字体色, 字号
|
|
//ui.pushButton_close->setStyleSheet("QToolTip{color: rgb(32, 36, 40);}"); //设置边框, 边框色, 背景色, 字体色, 字号
|
|
//ui.pushButton_max->setStyleSheet("QToolTip{color: rgb(32, 36, 40);}"); //设置边框, 边框色, 背景色, 字体色, 字号
|
|
//ui.pushButton_min->setToolTip(tr("最小化"));
|
|
//ui.pushButton_close->setToolTip(tr("关闭"));
|
|
//ui.pushButton_max->setToolTip(tr("向下还原"));
|
|
ui.label_icon->setStyleSheet("image: url(./Icon/logo_Dongsheng.png);");
|
|
ui.pushButton_min->setText("");
|
|
ui.pushButton_close->setText("");
|
|
ui.pushButton_max->setText("");
|
|
|
|
ui.pushButton_min->setStyleSheet("background-color:transparent;image: url(./Icon/min_n.png);");
|
|
ui.pushButton_close->setStyleSheet("background-color:transparent;image: url(./Icon/close_n.png);");
|
|
ui.pushButton_max->setStyleSheet("background-color:transparent;image: url(./Icon/Max_n.png);");
|
|
|
|
connect(ui.pushButton_min, &QPushButton::pressed, [=]() {
|
|
this->showMinimized();
|
|
qApp->processEvents();
|
|
});
|
|
connect(ui.pushButton_close, &QPushButton::clicked, [=]() {
|
|
this->close();
|
|
qApp->processEvents();
|
|
});
|
|
|
|
QScreen* screen = QGuiApplication::primaryScreen();
|
|
QSize screenSize = screen->size();
|
|
static int screenWidth = screenSize.width();
|
|
static int screenHeight = screenSize.height();
|
|
qDebug() << "Screen resolution: " << screenWidth << "x" << screenHeight;
|
|
|
|
static int maxClickedNum = 1;
|
|
connect(ui.pushButton_max, &QPushButton::pressed, [=]() {
|
|
if (maxClickedNum)
|
|
{
|
|
//ui.pushButton_max->setStyleSheet("background-color:transparent;image: url(Max_n.png);");
|
|
//ui.pushButton_max->setToolTip(tr("向下还原"));
|
|
setWindowFlags(Qt::FramelessWindowHint); //隐藏标题栏
|
|
this->showMaximized();
|
|
maxClickedNum = 0;
|
|
}
|
|
else {
|
|
//ui.pushButton_max->setStyleSheet("background-color:transparent;image: url(Max_n.png);");
|
|
//ui.pushButton_max->setToolTip(tr("最大化"));
|
|
setWindowFlags(Qt::FramelessWindowHint); //隐藏标题栏
|
|
this->showNormal();
|
|
|
|
this->resize(140 * maxCol + 20, max_row * 45 + 230);
|
|
if (max_row >= 6 || maxCol >= 6)
|
|
{
|
|
this->move(screenWidth / 5, screenHeight / 8);
|
|
}
|
|
else
|
|
{
|
|
this->move(screenWidth / 4, screenHeight / 4);
|
|
}
|
|
maxClickedNum = 1;
|
|
}
|
|
qApp->processEvents();
|
|
});
|
|
setWindowFlags(Qt::FramelessWindowHint); //隐藏标题栏
|
|
//this->showNormal();
|
|
this->resize(screenWidth / 4, screenHeight / 4);
|
|
this->move(screenWidth / 4, screenHeight / 4);
|
|
|
|
ui.pushButton_send->setMinimumSize(QSize(80, 30));
|
|
ui.pushButton_sendALL->setMinimumSize(QSize(80, 30));
|
|
ui.lineEdit->setMinimumSize(QSize(80, 30));
|
|
|
|
ui.pushButton_send->setStyleSheet("QPushButton{background-color:rgb(68, 74, 80);border-radius:10px;}"
|
|
"QPushButton:hover{ background-color: rgb(0, 0, 0); border:none; color:rgb(255, 255, 255); }");
|
|
connect(ui.pushButton_send, &QPushButton::clicked, [=]() {
|
|
if (clock() - pressStart < 100)
|
|
return;
|
|
pressStart = clock();
|
|
//ui.tabWidget->setCurrentIndex(2);
|
|
//QString cmd = sendPLCcmd[buttonNum];
|
|
QMessageBox* myBox = new QMessageBox;
|
|
if (ui.lineEdit->text().size() < 5 || ui.lineEdit->text().split("_").size() < 2)
|
|
{
|
|
myBox->information(this, "指令可能错误", "长度小于5,或者未包含_,指令:" + ui.lineEdit->text()); return;
|
|
}
|
|
QMessageBox::StandardButton result = QMessageBox::question(this, "确认发送此指令,指令错误可能导致程序崩溃?", ui.lineEdit->text());
|
|
if (result == QMessageBox::Yes)
|
|
emit sendPLC(ui.lineEdit->text());
|
|
});
|
|
|
|
ui.pushButton_sendALL->setStyleSheet("QPushButton{background-color:rgb(68, 74, 80);border-radius:10px;}"
|
|
"QPushButton:hover{ background-color: rgb(0, 0, 0); border:none; color:rgb(255, 255, 255); }");
|
|
connect(ui.pushButton_sendALL, &QPushButton::clicked, [=]() {
|
|
if (clock() - pressStart < 100)
|
|
return;
|
|
pressStart = clock();
|
|
|
|
if (sendPLCcmd.size() > 0)
|
|
{
|
|
QMessageBox::StandardButton result = QMessageBox::question(this, "确认补全部信号?", "补全部信号,可能导致通信流程变更!");
|
|
if (result == QMessageBox::Yes)
|
|
{
|
|
QString allCmd = "补全部信号#" + sendPLCcmd[0];
|
|
for (int i = 1; i < sendPLCcmd.size(); i++)
|
|
allCmd += "#" + sendPLCcmd[i];
|
|
emit sendPLC(allCmd);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
QMessageBox* myBox = new QMessageBox;
|
|
myBox->information(this, "失败", "无指令");
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
QtSendPLC::~QtSendPLC()
|
|
{}
|
|
|
|
void QtSendPLC::setData(QJsonArray qjsonArray)
|
|
{
|
|
setWindowFlags(Qt::FramelessWindowHint); //隐藏标题栏
|
|
ui.gridLayout->setSpacing(20);
|
|
ui.gridLayout->setVerticalSpacing(10);
|
|
ui.gridLayout->setHorizontalSpacing(10);
|
|
ui.gridLayout->setContentsMargins(10, 10, 10, 10);
|
|
ui.gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
|
|
|
|
|
|
int buttonNum = sendPLCbuttonS.size();
|
|
if (buttonNum > 0)
|
|
for (int i = 0; i < buttonNum; i++)
|
|
{
|
|
sendPLCbuttonS[i]->disconnect();
|
|
delete sendPLCbuttonS[i];
|
|
}
|
|
sendPLCbuttonS.clear();
|
|
sendPLCcmd.clear();
|
|
buttonNum = 0;
|
|
int m_row = 0;
|
|
int m_col = 0;
|
|
int startCol = 0;
|
|
|
|
QJsonArray sendPLCArray = qjsonArray;// programObj.value("手动补信号").toArray();
|
|
//for(int k=0;k<100;k++)
|
|
for (int i = 0; i < sendPLCArray.size(); i++)
|
|
{
|
|
m_col = startCol;
|
|
QJsonObject sendObj = sendPLCArray[i].toObject();
|
|
QString sendName = sendObj.value("信号名").toString();
|
|
QString sendAddress = sendObj.value("指令地址").toString();
|
|
QJsonArray sendDetail = sendObj.value("信号值").toArray();
|
|
for (int i = 0; i < sendDetail.size(); i++)
|
|
{
|
|
QSizePolicy sizePolicy4(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
sizePolicy4.setHorizontalStretch(0);
|
|
sizePolicy4.setVerticalStretch(0);
|
|
|
|
QJsonObject detailObj = sendDetail[i].toObject();
|
|
QString detailName = detailObj.value("标签名").toString();
|
|
QString detailValue = detailObj.value("标签值").toString();
|
|
sendPLCcmd.append(sendAddress + "_" + detailValue);
|
|
|
|
QLabel* mylabel = new QLabel(this);
|
|
mylabel->setText(" 指令: " + sendPLCcmd.last()+" ");
|
|
mylabel->setStyleSheet("background-color:rgb(229, 229, 229);color:rgb(0, 0, 0); border-radius: 0px;");
|
|
mylabel->setMinimumSize(QSize(80, 30));
|
|
mylabel->setSizePolicy(sizePolicy4);
|
|
ui.gridLayout->addWidget(mylabel, m_row, m_col++);
|
|
|
|
QPushButton* myButton = new QPushButton(this);
|
|
myButton->setText(sendName + "_" + detailName);
|
|
myButton->setStyleSheet("QPushButton{background-color:rgb(68, 74, 80);border-radius:10px;}"
|
|
"QPushButton:hover{ background-color: rgb(0, 0, 0); border:none; color:rgb(255, 255, 255); }");
|
|
//myButton->setStyleSheet("background-color:rgb(68, 74, 80);border-radius: 10px; ");
|
|
myButton->setMinimumSize(QSize(80, 30));
|
|
myButton->setSizePolicy(sizePolicy4);
|
|
sendPLCbuttonS.enqueue(myButton);
|
|
|
|
QPushButton* thisButton = sendPLCbuttonS[buttonNum];
|
|
connect(thisButton, &QPushButton::clicked, [=]() {
|
|
if (clock() - pressStart < 100)
|
|
return;
|
|
pressStart = clock();
|
|
//ui.tabWidget->setCurrentIndex(4);
|
|
/*QString cmd = sendPLCcmd[buttonNum];
|
|
ui.plainTextEdit_operat->appendPlainText("手动补信号:" + cmd);
|
|
my_Logic_transfer->sendPLC(cmd);*/
|
|
QMessageBox::StandardButton result = QMessageBox::question(this, "确认发送此指令,指令错误可能导致程序崩溃!", sendPLCcmd[buttonNum]);
|
|
if (result == QMessageBox::Yes)
|
|
emit sendPLC(sendPLCcmd[buttonNum]);
|
|
});
|
|
ui.gridLayout->addWidget(sendPLCbuttonS[buttonNum], m_row, m_col++);
|
|
|
|
buttonNum++;
|
|
maxCol = qMax(maxCol, m_col);
|
|
max_row = qMax(max_row, m_row);
|
|
}
|
|
m_row++;
|
|
|
|
//每20行,换列
|
|
if (m_row == 20)
|
|
{
|
|
m_row = 0;
|
|
startCol = maxCol + 1;
|
|
QLabel* myQL = new QLabel(this);
|
|
myQL->setText(" ");
|
|
ui.gridLayout->addWidget(myQL, m_row, startCol - 1);
|
|
}
|
|
}
|
|
|
|
m_col = 0;
|
|
//其余控件高度160;窗口初始826*505
|
|
//设置窗口
|
|
//if (max_row >= 6 || maxCol>=6)
|
|
{
|
|
this->resize(140 * maxCol + 20, max_row * 45 + 230);
|
|
QScreen* screen = QGuiApplication::primaryScreen();
|
|
if (max_row >= 6 || maxCol >= 6)
|
|
{
|
|
this->move(screen->size().width() / 5, screen->size().height() / 8);
|
|
}
|
|
else
|
|
{
|
|
this->move(screen->size().width() / 4, screen->size().height() / 4);
|
|
}
|
|
}
|
|
|
|
/*QPushButton* myButton = new QPushButton(this);
|
|
myButton->setText("补全部信号");
|
|
myButton->setStyleSheet("background-color:rgb(50, 55, 60);border-radius: 10px; ");
|
|
myButton->setMinimumSize(QSize(80, 30));
|
|
sendPLCbuttonS.enqueue(myButton);
|
|
|
|
QPushButton* thisButton = sendPLCbuttonS[buttonNum];*/
|
|
|
|
|
|
ui.gridLayout->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
|
|
//ui.gridLayout->setSpacing(4);
|
|
//setLayout(ui.gridLayout);
|
|
|
|
//QSizePolicy sizePolicy1(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
//sizePolicy1.setHorizontalStretch(0);
|
|
//sizePolicy1.setVerticalStretch(0);
|
|
////sizePolicy1.setHeightForWidth(ui.widget_image->sizePolicy().hasHeightForWidth());
|
|
////this->setStyleSheet(QString::fromUtf8("background-color: rgb(101, 103, 100);"));
|
|
//this->setSizePolicy(sizePolicy1);
|
|
this->setMinimumSize(QSize(800, 800));
|
|
|
|
pressStart = clock();
|
|
//this->show();
|
|
|
|
//设置窗口大小
|
|
|
|
|
|
}
|
|
|
|
|
|
#define MOUSE_MARGIN 2
|
|
//计算鼠标位置
|
|
QtSendPLC::MousePoss QtSendPLC::calcmouseposs(QPoint p)
|
|
{
|
|
QtSendPLC::MousePoss mp;
|
|
if (p.x() < MOUSE_MARGIN && p.y() < MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::LeftUp; //左上
|
|
else if (p.x() > MOUSE_MARGIN && p.y() < MOUSE_MARGIN && p.x() < this->width() - MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::Up; //上
|
|
else if (p.x() > this->width() - MOUSE_MARGIN && p.y() < MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::RightUp; //右上
|
|
else if (p.x() > this->width() - MOUSE_MARGIN && p.y() > MOUSE_MARGIN && p.y() < this->height() - MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::Right; //右
|
|
else if (p.x() > this->width() - MOUSE_MARGIN && p.y() > this->height() - MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::RightDown; //右下
|
|
else if (p.x() > MOUSE_MARGIN && p.x() < this->width() - MOUSE_MARGIN && p.y() > this->height() - MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::Down; //下
|
|
else if (p.x() < MOUSE_MARGIN && p.y() > this->height() - MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::LeftDown; //左下
|
|
else if (p.x() < MOUSE_MARGIN && p.y() < this->height() - MOUSE_MARGIN && p.y() > MOUSE_MARGIN)
|
|
mp = QtSendPLC::MousePoss::Left; //左
|
|
else
|
|
mp = QtSendPLC::MousePoss::Middle; //中间
|
|
return mp;
|
|
}
|
|
//修改鼠标样式
|
|
void QtSendPLC::setCursorType(QtSendPLC::MousePoss type)
|
|
{
|
|
Qt::CursorShape cursor;
|
|
switch (type)
|
|
{
|
|
case QtSendPLC::MousePoss::LeftUp:
|
|
case QtSendPLC::MousePoss::RightDown:
|
|
cursor = Qt::SizeFDiagCursor;
|
|
break;
|
|
case QtSendPLC::MousePoss::Up:
|
|
case QtSendPLC::MousePoss::Down:
|
|
cursor = Qt::SizeVerCursor;
|
|
break;
|
|
case QtSendPLC::MousePoss::RightUp:
|
|
case QtSendPLC::MousePoss::LeftDown:
|
|
cursor = Qt::SizeBDiagCursor;
|
|
break;
|
|
case QtSendPLC::MousePoss::Right:
|
|
case QtSendPLC::MousePoss::Left:
|
|
cursor = Qt::SizeHorCursor;
|
|
break;
|
|
default:
|
|
cursor = Qt::ArrowCursor;
|
|
break;
|
|
}
|
|
setCursor(cursor);
|
|
}
|
|
//改变窗口的大小
|
|
void QtSendPLC::changeWindowSize(QtSendPLC::MousePoss type, QRect& wid, QPoint ptemp, int minWidth, int minHeight)
|
|
{
|
|
switch (type)//改变窗口的大小
|
|
{
|
|
case QtSendPLC::MousePoss::LeftUp:
|
|
{
|
|
QPoint pos = wid.topLeft();
|
|
|
|
if (wid.width() > minWidth || ptemp.x() < 0)
|
|
pos.rx() = pos.rx() + ptemp.x();
|
|
if (wid.height() > minHeight || ptemp.y() < 0)
|
|
pos.ry() = pos.ry() + ptemp.y();
|
|
|
|
wid.setTopLeft(pos);
|
|
break;//左上角
|
|
}
|
|
case QtSendPLC::MousePoss::RightUp:
|
|
{
|
|
QPoint pos = wid.topRight();
|
|
|
|
if (wid.width() > minWidth || ptemp.x() > 0)
|
|
pos.rx() = pos.rx() + ptemp.x();
|
|
if (wid.height() > minHeight || ptemp.y() < 0)
|
|
pos.ry() = pos.ry() + ptemp.y();
|
|
|
|
wid.setTopRight(pos);
|
|
break;//右上角
|
|
}
|
|
case QtSendPLC::MousePoss::LeftDown:
|
|
{
|
|
QPoint pos = wid.bottomLeft();
|
|
|
|
if (wid.width() > minWidth || ptemp.x() < 0)
|
|
pos.rx() = pos.rx() + ptemp.x();
|
|
if (wid.height() > minHeight || ptemp.y() > 0)
|
|
pos.ry() = pos.ry() + ptemp.y();
|
|
|
|
wid.setBottomLeft(pos);
|
|
break;//左下角
|
|
}
|
|
case QtSendPLC::MousePoss::RightDown:
|
|
{
|
|
QPoint pos = wid.bottomRight();
|
|
|
|
if (wid.width() > minWidth || ptemp.x() > 0)
|
|
pos.rx() = pos.rx() + ptemp.x();
|
|
if (wid.height() > minHeight || ptemp.y() > 0)
|
|
pos.ry() = pos.ry() + ptemp.y();
|
|
|
|
wid.setBottomRight(pos);
|
|
break;//右下角
|
|
}
|
|
case QtSendPLC::MousePoss::Up:
|
|
{
|
|
int topY = wid.top();
|
|
if (wid.height() > minHeight || ptemp.y() < 0)
|
|
topY = topY + ptemp.y();
|
|
|
|
wid.setTop(topY);
|
|
break;//上
|
|
}
|
|
case QtSendPLC::MousePoss::Left:
|
|
{
|
|
int leftX = wid.left();
|
|
|
|
if (wid.width() > minWidth || ptemp.x() < 0)
|
|
leftX = leftX + ptemp.x();
|
|
|
|
wid.setLeft(leftX);
|
|
break;//左
|
|
}
|
|
case QtSendPLC::MousePoss::Right:
|
|
{
|
|
int rightX = wid.right();
|
|
|
|
if (wid.width() > minWidth || ptemp.x() > 0)
|
|
rightX = rightX + ptemp.x();
|
|
|
|
wid.setRight(rightX);
|
|
break;//右
|
|
}
|
|
case QtSendPLC::MousePoss::Down:
|
|
{
|
|
int botY = wid.bottom();
|
|
if (wid.height() > minHeight || ptemp.y() > 0)
|
|
botY = botY + ptemp.y();
|
|
|
|
wid.setBottom(botY);
|
|
break;//下
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
//三个鼠标事件的重写
|
|
void QtSendPLC::mousePressEvent(QMouseEvent* e)
|
|
{
|
|
|
|
if (e->button() == Qt::LeftButton) //鼠标左键按下
|
|
{
|
|
m_lastPos = e->globalPos();
|
|
e->ignore();
|
|
}
|
|
|
|
|
|
}
|
|
//鼠标移动
|
|
void QtSendPLC::mouseMoveEvent(QMouseEvent* e)
|
|
{
|
|
//最大化状态,鼠标是否在标题栏,拖拽还原
|
|
if (this->isMaximized())
|
|
{
|
|
if (ui.title->geometry().contains(this->mapFromGlobal(QCursor::pos())) && e->buttons() == Qt::LeftButton)
|
|
{
|
|
QPoint ptemp = e->globalPos();
|
|
if (ptemp != m_lastPos)
|
|
{
|
|
this->showNormal();
|
|
}
|
|
}
|
|
}
|
|
QtSendPLC::MousePoss curPos = calcmouseposs(e->pos());
|
|
if (!e->buttons())
|
|
{
|
|
setCursorType(curPos);
|
|
mp_curPos = curPos;
|
|
}
|
|
|
|
if (e->buttons() == Qt::LeftButton) //坐标左键按下并移动
|
|
{
|
|
QPoint ptemp = e->globalPos();
|
|
ptemp = ptemp - m_lastPos; //鼠标移动的偏移量
|
|
if (mp_curPos == QtSendPLC::MousePoss::Middle && ui.title->geometry().contains(this->mapFromGlobal(QCursor::pos()))
|
|
&& !ui.pushButton_max->geometry().contains(this->mapFromGlobal(QCursor::pos()))
|
|
&& !ui.pushButton_min->geometry().contains(this->mapFromGlobal(QCursor::pos()))
|
|
&& !ui.pushButton_close->geometry().contains(this->mapFromGlobal(QCursor::pos())))
|
|
{
|
|
move(ptemp + pos());
|
|
}
|
|
else
|
|
{
|
|
QRect wid = geometry();
|
|
int minWidth = this->minimumWidth();
|
|
int minHeight = this->minimumHeight();
|
|
if (ptemp.x() < 50 && ptemp.x() > -50)
|
|
{
|
|
changeWindowSize(mp_curPos, wid, ptemp, minWidth, minHeight);
|
|
//更新大小
|
|
setGeometry(wid);
|
|
}
|
|
}
|
|
m_lastPos = e->globalPos();
|
|
}
|
|
e->ignore();
|
|
}
|
|
//鼠标释放
|
|
void QtSendPLC::mouseReleaseEvent(QMouseEvent* e)
|
|
{
|
|
if (e->button() == Qt::LeftButton && ui.title->geometry().contains(this->mapFromGlobal(QCursor::pos()))
|
|
&& !ui.pushButton_max->geometry().contains(this->mapFromGlobal(QCursor::pos()))
|
|
&& !ui.pushButton_min->geometry().contains(this->mapFromGlobal(QCursor::pos()))
|
|
&& !ui.pushButton_close->geometry().contains(this->mapFromGlobal(QCursor::pos()))) //鼠标左键释放
|
|
{
|
|
int dx = e->globalX() - m_lastPos.x();
|
|
int dy = e->globalY() - m_lastPos.y();
|
|
move(x() + dx, y() + dy);
|
|
}
|
|
QApplication::restoreOverrideCursor();//恢复鼠标指针性状
|
|
e->ignore();
|
|
} |