610 lines
19 KiB
C++
610 lines
19 KiB
C++
#include "formmultiheads.h"
|
||
#include "ui_formmultiheads.h"
|
||
|
||
#include <QDebug>
|
||
#include <QMenu>
|
||
#include <QFile>
|
||
#include <QScrollBar>
|
||
#include "newheaddialog.h"
|
||
#include "formhead.h"
|
||
#include "qtcommonclass.h"
|
||
#include "CallManage.h"
|
||
#include "geometryutils.h"
|
||
extern QString g_prjname;
|
||
|
||
extern int g_iRows;
|
||
extern int g_iCols;
|
||
extern int g_iColsWidth;
|
||
extern int g_iRowsHight;
|
||
extern int g_iFixedWidth;
|
||
extern int g_iFixedHeight;
|
||
extern double g_dPixelPerCm;//每厘米像素数
|
||
|
||
FormMultiHeads::FormMultiHeads(QWidget *parent) :
|
||
QWidget(parent),
|
||
ui(new Ui::FormMultiHeads)
|
||
{
|
||
ui->setupUi(this);
|
||
|
||
//加载样式
|
||
loadStyle(":/qrc/qss/flatgray.css");
|
||
|
||
//-------------------------------------
|
||
// 强制使用Qt渲染引擎
|
||
//QApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true);
|
||
//取消全局样式表
|
||
// ui->tableWidget->setStyleSheet("");
|
||
// ui->tableWidget->setAutoFillBackground(true);
|
||
|
||
//ui->tableWidget->hide();
|
||
//隐藏网格线
|
||
ui->tableWidget->setShowGrid(false);
|
||
// //设置样式表,不显示竖直边框
|
||
// ui->tableWidget->setStyleSheet( "QTableView::item {border-left: 0px solid black;} \
|
||
// QTableView::item:selected {border-left: 0px solid black;}\
|
||
// QTableView::item {border-right: 0px solid black;} \
|
||
// QTableView::item:selected {border-right: 0px solid black;}");
|
||
//
|
||
ui->tableWidget->verticalHeader()->hide(); //行
|
||
ui->tableWidget->horizontalHeader()->hide(); //列
|
||
ui->tableWidget->verticalHeader()->setFixedWidth(0);//标题栏宽度
|
||
ui->tableWidget->horizontalHeader()->setFixedHeight(0);//标题栏高度
|
||
ui->tableWidget->setColumnCount(1); //动态设置列数
|
||
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);//最后一列铺满最后
|
||
//标题
|
||
QTableWidgetItem *headerItem = new QTableWidgetItem("");
|
||
ui->tableWidget->setHorizontalHeaderItem(0, headerItem);
|
||
//我们让一列也可以滑动
|
||
ui->tableWidget->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
|
||
ui->tableWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
||
// 设置右键菜单策略
|
||
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu(QPoint)));
|
||
// 在窗口构造函数中
|
||
//ui->tableWidget->installEventFilter(this);
|
||
//鼠标点击表格
|
||
connect(ui->tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(slotCellClicked(int, int)));
|
||
//取消选中
|
||
connect(CallManage::getInstance(), SIGNAL(sig_UnSelectTableItem(QString)), this, SLOT(s_UnSelectTableItem(QString)));
|
||
}
|
||
|
||
FormMultiHeads::~FormMultiHeads()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void FormMultiHeads::slotCellClicked(int row, int column)
|
||
{
|
||
//qDebug() << "FormMultiHeads slotCellClicked";
|
||
|
||
//取消其他表格的选中状态(图头、成果表)
|
||
emit CallManage::getInstance()->sig_UnSelectTableItem("");
|
||
}
|
||
|
||
//图头、成果表
|
||
//取消其他表格的选中状态
|
||
void FormMultiHeads::s_UnSelectTableItem(QString strUuid)
|
||
{
|
||
//取消表格的选中状态
|
||
ui->tableWidget->clearSelection();
|
||
}
|
||
|
||
QJsonObject FormMultiHeads::makeJsonModel()
|
||
{
|
||
// 创建根对象
|
||
QJsonObject rootObj;
|
||
if(m_strHeadOrTail == "Head")
|
||
{
|
||
//图头
|
||
rootObj["heads"] = makeJsonArray();
|
||
}
|
||
else {
|
||
//成果表
|
||
rootObj["tails"] = makeJsonArray();
|
||
}
|
||
return rootObj;
|
||
}
|
||
|
||
//保存图头模板
|
||
void FormMultiHeads::slotSaveJsonModel()
|
||
{
|
||
QString folderPath;
|
||
folderPath = GetLogdataPath();
|
||
folderPath = folderPath + g_prjname;
|
||
|
||
QString strJsonFile;
|
||
if(m_strHeadOrTail == "Head")
|
||
{
|
||
//图头
|
||
strJsonFile = "图头文件(*.jsonHead)";
|
||
}
|
||
else {
|
||
//成果表
|
||
strJsonFile = "成果表文件(*.jsonFoot)";
|
||
}
|
||
|
||
//
|
||
QString exPortPath = QFileDialog::getSaveFileName(this, "另存为", folderPath, strJsonFile);
|
||
if (exPortPath.isEmpty() == false)
|
||
{
|
||
QJsonObject rootObj = makeJsonModel();
|
||
// 生成JSON文档
|
||
QJsonDocument doc(rootObj);
|
||
// 写入文件
|
||
QFile file(exPortPath);
|
||
if(file.open(QIODevice::WriteOnly)){
|
||
file.write(doc.toJson(QJsonDocument::Indented));
|
||
file.close();
|
||
qDebug() << "JSON文件写入成功!";
|
||
//
|
||
//m_fileJson = exPortPath;
|
||
} else {
|
||
qWarning() << "文件打开失败:" << file.errorString();
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
void FormMultiHeads::loadStyle(const QString &qssFile)
|
||
{
|
||
//加载样式表
|
||
QString qss;
|
||
QFile file(qssFile);
|
||
if (file.open(QFile::ReadOnly)) {
|
||
//用QTextStream读取样式文件不用区分文件编码 带bom也行
|
||
QStringList list;
|
||
QTextStream in(&file);
|
||
//in.setCodec("utf-8");
|
||
while (!in.atEnd()) {
|
||
QString line;
|
||
in >> line;
|
||
list << line;
|
||
}
|
||
|
||
file.close();
|
||
qss = list.join("\n");
|
||
QString paletteColor = qss.mid(20, 7);
|
||
this->setPalette(QPalette(paletteColor));
|
||
//用时主要在下面这句
|
||
this->setStyleSheet(qss);
|
||
}
|
||
}
|
||
|
||
void FormMultiHeads::slotContextMenu(QPoint pos)
|
||
{
|
||
//
|
||
QString strMenu= "";
|
||
QString strMenu2= "";
|
||
if(m_strHeadOrTail == "Head")
|
||
{
|
||
strMenu = "新建图头";
|
||
strMenu2 = "加载图头模板";
|
||
}
|
||
else {
|
||
strMenu = "新建成果表";
|
||
strMenu2 = "加载成果表模板";
|
||
}
|
||
QMenu menu(ui->tableWidget);
|
||
|
||
menu.addAction(QIcon(::GetImagePath() + "icon/Edit.png"), strMenu, this, &FormMultiHeads::slotCreateHeadOrFoot);
|
||
menu.addAction(QIcon(::GetImagePath() + "icon/Sheet.png"), strMenu2, this, &FormMultiHeads::slotLoadHeadOrFoot);
|
||
menu.exec(ui->tableWidget->mapToGlobal(pos));
|
||
|
||
}
|
||
|
||
//创建图头或成果表
|
||
void FormMultiHeads::slotCreateHeadOrFoot()
|
||
{
|
||
NewHeadDialog *dlg = new NewHeadDialog(this);
|
||
//
|
||
dlg->setAttribute(Qt::WA_DeleteOnClose);//关闭时,自动删除窗口对象
|
||
int result = dlg->exec();//模态对话框
|
||
if (result == QDialog::Accepted) {
|
||
// 处理用户点击了确定按钮的逻辑
|
||
qDebug() << "Accepted=";
|
||
AddHead();
|
||
}
|
||
else if (result == QDialog::Rejected) {
|
||
// 处理用户点击了取消按钮的逻辑
|
||
qDebug() << "Rejected=";
|
||
}
|
||
else {
|
||
// 处理其他情况的逻辑
|
||
qDebug() << "other=";
|
||
}
|
||
}
|
||
|
||
//加载图头或成果表
|
||
void FormMultiHeads::slotLoadHeadOrFoot()
|
||
{
|
||
QString folderPath;
|
||
folderPath = GetLogdataPath();
|
||
folderPath = folderPath + g_prjname;
|
||
|
||
QString strJsonFile;
|
||
if(m_strHeadOrTail == "Head")
|
||
{
|
||
//图头
|
||
strJsonFile = "图头文件(*.jsonHead)";
|
||
}
|
||
else {
|
||
//成果表
|
||
strJsonFile = "成果表文件(*.jsonFoot)";
|
||
}
|
||
|
||
//打开
|
||
QString fileFull = "";
|
||
fileFull = QFileDialog::getOpenFileName(this,
|
||
tr("选择模板"),
|
||
folderPath,
|
||
strJsonFile);
|
||
if (fileFull.isEmpty())
|
||
{
|
||
return;
|
||
}
|
||
|
||
QJsonParseError jsonError;
|
||
// 文件
|
||
QFile file(fileFull);
|
||
if(file.open(QIODevice::ReadOnly))
|
||
{
|
||
// 解析JSON
|
||
QJsonDocument document = QJsonDocument::fromJson(file.readAll(), &jsonError);
|
||
if (!document.isNull() && (jsonError.error == QJsonParseError::NoError))
|
||
{
|
||
if (document.isObject())
|
||
{
|
||
QJsonObject object = document.object();
|
||
//
|
||
if(m_strHeadOrTail == "Head")
|
||
{
|
||
if (object.contains("heads"))
|
||
{
|
||
//从json加载图头
|
||
QJsonValue value = object.value("heads");
|
||
if (value.isArray()) {
|
||
QJsonArray headsArray = value.toArray();
|
||
|
||
bool bMultiProject = false;
|
||
DisplayHeads(headsArray, m_strHeadOrTail, bMultiProject);
|
||
//qDebug() << "headsArray number:" << QString::number(m_headsArray.size());
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (object.contains("tails"))
|
||
{
|
||
//从json加载结果表
|
||
QJsonValue value = object.value("tails");
|
||
if (value.isArray()) {
|
||
QJsonArray tailsArray = value.toArray();
|
||
//
|
||
bool bMultiProject = false;
|
||
DisplayHeads(tailsArray, m_strHeadOrTail, bMultiProject);
|
||
//qDebug() << "tailsArray number:" << QString::number(m_tailsArray.size());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//
|
||
file.close();
|
||
qDebug() << "JSON 模板文件读取成功!";
|
||
}
|
||
else {
|
||
qWarning() << "JSON 模板文件打开失败:" << file.errorString();
|
||
QMessageBox::information(NULL,"提示","模板加载失败!",QMessageBox::Ok);
|
||
}
|
||
}
|
||
|
||
//添加图头
|
||
void FormMultiHeads::AddHead()
|
||
{
|
||
int rowcount = ui->tableWidget->rowCount(); //总行数
|
||
//增加1行
|
||
ui->tableWidget->setRowCount(rowcount+1);
|
||
//行标题
|
||
QTableWidgetItem *headerItem = new QTableWidgetItem("");
|
||
ui->tableWidget->setVerticalHeaderItem(rowcount, headerItem);
|
||
|
||
int colWidth = g_iCols*g_iColsWidth+g_iFixedWidth+8;
|
||
int rowHight = g_iRows*g_iRowsHight+g_iFixedHeight+8;
|
||
//设置行高度
|
||
ui->tableWidget->setRowHeight(rowcount, rowHight);
|
||
if(rowcount==0)
|
||
{
|
||
//设置列宽
|
||
ui->tableWidget->setColumnWidth(0, colWidth);
|
||
}
|
||
else
|
||
{
|
||
int iWidth=ui->tableWidget->columnWidth(0);
|
||
if(iWidth<g_iCols*g_iColsWidth)//列宽变大,以最大列宽为准
|
||
{
|
||
//设置列宽
|
||
ui->tableWidget->setColumnWidth(0, colWidth);
|
||
}
|
||
}
|
||
//
|
||
QtCommonClass *qtCommon = new QtCommonClass(this);
|
||
QString strUuid = qtCommon->getUUid();
|
||
FormHead *widgetHead = new FormHead(this, strUuid);
|
||
widgetHead->Init(g_iRows, g_iCols);
|
||
ui->tableWidget->setCellWidget(rowcount, 0, widgetHead);
|
||
}
|
||
|
||
void FormMultiHeads::resizeItem(QString indexID, double colWidth, double rowHight, bool bDelete)
|
||
{
|
||
//
|
||
int rowCount = ui->tableWidget->rowCount();//总行数
|
||
for(int i=0; i<rowCount; i++)
|
||
{
|
||
if( ui->tableWidget->cellWidget(i, 0) != nullptr )
|
||
{
|
||
auto myWidget = ui->tableWidget->cellWidget(i, 0);
|
||
if(myWidget)
|
||
{
|
||
//
|
||
FormHead *widgetHead = (FormHead*)myWidget;//获得widget
|
||
if(widgetHead)
|
||
{
|
||
if(widgetHead->m_indexID == indexID)
|
||
{
|
||
if(bDelete)
|
||
{
|
||
//删除1行
|
||
ui->tableWidget->removeRow(i);
|
||
}
|
||
else {
|
||
//设置行高度
|
||
ui->tableWidget->setRowHeight(i, rowHight+8);
|
||
|
||
int iWidth=ui->tableWidget->columnWidth(0);
|
||
if(iWidth<colWidth+8)//列宽变大,以最大列宽为准
|
||
{
|
||
//设置列宽
|
||
ui->tableWidget->setColumnWidth(0, colWidth+8);
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
QJsonArray FormMultiHeads::makeJsonArray()
|
||
{
|
||
//列宽
|
||
double colWidth = ui->tableWidget->columnWidth(0)/g_dPixelPerCm;
|
||
double rowHeight = 0;
|
||
|
||
// 创建JSON数组并填充数据
|
||
QJsonArray subcaseArray;
|
||
//
|
||
int rowCount = ui->tableWidget->rowCount();//总行数
|
||
for(int i=0; i<rowCount; i++)
|
||
{
|
||
//行高
|
||
rowHeight = ui->tableWidget->rowHeight(i)/g_dPixelPerCm;
|
||
|
||
QJsonObject formHeadObj;
|
||
formHeadObj["id"] = i;
|
||
formHeadObj["colWidth"] = colWidth;
|
||
formHeadObj["rowHeight"] = rowHeight;
|
||
|
||
if( ui->tableWidget->cellWidget(i, 0) != nullptr )
|
||
{
|
||
auto myWidget = ui->tableWidget->cellWidget(i, 0);
|
||
FormHead *widgetHead = qobject_cast<FormHead*>(myWidget);
|
||
if(widgetHead)
|
||
{
|
||
QJsonObject jobj = widgetHead->makeJson();
|
||
formHeadObj["headinfo"] = jobj;
|
||
}
|
||
}
|
||
subcaseArray.append(formHeadObj);
|
||
}
|
||
|
||
return subcaseArray;
|
||
}
|
||
|
||
//展示所有图头/成果表
|
||
//Head代表图头, Tail代表成果表
|
||
void FormMultiHeads::DisplayHeads(QJsonArray headsArray, QString strHeadOrTail, bool bMultiProject)
|
||
{
|
||
QtCommonClass *qtCommon = new QtCommonClass(this);
|
||
QMap<int, int> mapHeads;
|
||
double colWidth = 0;
|
||
double rowHeight = 0;
|
||
|
||
int id = 0;
|
||
int iCount = headsArray.size();
|
||
for(int i=0; i<iCount; i++)
|
||
{
|
||
QJsonValue headValue = headsArray[i];
|
||
|
||
QJsonObject headObj = headValue.toObject();
|
||
//
|
||
if (headObj.contains("id"))
|
||
{
|
||
QJsonValue value = headObj.value("id");
|
||
if (value.isDouble()) {
|
||
id = value.toInt();
|
||
//qDebug() << "id:" << QString::number(id);
|
||
//
|
||
mapHeads.insert(id, i);
|
||
}
|
||
}
|
||
}
|
||
|
||
for(int id=0; id<iCount; id++)
|
||
{
|
||
if(mapHeads.contains(id))
|
||
{
|
||
int iNum = mapHeads.value(id);
|
||
//按照id顺序,展示井
|
||
QJsonValue headValue = headsArray[iNum];
|
||
QJsonObject headObj = headValue.toObject();
|
||
//
|
||
if(id==0)
|
||
{
|
||
if (headObj.contains("colWidth"))
|
||
{
|
||
QJsonValue value = headObj.value("colWidth");
|
||
if (value.isDouble()) {
|
||
colWidth = value.toDouble();
|
||
//qDebug() << "colWidth:" << QString::number(colWidth);
|
||
//设置列宽
|
||
ui->tableWidget->setColumnWidth(0, colWidth);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (headObj.contains("rowHeight"))
|
||
{
|
||
QJsonValue value = headObj.value("rowHeight");
|
||
if (value.isDouble()) {
|
||
rowHeight = value.toDouble();
|
||
//qDebug() << "rowHeight:" << QString::number(rowHeight);
|
||
//设置行高度
|
||
ui->tableWidget->setRowHeight(id, rowHeight);
|
||
}
|
||
}
|
||
|
||
int rowcount = ui->tableWidget->rowCount(); //总行数
|
||
//增加1行
|
||
ui->tableWidget->setRowCount(rowcount+1);
|
||
//行标题
|
||
QTableWidgetItem *headerItem = new QTableWidgetItem("");
|
||
ui->tableWidget->setVerticalHeaderItem(rowcount, headerItem);
|
||
|
||
if (headObj.contains("headinfo"))
|
||
{
|
||
QJsonValue value = headObj.value("headinfo");
|
||
if (value.isObject()) {
|
||
QJsonObject headObjInfo = value.toObject();
|
||
//展示其中一行
|
||
int iCols = 0;
|
||
int iRows = 0;
|
||
//
|
||
if (headObjInfo.contains("iCols"))
|
||
{
|
||
QJsonValue value = headObjInfo.value("iCols");
|
||
if (value.isDouble()) {
|
||
iCols = value.toInt();
|
||
//qDebug() << "iCols:" << QString::number(iCols);
|
||
}
|
||
}
|
||
//
|
||
if (headObjInfo.contains("iRows"))
|
||
{
|
||
QJsonValue value = headObjInfo.value("iRows");
|
||
if (value.isDouble()) {
|
||
iRows = value.toInt();
|
||
//qDebug() << "iRows:" << QString::number(iRows);
|
||
}
|
||
}
|
||
|
||
//
|
||
QString strUuid = qtCommon->getUUid();
|
||
FormHead *widgetHead = new FormHead(this, strUuid);
|
||
widgetHead->Init(iRows, iCols);
|
||
ui->tableWidget->setCellWidget(rowcount, 0, widgetHead);
|
||
//
|
||
widgetHead->updateJsonInfo(headObjInfo, bMultiProject);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//获取图头、结论的宽高,方便输出图
|
||
void FormMultiHeads::getTableSize(int &iWidth, int &iHight)
|
||
{
|
||
//获取可视视图大小 tableWidget
|
||
iHight = 0;
|
||
iWidth = 0;
|
||
|
||
if(ui->tableWidget->rowCount()==0)
|
||
{
|
||
iHight = 0;
|
||
iWidth = ui->tableWidget->width();
|
||
}
|
||
else {
|
||
iHight = ui->tableWidget->height();
|
||
iWidth = ui->tableWidget->width();
|
||
//
|
||
int left, top, right, bottom;
|
||
this->getContentsMargins(&left, &top, &right, &bottom);
|
||
|
||
//
|
||
iHight = iHight + top + bottom; //Margin上方
|
||
}
|
||
}
|
||
|
||
//获取表格的实际大小
|
||
void FormMultiHeads::getTableSize_Biggest(int &iWidth_Big, int &iHight_Big)
|
||
{
|
||
//获取可视视图大小 tableWidget
|
||
iHight_Big = 0;
|
||
iWidth_Big = 0;
|
||
|
||
// int iWidth_Big = 0;
|
||
// int iHight_Big = 0;
|
||
|
||
int columnCount = ui->tableWidget->columnCount();//总列数
|
||
int rowCount = ui->tableWidget->rowCount();//总行数
|
||
for(int i=0; i<rowCount; i++)
|
||
{
|
||
if( ui->tableWidget->cellWidget(i, 0) != nullptr )
|
||
{
|
||
auto myWidget = ui->tableWidget->cellWidget(i, 0);
|
||
if(myWidget)
|
||
{
|
||
//
|
||
FormHead *widgetHead = (FormHead*)myWidget;//获得widget
|
||
if(widgetHead)
|
||
{
|
||
int iWidth_Tmp = 0;
|
||
int iHight_Tmp = 0;
|
||
widgetHead->getTableSize_Biggest(iWidth_Tmp, iHight_Tmp);
|
||
|
||
//宽度取最大
|
||
if(iWidth_Tmp>iWidth_Big)
|
||
{
|
||
iWidth_Big = iWidth_Tmp;
|
||
}
|
||
//高度相加
|
||
iHight_Big += iHight_Tmp;
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
iWidth_Big += columnCount;
|
||
iHight_Big += rowCount;
|
||
|
||
//
|
||
int left, top, right, bottom;
|
||
this->getContentsMargins(&left, &top, &right, &bottom);
|
||
|
||
iWidth_Big = iWidth_Big + left + right;
|
||
iHight_Big = iHight_Big + top + bottom;//Margin上方
|
||
|
||
// if(ui->tableWidget->horizontalScrollBar()->isVisible())
|
||
{
|
||
iWidth_Big = iWidth_Big + ui->tableWidget->verticalScrollBar()->width();
|
||
iHight_Big = iHight_Big + ui->tableWidget->horizontalScrollBar()->height();
|
||
}
|
||
|
||
//
|
||
iWidth_Big += 30;
|
||
iHight_Big += 30;
|
||
}
|