qt/ledset/expertscreenconnwin.cpp

267 lines
8.5 KiB
C++
Raw Normal View History

2022-08-26 23:15:25 +08:00
#include "expertscreenconnwin.h"
2023-05-27 17:43:57 +08:00
#include "gutil/qgui.h"
2022-08-26 23:15:25 +08:00
#include <QGroupBox>
#include <QLabel>
#include <QToolButton>
#include <QPushButton>
#include <QPainter>
#include <QTableWidget>
#include <QHeaderView>
2022-09-06 23:40:02 +08:00
#include <QMouseEvent>
#include <QDebug>
2022-12-16 15:08:53 +08:00
#include <math.h>
#define M_PI 3.14159265358979323846
QColor cardColors[] {QColor(0xff2222), QColor(0xffaa00), QColor(0x00bb00), QColor(0x00bbcc), QColor(0x0044ff), QColor(0xffffff), QColor(0xffff00)};
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
ExpertScreenConnWin::ExpertScreenConnWin(QWidget *parent) : QSplitter{parent} {
auto vBox = new VBox(this);
2023-07-28 14:48:41 +08:00
auto gBox = new QGroupBox(tr("起始位置"));
vBox->addWidget(gBox);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
auto hh = new HBox(gBox);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
auto lb = new QLabel(tr("X坐标: "));
hh->addWidget(lb);
2022-08-26 23:15:25 +08:00
auto fdX = new QSpinBox;
2023-07-28 14:48:41 +08:00
fdX->setRange(0, 99999);
fdX->setValue(0);
hh->addWidget(fdX);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
lb = new QLabel(tr("Y坐标: "));
hh->addWidget(lb);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
auto fdY = new QSpinBox;
fdY->setRange(0, 99999);
fdY->setValue(0);
hh->addWidget(fdY);
2022-08-26 23:15:25 +08:00
2022-12-16 15:08:53 +08:00
gBox = new QGroupBox(tr("接收卡设置"));
2023-07-28 14:48:41 +08:00
vBox->addWidget(gBox);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
auto vv = new VBox(gBox);
hh = new HBox(vv);
lb = new QLabel(tr("卡列数: "));
hh->addWidget(lb);
2023-07-28 14:48:41 +08:00
auto fdCardColNum = new QSpinBox;
fdCardColNum->setRange(0, 9999);
fdCardColNum->setValue(2);
// connect(fdCardColNum, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this](int value) {
// table->setColumnCount(value);
// });
2023-07-28 14:48:41 +08:00
hh->addWidget(fdCardColNum);
2023-07-28 14:48:41 +08:00
lb = new QLabel(tr("卡行数: "));
hh->addWidget(lb);
2023-07-28 14:48:41 +08:00
auto fdCardRowNum = new QSpinBox;
fdCardRowNum->setRange(0, 9999);
fdCardRowNum->setValue(2);
hh->addWidget(fdCardRowNum);
2023-07-28 14:48:41 +08:00
hh = new HBox(vv);
2023-07-28 14:48:41 +08:00
lb = new QLabel(tr("卡宽度: "));
hh->addWidget(lb);
2023-07-28 14:48:41 +08:00
fdCardWidth = new QSpinBox;
fdCardWidth->setRange(0, 9999);
fdCardWidth->setValue(128);
hh->addWidget(fdCardWidth);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
lb = new QLabel(tr("卡高度: "));
hh->addWidget(lb);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
fdCardHeight = new QSpinBox;
fdCardHeight->setRange(0, 9999);
fdCardHeight->setValue(128);
hh->addWidget(fdCardHeight);
2022-08-26 23:15:25 +08:00
2022-12-16 15:08:53 +08:00
gBox = new QGroupBox(tr("网口选择"));
2023-07-28 14:48:41 +08:00
vBox->addWidget(gBox);
hh = new HBox(gBox);
2022-08-26 23:15:25 +08:00
auto pal = palette();
2022-09-06 23:40:02 +08:00
fdNet = new QButtonGroup(gBox);
2023-09-05 10:02:20 +08:00
for(int i=0; i<sizeof(netss)/sizeof(netss[0]); i++) {
auto bn = new QPushButton(QString::number(i+1));
2023-07-28 14:48:41 +08:00
bn->setMaximumWidth(30);
bn->setMinimumHeight(30);
bn->setCheckable(true);
if(i==0) bn->setChecked(true);
pal.setColor(QPalette::ButtonText, cardColors[i%(sizeof(cardColors)/sizeof(cardColors[0]))]);
bn->setPalette(pal);
2023-07-28 14:48:41 +08:00
hh->addWidget(bn);
fdNet->addButton(bn, i);
}
2022-08-26 23:15:25 +08:00
2022-12-16 15:08:53 +08:00
gBox = new QGroupBox(tr("快速串线:"));
2022-08-26 23:15:25 +08:00
gBox->setStyleSheet("QToolButton {border: none; }");
2023-07-28 14:48:41 +08:00
vBox->addWidget(gBox);
vv = new VBox(gBox);
hh = new HBox(vv);
auto bnConn1 = new QToolButton;
bnConn1->setIconSize(QSize(48,48));
bnConn1->setIcon(QIcon(":/imgs/conn1.png"));
hh->addWidget(bnConn1);
auto bnConn2 = new QToolButton;
bnConn2->setIconSize(QSize(48,48));
bnConn2->setIcon(QIcon(":/imgs/conn2.png"));
hh->addWidget(bnConn2);
auto bnConn3 = new QToolButton;
bnConn3->setIconSize(QSize(48,48));
bnConn3->setIcon(QIcon(":/imgs/conn3.png"));
hh->addWidget(bnConn3);
auto bnConn4 = new QToolButton;
bnConn4->setIconSize(QSize(48,48));
bnConn4->setIcon(QIcon(":/imgs/conn4.png"));
hh->addWidget(bnConn4);
hh = new HBox(vv);
2022-08-26 23:15:25 +08:00
2023-07-28 14:48:41 +08:00
auto bnConn5 = new QToolButton;
bnConn5->setIconSize(QSize(48,48));
bnConn5->setIcon(QIcon(":/imgs/conn5.png"));
hh->addWidget(bnConn5);
auto bnConn6 = new QToolButton;
bnConn6->setIconSize(QSize(48,48));
bnConn6->setIcon(QIcon(":/imgs/conn6.png"));
hh->addWidget(bnConn6);
auto bnConn7 = new QToolButton;
bnConn7->setIconSize(QSize(48,48));
bnConn7->setIcon(QIcon(":/imgs/conn7.png"));
hh->addWidget(bnConn7);
auto bnConn8 = new QToolButton;
bnConn8->setIconSize(QSize(48,48));
bnConn8->setIcon(QIcon(":/imgs/conn8.png"));
hh->addWidget(bnConn8);
auto hBox = new HBox(vBox);
2022-08-26 23:15:25 +08:00
2022-12-16 15:08:53 +08:00
auto bnRefresh = new QPushButton(tr("重新开始"));
connect(bnRefresh, &QPushButton::clicked, this, [this] {
table->clearContents();
2023-09-05 10:02:20 +08:00
for(int i=0; i<sizeof(netss)/sizeof(netss[0]); i++) if(! netss[i].empty()) netss[i].clear();
});
2023-07-28 14:48:41 +08:00
hBox->addWidget(bnRefresh);
2022-08-26 23:15:25 +08:00
2022-12-16 15:08:53 +08:00
auto bnBack = new QPushButton(tr("后退"));
2022-09-06 23:40:02 +08:00
connect(bnBack, &QPushButton::clicked, this, &ExpertScreenConnWin::connBack);
2023-07-28 14:48:41 +08:00
hBox->addWidget(bnBack);
hBox->addStretch();
2023-07-28 14:48:41 +08:00
vBox->addStretch();
table = new CardTable(fdCardRowNum->value(), fdCardColNum->value());
table->win = this;
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->horizontalHeader()->setDefaultSectionSize(90);
2022-12-16 15:08:53 +08:00
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
table->verticalHeader()->setDefaultSectionSize(90);
2022-12-16 15:08:53 +08:00
table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
table->verticalHeader()->setMinimumWidth(30);
2022-09-06 23:40:02 +08:00
table->setSelectionMode(QTableWidget::NoSelection);
connect(table, &QTableWidget::currentCellChanged, this, &ExpertScreenConnWin::conn);
connect(fdCardColNum, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, table, &QTableWidget::setColumnCount);
connect(fdCardRowNum, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, table, &QTableWidget::setRowCount);
2023-07-28 14:48:41 +08:00
addWidget(table);
setStretchFactor(0,0);
setStretchFactor(1,1);
setSizes({280, 1});
}
2022-09-06 23:40:02 +08:00
void ExpertScreenConnWin::conn(int row, int column) {
if(row < 0) return;
auto item = table->item(row, column);
if(item) return;
auto netIdx = fdNet->checkedId();
2023-09-05 10:02:20 +08:00
netss[netIdx].emplace_back(QPoint(column, row));
2022-12-16 15:08:53 +08:00
item = new QTableWidgetItem("网口: "+QString::number(netIdx+1)+"\n"+tr("接收卡: ")+QString::number(netss[netIdx].size())+"\n"+tr("宽度: ")+QString::number(fdCardWidth->value())+"\n高度: "+QString::number(fdCardHeight->value()));
2022-09-06 23:40:02 +08:00
auto color = cardColors[netIdx % (sizeof(cardColors)/sizeof(cardColors[0]))];
2022-12-16 15:08:53 +08:00
item->setForeground(QColor(255,255,255));
color.setAlpha(100);
item->setBackground(color);
2022-09-06 23:40:02 +08:00
item->setData(Qt::UserRole, netIdx);
table->setItem(row, column, item);
table->update();
}
void ExpertScreenConnWin::connBack() {
auto netIdx = fdNet->checkedId();
2023-09-05 10:02:20 +08:00
if(netss[netIdx].empty()) return;
2022-09-06 23:40:02 +08:00
table->selectionModel()->clearCurrentIndex();
2023-09-05 10:02:20 +08:00
auto point = netss[netIdx].back();
netss[netIdx].pop_back();
2022-09-06 23:40:02 +08:00
table->setItem(point.y(), point.x(), 0);
table->update();
}
void CardTable::paintEvent(QPaintEvent *event) {
QTableWidget::paintEvent(event);
QPainter painter(viewport());
2023-09-05 10:02:20 +08:00
for(int i=0; i<sizeof(win->netss)/sizeof(win->netss[0]); i++) {
auto size = win->netss[i].size();
if(size < 2) continue;
2022-12-16 15:08:53 +08:00
QColor a=cardColors[i % (sizeof(cardColors)/sizeof(cardColors[0]))];
//QColor b=QColor(255-a.red(),255-a.green(),255-a.blue(),200);
QColor b=QColor(a.red(),a.green(),a.blue(),255);
QPen pen=QPen(b);
int off = 45 + (i+1)/2 * (i%2 ? -4 : 4);
for(int j=1; j<size; j++) {
auto point1 = win->netss[i][j-1];
auto point2 = win->netss[i][j];
2022-12-16 15:08:53 +08:00
DrawLineWithArrow(painter,pen,QPoint(point1.x()*90+off, point1.y()*90+off), QPoint(point2.x()*90+off, point2.y()*90+off));
}
}
2022-08-26 23:15:25 +08:00
}
2022-09-06 23:40:02 +08:00
void CardTable::mousePressEvent(QMouseEvent *event) {
if(event->button() != Qt::RightButton) QTableWidget::mousePressEvent(event);
}
void CardTable::mouseReleaseEvent(QMouseEvent *event) {
QTableWidget::mouseReleaseEvent(event);
if(event->button() == Qt::RightButton) win->connBack();
}
2022-12-16 15:08:53 +08:00
void CardTable::DrawLineWithArrow(QPainter& painter, QPen pen, QPoint start, QPoint end)
{
painter.setRenderHint(QPainter::Antialiasing, true);
qreal arrowSize = 15;
pen.setWidth(2);
painter.setPen(pen);
painter.setBrush(pen.color());
QLineF line(end, start);
double angle = atan2(-line.dy(), line.dx());
QPointF arrowP1 = line.p1() + QPointF(sin(angle + M_PI / 3) * arrowSize,
cos(angle + M_PI / 3) * arrowSize);
QPointF arrowP2 = line.p1() + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
cos(angle + M_PI - M_PI / 3) * arrowSize);
QPolygonF arrowHead;
arrowHead.clear();
arrowHead << line.p1() << arrowP1 << arrowP2;
painter.drawLine(line);
painter.drawPolygon(arrowHead);
}