Quilting-lw/sharedviews/linegenerwidget.cpp
2026-01-23 16:37:18 +08:00

1012 lines
36 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "linegenerwidget.h"
#include "ui_linegenerwidget.h"
LineGenerWidget::LineGenerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::LineGenerWidget)
{
ui->setupUi(this);
setWindowFlags (Qt::Window | Qt::FramelessWindowHint);
setWindowModality(Qt::ApplicationModal);
// 创建一个QGraphicsView
m_view = new MyGraphicsView(this,GRIDSIZE254);
connect(m_view, SIGNAL(siCurrentIndex(int)), this, SLOT(slotShowCurrentIdx(int)));
ui->labelSizeInfo->setText(tr("Each grid is 1 inch"));//每个格子为1英寸
QString str= tr("Current number of needles: ") + QString("%1").arg(0);//当前针数:
ui->labelIndexInfo->setText(str);
initResolution();
initControlStyle();
m_XMoveValue = 0;
m_YMoveValue = 0;
}
LineGenerWidget::~LineGenerWidget()
{
if(m_view != NULL)
{
delete m_view;
m_view = NULL;
}
delete ui;
}
//初始化窗体控件为任意分辨率
void LineGenerWidget::initResolution()
{
double factoryX = getFactoryX();
double factoryY = getFactoryY();
this->resize(GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
ui->frameBack->setGeometry(0,0,GLB_SCR_WIGHT*factoryX,GLB_SCR_HEIGHT*factoryY);
ui->frameBtn->setGeometry(790*factoryX,10*factoryY,223*factoryX,581*factoryY);
// 设置QGraphicsView的位置和大小
m_view->setGeometry(10*factoryX, 10*factoryY, 771*factoryX, 551*factoryY); // 假设整个窗口用来显示图形
ui->pushBtnPre1->setGeometry(121*factoryX, 10*factoryY, 86*factoryX, 40*factoryY);
ui->pushBtnNext1->setGeometry(16*factoryX, 10*factoryY, 86*factoryX, 40*factoryY);
ui->pushBtnPre10->setGeometry(121*factoryX, 60*factoryY, 86*factoryX, 40*factoryY);
ui->pushBtnNext10->setGeometry(16*factoryX, 60*factoryY, 86*factoryX, 40*factoryY);
ui->pushBtnLine->setGeometry(16*factoryX,254*factoryY,86*factoryX,32*factoryY);
ui->pushBtnArc->setGeometry(121*factoryX,254*factoryY,86*factoryX,32*factoryY);
ui->pushBtnAdsorptionVertex->setGeometry(16*factoryX,296*factoryY,86*factoryX,32*factoryY);
ui->pushBtnRevoke->setGeometry(121*factoryX,296*factoryY,86*factoryX,32*factoryY);
ui->labelX->setGeometry(5*factoryX, 108*factoryY, 15*factoryX, 40*factoryY);
ui->pushButtonXValue->setGeometry(20*factoryX, 108*factoryY, 50*factoryX, 40*factoryY);
ui->labelXUnit->setGeometry(75*factoryX, 108*factoryY, 25*factoryX, 40*factoryY);
ui->pushButtonXAdd->setGeometry(110*factoryX, 108*factoryY, 50*factoryX, 40*factoryY);
ui->pushButtonXDec->setGeometry(170*factoryX, 108*factoryY, 50*factoryX, 40*factoryY);
ui->labelY->setGeometry(5*factoryX, 155*factoryY, 15*factoryX, 40*factoryY);
ui->pushButtonYValue->setGeometry(20*factoryX, 155*factoryY, 50*factoryX, 40*factoryY);
ui->labelYUnit->setGeometry(75*factoryX, 155*factoryY, 25*factoryX, 40*factoryY);
ui->pushButtonYAdd->setGeometry(110*factoryX, 155*factoryY, 50*factoryX, 40*factoryY);
ui->pushButtonYDec->setGeometry(170*factoryX, 155*factoryY, 50*factoryX, 40*factoryY);
ui->labelStepLen->setGeometry(5*factoryX, 205*factoryY, 100*factoryX, 40*factoryY);
ui->pushBtnSetLen->setGeometry(110*factoryX, 205*factoryY, 50*factoryX, 40*factoryY);
ui->labelStepLenUnit->setGeometry(165*factoryX, 205*factoryY, 25*factoryX, 40*factoryY);
ui->pushBtnEnlarge->setGeometry(16*factoryX, 339*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnNarrow->setGeometry(154*factoryX, 339*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnAuto->setGeometry(85*factoryX, 408*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnLeft->setGeometry(16*factoryX, 408*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnRight->setGeometry(154*factoryX, 408*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnUp->setGeometry(85*factoryX, 339*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnDn->setGeometry(85*factoryX, 477*factoryY, 53*factoryX, 53*factoryY);
ui->pushBtnDrag->setGeometry(16*factoryX, 477*factoryY, 53*factoryX, 53*factoryY); //结束本图元绘制
ui->pushBtnNeedleLayoutPreview->setGeometry(154*factoryX,477*factoryY,53*factoryX,53*factoryY); //排针预览
ui->pushBtnOk->setGeometry(16*factoryX, 545*factoryY, 80*factoryX, 30*factoryY);
ui->pushBtnCancel->setGeometry(127*factoryX, 545*factoryY, 80*factoryX, 30*factoryY);
ui->frameDown->setGeometry(10*factoryX,560*factoryY,771*factoryX,31*factoryY);
ui->labelIndexInfo->setGeometry(370*factoryX,5*factoryY,291*factoryX,21*factoryY);
ui->labelSizeInfo->setGeometry(10*factoryX,5*factoryY,291*factoryX,21*factoryY);
double value = static_cast<double>(m_view->getMaxSegmentLength())/10;
ui->pushBtnSetLen->setText(QString::number(value));
}
//设置按钮颜色 天蓝色渐变
void LineGenerWidget::setButtonColorForSkyBlue(QPushButton *button)
{
QString style = "QPushButton{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #7DE0E1, stop: 1 #39C2DC);border: none;}";
style += "QPushButton:pressed{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #6DC3C4, stop: 1 #32AAC0);border: none;}";
style += "QPushButton:disabled{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #E8E8E8, stop: 1 #B3B3B3);border: none;color:rgb(0,0,0);}";
style += "QPushButton:checked{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 rgb(247, 180, 61), stop: 1 rgb(222, 162, 55));border: none;color:rgb(0,0,0);}";
button->setStyleSheet(style);
}
//按钮颜色天空蓝--清空文本设置icon
void LineGenerWidget::setBlueButtonStyleClearTextAndSettingsIcon(QPushButton *button)
{
QString iconName = ":/images/LineGenerWidget/"+button->objectName()+".png";
button->setIcon(QIcon(iconName));
button->setIconSize(7*QSize(returnMinimumSize(button)/8, 7*returnMinimumSize(button)/8));
button->setText("");
QString style = "QPushButton{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #7DE0E1, stop: 1 #39C2DC);border: none;}";
style += "QPushButton:pressed{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #6DC3C4, stop: 1 #32AAC0);border: none;}";
style += "QPushButton:disabled{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #6DC3C4, stop: 1 #32AAC0);border: none;}";
style += "QPushButton:checked{background: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #6DC3C4, stop: 1 #32AAC0);border: none;}";
button->setStyleSheet(style);
}
//按钮颜色橘色--清空文本设置icon
void LineGenerWidget::setOrangeButtonStyleSettingsIcon(QPushButton *button)
{
QString iconName = ":/images/LineGenerWidget/"+button->objectName()+".png";
button->setIcon(QIcon(iconName));
button->setIconSize(QSize(returnMinimumSize(button), returnMinimumSize(button)));
button->setText("");
QString style = "QPushButton{border-radius:" + borderRadius() + "px;}";
// 橙色按钮,白字
style += "QPushButton{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(247, 180, 61), stop:1 rgb(222, 162, 55));color:rgb(255,255,255);}";
style += "QPushButton:pressed{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(222, 196, 128), stop:1 rgb(213,125,54));color:rgb(255,255,255);}";
style += "QPushButton:disabled{background-color: qlineargradient(spread:pad, x1:0.5 y1:0, x2:0.5, y2:1, stop:0 rgb(227, 227, 227), stop:1 rgb(177,177,177));color:rgb(255,255,255);}";
button->setStyleSheet(style);
}
void LineGenerWidget::setPatternNeedleWi(PatternNeedleLayoutWidget *widgetPtr)
{
m_pPatternNeedleWi = widgetPtr;
}
//初始化窗体控件的样式
void LineGenerWidget::initControlStyle()
{
ui->labelIndexInfo->setFont(fontSize_M());
ui->labelSizeInfo->setFont(fontSize_M());
ui->frameBack->setStyleSheet("QFrame {"
"background-color: rgb(0, 0, 0);"
"}");
ui->frameBtn->setStyleSheet("background-color:rgb(67, 67, 67);border-radius: "+borderRadius()+"px;");
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnEnlarge);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnNarrow);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnPre1);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnNext1);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnPre10);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnNext10);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnAuto);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnLeft);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnRight);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnUp);
setBlueButtonStyleClearTextAndSettingsIcon(ui->pushBtnDn);
setButtonColorForSkyBlue(ui->pushBtnLine);
setButtonColorForSkyBlue(ui->pushBtnArc);
setButtonColorForSkyBlue(ui->pushBtnNeedleLayoutPreview);
setButtonColorForSkyBlue(ui->pushBtnAdsorptionVertex);
setButtonColorForSkyBlue(ui->pushBtnRevoke);
setButtonColorForSkyBlue(ui->pushBtnDrag);
setButtonColorForSkyBlue(ui->pushButtonXValue);
setButtonColorForSkyBlue(ui->pushButtonXAdd);
setButtonColorForSkyBlue(ui->pushButtonXDec);
ui->pushButtonXValue->setFont(fontSize_M());
QFont size(FONTTYPE,g_fontsize + 4, FONTNORMAL);
size.setPointSize(size.pointSize()*getFactoryY());
ui->pushButtonXAdd->setFont(size);
ui->pushButtonXDec->setFont(size);
ui->labelX->setFont(fontSize_M());
ui->labelX->setStyleSheet(stateTextColour());
ui->labelXUnit->setFont(fontSize_M());
ui->labelXUnit->setStyleSheet(stateTextColour());
setButtonColorForSkyBlue(ui->pushButtonYValue);
setButtonColorForSkyBlue(ui->pushButtonYAdd);
setButtonColorForSkyBlue(ui->pushButtonYDec);
ui->pushButtonYValue->setFont(fontSize_M());
ui->pushButtonYAdd->setFont(size);
ui->pushButtonYDec->setFont(size);
ui->labelY->setFont(fontSize_M());
ui->labelY->setStyleSheet(stateTextColour());
ui->labelYUnit->setFont(fontSize_M());
ui->labelYUnit->setStyleSheet(stateTextColour());
ui->labelStepLen->setFont(fontSize_M());
ui->labelStepLen->setStyleSheet(stateTextColour());
setButtonColorForSkyBlue(ui->pushBtnSetLen);
ui->labelStepLenUnit->setFont(fontSize_M());
ui->labelStepLenUnit->setStyleSheet(stateTextColour());
ui->pushBtnLine->setFont(fontSize_M());
ui->pushBtnArc->setFont(fontSize_M());
ui->pushBtnNeedleLayoutPreview->setFont(fontSize_M());
ui->pushBtnAdsorptionVertex->setFont(fontSize_M());
ui->pushBtnSetLen->setFont(fontSize_M());
ui->pushBtnDrag->setFont(fontSize_M());
ui->pushBtnRevoke->setFont(fontSize_M());
setOrangeButtonStyleSettingsIcon(ui->pushBtnCancel);
setOrangeButtonStyleSettingsIcon(ui->pushBtnOk);
m_view->setStyleSheet("QGraphicsView {"
"border-top-left-radius: "+borderRadius()+"px;" // 左下角圆角
"border-top-right-radius: "+borderRadius()+"px;" // 右下角圆角
"}");
ui->frameDown->setStyleSheet("QFrame {"
"border-bottom-left-radius: "+borderRadius()+"px;" // 左下角圆角
"border-bottom-right-radius: "+borderRadius()+"px;" // 右下角圆角
"}");
ui->labelIndexInfo->setStyleSheet("color:rgb(255,255,255)");
ui->labelSizeInfo->setStyleSheet("color:rgb(255,255,255)");
}
//返回一个按钮-高和宽最小的一个数值
int LineGenerWidget::returnMinimumSize(QPushButton *button)
{
// 确保指针不是 nullptr以避免在按钮未正确初始化时导致的问题
if (button == NULL) {
return 0; // 或者抛出异常或处理这种情况
}
// 使用三元运算符来返回宽度和高度中的较大值
return button->width() < button->height() ? button->width() : button->height();
}
QByteArray LineGenerWidget::creatQuiDat()
{
int minX = S32_MAX;
int maxX = S32_MIN;
int minY = S32_MAX;
int maxY = S32_MIN;
int maxY1 = S32_MIN;
QList<GLineItem> lineList = m_view->getLineItemList();
QList<GPathItem> pathList = m_view->getPathItemList();
int firstFlag = 0;
for(int i = 0; i < lineList.size(); i++)
{
for(int j = 0; j < lineList[i].points.size(); j++)
{
double x = lineList[i].points[j].x()*100.0;
double y = lineList[i].points[j].y()*100.0;
lineList[i].points[j].setX(x);
lineList[i].points[j].setY(y);
if(maxY1 < lineList[i].points[j].y())
{
maxY1 = lineList[i].points[j].y();
}
}
}
for(int i = 0; i < pathList.size(); i++)
{
for(int j = 0; j < pathList[i].points.size(); j++)
{
double x = pathList[i].points[j].x()*100.0;
double y = pathList[i].points[j].y()*100.0;
pathList[i].points[j].setX(x);
pathList[i].points[j].setY(y);
if(maxY1 < pathList[i].points[j].y())
{
maxY1 = pathList[i].points[j].y();
}
}
}
qDebug()<<"maxY1"<<maxY1;
QByteArray fileData;
fileData.clear();
unsigned char ch1,ch2;
int firstX,endX;
firstX = endX= 0;
//y向取反并按顺序保存
int idx = 1;//序号
int add = 0;
QPoint lastP;
while(1)
{
if(add >= lineList.size()+pathList.size()+200)
{
break;//避免陷入死循环
}
if(idx > lineList.size()+pathList.size())
{
break;
}
for(int i = 0; i < lineList.size(); i++)
{
if(lineList[i].order == idx)
{
unsigned short int type = lineList[i].type;
ch1 = static_cast<unsigned char>(type & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((type >> 8) & 0xFF); // 高字节
fileData.append(ch2);
unsigned short int pointsSize = lineList[i].points.size();
ch1 = static_cast<unsigned char>(pointsSize & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((pointsSize >> 8) & 0xFF); // 高字节
fileData.append(ch2);
for(int j = 0; j < lineList[i].points.size(); j++)
{
unsigned short int x = lineList[i].points[j].x();
if(firstFlag == 0)
{
firstX = x;
firstFlag = 1;
}
endX = x;
lastP.setX(x);
ch1 = static_cast<unsigned char>(x & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((x >> 8) & 0xFF); // 高字节
fileData.append(ch2);
unsigned short int y = maxY1-lineList[i].points[j].y();
lastP.setY(y);
ch1 = static_cast<unsigned char>(y & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((y >> 8) & 0xFF); // 高字节
fileData.append(ch2);
qDebug()<<"line px"<<lineList[i].points[j].x()<<"line py"<<lineList[i].points[j].y();
qDebug()<<"line x"<<x<<"line y"<<y;
if (minX > x) {minX = x;}
if (maxX < x) {maxX = x;}
if (minY > y) {minY = y;}
if (maxY < y) {maxY = y;}
}
idx++;
}
}
for(int i = 0; i < pathList.size(); i++)
{
if(pathList[i].order == idx)
{
unsigned short int type = pathList[i].type;
if((pathList[i].points.size() == 3 && type == QUI_TYPE_ARC) ||
(pathList[i].points.size() == 4 && type == QUI_TYPE_BES))//圆弧固定为3个点贝塞尔为4个点
{
ch1 = static_cast<unsigned char>(type & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((type >> 8) & 0xFF); // 高字节
fileData.append(ch2);
for(int j = 0; j < pathList[i].points.size(); j++)
{
unsigned short int x = pathList[i].points[j].x();
if(firstFlag == 0)
{
firstX = x;
firstFlag = 1;
}
endX = x;
lastP.setX(x);
ch1 = static_cast<unsigned char>(x & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((x >> 8) & 0xFF); // 高字节
fileData.append(ch2);
unsigned short int y = maxY1-pathList[i].points[j].y();
lastP.setY(y);
ch1 = static_cast<unsigned char>(y & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((y >> 8) & 0xFF); // 高字节
fileData.append(ch2);
qDebug()<<"arc px"<<pathList[i].points[j].x()<<"arc py"<<pathList[i].points[j].y();
qDebug()<<"arc x"<<x<<"arc y"<<y;
if (minX > x) {minX = x;}
if (maxX < x) {maxX = x;}
if (minY > y) {minY = y;}
if (maxY < y) {maxY = y;}
}
}
idx++;
}
}
add++;
}
//如果起点和终点x不一致则添加一段跨步使起点x和终点x相等
if(firstX != endX)
{
unsigned short int type = QUI_TYPE_STEP;
ch1 = static_cast<unsigned char>(type & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((type >> 8) & 0xFF); // 高字节
fileData.append(ch2);
unsigned short int pointsSize = 2;
ch1 = static_cast<unsigned char>(pointsSize & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((pointsSize >> 8) & 0xFF); // 高字节
fileData.append(ch2);
//第一个点
unsigned short int x = lastP.x();
ch1 = static_cast<unsigned char>(x & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((x >> 8) & 0xFF); // 高字节
fileData.append(ch2);
unsigned short int y = lastP.y();
ch1 = static_cast<unsigned char>(y & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((y >> 8) & 0xFF); // 高字节
fileData.append(ch2);
qDebug()<<"end x1"<<x<<"end y1"<<y;
//第二个点
x = lastP.x()+(firstX-endX);
ch1 = static_cast<unsigned char>(x & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((x >> 8) & 0xFF); // 高字节
fileData.append(ch2);
y = lastP.y();
ch1 = static_cast<unsigned char>(y & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((y >> 8) & 0xFF); // 高字节
fileData.append(ch2);
qDebug()<<"end x2"<<x<<"end y2"<<y;
}
//增加结束
int end = 0xffff;
ch1 = static_cast<unsigned char>(end & 0xFF); // 低字节
fileData.append(ch1);
ch2 = static_cast<unsigned char>((end >> 8) & 0xFF); // 高字节
fileData.append(ch2);
QuiFileHead quiHead;
memset((char*)&quiHead,0,sizeof(QuiFileHead));
quiHead.scanX = maxX - minX;
quiHead.scanY = maxY - minY;
quiHead.rollaYUpDn = 0;
quiHead.unit = 0x00;
quiHead.ver = 0x01;
quiHead.leftRightLen = m_pPatternNeedleWi->getLeftRightNeedleSpace();//左右针间距
quiHead.HStep = m_pPatternNeedleWi->getNeedlesSpace();//针与针之间间距
quiHead.VStep12 = m_pPatternNeedleWi->getNeedleLayoutSpace12();//针排间距
quiHead.VStep23 = m_pPatternNeedleWi->getNeedleLayoutSpace23();//针排间距
memcpy((char*)&quiHead.arrayNeedle1,(char*)m_pPatternNeedleWi->getarrayNeedle1(),sizeof(unsigned char[LAYOUTNEEDLENUM]));
memcpy((char*)&quiHead.arrayNeedle2,(char*)m_pPatternNeedleWi->getarrayNeedle2(),sizeof(unsigned char[LAYOUTNEEDLENUM]));
memcpy((char*)&quiHead.arrayNeedle3,(char*)m_pPatternNeedleWi->getarrayNeedle3(),sizeof(unsigned char[LAYOUTNEEDLENUM]));
quiHead.normalStep = ui->pushBtnSetLen->text().toInt()*100;
fileData.insert(0, (char*)&quiHead, sizeof(QuiFileHead));
return fileData;
}
//放大
void LineGenerWidget::on_pushBtnEnlarge_clicked()
{
m_view->enlargeView();
}
//缩小
void LineGenerWidget::on_pushBtnNarrow_clicked()
{
m_view->narrowView();
}
void LineGenerWidget::on_pushBtnPre1_clicked()
{
m_view->forwardAndBackward(1);
}
void LineGenerWidget::on_pushBtnNext1_clicked()
{
m_view->forwardAndBackward(-1);
}
void LineGenerWidget::on_pushBtnPre10_clicked()
{
m_view->forwardAndBackward(10);
}
void LineGenerWidget::on_pushBtnNext10_clicked()
{
m_view->forwardAndBackward(-10);
}
void LineGenerWidget::on_pushBtnLine_clicked()
{
ui->pushBtnLine->setChecked(true);
ui->pushBtnArc->setChecked(false);
ui->pushBtnDrag->setChecked(false);
m_view->setShapeType(TYPE_LINE);
}
void LineGenerWidget::on_pushBtnArc_clicked()
{
ui->pushBtnArc->setChecked(true);
ui->pushBtnLine->setChecked(false);
ui->pushBtnDrag->setChecked(false);
m_view->setShapeType(TYPE_ARC);
}
void LineGenerWidget::on_pushBtnUp_clicked()
{
m_view->moveScene(0,10);
}
void LineGenerWidget::on_pushBtnDn_clicked()
{
m_view->moveScene(0,-10);
}
void LineGenerWidget::on_pushBtnLeft_clicked()
{
m_view->moveScene(10,0);
}
void LineGenerWidget::on_pushBtnRight_clicked()
{
m_view->moveScene(-10,0);
}
void LineGenerWidget::on_pushBtnAuto_clicked()
{
m_view->autoView();
}
//保存文件
void LineGenerWidget::on_pushBtnOk_clicked()
{
PromptDialog promptDlg;
promptDlg.initDialog(PromptDialog::BTN_OK_CANCEL);
promptDlg.setTitleStr(tr("Prompt"));//提示
QString str;
str = tr("Do you want to save as a new file?");//是否保存为一个新文件?
promptDlg.setContentStr(str);
if(promptDlg.exec() == 1)
{
// QByteArray dat = creatDsrDat();
QByteArray dat = creatQuiDat();
QDir apppath(qApp->applicationDirPath());
QString filePath;
WordsInputDialog wordInputDlg;
wordInputDlg.clean();
wordInputDlg.setTitleStr(tr("Name"));//命名
wordInputDlg.setOldWordsStr("");
if(wordInputDlg.exec() == 1)
{
//文件名称
QString name = wordInputDlg.getInputStr();
filePath = apppath.path() + apppath.separator() + PATTERNPATH + apppath.separator() + name + ".qui";
}
//保存成qui文件
QFile file(filePath);
if(file.exists())//文件已经存在
{
PromptDialog dlg(this);
dlg.initDialog(PromptDialog::BTN_OK);
dlg.setTitleStr(tr("Prompt"));
QString str;
str = tr("This file already exists!");//此文件已经存在!
dlg.setContentStr(str);
dlg.exec();
file.close();
return;
}
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
qDebug() << "open file fail when wirte, path =" << filePath;
filePath.clear();
}
else
{
file.write(dat);
#ifdef Q_OS_LINUX
system("sync");
#endif
file.close();
}
}
ui->pushBtnLine->setChecked(false);
ui->pushBtnArc->setChecked(false);
m_view->cleanView();
this->hide();
}
void LineGenerWidget::on_pushBtnCancel_clicked()
{
ui->pushBtnLine->setChecked(false);
ui->pushBtnArc->setChecked(false);
m_view->cleanView();
this->hide();
}
void LineGenerWidget::slotShowCurrentIdx(int idx)
{
QString str= tr("Current number of needles: ") + QString("%1").arg(idx);//当前针数:
ui->labelIndexInfo->setText(str);
}
//排针预览
void LineGenerWidget::on_pushBtnNeedleLayoutPreview_clicked()
{
QByteArray datArray = creatAbsDat();
emit siNeedleLayoutPreview(datArray);
// m_pPatternNeedleWi->showTempView(datArray);
}
QByteArray LineGenerWidget::creatDsrDat()
{
double unit = 100.0;
double minX = S32_MAX;
double maxX = S32_MIN;
double minY = S32_MAX;
double maxY = S32_MIN;
QList<QGraphicsEllipseItem *> list = m_view->getPointList();
//dsr数据
QByteArray datArray;
datArray.clear();
DsrStep absItemDat;
memset((char*)&absItemDat,0,sizeof(DsrStep));
s32 prex = 0;
s32 prey = 0;
s32 curx = 0;
s32 cury = 0;
//保存为dsr文件
for(int i = 0; i < list.size(); i++)
{
QPointF point = list[i]->pos();
absItemDat.ctrl = DSR_SEWING;
absItemDat.attr = 0;
absItemDat.dx = 0 - point.x() * unit;
absItemDat.dy = 0 - point.y() * unit;
curx = absItemDat.dx;
cury = absItemDat.dy;
int ddx = curx - prex;
int ddy = cury - prey;
double tar = atan2(ddy,ddx);
absItemDat.dr = (tar * 10000+0.5*(tar>0?1:-1));
if (minX > absItemDat.dx) { minX = absItemDat.dx;}
if (maxX < absItemDat.dx) { maxX = absItemDat.dx;}
if (minY > absItemDat.dy) { minY = absItemDat.dy;}
if (maxY < absItemDat.dy) { maxY = absItemDat.dy;}
datArray.append((char*)&absItemDat,sizeof(DsrStep));
prex = curx;//bin
prey = cury;//bin
}
//添加结束码
absItemDat.ctrl = DATA_END;
absItemDat.attr = 0;
datArray.append((char*)&absItemDat,sizeof(DsrStep));
//生成文件头
DsrHead dsrHead;
memset((char*)&dsrHead,0,sizeof(DsrHead));
int itemNums = datArray.length() / sizeof(DsrStep);
//重新生成文件头
float ver = DSR_VERSION_5;
dsrHead.verCode = *((DWORD*)(&ver));
dsrHead.stitches = itemNums;
dsrHead.xyUnit = 1;
dsrHead.rangePx = maxX-minX;
dsrHead.rangePy = maxY-minY;
dsrHead.rangeNx = minX-minX;
dsrHead.rangeNy = minY-minY;
dsrHead.startX = 0;
dsrHead.startY = 0;
dsrHead.anchorX = 0;
dsrHead.anchorY = 0;
dsrHead.dataBegin = sizeof(DsrHead);
dsrHead.stepSize = itemNums*sizeof(DsrStep); // 数据字节数
datArray.insert(0,(char*)&dsrHead,sizeof(DsrHead));
return datArray;
}
QByteArray LineGenerWidget::creatAbsDat()
{
double unit = 100.0;
double minX;
double maxX;
double minY;
double maxY;
minX = S32_MAX;
maxX = S32_MIN;
minY = S32_MAX;
maxY = S32_MIN;
QList<GLineItem> lineList = m_view->getLineItemList();
QList<GPathItem> pathList = m_view->getPathItemList();
//dsr数据
QByteArray datArray;
datArray.clear();
DsAbsItem absItemDat;
memset((char*)&absItemDat,0,sizeof(DsAbsItem));
s32 prex = 0;
s32 prey = 0;
s32 curx,cury;
curx = cury = 0;
s32 anchorX,anchorY,beginX,beginY;
anchorX = anchorY = beginX = beginY = 0;
//保存为dsr
//y向取反并按顺序保存
int idx = 1;//序号
int add = 0;
while(1)
{
if(add >= lineList.size()+pathList.size()+200)
{
break;//避免陷入死循环
}
if(idx > lineList.size()+pathList.size())
{
break;
}
for(int i = 0; i < lineList.size(); i++)
{
if(lineList[i].order == idx)
{
for(int j = 0; j < lineList[i].points.size(); j++)
{
QPointF point = lineList[i].points[j];
absItemDat.ctrl = DATA_SEWING;
absItemDat.attr = 0;
absItemDat.ax = point.x() * unit;
absItemDat.ay = point.y() * unit * -1;//changey取反
curx = absItemDat.ax;
cury = absItemDat.ay;
int ddx = curx - prex;
int ddy = cury - prey;
double tar = atan2(ddy,ddx);
absItemDat.ar = (tar * 10000+0.5*(tar>0?1:-1));
if (minX > absItemDat.ax) { minX = absItemDat.ax;}
if (maxX < absItemDat.ax) { maxX = absItemDat.ax;}
if (minY > absItemDat.ay) { minY = absItemDat.ay;}
if (maxY < absItemDat.ay) { maxY = absItemDat.ay;}
if(lineList[i].order == 1 && j == 0)//第一个图元的第一个点
{
anchorX = absItemDat.ax;
beginX = absItemDat.ax;
anchorY = absItemDat.ay;
beginY = absItemDat.ay;
}
datArray.append((char*)&absItemDat,sizeof(DsAbsItem));
prex = curx;//bin
prey = cury;//bin
}
idx++;
}
}
for(int i = 0; i < pathList.size(); i++)
{
if(pathList[i].order == idx)
{
unsigned short int type = pathList[i].type;
if((pathList[i].points.size() == 3 && type == QUI_TYPE_ARC) ||
(pathList[i].points.size() == 4 && type == QUI_TYPE_BES))//圆弧固定为3个点贝塞尔为4个点
{
if(pathList[i].points.size() == 3 && type == QUI_TYPE_ARC)
{
double threex[3], threey[3];
threex[0] = pathList[i].points[0].x() * unit;
threex[1] = pathList[i].points[1].x() * unit;
threex[2] = pathList[i].points[2].x() * unit;
threey[0] = pathList[i].points[0].y() * unit * -1;
threey[1] = pathList[i].points[1].y() * unit * -1;
threey[2] = pathList[i].points[2].y() * unit * -1;
calcCurve(threex, threey, ui->pushBtnSetLen->text().toInt()*unit, datArray, DATA_SEWING);
}
for(int j = 0; j < pathList[i].points.size(); j++)
{
QPointF point = pathList[i].points[j];
double ax = point.x() * unit;
double ay = point.y() * unit * -1;//changey取反
curx = ax;
cury = ay;
if (minX > ax) { minX = ax;}
if (maxX < ax) { maxX = ax;}
if (minY > ay) { minY = ay;}
if (maxY < ay) { maxY = ay;}
prex = curx;//bin
prey = cury;//bin
}
}
idx++;
}
}
add++;
}
//添加结束码
memset((char*)&absItemDat, 0, sizeof(DsrStep)); // 重置 absItemDat
absItemDat.ctrl = DATA_END;
absItemDat.attr = 0;
datArray.append((char*)&absItemDat,sizeof(DsAbsItem));
//生成文件头
DataDs16FileHead dhead;
dhead.dataSize = datArray.size(); // 数据字节数
dhead.itemNums = datArray.size()/sizeof(DsAbsItem); // 数据项个数
dhead.bytesPerItem = sizeof(DsAbsItem); // 每项占的字节数
dhead.bytesPerBlk = MAX_EXDP_LEN; // 数据内容划分块大小
dhead.dataChecksum = calcCheckSum32((u8 *)(datArray.data()) , datArray.length()); // 数据累加校验和
dhead.checkCrc = calcCrc16((u8 *)(&dhead), HEAD_FIX_INFO_LEN); // 前面6个字段的CRC校验6个字段分别为文件名称字节数项个数每项字节数每块字节数数据累加和的CRC校验值
u32 fileID = 0;
fileID = calcCheckSum32((u8 *)(datArray.data()) , datArray.length()); // 原始数据累加校验和
dhead.fileid = fileID; //中间数据的 fileid =
dhead.anchorX = anchorX; // 定位点坐标X
dhead.anchorY = anchorY; // 定位点坐标Y
dhead.beginX = beginX; // 数据起点坐标X
dhead.beginY = beginY; // 数据起点坐标Y
dhead.beginR = 0;
dhead.minX = minX;
dhead.maxX = maxX;
dhead.minY = minY;
dhead.maxY = maxY; // 轮廓范围,使用重新计算之后的值
datArray.insert(0,(char*)&dhead,sizeof(dhead));
return datArray;
}
void LineGenerWidget::refreshView()
{
m_view->restoreView();
QWidget::show();
}
void LineGenerWidget::on_pushBtnAdsorptionVertex_clicked()
{
if(ui->pushBtnAdsorptionVertex->isChecked())
{
ui->pushBtnAdsorptionVertex->setChecked(true);
m_view->setAdsorption(true);//吸附
}
else
{
ui->pushBtnAdsorptionVertex->setChecked(false);
m_view->setAdsorption(false);//不吸附
}
}
void LineGenerWidget::on_pushBtnDrag_clicked()
{
ui->pushBtnLine->setChecked(false);
ui->pushBtnArc->setChecked(false);
ui->pushBtnAdsorptionVertex->setChecked(false);
ui->pushBtnDrag->setChecked(true);
m_view->setShapeType(TYPE_DRAG);
}
void LineGenerWidget::on_pushBtnSetLen_clicked()
{
NumerInputDialog ndlg(this);
ndlg.setTitleStr(tr("Step Length"));//针步长度
ndlg.setUnitStr(tr("mm"));
ndlg.setValueAndRange(m_view->getMaxSegmentLength(), 20, 80, 1);
if (ndlg.exec())//输入弹窗点击了确认
{
m_view->setMaxSegmentLength(ndlg.getValue());
}
double value = static_cast<double>(m_view->getMaxSegmentLength())/10;
ui->pushBtnSetLen->setText(QString::number(value));
}
void LineGenerWidget::on_pushButtonXValue_clicked()
{
int rslt=0;
m_XMoveValue= ui->pushButtonXValue->text().toDouble()*10;
NumerInputDialog ndlg(this);
ndlg.setTitleStr("X compensation value setting");
ndlg.setUnitStr("mm");
ndlg.setValueAndRange(m_XMoveValue, 0, 100, 1);
rslt = ndlg.exec();
if (rslt == 1)//点了确认按钮
{
s32 val = ndlg.getValue();
if (val != m_XMoveValue)//更改后的值不等于之前存的值
{
m_XMoveValue = val/10.0;
}
}
ui->pushButtonXValue->setText(QString::number(m_XMoveValue));
}
void LineGenerWidget::on_pushButtonXAdd_clicked()
{
if(m_view != NULL)
{
m_view->setXYMove(m_XMoveValue,0);
}
}
void LineGenerWidget::on_pushButtonXDec_clicked()
{
if(m_view != NULL)
{
m_view->setXYMove(0-m_XMoveValue,0);
}
}
void LineGenerWidget::on_pushButtonYValue_clicked()
{
int rslt=0;
m_YMoveValue= ui->pushButtonYValue->text().toDouble()*10;
NumerInputDialog ndlg(this);
ndlg.setTitleStr("Y compensation value setting");
ndlg.setUnitStr("mm");
ndlg.setValueAndRange(m_YMoveValue, 0, 100, 1);
rslt = ndlg.exec();
if (rslt == 1)//点了确认按钮
{
s32 val = ndlg.getValue();
if (val != m_YMoveValue)//更改后的值不等于之前存的值
{
m_YMoveValue = val/10.0;
}
}
ui->pushButtonYValue->setText(QString::number(m_YMoveValue));
}
void LineGenerWidget::on_pushButtonYAdd_clicked()
{
if(m_view != NULL)
{
m_view->setXYMove(0,m_YMoveValue);
}
}
void LineGenerWidget::on_pushButtonYDec_clicked()
{
if(m_view != NULL)
{
m_view->setXYMove(0,0-m_YMoveValue);
}
}
//撤销(去掉)最后一个点
void LineGenerWidget::on_pushBtnRevoke_clicked()
{
if(m_view != NULL)
{
ui->pushBtnArc->setChecked(false);
ui->pushBtnLine->setChecked(false);
ui->pushBtnDrag->setChecked(true);
m_view->revokeLastItem();
}
}