-----------------------------------------------------------------------------
| 159 | |
| 160 | //----------------------------------------------------------------------------- |
| 161 | void ctkCheckableModelHelperPrivate |
| 162 | ::updateCheckState(const QModelIndex& modelIndex) |
| 163 | { |
| 164 | Q_Q(ctkCheckableModelHelper); |
| 165 | bool checkable = false; |
| 166 | int oldCheckState = this->checkState(modelIndex, &checkable); |
| 167 | if (!checkable) |
| 168 | { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | Qt::CheckState newCheckState = Qt::PartiallyChecked; |
| 173 | bool firstCheckableChild = true; |
| 174 | const int rowCount = q->orientation() == Qt::Horizontal ? |
| 175 | q->model()->rowCount(modelIndex) : 1; |
| 176 | const int columnCount = q->orientation() == Qt::Vertical ? |
| 177 | q->model()->columnCount(modelIndex) : 1; |
| 178 | for (int r = 0; r < rowCount; ++r) |
| 179 | { |
| 180 | for (int c = 0; c < columnCount; ++c) |
| 181 | { |
| 182 | QModelIndex child = q->model()->index(r, c, modelIndex); |
| 183 | QVariant childCheckState = q->model()->data(child, Qt::CheckStateRole); |
| 184 | int childState = childCheckState.toInt(&checkable); |
| 185 | if (!checkable) |
| 186 | { |
| 187 | continue; |
| 188 | } |
| 189 | if (firstCheckableChild) |
| 190 | { |
| 191 | newCheckState = static_cast<Qt::CheckState>(childState); |
| 192 | firstCheckableChild = false; |
| 193 | } |
| 194 | if (newCheckState != childState) |
| 195 | { |
| 196 | newCheckState = Qt::PartiallyChecked; |
| 197 | } |
| 198 | if (newCheckState == Qt::PartiallyChecked) |
| 199 | { |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | if (!firstCheckableChild && newCheckState == Qt::PartiallyChecked) |
| 204 | { |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | if (oldCheckState == newCheckState) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | this->setCheckState(modelIndex, newCheckState); |
| 213 | if (modelIndex != q->rootIndex()) |
| 214 | { |
| 215 | this->updateCheckState(modelIndex.parent()); |
| 216 | } |
| 217 | } |
| 218 |
no test coverage detected