diff options
-rw-r--r-- | src/controls/qquickexclusivegroup.cpp | 143 | ||||
-rw-r--r-- | src/controls/qquickexclusivegroup_p.h | 5 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_exclusivegroup.qml | 93 |
3 files changed, 227 insertions, 14 deletions
diff --git a/src/controls/qquickexclusivegroup.cpp b/src/controls/qquickexclusivegroup.cpp index 1531b11e..20a863cc 100644 --- a/src/controls/qquickexclusivegroup.cpp +++ b/src/controls/qquickexclusivegroup.cpp @@ -51,15 +51,53 @@ QT_BEGIN_NAMESPACE \brief An exclusive group of checkable controls. ExclusiveGroup is a non-visual, mutually exclusive group of checkable - controls and objects. It is used with controls such as RadioButton, where only one of the options can be selected at a time. + controls and objects. It is used with controls such as RadioButton, + where only one of the options can be selected at a time. Any control or object that has a \c checked property, and either a \c checkedChanged(), \c toggled(), or \c toggled(bool) signal, can be - attached to an ExclusiveGroup. + added to an ExclusiveGroup. + + The most straight-forward way to use ExclusiveGroup is to assign + a list of checkable items. For example, the list of children of a + \l{Item Positioners}{positioner} or a \l{Qt Quick Layouts}{layout} + that manages a group of mutually exclusive controls. \code + ExclusiveGroup { + checkables: column.children + } + Column { - ExclusiveGroup { id: radioGroup } + id: column + + RadioButton { + checked: true + text: qsTr("DAB") + } + + RadioButton { + text: qsTr("FM") + } + + RadioButton { + text: qsTr("AM") + } + } + \endcode + + Mutually exclusive controls do not always share the same parent item, + or the parent layout may sometimes contain items that should not be + included to the exclusive group. Such cases are best handled using + the \l group attached property. + + \code + ExclusiveGroup { id: radioGroup } + + Column { + Label { + text: qsTr("Radio:") + } RadioButton { checked: true @@ -79,6 +117,9 @@ QT_BEGIN_NAMESPACE } \endcode + More advanced use cases can be handled using the addCheckable() and + removeCheckable() methods. + \sa RadioButton */ @@ -119,7 +160,13 @@ public: void _q_updateCurrent(); + static void checkables_append(QQmlListProperty<QObject> *prop, QObject *obj); + static int checkables_count(QQmlListProperty<QObject> *prop); + static QObject *checkables_at(QQmlListProperty<QObject> *prop, int index); + static void checkables_clear(QQmlListProperty<QObject> *prop); + QObject *current; + QObjectList checkables; QMetaMethod updateCurrentMethod; }; @@ -131,6 +178,35 @@ void QQuickExclusiveGroupPrivate::_q_updateCurrent() q->setCurrent(object); } +void QQuickExclusiveGroupPrivate::checkables_append(QQmlListProperty<QObject> *prop, QObject *obj) +{ + QQuickExclusiveGroup *q = static_cast<QQuickExclusiveGroup *>(prop->object); + q->addCheckable(obj); +} + +int QQuickExclusiveGroupPrivate::checkables_count(QQmlListProperty<QObject> *prop) +{ + QQuickExclusiveGroupPrivate *p = static_cast<QQuickExclusiveGroupPrivate *>(prop->data); + return p->checkables.count(); +} + +QObject *QQuickExclusiveGroupPrivate::checkables_at(QQmlListProperty<QObject> *prop, int index) +{ + QQuickExclusiveGroupPrivate *p = static_cast<QQuickExclusiveGroupPrivate *>(prop->data); + return p->checkables.value(index); +} + +void QQuickExclusiveGroupPrivate::checkables_clear(QQmlListProperty<QObject> *prop) +{ + QQuickExclusiveGroupPrivate *p = static_cast<QQuickExclusiveGroupPrivate *>(prop->data); + if (!p->checkables.isEmpty()) { + p->checkables.clear(); + QQuickExclusiveGroup *q = static_cast<QQuickExclusiveGroup *>(prop->object); + q->setCurrent(0); + emit q->checkablesChanged(); + } +} + QQuickExclusiveGroup::QQuickExclusiveGroup(QObject *parent) : QObject(*(new QQuickExclusiveGroupPrivate), parent) { @@ -149,7 +225,7 @@ QQuickExclusiveGroupAttached *QQuickExclusiveGroup::qmlAttachedProperties(QObjec This property holds the currently selected object or \c null if there is none. - By default, it is the first checked object attached to the exclusive group. + By default, it is the first checked object added to the exclusive group. */ QObject *QQuickExclusiveGroup::current() const { @@ -171,19 +247,57 @@ void QQuickExclusiveGroup::setCurrent(QObject *current) } /*! + \qmlproperty list<Object> QtQuickControls2::ExclusiveGroup::checkables + \default + + This property holds the list of checkables. + + \code + ExclusiveGroup { + checkables: column.children + } + + Column { + id: column + + RadioButton { + checked: true + text: qsTr("Option A") + } + + RadioButton { + text: qsTr("Option B") + } + } + \endcode + + \sa group +*/ +QQmlListProperty<QObject> QQuickExclusiveGroup::checkables() +{ + Q_D(QQuickExclusiveGroup); + return QQmlListProperty<QObject>(this, d, + QQuickExclusiveGroupPrivate::checkables_append, + QQuickExclusiveGroupPrivate::checkables_count, + QQuickExclusiveGroupPrivate::checkables_at, + QQuickExclusiveGroupPrivate::checkables_clear); +} + +/*! \qmlmethod void QtQuickControls2::ExclusiveGroup::addCheckable(QtObject object) Adds an \a object to the exclusive group. \note Manually adding objects to an exclusive group is typically unnecessary. - The \l group attached property provides a convenient and declarative syntax. + The \l checkables property and the \l group attached property provide a + convenient and declarative syntax. - \sa group + \sa checkables, group */ void QQuickExclusiveGroup::addCheckable(QObject *object) { Q_D(QQuickExclusiveGroup); - if (!object) + if (!object || d->checkables.contains(object)) return; QMetaMethod signal = checkableSignal(object); @@ -193,6 +307,9 @@ void QQuickExclusiveGroup::addCheckable(QObject *object) if (isChecked(object)) setCurrent(object); + + d->checkables.append(object); + emit checkablesChanged(); } else { qmlInfo(this) << "The object has no checkedChanged() or toggled() signal."; } @@ -204,14 +321,15 @@ void QQuickExclusiveGroup::addCheckable(QObject *object) Removes an \a object from the exclusive group. \note Manually removing objects from an exclusive group is typically unnecessary. - The \l group attached property provides a convenient and declarative syntax. + The \l checkables property and the \l group attached property provide a + convenient and declarative syntax. - \sa group + \sa checkables, group */ void QQuickExclusiveGroup::removeCheckable(QObject *object) { Q_D(QQuickExclusiveGroup); - if (!object) + if (!object || !d->checkables.contains(object)) return; QMetaMethod signal = checkableSignal(object); @@ -222,6 +340,9 @@ void QQuickExclusiveGroup::removeCheckable(QObject *object) if (d->current == object) setCurrent(Q_NULLPTR); + + d->checkables.removeOne(object); + emit checkablesChanged(); } class QQuickExclusiveGroupAttachedPrivate : public QObjectPrivate @@ -256,6 +377,8 @@ QQuickExclusiveGroupAttached::QQuickExclusiveGroupAttached(QObject *parent) : ExclusiveGroup.group: group } \endcode + + \sa checkables */ QQuickExclusiveGroup *QQuickExclusiveGroupAttached::group() const { diff --git a/src/controls/qquickexclusivegroup_p.h b/src/controls/qquickexclusivegroup_p.h index 434425d0..0c4962aa 100644 --- a/src/controls/qquickexclusivegroup_p.h +++ b/src/controls/qquickexclusivegroup_p.h @@ -62,6 +62,8 @@ class Q_QUICKCONTROLS_EXPORT QQuickExclusiveGroup : public QObject { Q_OBJECT Q_PROPERTY(QObject *current READ current WRITE setCurrent NOTIFY currentChanged) + Q_PROPERTY(QQmlListProperty<QObject> checkables READ checkables NOTIFY checkablesChanged FINAL) + Q_CLASSINFO("DefaultProperty", "checkables") public: explicit QQuickExclusiveGroup(QObject *parent = Q_NULLPTR); @@ -71,12 +73,15 @@ public: QObject *current() const; void setCurrent(QObject *current); + QQmlListProperty<QObject> checkables(); + public Q_SLOTS: void addCheckable(QObject *object); void removeCheckable(QObject *object); Q_SIGNALS: void currentChanged(); + void checkablesChanged(); private: Q_DISABLE_COPY(QQuickExclusiveGroup) diff --git a/tests/auto/controls/data/tst_exclusivegroup.qml b/tests/auto/controls/data/tst_exclusivegroup.qml index 537121d0..41794ab2 100644 --- a/tests/auto/controls/data/tst_exclusivegroup.qml +++ b/tests/auto/controls/data/tst_exclusivegroup.qml @@ -50,24 +50,45 @@ TestCase { when: windowShown name: "ExclusiveGroup" + Component { + id: exclusiveGroup + ExclusiveGroup { } + } + + Component { + id: checkableGroup + ExclusiveGroup { + QtObject { objectName: "non-checkable" } + QtObject { objectName: "checkable1"; property bool checked: false } + QtObject { objectName: "checkable2"; property bool checked: true } + QtObject { objectName: "checkable3"; property bool checked: false } + } + } + SignalSpy { id: currentSpy signalName: "currentChanged" } - Component { - id: exclusiveGroup - ExclusiveGroup { } + SignalSpy { + id: checkablesSpy + signalName: "checkablesChanged" } function init() { verify(!currentSpy.target) compare(currentSpy.count, 0) + + verify(!checkablesSpy.target) + compare(checkablesSpy.count, 0) } function cleanup() { currentSpy.target = null currentSpy.clear() + + checkablesSpy.target = null + checkablesSpy.clear() } function test_null() { @@ -156,6 +177,55 @@ TestCase { group.destroy() } + function test_checkables() { + ignoreWarning(Qt.resolvedUrl("tst_exclusivegroup.qml") + ":60:9: QML ExclusiveGroup: The object has no checkedChanged() or toggled() signal.") + var group = checkableGroup.createObject(testCase) + verify(group) + + checkablesSpy.target = group + verify(checkablesSpy.valid) + + compare(group.checkables.length, 3) + compare(group.checkables[0].objectName, "checkable1") + compare(group.checkables[1].objectName, "checkable2") + compare(group.checkables[2].objectName, "checkable3") + compare(group.current, group.checkables[1]) + + var checkable4 = checkable.createObject(testCase, {checked: true}) + var checkable5 = checkable.createObject(testCase, {checked: false}) + + group.checkables = [checkable4, checkable5] + compare(group.checkables.length, 2) + compare(group.checkables[0], checkable4) + compare(group.checkables[1], checkable5) + compare(group.current, checkable4) + compare(checkablesSpy.count, 3) // clear + 2 * append :/ + + var checkable6 = checkable.createObject(testCase, {checked: true}) + + group.addCheckable(checkable6) + compare(group.checkables.length, 3) + compare(group.checkables[0], checkable4) + compare(group.checkables[1], checkable5) + compare(group.checkables[2], checkable6) + compare(group.current, checkable6) + compare(checkablesSpy.count, 4) + + group.removeCheckable(checkable4) + compare(group.checkables.length, 2) + compare(group.checkables[0], checkable5) + compare(group.checkables[1], checkable6) + compare(group.current, checkable6) + compare(checkablesSpy.count, 5) + + group.checkables = [] + compare(group.checkables.length, 0) + compare(group.current, null) + compare(checkablesSpy.count, 6) + + group.destroy() + } + Component { id: checkBoxes Item { @@ -196,12 +266,27 @@ TestCase { } } + Component { + id: childControls + Item { + id: container + property ExclusiveGroup group: ExclusiveGroup { id: group; checkables: container.children } + property alias control1: control1 + property alias control2: control2 + property alias control3: control3 + CheckBox { id: control1 } + RadioButton { id: control2 } + Switch { id: control3 } + } + } + function test_controls_data() { return [ { tag: "CheckBox", component: checkBoxes }, { tag: "RadioButton", component: radioButtons }, { tag: "Switch", component: switches }, - { tag: "ToggleButton", component: toggleButtons } + { tag: "ToggleButton", component: toggleButtons }, + { tag: "Children", component: childControls } ] } |