From 5df44d55b5230a39f512e36e308569f28c80264b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 24 Apr 2015 19:12:20 +0200 Subject: Button: provide a default size for the label delegate If the label delegate has no explicit size defined, make it follow the size of the button minus padding. This should be a convenient default behavior that satisfies the most users, and saves a few of QML bindings. Makes also the upcoming "the default delegate implementation looks like this" -snippet nice and short. ;) Change-Id: I574439c1e52c2904746be7d830f9536d4cfd9083 Reviewed-by: Frederik Gladhorn --- src/controls/qquickbutton.cpp | 40 +++++++++++++++++++++++++++++++++++++++- src/controls/qquickbutton_p.h | 5 +++++ src/controls/qquickbutton_p_p.h | 4 ++++ src/imports/controls/Button.qml | 5 ----- 4 files changed, 48 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/controls/qquickbutton.cpp b/src/controls/qquickbutton.cpp index 64d7057e..65d16494 100644 --- a/src/controls/qquickbutton.cpp +++ b/src/controls/qquickbutton.cpp @@ -69,10 +69,25 @@ QT_BEGIN_NAMESPACE */ QQuickButtonPrivate::QQuickButtonPrivate() : - pressed(false), label(Q_NULLPTR) + pressed(false), label(Q_NULLPTR), labelHasWidth(true), labelHasHeight(true) { } +void QQuickButtonPrivate::updateGeometry() +{ + Q_Q(QQuickButton); + if (label) { + if (!labelHasWidth) { + label->setX(q->leftPadding()); + label->setWidth(q->width() - q->leftPadding() - q->rightPadding()); + } + if (!labelHasHeight) { + label->setY(q->topPadding()); + label->setHeight(q->height() - q->topPadding() - q->bottomPadding()); + } + } +} + QQuickButton::QQuickButton(QQuickItem *parent) : QQuickControl(*(new QQuickButtonPrivate), parent) { @@ -208,4 +223,27 @@ void QQuickButton::mouseUngrabEvent() setPressed(false); } +void QQuickButton::componentComplete() +{ + Q_D(QQuickButton); + QQuickControl::componentComplete(); + d->labelHasWidth = d->label && QQuickItemPrivate::get(d->label)->widthValid; + d->labelHasHeight = d->label && QQuickItemPrivate::get(d->label)->heightValid; + d->updateGeometry(); +} + +void QQuickButton::paddingChange() +{ + Q_D(QQuickButton); + QQuickControl::paddingChange(); + d->updateGeometry(); +} + +void QQuickButton::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) +{ + Q_D(QQuickButton); + QQuickControl::geometryChanged(newGeometry, oldGeometry); + d->updateGeometry(); +} + QT_END_NAMESPACE diff --git a/src/controls/qquickbutton_p.h b/src/controls/qquickbutton_p.h index ecd4b3d4..1622be76 100644 --- a/src/controls/qquickbutton_p.h +++ b/src/controls/qquickbutton_p.h @@ -92,6 +92,11 @@ protected: void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseUngrabEvent() Q_DECL_OVERRIDE; + void componentComplete() Q_DECL_OVERRIDE; + + void paddingChange() Q_DECL_OVERRIDE; + void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) Q_DECL_OVERRIDE; + private: Q_DISABLE_COPY(QQuickButton) Q_DECLARE_PRIVATE(QQuickButton) diff --git a/src/controls/qquickbutton_p_p.h b/src/controls/qquickbutton_p_p.h index ca67c224..42827fd2 100644 --- a/src/controls/qquickbutton_p_p.h +++ b/src/controls/qquickbutton_p_p.h @@ -59,9 +59,13 @@ class QQuickButtonPrivate : public QQuickControlPrivate public: QQuickButtonPrivate(); + void updateGeometry(); + QString text; bool pressed; QQuickItem *label; + bool labelHasWidth; + bool labelHasHeight; }; QT_END_NAMESPACE diff --git a/src/imports/controls/Button.qml b/src/imports/controls/Button.qml index 6ce91739..7cf1424d 100644 --- a/src/imports/controls/Button.qml +++ b/src/imports/controls/Button.qml @@ -52,11 +52,6 @@ AbstractButton { padding: Theme.padding label: Text { - x: control.leftPadding - y: control.topPadding - width: parent.width - control.leftPadding - control.rightPadding - height: parent.height - control.topPadding - control.bottomPadding - text: control.text color: control.Theme.selectedTextColor elide: Text.ElideRight -- cgit v1.2.3