fix: adjust list item height calculation for dynamic rows#317
Conversation
Previously the list item height was fixed to 36px, redefined as the macro `ListItemHeight`. This change removes the fixed height and calculates the row height dynamically based on font metrics, allowing the list to properly display items with multi-line content. Additionally, the drawing rectangle for the item name now uses `nameTextMaxWidth` instead of the actual text width, ensuring consistent alignment. The web static text ellipsis handling is also fixed to correctly subtract the marker width. Log: Adjusted list item height for dynamic rows in grand search results Influence: 1. Test single-line and multi-line item display in grand search results list 2. Verify that row height adapts correctly for items with and without second line 3. Check alignment of item name text when ellipsis is applied 4. Confirm web static text items show correct marker and ellipsis behavior 5. Test under different font sizes and DPI settings fix: 调整列表项行高为动态计算 之前列表项高度固定为 36px,定义为宏 `ListItemHeight`。此更改移除了固定高 度,基于字体度量动态计算行高,使列表能够正常显示多行内容。同时修改了名称 绘制矩形区域,使用 `nameTextMaxWidth` 确保对齐一致。修复了 web 静态文本 省略号处理中标记宽度的减法逻辑。 Log: 调整全局搜索结果列表行高为动态高度 Influence: 1. 测试全局搜索结果列表中单行和多行项目的显示效果 2. 验证有/无第二行时行高是否正确自适应 3. 检查名称文本省略时的对齐情况 4. 确认 web 静态文本项目标记和省略行为正确 5. 在不同字体大小和 DPI 设置下测试
as title Log: update version
|
TAG Bot TAG: 6.0.37 |
deepin pr auto review你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff代码。这次修改主要涉及列表项行高的动态计算优化、文本省略逻辑的调整以及Tooltip区域的修正。 整体来看,这次重构提升了代码的灵活性和逻辑的准确性,但也存在一些潜在的逻辑漏洞、性能瓶颈和安全隐患。以下是我的详细审查意见: 1. 语法与逻辑问题 1.1: static const QString markStr = name.right(1);
static const int markWidth = fontMetrics.size(Qt::TextSingleLine, markStr).width();这里使用了
问题 1.2:空字符串潜在的越界风险 elidedName = fontMetrics.elidedText(name.left(name.size() - 1), Qt::ElideRight, nameTextMaxWidth - markWidth);如果
2. 代码性能问题 2.1: QFontMetrics nameFm(DFontSizeManager::instance()->get(DFontSizeManager::T6));
// ...
if (hasSecondLine) {
QFontMetrics contextFm(DFontSizeManager::instance()->get(DFontSizeManager::T8));
// ...
}
3. 代码安全与健壮性问题 3.1:Tooltip 区域宽度过大导致遮挡点击(交互安全/逻辑缺陷) QRect drawRect(textStartX, startY, nameTextMaxWidth, fontMetrics.height());
// 文件名被省略时,记录 tooltip 区域
if (elidedName != name) {
// ... 使用 drawRect 记录 tooltip 区域
}旧代码使用的是
4. 代码质量与可读性建议 4.1:魔法数字的消除 综合修改建议代码示例针对以上问题,我为你提供了修改后的代码片段参考: // src/grand-search/gui/exhibition/matchresult/listview/grandsearchlistdelegate.cpp
// 1. sizeHint 性能优化 (建议将 QFontMetrics 移至类成员或缓存)
QSize GrandSearchListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
// ... 前置逻辑保持不变 ...
// 建议: 如果字体在运行期不变,可以声明为 static;否则移至类成员变量中更新
static QFontMetrics nameFm(DFontSizeManager::instance()->get(DFontSizeManager::T6));
int totalHeight = ListLineMargin * 2 + nameFm.height();
if (hasSecondLine) {
static QFontMetrics contextFm(DFontSizeManager::instance()->get(DFontSizeManager::T8));
totalHeight += ContextLineSpacing + contextFm.height();
}
return QSize(ListRowWidth, totalHeight);
}
// 2. drawItemName 逻辑与安全修复
void GrandSearchListDelegate::drawItemName(QPainter *painter, const QModelIndex &index, ...)
{
// ... 前置逻辑 ...
// 修复:移除 static,避免缓存旧的字符和宽度;增加长度安全检查
if (elidedName != name && GRANDSEARCH_CLASS_WEB_STATICTEXT == searcher) {
if (name.size() > 0) { // 安全检查,防止 name 为空时越界
const QString markStr = name.right(1); // 去掉 static
const int markWidth = fontMetrics.size(Qt::TextSingleLine, markStr).width(); // 去掉 static
nameTextMaxWidth -= markWidth;
// 安全保护:确保减去标记宽度后可用宽度不为负
if (nameTextMaxWidth > 0) {
elidedName = fontMetrics.elidedText(name.left(name.size() - 1), Qt::ElideRight, nameTextMaxWidth);
elidedName.append(markStr);
} else {
// 极端情况:空间连最后一个字符都放不下,直接整体省略
elidedName = fontMetrics.elidedText(name, Qt::ElideRight, nameTextMaxWidth + markWidth);
}
}
}
// ... 中间逻辑 ...
QAbstractTextDocumentLayout::PaintContext paintContext;
// 修复:恢复计算实际宽度,避免 Tooltip 区域过大遮挡其他交互控件
int actualNameWidth = fontMetrics.size(Qt::TextSingleLine, elidedName).width();
QRect drawRect(textStartX, startY, actualNameWidth, fontMetrics.height());
// 文件名被省略时,记录 tooltip 区域
if (elidedName != name) {
// ... tooltip 逻辑 ...
}
}希望这些审查意见对你有所帮助!如果还有其他代码需要审查,随时告诉我。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Johnson-zs, Kakueeen 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 |
|
/forcemerge |
|
This pr force merged! (status: blocked) |
|
TAG Bot ✅ Tag created successfully 📋 Tag Details
|
Previously the list item height was fixed to 36px, redefined as
the macro
ListItemHeight. This change removes the fixed heightand calculates the row height dynamically based on font metrics,
allowing the list to properly display items with multi-line content.
Additionally, the drawing rectangle for the item name now uses
nameTextMaxWidthinstead of the actual text width, ensuring consistentalignment. The web static text ellipsis handling is also fixed to
correctly subtract the marker width.
Log: Adjusted list item height for dynamic rows in grand search results
Influence:
list
second line
behavior
fix: 调整列表项行高为动态计算
之前列表项高度固定为 36px,定义为宏
ListItemHeight。此更改移除了固定高度,基于字体度量动态计算行高,使列表能够正常显示多行内容。同时修改了名称
绘制矩形区域,使用
nameTextMaxWidth确保对齐一致。修复了 web 静态文本省略号处理中标记宽度的减法逻辑。
Log: 调整全局搜索结果列表行高为动态高度
Influence: