diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-11-08 10:36:00 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-11-08 10:13:55 +0000 |
commit | 03155ec2e2fd6cc60b4293af19964d2e1a65b7d9 (patch) | |
tree | 9aacfeca5df6f8e65ee3f66d8434bed392733b96 | |
parent | f246314b21604c5fe2a2abe0aec8af89f06eb610 (diff) |
Expose QQuickCheckDelegate::nextCheckState() to QML
Same as 043e3d24 for CheckBox.
[ChangeLog][Controls][CheckDelegate] Made it possible to implement
nextCheckState() in QML.
Task-number: QTBUG-63238
Change-Id: Id765191b1e5559393696183ffea9ed9c69eed475
Reviewed-by: Liang Qi <liang.qi@qt.io>
-rw-r--r-- | src/imports/templates/qtquicktemplates2plugin.cpp | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickcheckdelegate.cpp | 50 | ||||
-rw-r--r-- | src/quicktemplates2/qquickcheckdelegate_p.h | 4 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_checkdelegate.qml | 47 |
4 files changed, 101 insertions, 1 deletions
diff --git a/src/imports/templates/qtquicktemplates2plugin.cpp b/src/imports/templates/qtquicktemplates2plugin.cpp index 3564f957..a491c031 100644 --- a/src/imports/templates/qtquicktemplates2plugin.cpp +++ b/src/imports/templates/qtquicktemplates2plugin.cpp @@ -323,6 +323,7 @@ void QtQuickTemplates2Plugin::registerTypes(const char *uri) qmlRegisterType<QQuickAbstractButton, 4>(uri, 2, 4, "AbstractButton"); qmlRegisterType<QQuickButtonGroup, 4>(uri, 2, 4, "ButtonGroup"); qmlRegisterType<QQuickCheckBox, 4>(uri, 2, 4, "CheckBox"); + qmlRegisterType<QQuickCheckDelegate, 4>(uri, 2, 4, "CheckDelegate"); qmlRegisterType<QQuickScrollBar, 4>(uri, 2, 4, "ScrollBar"); qmlRegisterType<QQuickScrollIndicator, 4>(uri, 2, 4, "ScrollIndicator"); qmlRegisterType<QQuickSpinBox, 4>(uri, 2, 4, "SpinBox"); diff --git a/src/quicktemplates2/qquickcheckdelegate.cpp b/src/quicktemplates2/qquickcheckdelegate.cpp index 78d55e1d..9fd8c183 100644 --- a/src/quicktemplates2/qquickcheckdelegate.cpp +++ b/src/quicktemplates2/qquickcheckdelegate.cpp @@ -38,6 +38,7 @@ #include "qquickitemdelegate_p_p.h" #include <QtGui/qpa/qplatformtheme.h> +#include <QtQml/qjsvalue.h> QT_BEGIN_NAMESPACE @@ -94,10 +95,20 @@ public: { } + void setNextCheckState(const QJSValue &callback); + bool tristate; Qt::CheckState checkState; + QJSValue nextCheckState; }; +void QQuickCheckDelegatePrivate::setNextCheckState(const QJSValue &callback) +{ + Q_Q(QQuickCheckDelegate); + nextCheckState = callback; + emit q->nextCheckStateChanged(); +} + QQuickCheckDelegate::QQuickCheckDelegate(QQuickItem *parent) : QQuickItemDelegate(*(new QQuickCheckDelegatePrivate), parent) { @@ -179,10 +190,45 @@ void QQuickCheckDelegate::buttonChange(ButtonChange change) QQuickAbstractButton::buttonChange(change); } +/*! + \since QtQuick.Controls 2.4 (Qt 5.11) + \qmlproperty function QtQuick.Controls::CheckDelegate::nextCheckState + + This property holds a callback function that is called to determine + the next check state whenever the check delegate is interactively toggled + by the user via touch, mouse, or keyboard. + + By default, a normal check delegate cycles between \c Qt.Unchecked and + \c Qt.Checked states, and a tri-state check delegate cycles between + \c Qt.Unchecked, \c Qt.PartiallyChecked, and \c Qt.Checked states. + + The \c nextCheckState callback function can override the default behavior. + The following example implements a tri-state check delegate that can present + a partially checked state depending on external conditions, but never + cycles to the partially checked state when interactively toggled by + the user. + + \code + CheckDelegate { + tristate: true + checkState: allChildrenChecked ? Qt.Checked : + anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked + + nextCheckState: function() { + if (checkState === Qt.Checked) + return Qt.Unchecked + else + return Qt.Checked + } + } + \endcode +*/ void QQuickCheckDelegate::nextCheckState() { Q_D(QQuickCheckDelegate); - if (d->tristate) + if (d->nextCheckState.isCallable()) + setCheckState(static_cast<Qt::CheckState>(d->nextCheckState.call().toInt())); + else if (d->tristate) setCheckState(static_cast<Qt::CheckState>((d->checkState + 1) % 3)); else QQuickItemDelegate::nextCheckState(); @@ -196,3 +242,5 @@ QAccessible::Role QQuickCheckDelegate::accessibleRole() const #endif QT_END_NAMESPACE + +#include "moc_qquickcheckdelegate_p.cpp" diff --git a/src/quicktemplates2/qquickcheckdelegate_p.h b/src/quicktemplates2/qquickcheckdelegate_p.h index ae2b4f52..67dcf706 100644 --- a/src/quicktemplates2/qquickcheckdelegate_p.h +++ b/src/quicktemplates2/qquickcheckdelegate_p.h @@ -59,6 +59,8 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickCheckDelegate : public QQuickItemDe Q_OBJECT Q_PROPERTY(bool tristate READ isTristate WRITE setTristate NOTIFY tristateChanged FINAL) Q_PROPERTY(Qt::CheckState checkState READ checkState WRITE setCheckState NOTIFY checkStateChanged FINAL) + // 2.4 (Qt 5.11) + Q_PRIVATE_PROPERTY(QQuickCheckDelegate::d_func(), QJSValue nextCheckState MEMBER nextCheckState WRITE setNextCheckState NOTIFY nextCheckStateChanged FINAL REVISION 4) public: explicit QQuickCheckDelegate(QQuickItem *parent = nullptr); @@ -72,6 +74,8 @@ public: Q_SIGNALS: void tristateChanged(); void checkStateChanged(); + // 2.4 (Qt 5.11) + Q_REVISION(4) void nextCheckStateChanged(); protected: QFont defaultFont() const override; diff --git a/tests/auto/controls/data/tst_checkdelegate.qml b/tests/auto/controls/data/tst_checkdelegate.qml index 9f92b4dc..72b45251 100644 --- a/tests/auto/controls/data/tst_checkdelegate.qml +++ b/tests/auto/controls/data/tst_checkdelegate.qml @@ -170,4 +170,51 @@ TestCase { break; } } + + Component { + id: nextCheckStateDelegate + CheckDelegate { + tristate: true + nextCheckState: function() { + if (checkState === Qt.Checked) + return Qt.Unchecked + else + return Qt.Checked + } + } + } + + function test_nextCheckState_data() { + return [ + { tag: "unchecked", checkState: Qt.Unchecked, expectedState: Qt.Checked }, + { tag: "partially-checked", checkState: Qt.PartiallyChecked, expectedState: Qt.Checked }, + { tag: "checked", checkState: Qt.Checked, expectedState: Qt.Unchecked } + ] + } + + function test_nextCheckState(data) { + var control = createTemporaryObject(nextCheckStateDelegate, testCase) + verify(control) + + // mouse + control.checkState = data.checkState + compare(control.checkState, data.checkState) + mouseClick(control) + compare(control.checkState, data.expectedState) + + // touch + control.checkState = data.checkState + compare(control.checkState, data.checkState) + var touch = touchEvent(control) + touch.press(0, control).commit().release(0, control).commit() + compare(control.checkState, data.expectedState) + + // keyboard + control.forceActiveFocus() + tryCompare(control, "activeFocus", true) + control.checkState = data.checkState + compare(control.checkState, data.checkState) + keyClick(Qt.Key_Space) + compare(control.checkState, data.expectedState) + } } |