diff --git a/src/tiled/propertieswidget.cpp b/src/tiled/propertieswidget.cpp index e1eb88527b..39c23d602a 100644 --- a/src/tiled/propertieswidget.cpp +++ b/src/tiled/propertieswidget.cpp @@ -2538,19 +2538,20 @@ Property *VariantMapProperty::createProperty(const QStringList &path, std::function get, std::function 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().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(), static_cast(mDocument)); @@ -2558,10 +2559,8 @@ Property *VariantMapProperty::createProperty(const QStringList &path, 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(); if (auto propertyType = propertyValue.type()) { switch (propertyType->type) { @@ -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( @@ -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 : %2") + .arg(property->name(), typeName)); } - return createVariantProperty(name, std::move(get), std::move(set)); + return property; } void VariantMapProperty::createClassMembers(const QStringList &path, diff --git a/src/tiled/propertyeditorwidgets.cpp b/src/tiled/propertyeditorwidgets.cpp index c0b9e6ebd3..bab7b58d70 100644 --- a/src/tiled/propertyeditorwidgets.cpp +++ b/src/tiled/propertyeditorwidgets.cpp @@ -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(); @@ -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); @@ -554,6 +574,9 @@ bool PropertyLabel::event(QEvent *event) } } + if (event->type() == QEvent::LayoutDirectionChange) + updateContentMargins(); + return ElidingLabel::event(event); } @@ -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) @@ -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); } /** diff --git a/src/tiled/propertyeditorwidgets.h b/src/tiled/propertyeditorwidgets.h index 6c3722798d..400018d3b7 100644 --- a/src/tiled/propertyeditorwidgets.h +++ b/src/tiled/propertyeditorwidgets.h @@ -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; }; diff --git a/src/tiled/varianteditor.cpp b/src/tiled/varianteditor.cpp index 19f2fd2154..be4862808b 100644 --- a/src/tiled/varianteditor.cpp +++ b/src/tiled/varianteditor.cpp @@ -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); diff --git a/src/tiled/varianteditor.h b/src/tiled/varianteditor.h index b21d2df0f3..988fcfad2d 100644 --- a/src/tiled/varianteditor.h +++ b/src/tiled/varianteditor.h @@ -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); @@ -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;