From 85a7bc184d017858910443100158ccb6b65fdf6a Mon Sep 17 00:00:00 2001 From: Kevin Krammer Date: Fri, 19 Oct 2012 17:36:53 +0200 Subject: [PATCH] Fixing delayed setting of BoxLayout::alignment --- declarativeobjects.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++---- declarativeobjects_p.h | 5 ++- layouts.qml | 5 ++- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/declarativeobjects.cpp b/declarativeobjects.cpp index c78ca58..8747485 100644 --- a/declarativeobjects.cpp +++ b/declarativeobjects.cpp @@ -163,14 +163,23 @@ CUSTOM_METAOBJECT(DeclarativeSeparator, QAction) class DeclarativeBoxLayoutAttached::Private { public: - Private() : stretch(0), alignment(0) {} + Private(QWidget *w, QLayout *l) : stretch(0), alignment(0), widget(w), layout(l) {} int stretch; Qt::Alignment alignment; + + QPointer widget; + QPointer layout; + QPointer parentLayout; }; -DeclarativeBoxLayoutAttached::DeclarativeBoxLayoutAttached(QObject *parent) - : QObject(parent), d(new Private) +DeclarativeBoxLayoutAttached::DeclarativeBoxLayoutAttached(QWidget *widget, QObject *parent) + : QObject(parent), d(new Private(widget, 0)) +{ +} + +DeclarativeBoxLayoutAttached::DeclarativeBoxLayoutAttached(QLayout *layout, QObject *parent) + : QObject(parent), d(new Private(0, layout)) { } @@ -179,6 +188,11 @@ DeclarativeBoxLayoutAttached::~DeclarativeBoxLayoutAttached() delete d; } +void DeclarativeBoxLayoutAttached::setParentLayout(QBoxLayout *parentLayout) +{ + d->parentLayout = parentLayout; +} + void DeclarativeBoxLayoutAttached::setStretch(int stretch) { if (stretch == d->stretch) @@ -200,6 +214,14 @@ void DeclarativeBoxLayoutAttached::setAlignment(Qt::Alignment alignment) d->alignment = alignment; emit alignmentChanged(alignment); + + if (d->parentLayout) { + if (d->widget) + d->parentLayout->setAlignment(d->widget, d->alignment); + + if (d->layout) + d->parentLayout->setAlignment(d->layout, d->alignment); + } } Qt::Alignment DeclarativeBoxLayoutAttached::alignment() const @@ -438,7 +460,19 @@ DeclarativeHBoxLayout::DeclarativeHBoxLayout(QObject *parent) : DeclarativeLayou DeclarativeBoxLayoutAttached *DeclarativeHBoxLayout::qmlAttachedProperties(QObject *parent) { - return new DeclarativeBoxLayoutAttached(parent); + AbstractDeclarativeObject *declarativeObject = dynamic_cast(parent); + if (declarativeObject) { + QWidget *widget = qobject_cast(declarativeObject->object()); + if (widget) + return new DeclarativeBoxLayoutAttached(widget, parent); + + QLayout *layout = qobject_cast(declarativeObject->object()); + if (layout) + return new DeclarativeBoxLayoutAttached(layout, parent); + } + + qmlInfo(parent) << "Can only attach HBoxLayout to widgets and layouts"; + return 0; } void DeclarativeHBoxLayout::addWidget(QWidget *widget, AbstractDeclarativeObject *declarativeObject) @@ -451,6 +485,8 @@ void DeclarativeHBoxLayout::addWidget(QWidget *widget, AbstractDeclarativeObject if (properties) { stretch = properties->stretch(); alignment = properties->alignment(); + + properties->setParentLayout(m_proxiedObject); } m_proxiedObject->addWidget(widget, stretch, alignment); @@ -460,13 +496,19 @@ void DeclarativeHBoxLayout::addWidget(QWidget *widget, AbstractDeclarativeObject void DeclarativeHBoxLayout::addLayout(QLayout *layout, AbstractDeclarativeObject *declarativeObject) { int stretch = 0; + Qt::Alignment alignment = 0; QObject *attachedProperties = qmlAttachedPropertiesObject(declarativeObject, false); DeclarativeBoxLayoutAttached *properties = qobject_cast(attachedProperties); - if (properties) + if (properties) { stretch = properties->stretch(); + alignment = properties->alignment(); + + properties->setParentLayout(m_proxiedObject); + } m_proxiedObject->addLayout(layout, stretch); + m_proxiedObject->setAlignment(layout, alignment); m_children.append(declarativeObject); } @@ -480,7 +522,19 @@ DeclarativeVBoxLayout::DeclarativeVBoxLayout(QObject *parent) : DeclarativeLayou DeclarativeBoxLayoutAttached *DeclarativeVBoxLayout::qmlAttachedProperties(QObject *parent) { - return new DeclarativeBoxLayoutAttached(parent); + AbstractDeclarativeObject *declarativeObject = dynamic_cast(parent); + if (declarativeObject) { + QWidget *widget = qobject_cast(declarativeObject->object()); + if (widget) + return new DeclarativeBoxLayoutAttached(widget, parent); + + QLayout *layout = qobject_cast(declarativeObject->object()); + if (layout) + return new DeclarativeBoxLayoutAttached(layout, parent); + } + + qmlInfo(parent) << "Can only attach VBoxLayout to widgets and layouts"; + return 0; } void DeclarativeVBoxLayout::addWidget(QWidget *widget, AbstractDeclarativeObject *declarativeObject) @@ -493,6 +547,8 @@ void DeclarativeVBoxLayout::addWidget(QWidget *widget, AbstractDeclarativeObject if (properties) { stretch = properties->stretch(); alignment = properties->alignment(); + + properties->setParentLayout(m_proxiedObject); } m_proxiedObject->addWidget(widget, stretch, alignment); @@ -502,13 +558,19 @@ void DeclarativeVBoxLayout::addWidget(QWidget *widget, AbstractDeclarativeObject void DeclarativeVBoxLayout::addLayout(QLayout *layout, AbstractDeclarativeObject *declarativeObject) { int stretch = 0; + Qt::Alignment alignment = 0; QObject *attachedProperties = qmlAttachedPropertiesObject(declarativeObject, false); DeclarativeBoxLayoutAttached *properties = qobject_cast(attachedProperties); - if (properties) + if (properties) { stretch = properties->stretch(); + alignment = properties->alignment(); + + properties->setParentLayout(m_proxiedObject); + } m_proxiedObject->addLayout(layout, stretch); + m_proxiedObject->setAlignment(layout, alignment); m_children.append(declarativeObject); } diff --git a/declarativeobjects_p.h b/declarativeobjects_p.h index 353f165..38edf9f 100644 --- a/declarativeobjects_p.h +++ b/declarativeobjects_p.h @@ -224,9 +224,12 @@ class DeclarativeBoxLayoutAttached : public QObject Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged) public: - DeclarativeBoxLayoutAttached(QObject *parent); + DeclarativeBoxLayoutAttached(QWidget *widget, QObject *parent); + DeclarativeBoxLayoutAttached(QLayout *layout, QObject *parent); ~DeclarativeBoxLayoutAttached(); + void setParentLayout(QBoxLayout *parentLayout); + void setStretch(int stretch); int stretch() const; diff --git a/layouts.qml b/layouts.qml index 259d537..3c16710 100644 --- a/layouts.qml +++ b/layouts.qml @@ -24,7 +24,7 @@ TabWidget { Widget { HBoxLayout { Label { - HBoxLayout.alignment: Qt.AlignRight + HBoxLayout.alignment: Qt.AlignTop text: "Top" } Label { @@ -35,6 +35,9 @@ TabWidget { HBoxLayout.alignment: Qt.AlignBottom text: "Bottom" } + Label { // just for increasing the parent's height + text: "\n\n\n" + } } } -- 1.7.2.5