diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2017-12-20 16:36:39 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2017-12-22 12:59:46 +0000 |
commit | 146bc9517c56feda4eba34282d3cc53bd47b6267 (patch) | |
tree | dd84bbabd42e16db89e8e2af2d6becab136b05b3 /src/quicktemplates2/qquickabstractbutton.cpp | |
parent | b12371c4bbc6035ad0ea60f2bc8a5ca4d48610ad (diff) |
Make AbstractButton's text win over action's when both are specified
The idea is that you share a generic Action in different places in
the UI, and then you override things locally in specific controls.
This patch enforces that by changing QQuickAbstractButton::text()
to return the Button's text if explicitly set (or there isn't an
Action), or the action's text if one is set.
Change-Id: Iae422abca35c9598f73ba7a1d3f1387c36037ec0
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickabstractbutton.cpp')
-rw-r--r-- | src/quicktemplates2/qquickabstractbutton.cpp | 64 |
1 files changed, 48 insertions, 16 deletions
diff --git a/src/quicktemplates2/qquickabstractbutton.cpp b/src/quicktemplates2/qquickabstractbutton.cpp index cc918c0c..ba48029c 100644 --- a/src/quicktemplates2/qquickabstractbutton.cpp +++ b/src/quicktemplates2/qquickabstractbutton.cpp @@ -117,7 +117,8 @@ static const int AUTO_REPEAT_INTERVAL = 100; */ QQuickAbstractButtonPrivate::QQuickAbstractButtonPrivate() - : down(false), + : explicitText(false), + down(false), explicitDown(false), pressed(false), keepPressed(false), @@ -283,6 +284,27 @@ void QQuickAbstractButtonPrivate::ungrabShortcut() } #endif +void QQuickAbstractButtonPrivate::actionTextChange() +{ + Q_Q(QQuickAbstractButton); + if (explicitText) + return; + + q->buttonChange(QQuickAbstractButton::ButtonTextChange); +} + +void QQuickAbstractButtonPrivate::setText(const QString &newText, bool isExplicit) +{ + Q_Q(QQuickAbstractButton); + const QString oldText = q->text(); + explicitText = isExplicit; + text = newText; + if (oldText == q->text()) + return; + + q->buttonChange(QQuickAbstractButton::ButtonTextChange); +} + void QQuickAbstractButtonPrivate::click() { Q_Q(QQuickAbstractButton); @@ -396,23 +418,19 @@ QQuickAbstractButton::~QQuickAbstractButton() QString QQuickAbstractButton::text() const { Q_D(const QQuickAbstractButton); - return d->text; + return d->explicitText || !d->action ? d->text : d->action->text(); } void QQuickAbstractButton::setText(const QString &text) { Q_D(QQuickAbstractButton); - if (d->text == text) - return; - -#if QT_CONFIG(shortcut) - setShortcut(QKeySequence::mnemonic(text)); -#endif + d->setText(text, true); +} - d->text = text; - setAccessibleName(d->text); - buttonChange(ButtonTextChange); - emit textChanged(); +void QQuickAbstractButton::resetText() +{ + Q_D(QQuickAbstractButton); + d->setText(QString(), false); } /*! @@ -707,11 +725,13 @@ void QQuickAbstractButton::setAction(QQuickAction *action) if (d->action == action) return; + const QString oldText = text(); + if (QQuickAction *oldAction = d->action.data()) { QQuickActionPrivate::get(oldAction)->unregisterItem(this); QObjectPrivate::disconnect(oldAction, &QQuickAction::triggered, d, &QQuickAbstractButtonPrivate::click); + QObjectPrivate::disconnect(oldAction, &QQuickAction::textChanged, d, &QQuickAbstractButtonPrivate::actionTextChange); - disconnect(oldAction, &QQuickAction::textChanged, this, &QQuickAbstractButton::setText); disconnect(oldAction, &QQuickAction::iconChanged, this, &QQuickAbstractButton::setIcon); disconnect(oldAction, &QQuickAction::checkedChanged, this, &QQuickAbstractButton::setChecked); disconnect(oldAction, &QQuickAction::checkableChanged, this, &QQuickAbstractButton::setCheckable); @@ -721,8 +741,8 @@ void QQuickAbstractButton::setAction(QQuickAction *action) if (action) { QQuickActionPrivate::get(action)->registerItem(this); QObjectPrivate::connect(action, &QQuickAction::triggered, d, &QQuickAbstractButtonPrivate::click); + QObjectPrivate::connect(action, &QQuickAction::textChanged, d, &QQuickAbstractButtonPrivate::actionTextChange); - connect(action, &QQuickAction::textChanged, this, &QQuickAbstractButton::setText); connect(action, &QQuickAction::iconChanged, this, &QQuickAbstractButton::setIcon); connect(action, &QQuickAction::checkedChanged, this, &QQuickAbstractButton::setChecked); connect(action, &QQuickAction::checkableChanged, this, &QQuickAbstractButton::setCheckable); @@ -750,13 +770,16 @@ void QQuickAbstractButton::setAction(QQuickAction *action) if (color != Qt::transparent) d->icon.setColor(color); - setText(action->text()); setChecked(action->isChecked()); setCheckable(action->isCheckable()); setEnabled(action->isEnabled()); } d->action = action; + + if (oldText != text()) + buttonChange(ButtonTextChange); + emit actionChanged(); } @@ -902,6 +925,15 @@ void QQuickAbstractButton::buttonChange(ButtonChange change) button->setChecked(false); } break; + case ButtonTextChange: { + const QString txt = text(); + setAccessibleName(txt); +#if QT_CONFIG(shortcut) + setShortcut(QKeySequence::mnemonic(txt)); +#endif + emit textChanged(); + break; + } default: break; } @@ -921,7 +953,7 @@ void QQuickAbstractButton::accessibilityActiveChanged(bool active) Q_D(QQuickAbstractButton); if (active) { - setAccessibleName(d->text); + setAccessibleName(text()); setAccessibleProperty("pressed", d->pressed); setAccessibleProperty("checked", d->checked); setAccessibleProperty("checkable", d->checkable); |