| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at http://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qquickcheckdelegate_p.h" |
| 38 | #include "qquickitemdelegate_p_p.h" |
| 39 | |
| 40 | #include <QtGui/qpa/qplatformtheme.h> |
| 41 | #include <QtQml/qjsvalue.h> |
| 42 | |
| 43 | QT_BEGIN_NAMESPACE |
| 44 | |
| 45 | /*! |
| 46 | \qmltype CheckDelegate |
| 47 | \inherits ItemDelegate |
| 48 | //! \instantiates QQuickCheckDelegate |
| 49 | \inqmlmodule QtQuick.Controls |
| 50 | \since 5.7 |
| 51 | \ingroup qtquickcontrols2-delegates |
| 52 | \brief Item delegate with a check indicator that can be toggled on or off. |
| 53 | |
| 54 | \image qtquickcontrols2-checkdelegate.gif |
| 55 | |
| 56 | CheckDelegate presents an item delegate that can be toggled on (checked) or |
| 57 | off (unchecked). Check delegates are typically used to select one or more |
| 58 | options from a set of options in a list. For smaller sets of options, or |
| 59 | for options that need to be uniquely identifiable, consider using |
| 60 | \l CheckBox instead. |
| 61 | |
| 62 | CheckDelegate inherits its API from \l ItemDelegate, which is inherited |
| 63 | from AbstractButton. For instance, you can set \l {AbstractButton::text}{text}, |
| 64 | and react to \l {AbstractButton::clicked}{clicks} using the AbstractButton |
| 65 | API. The state of the check delegate can be set with the |
| 66 | \l {AbstractButton::}{checked} property. |
| 67 | |
| 68 | In addition to the checked and unchecked states, there is a third state: |
| 69 | partially checked. The partially checked state can be enabled using the |
| 70 | \l tristate property. This state indicates that the regular checked/unchecked |
| 71 | state can not be determined; generally because of other states that affect |
| 72 | the check delegate. This state is useful when several child nodes are selected |
| 73 | in a treeview, for example. |
| 74 | |
| 75 | \code |
| 76 | ListView { |
| 77 | model: ["Option 1", "Option 2", "Option 3"] |
| 78 | delegate: CheckDelegate { |
| 79 | text: modelData |
| 80 | } |
| 81 | } |
| 82 | \endcode |
| 83 | |
| 84 | \sa {Customizing CheckDelegate}, {Delegate Controls}, CheckBox |
| 85 | */ |
| 86 | |
| 87 | class QQuickCheckDelegatePrivate : public QQuickItemDelegatePrivate |
| 88 | { |
| 89 | Q_DECLARE_PUBLIC(QQuickCheckDelegate) |
| 90 | |
| 91 | public: |
| 92 | void setNextCheckState(const QJSValue &callback); |
| 93 | |
| 94 | bool tristate = false; |
| 95 | Qt::CheckState checkState = Qt::Unchecked; |
| 96 | QJSValue nextCheckState; |
| 97 | }; |
| 98 | |
| 99 | void QQuickCheckDelegatePrivate::setNextCheckState(const QJSValue &callback) |
| 100 | { |
| 101 | Q_Q(QQuickCheckDelegate); |
| 102 | nextCheckState = callback; |
| 103 | emit q->nextCheckStateChanged(); |
| 104 | } |
| 105 | |
| 106 | QQuickCheckDelegate::QQuickCheckDelegate(QQuickItem *parent) |
| 107 | : QQuickItemDelegate(*(new QQuickCheckDelegatePrivate), parent) |
| 108 | { |
| 109 | setCheckable(true); |
| 110 | } |
| 111 | |
| 112 | /*! |
| 113 | \qmlproperty bool QtQuick.Controls::CheckDelegate::tristate |
| 114 | |
| 115 | This property determines whether the check delegate has three states. |
| 116 | |
| 117 | In the animation below, the first checkdelegate is tri-state: |
| 118 | |
| 119 | \image qtquickcontrols2-checkdelegate-tristate.gif |
| 120 | |
| 121 | The default is \c false, i.e., the delegate has only two states. |
| 122 | */ |
| 123 | bool QQuickCheckDelegate::isTristate() const |
| 124 | { |
| 125 | Q_D(const QQuickCheckDelegate); |
| 126 | return d->tristate; |
| 127 | } |
| 128 | |
| 129 | void QQuickCheckDelegate::setTristate(bool tristate) |
| 130 | { |
| 131 | Q_D(QQuickCheckDelegate); |
| 132 | if (d->tristate == tristate) |
| 133 | return; |
| 134 | |
| 135 | d->tristate = tristate; |
| 136 | emit tristateChanged(); |
| 137 | } |
| 138 | |
| 139 | /*! |
| 140 | \qmlproperty enumeration QtQuick.Controls::CheckDelegate::checkState |
| 141 | |
| 142 | This property determines the check state of the check delegate. |
| 143 | |
| 144 | Available states: |
| 145 | \value Qt.Unchecked The delegate is unchecked. |
| 146 | \value Qt.PartiallyChecked The delegate is partially checked. This state is only used when \l tristate is enabled. |
| 147 | \value Qt.Checked The delegate is checked. |
| 148 | |
| 149 | \sa tristate, {AbstractButton::checked}{checked} |
| 150 | */ |
| 151 | Qt::CheckState QQuickCheckDelegate::checkState() const |
| 152 | { |
| 153 | Q_D(const QQuickCheckDelegate); |
| 154 | return d->checkState; |
| 155 | } |
| 156 | |
| 157 | void QQuickCheckDelegate::setCheckState(Qt::CheckState state) |
| 158 | { |
| 159 | Q_D(QQuickCheckDelegate); |
| 160 | if (d->checkState == state) |
| 161 | return; |
| 162 | |
| 163 | bool wasChecked = isChecked(); |
| 164 | d->checked = state == Qt::Checked; |
| 165 | d->checkState = state; |
| 166 | emit checkStateChanged(); |
| 167 | if (d->checked != wasChecked) |
| 168 | emit checkedChanged(); |
| 169 | } |
| 170 | |
| 171 | QFont QQuickCheckDelegate::defaultFont() const |
| 172 | { |
| 173 | return QQuickTheme::font(scope: QQuickTheme::ListView); |
| 174 | } |
| 175 | |
| 176 | QPalette QQuickCheckDelegate::defaultPalette() const |
| 177 | { |
| 178 | return QQuickTheme::palette(scope: QQuickTheme::ListView); |
| 179 | } |
| 180 | |
| 181 | void QQuickCheckDelegate::buttonChange(ButtonChange change) |
| 182 | { |
| 183 | if (change == ButtonCheckedChange) |
| 184 | setCheckState(isChecked() ? Qt::Checked : Qt::Unchecked); |
| 185 | else |
| 186 | QQuickAbstractButton::buttonChange(change); |
| 187 | } |
| 188 | |
| 189 | /*! |
| 190 | \since QtQuick.Controls 2.4 (Qt 5.11) |
| 191 | \qmlproperty function QtQuick.Controls::CheckDelegate::nextCheckState |
| 192 | |
| 193 | This property holds a callback function that is called to determine |
| 194 | the next check state whenever the check delegate is interactively toggled |
| 195 | by the user via touch, mouse, or keyboard. |
| 196 | |
| 197 | By default, a normal check delegate cycles between \c Qt.Unchecked and |
| 198 | \c Qt.Checked states, and a tri-state check delegate cycles between |
| 199 | \c Qt.Unchecked, \c Qt.PartiallyChecked, and \c Qt.Checked states. |
| 200 | |
| 201 | The \c nextCheckState callback function can override the default behavior. |
| 202 | The following example implements a tri-state check delegate that can present |
| 203 | a partially checked state depending on external conditions, but never |
| 204 | cycles to the partially checked state when interactively toggled by |
| 205 | the user. |
| 206 | |
| 207 | \code |
| 208 | CheckDelegate { |
| 209 | tristate: true |
| 210 | checkState: allChildrenChecked ? Qt.Checked : |
| 211 | anyChildChecked ? Qt.PartiallyChecked : Qt.Unchecked |
| 212 | |
| 213 | nextCheckState: function() { |
| 214 | if (checkState === Qt.Checked) |
| 215 | return Qt.Unchecked |
| 216 | else |
| 217 | return Qt.Checked |
| 218 | } |
| 219 | } |
| 220 | \endcode |
| 221 | */ |
| 222 | void QQuickCheckDelegate::nextCheckState() |
| 223 | { |
| 224 | Q_D(QQuickCheckDelegate); |
| 225 | if (d->nextCheckState.isCallable()) |
| 226 | setCheckState(static_cast<Qt::CheckState>(d->nextCheckState.call().toInt())); |
| 227 | else if (d->tristate) |
| 228 | setCheckState(static_cast<Qt::CheckState>((d->checkState + 1) % 3)); |
| 229 | else |
| 230 | QQuickItemDelegate::nextCheckState(); |
| 231 | } |
| 232 | |
| 233 | #if QT_CONFIG(accessibility) |
| 234 | QAccessible::Role QQuickCheckDelegate::accessibleRole() const |
| 235 | { |
| 236 | return QAccessible::CheckBox; |
| 237 | } |
| 238 | #endif |
| 239 | |
| 240 | QT_END_NAMESPACE |
| 241 | |
| 242 | #include "moc_qquickcheckdelegate_p.cpp" |
| 243 | |