Skip to content

fix: DCrumbEdit crumb 自适应容器宽度,修复缩小溢出与放大留白 - #767

Draft
18202781743 wants to merge 3 commits into
masterfrom
agent/developer/1891b0fd
Draft

fix: DCrumbEdit crumb 自适应容器宽度,修复缩小溢出与放大留白#767
18202781743 wants to merge 3 commits into
masterfrom
agent/developer/1891b0fd

Conversation

@18202781743

@18202781743 18202781743 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

问题描述

预览侧边栏放大/缩小时,图片标记(DCrumbEdit crumb)自适应不好:

  • 缩小时:标记文字信息溢出框外,没有都显示在框内;
  • 放大时:标记颜色块与标记文字之间出现较多空白。

PMS 单:https://pms.uniontech.com/bug-view-346439.html

修复方案

修改 src/widgets/dcrumbedit.cppCrumbObjectInterface::drawObject 绘制逻辑(intrinsicSize 与公开 API 不变,二进制兼容):

修改点 A — 文字对齐:带 tagColor 的 crumb 文字由 Qt::AlignRight 改为 Qt::AlignLeft,文字紧贴色块右侧,消除放大时色块与文字之间的空白。无 tagColor 分支(Qt::AlignCenter)保持不变。

修改点 B — 文字省略 / 收缩drawObject 收到的 rect 始终是 intrinsicSize 返回的固有宽度,不随容器收窄,因此原 elidedText 不会真正截断。本次依据 doc->textWidth() 在绘制阶段收缩绘制矩形(仅当 crumb 右边缘超出文档可用宽度时),收缩后对超长文字做 elidedText 省略,避免缩小侧边栏时标记信息溢出框外;同时 setClipRect 兜底防止背景越界。intrinsicSize 未动(拿不到可靠视口宽度)。

改动文件

文件 改动
src/widgets/dcrumbedit.cpp CrumbObjectInterface::drawObject 绘制逻辑(+30 −4)
tests/testcases/widgets/ut_dcrumbedit.cpp 新增 tagCrumbElidedAndNoOverflowWhenContainerNarrow 用例(+65)

单测结果

dtkwidget(DTK6 / Qt 6.8.0)构建并运行 ut_dcrumbedit

  • ut_DCrumbedit.createMimeDataFromSelection — 既有用例,通过(无回归)
  • ut_DCrumbedit.tagCrumbElidedAndNoOverflowWhenContainerNarrow — 本次新增用例,通过

目标用例 2/2 通过;全量回归 433/437 通过(4 项失败均为 offscreen 环境下既有像素/字体对比用例,与本次改动无关)。本次改动关键路径(shrink 收缩、elidedText 省略、AlignLeft 对齐、setClipRect 兜底)均被覆盖。

状态:draft,待人工审核合并。

Summary by Sourcery

Adjust DCrumbEdit crumb rendering to better adapt to the container width and avoid overflow or excessive spacing.

Bug Fixes:

  • Prevent tagged crumb text from overflowing the available width when the container becomes narrower by shrinking the drawn crumb and ellipsizing its text.
  • Eliminate excessive blank space between the color tag block and the crumb text when the container is enlarged by changing text alignment.

Enhancements:

  • Constrain crumb background painting with a clipping rect when shrunk to ensure visuals do not exceed the available width.

Tests:

  • Add a pixel-level rendering test to verify that long tagged crumbs are ellipsized and do not render beyond the document’s available width while preserving the full underlying text.

drawObject 中带 tagColor 的 crumb 文字由 AlignRight 改为 AlignLeft,
消除放大时色块与文字间的空白;并依据 doc->textWidth() 收缩绘制矩形、
对超长文字做 elidedText 省略,避免缩小侧边栏时标记信息溢出框外。
无 tagColor 分支与 intrinsicSize 保持不变。

新增 ut_dcrumbedit 用例 tagCrumbElidedAndNoOverflowWhenContainerNarrow,
验证容器宽度小于 crumb 宽度时文字省略、不溢出。

Log: 修复预览侧边栏标记在放大/缩小时不自适应的问题
Bug: https://pms.uniontech.com/bug-view-346439.html
@deepin-ci-robot

Copy link
Copy Markdown
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@deepin-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adjusts DCrumbEdit crumb drawing to respect the document’s available width and improve alignment for tagged crumbs, and adds a regression test that verifies text is elided and does not overflow when the container is narrow.

Flow diagram for updated CrumbObjectInterface::drawObject rendering logic

flowchart TD
    A[drawObject called with painter, rect, doc, format] --> B[Compute new_rect from rect margins]
    B --> C[Create DCrumbTextFormat crumb_format]
    C --> D[Compute QFontMetricsF font_metrics and radius]
    D --> E{crumb_format.tagColor is valid?}

    E -->|No| F[Draw background with backgroundBrush]
    F --> G[Set pen to textColor]
    G --> H[drawText new_rect with AlignCenter]

    E -->|Yes| I[Check doc->textWidth and right edge]
    I --> J{Need shrink?}

    J -->|Yes| K[Adjust new_rect.setRight based on doc->textWidth]
    K --> L[Draw background with backgroundBrush]
    L --> M[save painter and setClipRect new_rect]
    M --> N[Compute tag_rect and draw tag_path]
    N --> O[Compute textRect from new_rect]
    O --> P[Compute displayText via font_metrics.elidedText]
    P --> Q[drawText textRect with displayText AlignVCenter|AlignLeft]
    Q --> R[restore painter]

    J -->|No| S[Draw background with backgroundBrush]
    S --> T[Compute tag_rect and draw tag_path]
    T --> U[Compute textRect from new_rect]
    U --> V[Compute displayText via font_metrics.elidedText]
    V --> W[drawText textRect with displayText AlignVCenter|AlignLeft]
Loading

File-Level Changes

Change Details Files
Make tagged crumb rendering shrink to document width and elide text to avoid overflow while adjusting alignment to remove excess whitespace.
  • Compute whether the crumb should shrink based on tag presence, document textWidth, and the crumb’s right edge compared to available width.
  • When shrinking, clamp the crumb’s right edge to the document width minus a small margin and clip painting to the shrunken rect to prevent background overflow.
  • For tagged crumbs, switch text alignment from right-aligned to left-aligned, and draw elided text using QFontMetricsF::elidedText within the available text rect.
  • Leave untagged crumbs’ layout unchanged while retaining intrinsicSize and public API for binary compatibility.
src/widgets/dcrumbedit.cpp
Add a pixel-based regression test to ensure tagged crumb text is elided and stays within the container when the document width is narrower than the crumb’s intrinsic width.
  • Construct a tagged crumb with very long text and set a small QTextDocument::textWidth to simulate a narrow container.
  • Render the document into a QImage and scan pixels to assert that non-background content does not extend beyond the document width (with small tolerance).
  • Verify that the underlying crumb text stored in the widget is unchanged while elidedText for the narrow width produces a shorter string containing an ellipsis.
tests/testcases/widgets/ut_dcrumbedit.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

采纳代码审核建议 A(非阻塞优化):带 tagColor 分支中,elidedText
仅在容器宽度不足(shrink=true)时调用,常规宽度直接绘制原文。
行为等价(常规宽度下 elidedText 本就返回原文),但意图更清晰、
省去常规路径的无谓省略计算,并避免将来口径微调导致意外省略号回归。

Log: 优化 DCrumbEdit drawObject 省略逻辑
Bug: https://pms.uniontech.com/bug-view-346439.html
intrinsicSize 带 tagColor 分支将固有宽度限制在文档可用内容宽度内
(doc->textWidth() - 2*documentMargin()),超宽时收缩到 avail,
避免单个超宽 crumb 撑开文档导致水平滚动条。
drawObject 相应简化:删除原 shrink 矩形收缩块(已成死代码),
省略条件改为文字宽度 > 可用宽度时才 elidedText,setClipRect 改为
无条件 save/restore 兜底。
UT 补 EXPECT_LE(doc->idealWidth(), narrowWidth) 断言锁定根因。

Log: 修复单个超宽标记撑开容器出现水平滚动条的问题
Bug: https://pms.uniontech.com/bug-view-346439.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants