Skip to content

Conversation

@add-uos
Copy link
Contributor

@add-uos add-uos commented Jan 30, 2026

Upgrade the height computation mechanism to utilize QTextDocument instead of QFontMetrics. This change accommodates HTML formatting in the license display and adjusts minimum dimensions to improve visual proportions across different content volumes.

log: replace font metrics with document renderer
pms: BUG-314073

Summary by Sourcery

Update license dialog content height calculation to use rich-text document rendering instead of font metrics for better handling of HTML content and proportional sizing.

Bug Fixes:

  • Fix incorrect window height calculation when displaying HTML-formatted license text by basing sizing on rendered document height.

Enhancements:

  • Adjust minimum scroll area height bounds to improve visual proportions across varying amounts of license content.

Upgrade the height computation mechanism to utilize QTextDocument
instead of QFontMetrics. This change accommodates HTML formatting in
the license display and adjusts minimum dimensions to improve visual
proportions across different content volumes.

log: replace font metrics with document renderer
pms: BUG-314073
@sourcery-ai
Copy link

sourcery-ai bot commented Jan 30, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaces font-metrics-based height calculation with a QTextDocument-based HTML-aware content height computation and adjusts scroll area minimum height bounds to better fit varying content lengths.

Sequence diagram for updated window height calculation

sequenceDiagram
actor User
participant LicenseDialog
participant Content
participant QTextDocument
participant QScrollArea

User->>LicenseDialog: open()
LicenseDialog->>Content: updateWindowHeight()
Content->>QTextDocument: construct()
Content->>QTextDocument: setHtml(m_source.text())
Content->>QTextDocument: setTextWidth(m_scrollArea.width() - 40)
QTextDocument-->>Content: size().height()
Content->>QScrollArea: setMinimumHeight(qBound(100, contentHeight, 491))
Loading

Class diagram for Content height computation update

classDiagram
class Content {
  - QWidget m_source
  - QScrollArea m_scrollArea
  + updateWindowHeight()
}

class QTextDocument {
  + setHtml(html)
  + setTextWidth(width)
  + size()
}

Content ..> QTextDocument : uses
Content ..> QWidget : reads_text
Content ..> QScrollArea : sets_minimumHeight
Loading

File-Level Changes

Change Details Files
Switch content height calculation from QFontMetrics to QTextDocument to support HTML-aware layout and update window height constraints.
  • Remove use of label rect and QFontMetrics::boundingRect for wrapped text sizing.
  • Instantiate a temporary QTextDocument and populate it with the label's HTML text via setHtml.
  • Set the document text width based on the scroll area's width minus a fixed horizontal margin.
  • Compute content height from QTextDocument::size().height() and use it to derive the scroll area's minimum height via qBound with updated limits (100–491).
dde-license-dialog/src/content.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

@deepin-ci-robot
Copy link

deepin pr auto review

这份代码修改主要是为了优化 Content::updateWindowHeight() 函数,使其更准确地计算文本内容的高度。以下是对这段代码的详细审查和改进建议:

1. 语法逻辑审查

现状:

  • 原代码使用 QFontMetrics::boundingRect 计算文本尺寸,这在处理多行文本和换行时可能不够精确,尤其是当文本包含 HTML 标签时。
  • 新代码引入 QTextDocument 来处理 HTML 文本,并通过 setTextWidth 设置文本宽度,然后获取其高度,逻辑上是正确的。

潜在问题:

  • 新代码假设 m_source->text() 返回的是 HTML 格式的文本。如果实际文本是纯文本,使用 setHtml 可能会导致不必要的解析开销。
  • m_scrollArea->width() - 40 的硬编码宽度减法可能不够灵活,如果布局发生变化,这个值可能需要调整。

2. 代码质量审查

优点:

  • 使用 QTextDocument 可以更准确地处理富文本和换行,比 QFontMetrics 更可靠。
  • 使用 static_cast<int> 显式转换类型,比隐式转换更清晰。

改进建议:

  • 建议添加注释说明为什么使用 QTextDocument 以及 40 的含义。
  • 如果 m_source->text() 可能包含纯文本,可以考虑使用 QTextDocument::setPlainText 或添加判断逻辑。

3. 代码性能审查

潜在问题:

  • QTextDocument 的创建和解析 HTML 文本可能比 QFontMetrics 更耗时,尤其是在频繁调用 updateWindowHeight() 时。
  • 如果 m_source->text() 是静态文本,可以考虑缓存 QTextDocument 或其高度值,避免重复计算。

改进建议:

  • 如果 updateWindowHeight() 调用频率较高,可以优化为仅在文本内容或宽度变化时重新计算高度。
  • 示例优化:
    void Content::updateWindowHeight()
    {
        static QString lastText;
        static int lastWidth = 0;
        static int cachedHeight = 0;
    
        QString currentText = m_source->text();
        int currentWidth = m_scrollArea->width() - 40;
    
        if (currentText != lastText || currentWidth != lastWidth) {
            QTextDocument doc;
            doc.setHtml(currentText);
            doc.setTextWidth(currentWidth);
            cachedHeight = static_cast<int>(doc.size().height());
    
            lastText = currentText;
            lastWidth = currentWidth;
        }
    
        int minHeight = qBound(100, cachedHeight, 491);
        m_scrollArea->setMinimumHeight(minHeight);
    }

4. 代码安全审查

潜在问题:

  • m_scrollArea->width() 可能为 0 或负数(例如窗口未完全初始化时),导致 setTextWidth 设置为负值,可能引发未定义行为。
  • m_source->text() 可能包含恶意 HTML 内容(如果来自用户输入),可能导致 XSS 或渲染问题。

改进建议:

  • 添加对 m_scrollArea->width() 的检查:
    int availableWidth = m_scrollArea->width() - 40;
    if (availableWidth <= 0) {
        availableWidth = 100; // 设置一个合理的默认值
    }
    doc.setTextWidth(availableWidth);
  • 如果文本来自不可信来源,建议使用 setPlainText 或对 HTML 进行清理。

5. 其他改进建议

  • 如果 m_sourceQLabel 或类似控件,可以直接使用其 heightForWidth 方法(如果支持),避免手动计算。
  • 如果 40 是边距,建议定义为常量:
    const int kTextMargin = 40;
    doc.setTextWidth(m_scrollArea->width() - kTextMargin);

优化后的代码示例

void Content::updateWindowHeight()
{
    static QString lastText;
    static int lastWidth = 0;
    static int cachedHeight = 0;

    QString currentText = m_source->text();
    const int kTextMargin = 40;
    int availableWidth = m_scrollArea->width() - kTextMargin;
    if (availableWidth <= 0) {
        availableWidth = 100; // 默认宽度
    }

    if (currentText != lastText || availableWidth != lastWidth) {
        QTextDocument doc;
        doc.setHtml(currentText); // 或 setPlainText,根据实际需求
        doc.setTextWidth(availableWidth);
        cachedHeight = static_cast<int>(doc.size().height());

        lastText = currentText;
        lastWidth = availableWidth;
    }

    int minHeight = qBound(100, cachedHeight, 491);
    m_scrollArea->setMinimumHeight(minHeight);
}

总结

新代码在逻辑上是正确的,但可以通过以下方式进一步优化:

  1. 添加缓存机制减少重复计算。
  2. 增加对宽度的安全检查。
  3. 将硬编码值定义为常量。
  4. 根据实际需求选择 setHtmlsetPlainText

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Using m_scrollArea->width() as the layout width may be fragile if this is called before the scroll area is laid out (width could be 0 or stale); consider basing it on m_source’s available width or ensuring this is only invoked after layout is finalized or on resize.
  • The bounds for minHeight have changed from [300, 491] to [100, 491], which can significantly reduce the dialog’s minimum height; double-check whether this is intentional for all license contents or whether a higher lower-bound (or content-dependent heuristic) would keep the layout more consistent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Using `m_scrollArea->width()` as the layout width may be fragile if this is called before the scroll area is laid out (width could be 0 or stale); consider basing it on `m_source`’s available width or ensuring this is only invoked after layout is finalized or on resize.
- The bounds for `minHeight` have changed from `[300, 491]` to `[100, 491]`, which can significantly reduce the dialog’s minimum height; double-check whether this is intentional for all license contents or whether a higher lower-bound (or content-dependent heuristic) would keep the layout more consistent.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, caixr23

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

@add-uos
Copy link
Contributor Author

add-uos commented Jan 30, 2026

/merge

@deepin-bot deepin-bot bot merged commit 339887e into linuxdeepin:master Jan 30, 2026
18 checks passed
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.

3 participants