qt/ledset/expertscreenconnwin.cpp
2023-09-15 10:59:00 +08:00

280 lines
9.0 KiB
C++

#include "expertscreenconnwin.h"
#include "gutil/qgui.h"
#include <QGroupBox>
#include <QLabel>
#include <QToolButton>
#include <QPushButton>
#include <QPainter>
#include <QTableWidget>
#include <QHeaderView>
#include <QMouseEvent>
#include <QDebug>
#include <math.h>
#define M_PI 3.14159265358979323846
QColor cardColors[] {QColor(0xff2222), QColor(0xffaa00), QColor(0x00bb00), QColor(0x00bbcc), QColor(0x0044ff), QColor(0xffffff), QColor(0xffff00)};
ExpertScreenConnWin::ExpertScreenConnWin(QWidget *parent) : QSplitter{parent} {
auto vBox = new VBox(this);
auto gBox = new QGroupBox(tr("起始位置"));
vBox->addWidget(gBox);
auto hh = new HBox(gBox);
auto lb = new QLabel(tr("X坐标: "));
hh->addWidget(lb);
auto fdX = new QSpinBox;
fdX->setRange(0, 99999);
fdX->setValue(0);
hh->addWidget(fdX);
lb = new QLabel(tr("Y坐标: "));
hh->addWidget(lb);
auto fdY = new QSpinBox;
fdY->setRange(0, 99999);
fdY->setValue(0);
hh->addWidget(fdY);
gBox = new QGroupBox(tr("接收卡设置"));
vBox->addWidget(gBox);
auto vv = new VBox(gBox);
hh = new HBox(vv);
lb = new QLabel(tr("卡列数: "));
hh->addWidget(lb);
fdCardColNum = new QSpinBox;
fdCardColNum->setRange(0, 9999);
fdCardColNum->setValue(2);
hh->addWidget(fdCardColNum);
lb = new QLabel(tr("卡行数: "));
hh->addWidget(lb);
fdCardRowNum = new QSpinBox;
fdCardRowNum->setRange(0, 9999);
fdCardRowNum->setValue(2);
hh->addWidget(fdCardRowNum);
hh = new HBox(vv);
lb = new QLabel(tr("卡宽度: "));
hh->addWidget(lb);
fdCardWidth = new QSpinBox;
fdCardWidth->setRange(0, 9999);
fdCardWidth->setValue(128);
hh->addWidget(fdCardWidth);
lb = new QLabel(tr("卡高度: "));
hh->addWidget(lb);
fdCardHeight = new QSpinBox;
fdCardHeight->setRange(0, 9999);
fdCardHeight->setValue(128);
hh->addWidget(fdCardHeight);
gBox = new QGroupBox(tr("网口选择"));
vBox->addWidget(gBox);
hh = new HBox(gBox);
auto pal = palette();
fdNet = new QButtonGroup(gBox);
for(int i=0; i<sizeof(netss)/sizeof(netss[0]); i++) {
auto bn = new QPushButton(QString::number(i+1));
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);
hh->addWidget(bn);
fdNet->addButton(bn, i);
}
gBox = new QGroupBox(tr("快速串线:"));
gBox->setStyleSheet("QToolButton {border: none; }");
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);
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);
auto bnRefresh = new QPushButton(tr("重新开始"));
connect(bnRefresh, &QPushButton::clicked, this, [this] {
table->clearContents();
for(int i=0; i<sizeof(netss)/sizeof(netss[0]); i++) if(! netss[i].empty()) netss[i].clear();
});
hBox->addWidget(bnRefresh);
auto bnBack = new QPushButton(tr("后退"));
connect(bnBack, &QPushButton::clicked, this, &ExpertScreenConnWin::connBack);
hBox->addWidget(bnBack);
hBox->addStretch();
vBox->addStretch();
table = new CardTable(fdCardRowNum->value(), fdCardColNum->value());
table->win = this;
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
table->setSelectionMode(QTableWidget::NoSelection);
table->setColWidth(90)->setColResize(QHeaderView::Fixed)->setRowHeight(90)->setRowResize(QHeaderView::Fixed);
table->verticalHeader()->setMinimumWidth(30);
connect(table, &QTableWidget::currentCellChanged, this, [=](int row, int column) {
if(row < 0) return;
auto item = table->item(row, column);
if(item) return;
auto netIdx = fdNet->checkedId();
netss[netIdx].emplace_back(QPoint(column, row));
updCell(row, column, netIdx);
table->update();
});
connect(fdCardColNum, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, table, &QTableWidget::setColumnCount);
connect(fdCardRowNum, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, table, &QTableWidget::setRowCount);
addWidget(table);
setStretchFactor(0,0);
setStretchFactor(1,1);
setSizes({280, 1});
}
void ExpertScreenConnWin::updCell(int row, int column, int netIdx) {
auto item = table->setText(row, column, "网口: "+QString::number(netIdx+1)+"\n"+tr("接收卡: ")+QString::number(netss[netIdx].size())+"\n"+tr("宽度: ")+QString::number(fdCardWidth->value())+"\n高度: "+QString::number(fdCardHeight->value()), netIdx);
auto color = cardColors[netIdx % (sizeof(cardColors)/sizeof(cardColors[0]))];
color.setAlpha(100);
item->setBackground(color);
item->setForeground(QColor(255,255,255));
}
void ExpertScreenConnWin::connBack() {
auto netIdx = fdNet->checkedId();
if(netss[netIdx].empty()) return;
table->selectionModel()->clearCurrentIndex();
auto point = netss[netIdx].back();
netss[netIdx].pop_back();
table->setItem(point.y(), point.x(), 0);
table->update();
}
void CardTable::paintEvent(QPaintEvent *event) {
QTableWidget::paintEvent(event);
QPainter painter(viewport());
for(int i=0; i<sizeof(win->netss)/sizeof(win->netss[0]); i++) {
auto size = win->netss[i].size();
if(size < 2) continue;
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];
DrawLineWithArrow(painter,pen,QPoint(point1.x()*90+off, point1.y()*90+off), QPoint(point2.x()*90+off, point2.y()*90+off));
}
}
}
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();
}
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);
}
//added by alahover 20230903 -s
//根据JObj初始化重绘表格
void::ExpertScreenConnWin::InitByJObj(const JObj &obj) {
auto boxList = obj["boxList"].toArray();
qDebug()<<"由JObj初始化表格"<<boxList.size();
if(boxList.empty()) return;
fdCardColNum->setValue(boxList[0]["ScreenCol"].toInt());
fdCardRowNum->setValue(boxList[0]["ScreenRow"].toInt());
fdCardWidth->setValue(boxList[0]["ModuleWidth"].toInt());
fdCardHeight->setValue(boxList[0]["ModuleHeight"].toInt());
for(auto &box : boxList) {
auto PortId = box["PortId"].toInt();
auto ScreenPosCol = box["ScreenPosCol"].toInt();
auto ScreenPosRow = box["ScreenPosRow"].toInt();
netss[PortId].emplace_back(QPoint{ScreenPosCol, ScreenPosRow});
updCell(ScreenPosRow, ScreenPosCol, PortId);
}
}
//added by alahover 20230903 -o