Skip to content

Commit

Permalink
Show the type of custom properties in their tool tip
Browse files Browse the repository at this point in the history
Also fixed some issues in left-to-right mode.
  • Loading branch information
bjorn committed Oct 14, 2024
1 parent 576ac1f commit 1385a01
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
37 changes: 26 additions & 11 deletions src/tiled/propertieswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2538,30 +2538,29 @@ Property *VariantMapProperty::createProperty(const QStringList &path,
std::function<QVariant ()> get,
std::function<void (const QVariant &)> set)
{
Property *property = nullptr;

const auto value = get();
const auto type = value.userType();
const auto &name = path.last();
QString typeName;

if (type == filePathTypeId()) {
auto getUrl = [get = std::move(get)] { return get().value<FilePath>().url; };
auto setUrl = [set = std::move(set)] (const QUrl &value) {
set(QVariant::fromValue(FilePath { value }));
};
return new UrlProperty(name, std::move(getUrl), std::move(setUrl));
}

if (type == objectRefTypeId()) {
property = new UrlProperty(name, std::move(getUrl), std::move(setUrl));
} else if (type == objectRefTypeId()) {
auto getObjectRef = [get = std::move(get), this] {
return DisplayObjectRef(get().value<ObjectRef>(),
static_cast<MapDocument*>(mDocument));
};
auto setObjectRef = [set = std::move(set)](const DisplayObjectRef &value) {
set(QVariant::fromValue(value.ref));
};
return new ObjectRefProperty(name, std::move(getObjectRef), std::move(setObjectRef));
}

if (type == propertyValueId()) {
property = new ObjectRefProperty(name, std::move(getObjectRef), std::move(setObjectRef));
} else if (type == propertyValueId()) {
const auto propertyValue = value.value<PropertyValue>();
if (auto propertyType = propertyValue.type()) {
switch (propertyType->type) {
Expand All @@ -2575,7 +2574,8 @@ Property *VariantMapProperty::createProperty(const QStringList &path,

createClassMembers(path, groupProperty, classType, std::move(get));

return groupProperty;
property = groupProperty;
break;
}
case PropertyType::PT_Enum: {
auto enumProperty = new BaseEnumProperty(
Expand All @@ -2589,13 +2589,28 @@ Property *VariantMapProperty::createProperty(const QStringList &path,
enumProperty->setEnumData(enumType.values);
enumProperty->setFlags(enumType.valuesAsFlags);

return enumProperty;
property = enumProperty;
break;
}
}

typeName = propertyType->name;
} else {
typeName = tr("Unknown type");
}
} else {
property = createVariantProperty(name, std::move(get), std::move(set));
}

if (property) {
if (typeName.isEmpty())
typeName = typeToName(type);

property->setToolTip(QStringLiteral("%1&nbsp;<span style=\"color: gray;\">:&nbsp;%2<span>")
.arg(property->name(), typeName));
}

return createVariantProperty(name, std::move(get), std::move(set));
return property;
}

void VariantMapProperty::createClassMembers(const QStringList &path,
Expand Down
55 changes: 43 additions & 12 deletions src/tiled/propertyeditorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,25 @@ ElidingLabel::ElidingLabel(const QString &text, QWidget *parent)
setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
}

/**
* Sets a tool tip on the label.
*
* When a tool tip is set, it will be shown instead of the on-demand tool tip
* that shows the full text when the text is elided.
*/
void ElidingLabel::setToolTip(const QString &toolTip)
{
if (m_toolTip == toolTip)
return;

m_toolTip = toolTip;

if (m_toolTip.isEmpty())
QLabel::setToolTip(m_isElided ? text() : QString());
else
QLabel::setToolTip(m_toolTip);
}

QSize ElidingLabel::minimumSizeHint() const
{
auto hint = QLabel::minimumSizeHint();
Expand All @@ -468,22 +487,23 @@ QSize ElidingLabel::minimumSizeHint() const

void ElidingLabel::paintEvent(QPaintEvent *)
{
const int m = margin();
const QRect cr = contentsRect().adjusted(m, m, -m, -m);
const Qt::LayoutDirection dir = text().isRightToLeft() ? Qt::RightToLeft : Qt::LeftToRight;
const int align = QStyle::visualAlignment(dir, alignment());
const int flags = align | (dir == Qt::LeftToRight ? Qt::TextForceLeftToRight
: Qt::TextForceRightToLeft);

QStyleOption opt;
opt.initFrom(this);

const int m = margin();
const QRect cr = contentsRect().adjusted(m, m, -m, -m);
const int align = QStyle::visualAlignment(opt.direction, alignment());
const int flags = align | (opt.direction == Qt::LeftToRight ? Qt::TextForceLeftToRight
: Qt::TextForceRightToLeft);

const auto elidedText = opt.fontMetrics.elidedText(text(), Qt::ElideRight, cr.width());
const bool isElided = elidedText != text();

if (isElided != m_isElided) {
m_isElided = isElided;
setToolTip(isElided ? text() : QString());

if (m_toolTip.isEmpty())
QLabel::setToolTip(m_isElided ? text() : QString());
}

QStylePainter p(this);
Expand Down Expand Up @@ -554,6 +574,9 @@ bool PropertyLabel::event(QEvent *event)
}
}

if (event->type() == QEvent::LayoutDirectionChange)
updateContentMargins();

return ElidingLabel::event(event);
}

Expand All @@ -563,11 +586,16 @@ void PropertyLabel::paintEvent(QPaintEvent *event)

const int spacing = Utils::dpiScaled(3);
const int branchIndicatorWidth = Utils::dpiScaled(14);
const int indent = branchIndicatorWidth * std::max(m_level - 1, 0);

QStyleOption branchOption;
branchOption.initFrom(this);
branchOption.rect = QRect(branchIndicatorWidth * std::max(m_level - 1, 0), 0,
branchIndicatorWidth + spacing, height());
if (branchOption.direction == Qt::LeftToRight)
branchOption.rect = QRect(indent, 0,
branchIndicatorWidth + spacing, height());
else
branchOption.rect = QRect(width() - indent - branchIndicatorWidth - spacing, 0,
branchIndicatorWidth + spacing, height());
if (m_expandable)
branchOption.state |= QStyle::State_Children;
if (m_expanded)
Expand All @@ -590,9 +618,12 @@ 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);
const int indent = branchIndicatorWidth * std::max(m_level, 1);

if (isLeftToRight())
setContentsMargins(spacing + indent, verticalSpacing, spacing, verticalSpacing);
else
setContentsMargins(spacing, verticalSpacing, spacing + indent, verticalSpacing);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/tiled/propertyeditorwidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,15 @@ class ElidingLabel : public QLabel
explicit ElidingLabel(QWidget *parent = nullptr);
ElidingLabel(const QString &text, QWidget *parent = nullptr);

void setToolTip(const QString &toolTip);

QSize minimumSizeHint() const override;

protected:
void paintEvent(QPaintEvent *) override;

private:
QString m_toolTip;
bool m_isElided = false;
};

Expand Down
4 changes: 3 additions & 1 deletion src/tiled/varianteditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,10 @@ QLayout *VariantEditor::createPropertyLayout(Property *property)

if (displayMode == Property::DisplayMode::Header)
widgets.label->setHeader(true);
else
else if (isLeftToRight())
rowLayout->setContentsMargins(0, halfSpacing, halfSpacing * 2, halfSpacing);
else
rowLayout->setContentsMargins(halfSpacing * 2, halfSpacing, 0, halfSpacing);

rowLayout->addWidget(widgets.label, LabelStretch, Qt::AlignTop);

Expand Down
2 changes: 2 additions & 0 deletions src/tiled/varianteditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Property : public QObject
virtual QWidget *createEditor(QWidget *parent) = 0;

signals:
void typeNameChanged(const QString &typeName);
void toolTipChanged(const QString &toolTip);
void valueChanged();
void enabledChanged(bool enabled);
Expand All @@ -102,6 +103,7 @@ class Property : public QObject
friend class GroupProperty;

QString m_name;
QString m_typeName;
QString m_toolTip;
bool m_enabled = true;
bool m_modified = false;
Expand Down

0 comments on commit 1385a01

Please sign in to comment.