Skip to content

Commit

Permalink
Optimized PropertyLabel to not need a QLineEdit instance
Browse files Browse the repository at this point in the history
It was only used for calculating the size hint, which is now done with
some code copied and adjusted from QLineEdit::sizeHint.
  • Loading branch information
bjorn committed Oct 10, 2024
1 parent 53154ef commit 6b23c51
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
36 changes: 28 additions & 8 deletions src/tiled/propertyeditorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,7 @@ PropertyLabel::PropertyLabel(int level, QWidget *parent)
void PropertyLabel::setLevel(int level)
{
m_level = level;

const int spacing = Utils::dpiScaled(3);
const int branchIndicatorWidth = Utils::dpiScaled(14);
setContentsMargins(spacing + branchIndicatorWidth * std::max(m_level, 1),
spacing, spacing, spacing);
updateContentMargins();
}

void PropertyLabel::setHeader(bool header)
Expand All @@ -517,6 +513,7 @@ void PropertyLabel::setHeader(bool header)
setBackgroundRole(header ? QPalette::Dark : QPalette::NoRole);
setForegroundRole(header ? QPalette::BrightText : QPalette::NoRole);
setAutoFillBackground(header);
updateContentMargins();
}

void PropertyLabel::setExpandable(bool expandable)
Expand Down Expand Up @@ -588,12 +585,35 @@ void PropertyLabel::paintEvent(QPaintEvent *event)
}
}

void PropertyLabel::updateContentMargins()
{
const int spacing = Utils::dpiScaled(3);
const int branchIndicatorWidth = Utils::dpiScaled(14);
const int verticalSpacing = m_header ? spacing : 0;
setContentsMargins(spacing + branchIndicatorWidth * std::max(m_level, 1),
verticalSpacing, spacing, verticalSpacing);

}

/**
* To fit better alongside other widgets without vertical centering, the size
* hint is adjusted to match that of a QLineEdit.
*/
QSize PropertyLabel::sizeHint() const
{
auto hint = ElidingLabel::sizeHint();
hint.setHeight(m_lineEdit.sizeHint().height());
return hint;
constexpr int QLineEditPrivate_verticalMargin = 1;
constexpr int QLineEditPrivate_horizontalMargin = 2;

auto fm = fontMetrics();
auto cm = contentsMargins();
const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this);
int h = qMax(fm.height(), qMax(14, iconSize - 2)) + 2 * QLineEditPrivate_verticalMargin
+ cm.top() + cm.bottom();
int w = fm.horizontalAdvance(u'x') * 17 + 2 * QLineEditPrivate_horizontalMargin
+ cm.left() + cm.right();
QStyleOptionFrame opt;
initStyleOption(&opt);
return style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(w, h), this);
}

} // namespace Tiled
Expand Down
4 changes: 2 additions & 2 deletions src/tiled/propertyeditorwidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#pragma once

#include <QLabel>
#include <QLineEdit>
#include <QSpinBox>

class QLabel;
Expand Down Expand Up @@ -306,7 +305,8 @@ class PropertyLabel : public ElidingLabel
void paintEvent(QPaintEvent *) override;

private:
QLineEdit m_lineEdit;
void updateContentMargins();

int m_level = 0;
bool m_header = false;
bool m_expandable = false;
Expand Down

0 comments on commit 6b23c51

Please sign in to comment.