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
45 changes: 24 additions & 21 deletions deepin-devicemanager/src/DeviceManager/DeviceInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -330,7 +330,21 @@
}
}

void DeviceBaseInfo::tableInfoToTxt(QTextStream &out)
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))

Check warning on line 339 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 339 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 347 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 347 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.
{
// 获取表格内容
getTableData();
Expand All @@ -339,21 +353,15 @@
if (m_TableDataTr.size() < 1)
return;

// 设置占位宽度
QString text = m_TableDataTr[0];
out.setFieldWidth(int(text.size() * 1.5));
out.setFieldAlignment(QTextStream::FieldAlignment::AlignRight);

foreach (auto item, m_TableDataTr) {
out.setFieldWidth(28);
out << 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";
}

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

Check warning on line 364 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 364 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.
{
// 获取表头
getTableHeader();
Expand All @@ -362,17 +370,12 @@
if (m_TableHeaderTr.size() < 1)
return;

// 设置占位宽度
QString text = m_TableHeaderTr[0];
out.setFieldWidth(int(text.size() * 1.5));
out.setFieldAlignment(QTextStream::FieldAlignment::AlignLeft);

out << "\n";
for (int col = 0; col < m_TableHeaderTr.size() - 1; ++col) {
out.setFieldWidth(30);
out << 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";
}

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