Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,21 @@
qCDebug(appLog) << "Finished writing items to Txt.";
}

void DeviceBaseInfo::tableInfoToTxt(QTextStream &out)
double DeviceBaseInfo::displayWidth(const QString &str)
{
double width = 0;
for (const QChar &ch : str) {
ushort code = ch.unicode();
// CJK字符占4列,其余占2列
if (code > 0x7E || (code >= 0xA1 && code <= 0xFF))

Check warning on line 414 in deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Condition 'code>=0xA1' is always false

Check warning on line 414 in deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Condition 'code>=0xA1' is always false
width += 1.5;
else
width += 1;
}
return width;
}

void DeviceBaseInfo::tableInfoToTxt(QTextStream &out, const QList<double> &colWidths)

Check warning on line 422 in deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'tableInfoToTxt' is never used.

Check warning on line 422 in deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

The function 'tableInfoToTxt' is never used.
{
qCDebug(appLog) << "DeviceBaseInfo::tableInfoToTxt called.";
// 获取表格内容
Expand All @@ -417,24 +431,16 @@
return;
}

// 设置占位宽度
QString text = m_TableDataTr[0];
out.setFieldWidth(int(text.size() * 1.5));
out.setFieldAlignment(QTextStream::FieldAlignment::AlignRight);
qCDebug(appLog) << "Txt field width set for table info.";

foreach (auto item, m_TableDataTr) {
out.setFieldWidth(28);
out << item;
// qCDebug(appLog) << "Written table item to Txt: " << item;
for (int col = 0; col < m_TableDataTr.size(); ++col) {
double w = (col < colWidths.size()) ? colWidths[col] : 30;
int pad = qMax(0, int(w - displayWidth(m_TableDataTr[col])));
out << m_TableDataTr[col] << QString(pad, ' ');
}

out.setFieldWidth(0);
out << "\n";
qCDebug(appLog) << "Finished writing table info to Txt.";
}

void DeviceBaseInfo::tableHeaderToTxt(QTextStream &out)
void DeviceBaseInfo::tableHeaderToTxt(QTextStream &out, const QList<double> &colWidths)

Check warning on line 443 in deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'tableHeaderToTxt' is never used.

Check warning on line 443 in deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

The function 'tableHeaderToTxt' is never used.
{
qCDebug(appLog) << "DeviceBaseInfo::tableHeaderToTxt called.";
// 获取表头
Expand All @@ -446,19 +452,12 @@
return;
}

// 设置占位宽度
QString text = m_TableHeaderTr[0];
out.setFieldWidth(int(text.size() * 1.5));
out.setFieldAlignment(QTextStream::FieldAlignment::AlignLeft);
qCDebug(appLog) << "Txt field width set for table header.";

out << "\n";
for (int col = 0; col < m_TableHeaderTr.size() - 1; ++col) {
out.setFieldWidth(30);
out << m_TableHeaderTr[col];
// qCDebug(appLog) << "Written table header item to Txt: " << m_TableHeaderTr[col];
double w = (col < colWidths.size()) ? colWidths[col] : 30;
int pad = qMax(0, int(w - displayWidth(m_TableHeaderTr[col])));
out << m_TableHeaderTr[col] << QString(pad, ' ');
}
out.setFieldWidth(0);
out << "\n";
qCDebug(appLog) << "Finished writing table header to Txt.";
}
Expand Down
16 changes: 12 additions & 4 deletions deepin-devicemanager/src/DeviceManager/DeviceInfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -334,14 +333,23 @@ class DeviceBaseInfo : public QObject
/**
* @brief tableInfoToTxt:表格内容写到txt
* @param out:txt文件流
* @param colWidths:每列的显示宽度列表
*/
void tableInfoToTxt(QTextStream &out);
void tableInfoToTxt(QTextStream &out, const QList<double> &colWidths);

/**
* @brief tableHeaderToTxt:表头信息写到txt
* @param out:txt文件流
* @param colWidths:每列的显示宽度列表
*/
void tableHeaderToTxt(QTextStream &out);
void tableHeaderToTxt(QTextStream &out, const QList<double> &colWidths);

/**
* @brief displayWidth:计算字符串的显示宽度(CJK字符算1.5,其余算1)
* @param str:输入字符串
* @return 显示宽度
*/
static double displayWidth(const QString &str);

/**
* @brief tableInfoToHtml:表格内容写到html
Expand Down
20 changes: 16 additions & 4 deletions deepin-devicemanager/src/MacroDefinition.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -33,9 +32,22 @@
\
/**添加Table信息**/ \
if (deviceLst.size() > 1) { \
deviceLst[0]->tableHeaderToTxt(out); \
/* 第一遍扫描:计算每列最大显示宽度 */ \
const QStringList &_hdr = deviceLst[0]->getTableHeader(); \
int _colCount = _hdr.size() - 1; \
QList<double> _colWidths; \
for (int _c = 0; _c < _colCount; ++_c) \
_colWidths.append(DeviceBaseInfo::displayWidth(_hdr[_c]) + 4); \
foreach (auto _dev, deviceLst) { \
const QStringList &_dat = _dev->getTableData(); \
for (int _c = 0; _c < qMin(_dat.size(), _colCount); ++_c) { \
double _w = DeviceBaseInfo::displayWidth(_dat[_c]) + 4; \
if (_w > _colWidths[_c]) _colWidths[_c] = _w; \
} \
} \
deviceLst[0]->tableHeaderToTxt(out, _colWidths); \
foreach (auto device, deviceLst) { \
device->tableInfoToTxt(out); \
device->tableInfoToTxt(out, _colWidths); \
} \
} \
\
Expand Down
Loading