fix(export): align table header and content in exported TXT file#656
Conversation
There was a problem hiding this comment.
Sorry @GongHeng2017, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
6c82524 to
595b1e5
Compare
Replace fixed QTextStream::setFieldWidth with display-width-aware padding. Add displayWidth() to calculate CJK characters as 1.5 columns, and use two-pass scanning in EXPORT_TO_TXT to compute per-column max width for accurate alignment. 修复导出TXT文件中表头与内容列不对齐的问题。 用基于显示宽度的空格填充替换固定setFieldWidth方式, 新增displayWidth计算CJK字符为1.5列宽度, 通过两遍扫描计算每列最大宽度实现精确对齐。 Log: 修复导出TXT文件表头与内容列不对齐 Bug: https://pms.uniontech.com/bug-view-357619.html Influence: 导出TXT文件时,表头和内容的列能正确对齐,中英文混排场景下不再错位。
595b1e5 to
211ad41
Compare
deepin pr auto reviewGit Diff 代码审查报告1. 整体评估这段代码主要修改了设备管理器中表格导出到文本文件的功能,改进了列宽计算方式,使其能够更好地处理中文字符的显示宽度。整体上代码逻辑清晰,但在实现细节上存在一些可以优化的地方。 2. 语法逻辑分析2.1 displayWidth函数double DeviceBaseInfo::displayWidth(const QString &str)
{
double width = 0;
for (const QChar &ch : str) {
ushort code = ch.unicode();
// CJK字符占1.5列,其余占1列
if (code > 0x7E || (code >= 0xA1 && code <= 0xFF))
width += 1.5;
else
width += 1;
}
return width;
}问题:
改进建议: double DeviceBaseInfo::displayWidth(const QString &str)
{
double width = 0;
for (const QChar &ch : str) {
uint code = ch.unicode();
// 处理代理对(surrogate pairs)
if (ch.isHighSurrogate()) {
continue; // 跳过高代理,等待低代理
}
if (ch.isLowSurrogate()) {
// 代理对字符,通常显示宽度为2
width += 2;
continue;
}
// 更精确的CJK字符范围判断
if ((code >= 0x4E00 && code <= 0x9FFF) || // CJK统一表意文字
(code >= 0x3400 && code <= 0x4DBF) || // CJK扩展A
(code >= 0x20000 && code <= 0x2A6DF) || // CJK扩展B
(code >= 0x2A700 && code <= 0x2B73F) || // CJK扩展C
(code >= 0x2B740 && code <= 0x2B81F) || // CJK扩展D
(code >= 0x2B820 && code <= 0x2CEAF) || // CJK扩展E
(code >= 0xF900 && code <= 0xFAFF) || // CJK兼容表意文字
(code >= 0x2F800 && code <= 0x2FA1F)) // CJK兼容表意文字补充
width += 1.5;
else
width += 1;
}
return width;
}2.2 tableInfoToTxt和tableHeaderToTxt函数问题:
改进建议: void DeviceBaseInfo::tableInfoToTxt(QTextStream &out, const QList<double> &colWidths)
{
// 获取表格内容
getTableData();
if (m_TableDataTr.isEmpty())
return;
// 确保colWidths至少有足够的列
QList<double> safeColWidths = colWidths;
while (safeColWidths.size() < m_TableDataTr.size()) {
safeColWidths.append(30.0); // 默认宽度
}
for (int col = 0; col < m_TableDataTr.size(); ++col) {
double w = safeColWidths[col];
int pad = qMax(0, static_cast<int>(w - displayWidth(m_TableDataTr[col])));
out << m_TableDataTr[col] << QString(pad, ' ');
}
out << "\n";
}
void DeviceBaseInfo::tableHeaderToTxt(QTextStream &out, const QList<double> &colWidths)
{
// 获取表头
getTableHeader();
if (m_TableHeaderTr.isEmpty())
return;
// 确保colWidths至少有足够的列
QList<double> safeColWidths = colWidths;
while (safeColWidths.size() < m_TableHeaderTr.size()) {
safeColWidths.append(30.0); // 默认宽度
}
out << "\n";
for (int col = 0; col < m_TableHeaderTr.size(); ++col) {
double w = safeColWidths[col];
int pad = qMax(0, static_cast<int>(w - displayWidth(m_TableHeaderTr[col])));
out << m_TableHeaderTr[col] << QString(pad, ' ');
}
out << "\n";
}3. 代码质量分析3.1 变量命名
3.2 代码重复
void DeviceBaseInfo::writeTableRowToTxt(QTextStream &out, const QStringList &rowData, const QList<double> &colWidths)
{
if (rowData.isEmpty())
return;
QList<double> safeColWidths = colWidths;
while (safeColWidths.size() < rowData.size()) {
safeColWidths.append(30.0);
}
for (int col = 0; col < rowData.size(); ++col) {
double w = safeColWidths[col];
int pad = qMax(0, static_cast<int>(w - displayWidth(rowData[col])));
out << rowData[col] << QString(pad, ' ');
}
out << "\n";
}4. 代码性能分析4.1 字符串处理
4.2 宏定义中的计算
5. 代码安全分析5.1 输入验证
5.2 类型转换
6. 其他建议
double DeviceBaseInfo::displayWidth(const QString &str)
{
static QFontMetrics fm(QApplication::font());
return fm.horizontalAdvance(str) / fm.horizontalAdvance('0');
}
总结这段代码改进了表格导出功能,使其能够更好地处理中文字符的显示宽度。主要改进点包括:
建议的改进主要集中在:
这些改进将使代码更健壮、更高效,并提高其可维护性。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: GongHeng2017, max-lvs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/merge |
Replace fixed QTextStream::setFieldWidth with display-width-aware padding. Add displayWidth() to calculate CJK characters as 1.5 columns, and use two-pass scanning in EXPORT_TO_TXT to compute per-column max width for accurate alignment.
修复导出TXT文件中表头与内容列不对齐的问题。
用基于显示宽度的空格填充替换固定setFieldWidth方式,
新增displayWidth计算CJK字符为1.5列宽度,
通过两遍扫描计算每列最大宽度实现精确对齐。
Log: 修复导出TXT文件表头与内容列不对齐
Bug: https://pms.uniontech.com/bug-view-357619.html
Influence: 导出TXT文件时,表头和内容的列能正确对齐,中英文混排场景下不再错位。