-
Notifications
You must be signed in to change notification settings - Fork 50
fix: The control resizes based on its content #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Reviewer's GuideRefactor NetTipsWidget to dynamically resize based on its content and font changes by extracting height-calculation logic into a member function, storing the label as a class member, handling QEvent::FontChange, and updating includes. Sequence diagram for dynamic resizing on font change in NetTipsWidgetsequenceDiagram
participant "QGuiApplication"
participant "NetTipsWidget"
participant "QEvent"
participant "QLabel"
QGuiApplication->>NetTipsWidget: Font change event
NetTipsWidget->>NetTipsWidget: event(QEvent::FontChange)
NetTipsWidget->>NetTipsWidget: updateHeight()
NetTipsWidget->>QLabel: setFixedHeight(newHeight)
NetTipsWidget->>QLabel: setText(updatedText)
Class diagram for updated NetTipsWidget structureclassDiagram
class NetTipsWidget {
+NetTipsWidget(NetTipsItem *item, QWidget *parent)
+~NetTipsWidget()
+protected bool event(QEvent *event) override
+protected void updateHeight()
-QLabel *m_label
}
NetTipsWidget --|> NetWidget
NetAirplaneModeTipsWidget --|> NetTipsWidget
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Avoid the hardcoded width constant (294) in updateHeight—derive the layout width from the widget or label’s actual width instead of using a magic number.
- You can simplify the height calculation by using QTextDocument::setTextWidth and document.size().height() rather than manual line-by-line QFontMetrics/drawText logic.
- The include appears unused in the new implementation—consider removing it to clean up the headers.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Avoid the hardcoded width constant (294) in updateHeight—derive the layout width from the widget or label’s actual width instead of using a magic number.
- You can simplify the height calculation by using QTextDocument::setTextWidth and document.size().height() rather than manual line-by-line QFontMetrics/drawText logic.
- The <QTextLine> include appears unused in the new implementation—consider removing it to clean up the headers.
## Individual Comments
### Comment 1
<location> `net-view/window/private/netdelegate.cpp:702` </location>
<code_context>
- option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
- layout.setTextOption(option);
- QFontMetrics fm(label->font());
- int line = drawText(nullptr, QRectF(QPointF(0, 0), QSizeF(294, INT_MAX)), fm.boundingRect(plainText).height(), &layout, Qt::ElideNone);
- label->setFixedHeight(line * fm.boundingRect(plainText).height());
- label->setText(item->tipsLinkEnabled() ? name : plainText);
</code_context>
<issue_to_address>
**suggestion:** Hardcoded width value (294) may reduce flexibility.
Consider making the width value configurable or dynamically based on the widget's actual width to ensure proper scaling with different UI sizes and fonts.
Suggested implementation:
```cpp
int availableWidth = label->width() > 0 ? label->width() : label->sizeHint().width();
int line = drawText(nullptr, QRectF(QPointF(0, 0), QSizeF(availableWidth, INT_MAX)), fm.boundingRect(plainText).height(), &layout, Qt::ElideNone);
```
If this code is called before the label is shown and its width is not set, you may need to ensure the label has a valid width (e.g., by calling this after layout or using a default value). If you want to make the width configurable, you could add a member variable or a setter method to allow external configuration.
</issue_to_address>
### Comment 2
<location> `net-view/window/private/netdelegate.cpp:718-721` </location>
<code_context>
+ layout.setTextOption(option);
+ QFontMetrics fm(m_label->font());
+ int line = drawText(nullptr, QRectF(QPointF(0, 0), QSizeF(294, INT_MAX)), fm.boundingRect(plainText).height(), &layout, Qt::ElideNone);
+ m_label->setFixedHeight(line * fm.boundingRect(plainText).height());
+ m_label->setText(item->tipsLinkEnabled() ? name : plainText);
+}
</code_context>
<issue_to_address>
**suggestion:** Height calculation may not account for line spacing or font metrics nuances.
Using fm.boundingRect(plainText).height() may not yield accurate results for all fonts. For better precision, use QFontMetrics::lineSpacing() or QTextLayout's line metrics to calculate the required label height.
```suggestion
QFontMetrics fm(m_label->font());
layout.beginLayout();
int totalHeight = 0;
int lineCount = 0;
while (true) {
QTextLine textLine = layout.createLine();
if (!textLine.isValid())
break;
textLine.setLineWidth(294);
totalHeight += textLine.height();
++lineCount;
}
layout.endLayout();
// Add extra spacing for each line except the last
if (lineCount > 1) {
totalHeight += (lineCount - 1) * fm.lineSpacing();
}
m_label->setFixedHeight(totalHeight);
m_label->setText(item->tipsLinkEnabled() ? name : plainText);
```
</issue_to_address>
### Comment 3
<location> `net-view/window/private/netdelegate.cpp:706` </location>
<code_context>
+ return NetWidget::event(event);
+}
+
+void NetTipsWidget::updateHeight()
+{
+ NetTipsItem *item = static_cast<NetTipsItem *>(this->item());
</code_context>
<issue_to_address>
**issue (complexity):** Consider replacing manual text layout logic with QLabel's built-in rich text and sizing features for updateHeight().
```cpp
// In NetTipsWidget.hpp, drop the manual QTextDocument/QTextLayout includes:
-#include <QTextDocument>
-#include <QTextLine>
// Replace your heavy updateHeight() with QLabel’s built-ins:
void NetTipsWidget::updateHeight()
{
auto *item = static_cast<NetTipsItem *>(this->item());
const QString name = item->name();
// If you need rich‐text links, tell QLabel to interpret HTML:
m_label->setTextFormat(Qt::RichText);
m_label->setText(item->tipsLinkEnabled() ? name : name.toPlainText());
// Constrain width and let QLabel word-wrap + sizeHint fix the height:
constexpr int maxW = MAX_TEXT_WIDTH;
m_label->setFixedWidth(maxW);
m_label->setWordWrap(true);
m_label->adjustSize();
}
// You can now remove all of your QTextDocument/QTextLayout/drawText logic.
// You still connect:
connect(item, &NetTipsItem::nameChanged, this, &NetTipsWidget::updateHeight);
connect(qApp,&QGuiApplication::fontChanged,this, &NetTipsWidget::updateHeight);
// And you can also drop the custom event() override if you rely on
// QGuiApplication::fontChanged instead of catching QEvent::FontChange.
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
deepin pr auto review我来分析一下这段代码的修改:
改进后的代码示例: class NetTipsWidget : public NetWidget
{
private:
static constexpr int DEFAULT_WIDTH = 294;
QLabel *m_label;
QTextDocument m_textDocument;
QTextLayout m_textLayout;
protected:
bool event(QEvent *event) override {
if (event->type() == QEvent::FontChange) {
updateHeight();
}
return NetWidget::event(event);
}
void updateHeight() {
NetTipsItem *item = dynamic_cast<NetTipsItem*>(this->item());
if (!item) return;
const QString &name = item->name();
m_textDocument.setHtml(name);
const QString &plainText = m_textDocument.toPlainText();
m_textLayout.setText(plainText);
m_textLayout.setFont(m_label->font());
QTextOption option;
option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_textLayout.setTextOption(option);
QFontMetrics fm(m_label->font());
int line = drawText(nullptr, QRectF(QPointF(0, 0), QSizeF(DEFAULT_WIDTH, INT_MAX)),
fm.boundingRect(plainText).height(), &m_textLayout, Qt::ElideNone);
m_label->setFixedHeight(line * fm.boundingRect(plainText).height());
m_label->setText(item->tipsLinkEnabled() ? name : plainText);
}
};这些改进提高了代码的健壮性、可维护性和性能,同时保持了原有功能的完整性。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: caixr23, mhduiy 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 |
The control resizes based on its content pms: BUG-327527
The control resizes based on its content
pms: BUG-327527
Summary by Sourcery
Refactor NetTipsWidget to dynamically adjust its label height based on HTML content and font changes, fixing incorrect resizing.
Bug Fixes:
Enhancements: