2 Star 1 Fork 1

liuzhongtu/HelloQocct

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
inputdlg.cpp 4.35 KB
一键复制 编辑 原始数据 按行查看 历史
Monss 提交于 2024-03-11 19:57 . 以MVC模式构建点云运行框架;
#include "inputdlg.h"
#include "util.h"
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
InputDlg::InputDlg(QString& file, QWidget *parent)
: QDialog(parent)
, ui(new Ui::InputDlg())
, filepath(file)
{
ui->setupUi(this);
connect(ui->btnConfirm, &QPushButton::clicked, [this]() { accept(); });
connect(ui->btnCancel, &QPushButton::clicked, [this]() { reject(); });
connect(ui->cbBoxDataFormat, SIGNAL(currentIndexChanged(int)), this, SLOT(onPreview(int)));
}
InputDlg::~InputDlg()
{
delete ui;
}
void InputDlg::getDivideSign(QVector<QChar>& divideSign)
{
divideSign.clear();
if (ui->commaDivide->isChecked()) divideSign << ',';
if (ui->semicolonDivide->isChecked()) divideSign << ';';
if (ui->spaceDivide->isChecked()) divideSign << ' ';
if (ui->tabDivide->isChecked()) divideSign << '\t';
}
void InputDlg::showEvent(QShowEvent*)
{
initShow(); // 初始化显示
adjust(); // 调整窗口大小
}
void InputDlg::onPreview(int format)
{
refreshPreview(static_cast<DataFormat>(format)); // 刷新预览模块界面
adjust(); // 调整窗口大小
}
void InputDlg::initShow()
{
// 获取文件类型
file_filter::Filter type = util::getFileType(filepath.toStdString());
// 统一隐藏区域
ui->groupTxt->hide();
setDataformatVisible(false);
switch (type)
{
case file_filter::PCD:
setDataformatVisible(true); // 显示选择数据模式区域
break;
case file_filter::STL:
break;
case file_filter::PLY:
break;
default:
// 不限类型,作文本文件处理
setDataformatVisible(true); // 显示选择数据模式区域
ui->groupTxt->show(); // 显示文本文件区域
// 预览
refreshPreview(getDataFormat());
break;
}
}
void InputDlg::adjust()
{
// 各模块调整 显隐、大小
if (ui->groupTxt->isVisible())
{
ui->tablePreview->adjustSize();
ui->groupTxt->adjustSize();
}
adjustSize();
}
void InputDlg::refreshPreview(DataFormat dataFormat)
{
// 清空表格
ui->tablePreview->clear();
// 设置分隔符
QVector<QChar> divideSign;
getDivideSign(divideSign);
// 设置待编辑变量
int tableCols = 0; // 表格列数
QStringList horizontalHeader; // 表格水平头,即每一列的头部
QVector<LegalFormat> legalFormat; // 合法数据形式
// 根据数据解析模式分类处理
switch (dataFormat)
{
case XYZ:
// 编辑该模式下的变量
tableCols = 3;
horizontalHeader << "X" << "Y" << "Z";
legalFormat << Number << Number << Number;
break;
case XYZNormal:
// 编辑该模式下的变量
tableCols = 7;
horizontalHeader << "X" << "Y" << "Z" << "I" << "J" << "K" << "Curvature";
legalFormat << Number << Number << Number << Number << Number << Number << Number;
break;
default:
break;
}
// 设置表头
ui->tablePreview->setColumnCount(tableCols);
ui->tablePreview->setHorizontalHeaderLabels(horizontalHeader);
// 打开文件
QFile file(filepath);
// 文件打开失败则提示
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(nullptr, "警告", "文件打开失败");
reject();
return;
}
// 跳过文件开头的注释行
QTextStream in(&file);
int rowsSkiped = 0;
QString line;
while (true)
{
line = in.readLine();
// 若文件开始几行以#开头,则视为注释行,跳过
if (line.size() > 0 && line.at(0) == '#')
rowsSkiped++;
else
break;
}
// 界面上显示跳过的行
ui->rowsSkiped->setText(QString::number(rowsSkiped));
// 跳过注释行后,读取文件前10行
QVector<QString> rowData;
// 确认表格设置了10行
ui->tablePreview->setRowCount(10);
for (int i = 0; i < 10; i++)
{
// 以Vector获取该行数据
util::divideLine(line, tableCols, divideSign, &rowData);
// 填写表格数据
bool legal = true; // 默认合法数据
for (int j = 0; j < tableCols; j++)
{
QTableWidgetItem* item = new QTableWidgetItem(rowData[j]);
// 非法内容
if (!isLegal(legalFormat[j], rowData[j]))
{
item->setTextColor(QColor(255, 0, 0));
legal = false;
}
// 展示数据
ui->tablePreview->setItem(i, j, item);
}
// 行头随行内内容合法性变化
if (legal)
ui->tablePreview->setVerticalHeaderItem(i, new QTableWidgetItem(QIcon("res/accept.png"), ""));
else
ui->tablePreview->setVerticalHeaderItem(i, new QTableWidgetItem(QIcon("res/reject.png"), ""));
// 读取一行数据
line = in.readLine();
}
// 关闭文件
file.close();
}
bool InputDlg::isLegal(LegalFormat format, const QString& data)
{
switch (format)
{
case InputDlg::Number: return util::isAllDigitsOrDecimal(data);
case InputDlg::String: return true;
case InputDlg::RGB: return util::isRGB(data);
default:
break;
}
return false;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/liuzhongtu/hello-qocct.git
git@gitee.com:liuzhongtu/hello-qocct.git
liuzhongtu
hello-qocct
HelloQocct
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385