ctkCheckableModelHelper.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QAbstractItemModel>
  16. #include <QDebug>
  17. #include <QStandardItemModel>
  18. #include <QPointer>
  19. // CTK includes
  20. #include "ctkCheckableModelHelper.h"
  21. //-----------------------------------------------------------------------------
  22. class ctkCheckableModelHelperPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkCheckableModelHelper);
  25. protected:
  26. ctkCheckableModelHelper* const q_ptr;
  27. public:
  28. ctkCheckableModelHelperPrivate(ctkCheckableModelHelper& object);
  29. ~ctkCheckableModelHelperPrivate();
  30. void init();
  31. /// Set index checkstate and call propagate
  32. void setIndexCheckState(const QModelIndex& index, Qt::CheckState checkState);
  33. /// Return the depth in the model tree of the index.
  34. /// -1 if the index is the root element a header or a header, 0 if the index
  35. /// is a toplevel index, 1 if its parent is toplevel, 2 if its grandparent is
  36. /// toplevel, etc.
  37. int indexDepth(const QModelIndex& modelIndex)const;
  38. /// Set the checkstate of the index based on its children and grand children
  39. void updateCheckState(const QModelIndex& modelIndex);
  40. /// Set the check state of the index to all its children and grand children
  41. void propagateCheckStateToChildren(const QModelIndex& modelIndex);
  42. Qt::CheckState checkState(const QModelIndex& index, bool *checkable)const;
  43. void setCheckState(const QModelIndex& index, Qt::CheckState newCheckState);
  44. void forceCheckability(const QModelIndex& index);
  45. QPointer<QAbstractItemModel> Model;
  46. QModelIndex RootIndex;
  47. Qt::Orientation Orientation;
  48. bool HeaderIsUpdating;
  49. bool ItemsAreUpdating;
  50. bool ForceCheckability;
  51. /// 0 means no propagation
  52. /// -1 means unlimited propagation
  53. /// 1 means propagate to top-level indexes
  54. /// 2 means propagate to top-level and their children
  55. /// ...
  56. int PropagateDepth;
  57. Qt::CheckState DefaultCheckState;
  58. };
  59. //----------------------------------------------------------------------------
  60. ctkCheckableModelHelperPrivate::ctkCheckableModelHelperPrivate(ctkCheckableModelHelper& object)
  61. : q_ptr(&object)
  62. {
  63. this->HeaderIsUpdating = false;
  64. this->ItemsAreUpdating = false;
  65. this->ForceCheckability = false;
  66. this->PropagateDepth = -1;
  67. this->DefaultCheckState = Qt::Unchecked;
  68. }
  69. //-----------------------------------------------------------------------------
  70. ctkCheckableModelHelperPrivate::~ctkCheckableModelHelperPrivate()
  71. {
  72. }
  73. //----------------------------------------------------------------------------
  74. void ctkCheckableModelHelperPrivate::init()
  75. {
  76. }
  77. //----------------------------------------------------------------------------
  78. Qt::CheckState ctkCheckableModelHelperPrivate::checkState(
  79. const QModelIndex& index, bool *checkable)const
  80. {
  81. Q_Q(const ctkCheckableModelHelper);
  82. if (!q->model())
  83. {
  84. qWarning() << "Model has not been set.";
  85. return q->defaultCheckState();
  86. }
  87. QVariant indexCheckState = index != q->rootIndex() ?
  88. q->model()->data(index, Qt::CheckStateRole):
  89. q->model()->headerData(0, q->orientation(), Qt::CheckStateRole);
  90. return static_cast<Qt::CheckState>(indexCheckState.toInt(checkable));
  91. }
  92. //----------------------------------------------------------------------------
  93. void ctkCheckableModelHelperPrivate::setCheckState(
  94. const QModelIndex& modelIndex, Qt::CheckState newCheckState)
  95. {
  96. Q_Q(ctkCheckableModelHelper);
  97. if (!q->model())
  98. {
  99. qWarning() << "Model has not been set.";
  100. return;
  101. }
  102. else if (modelIndex != q->rootIndex())
  103. {
  104. q->model()->setData(modelIndex, static_cast<int>(newCheckState),
  105. Qt::CheckStateRole);
  106. }
  107. else
  108. {
  109. q->model()->setHeaderData(0, q->orientation(), static_cast<int>(newCheckState),
  110. Qt::CheckStateRole);
  111. }
  112. }
  113. //----------------------------------------------------------------------------
  114. void ctkCheckableModelHelperPrivate::setIndexCheckState(
  115. const QModelIndex& index, Qt::CheckState checkState)
  116. {
  117. bool checkable = false;
  118. this->checkState(index, &checkable);
  119. if (!checkable && !this->ForceCheckability)
  120. {
  121. // The index is not checkable and we don't want to force checkability
  122. return;
  123. }
  124. this->setCheckState(index, checkState);
  125. this->propagateCheckStateToChildren(index);
  126. }
  127. //-----------------------------------------------------------------------------
  128. int ctkCheckableModelHelperPrivate::indexDepth(const QModelIndex& modelIndex)const
  129. {
  130. int depth = -1;
  131. QModelIndex parentIndex = modelIndex;
  132. while (parentIndex.isValid())
  133. {
  134. ++depth;
  135. parentIndex = parentIndex.parent();
  136. }
  137. return depth;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void ctkCheckableModelHelperPrivate
  141. ::updateCheckState(const QModelIndex& modelIndex)
  142. {
  143. Q_Q(ctkCheckableModelHelper);
  144. bool checkable = false;
  145. int oldCheckState = this->checkState(modelIndex, &checkable);
  146. if (!checkable)
  147. {
  148. return;
  149. }
  150. Qt::CheckState newCheckState = Qt::PartiallyChecked;
  151. bool firstCheckableChild = true;
  152. const int rowCount = q->orientation() == Qt::Horizontal ?
  153. q->model()->rowCount(modelIndex) : 1;
  154. const int columnCount = q->orientation() == Qt::Vertical ?
  155. q->model()->columnCount(modelIndex) : 1;
  156. for (int r = 0; r < rowCount; ++r)
  157. {
  158. for (int c = 0; c < columnCount; ++c)
  159. {
  160. QModelIndex child = q->model()->index(r, c, modelIndex);
  161. QVariant childCheckState = q->model()->data(child, Qt::CheckStateRole);
  162. int childState = childCheckState.toInt(&checkable);
  163. if (!checkable)
  164. {
  165. continue;
  166. }
  167. if (firstCheckableChild)
  168. {
  169. newCheckState = static_cast<Qt::CheckState>(childState);
  170. firstCheckableChild = false;
  171. }
  172. if (newCheckState != childState)
  173. {
  174. newCheckState = Qt::PartiallyChecked;
  175. }
  176. if (newCheckState == Qt::PartiallyChecked)
  177. {
  178. break;
  179. }
  180. }
  181. if (!firstCheckableChild && newCheckState == Qt::PartiallyChecked)
  182. {
  183. break;
  184. }
  185. }
  186. if (oldCheckState == newCheckState)
  187. {
  188. return;
  189. }
  190. this->setCheckState(modelIndex, newCheckState);
  191. if (modelIndex != q->rootIndex())
  192. {
  193. this->updateCheckState(modelIndex.parent());
  194. }
  195. }
  196. //-----------------------------------------------------------------------------
  197. void ctkCheckableModelHelperPrivate
  198. ::propagateCheckStateToChildren(const QModelIndex& modelIndex)
  199. {
  200. Q_Q(ctkCheckableModelHelper);
  201. int indexDepth = this->indexDepth(modelIndex);
  202. if (this->PropagateDepth == 0 ||
  203. !(indexDepth < this->PropagateDepth || this->PropagateDepth == -1))
  204. {
  205. return;
  206. }
  207. bool checkable = false;
  208. Qt::CheckState checkState = this->checkState(modelIndex, &checkable);
  209. if (!checkable || checkState == Qt::PartiallyChecked)
  210. {
  211. return;
  212. }
  213. while (this->ForceCheckability && q->model()->canFetchMore(modelIndex))
  214. {
  215. q->model()->fetchMore(modelIndex);
  216. }
  217. const int rowCount = q->orientation() == Qt::Horizontal ?
  218. q->model()->rowCount(modelIndex) : 1;
  219. const int columnCount = q->orientation() == Qt::Vertical ?
  220. q->model()->columnCount(modelIndex) : 1;
  221. for (int r = 0; r < rowCount; ++r)
  222. {
  223. for (int c = 0; c < columnCount; ++c)
  224. {
  225. QModelIndex child = q->model()->index(r, c, modelIndex);
  226. this->setIndexCheckState(child, checkState);
  227. }
  228. }
  229. }
  230. //-----------------------------------------------------------------------------
  231. void ctkCheckableModelHelperPrivate
  232. ::forceCheckability(const QModelIndex& modelIndex)
  233. {
  234. Q_Q(ctkCheckableModelHelper);
  235. if (!this->ForceCheckability)
  236. {
  237. return;
  238. }
  239. this->setCheckState(modelIndex, this->DefaultCheckState);
  240. // Apparently (not sure) some views require the User-checkable
  241. // flag to be set to be able to show the checkboxes
  242. if (qobject_cast<QStandardItemModel*>(q->model()))
  243. {
  244. QStandardItem* item = modelIndex != q->rootIndex() ?
  245. qobject_cast<QStandardItemModel*>(q->model())->itemFromIndex(modelIndex) :
  246. (q->orientation() == Qt::Horizontal ?
  247. qobject_cast<QStandardItemModel*>(q->model())->horizontalHeaderItem(0) :
  248. qobject_cast<QStandardItemModel*>(q->model())->verticalHeaderItem(0));
  249. item->setCheckable(true);
  250. }
  251. }
  252. //----------------------------------------------------------------------------
  253. ctkCheckableModelHelper::ctkCheckableModelHelper(
  254. Qt::Orientation orient, QObject* objectParent)
  255. : QObject(objectParent)
  256. , d_ptr(new ctkCheckableModelHelperPrivate(*this))
  257. {
  258. Q_D(ctkCheckableModelHelper);
  259. d->Orientation = orient;
  260. d->init();
  261. }
  262. //-----------------------------------------------------------------------------
  263. ctkCheckableModelHelper::~ctkCheckableModelHelper()
  264. {
  265. }
  266. //-----------------------------------------------------------------------------
  267. Qt::Orientation ctkCheckableModelHelper::orientation()const
  268. {
  269. Q_D(const ctkCheckableModelHelper);
  270. return d->Orientation;
  271. }
  272. //-----------------------------------------------------------------------------
  273. QAbstractItemModel* ctkCheckableModelHelper::model()const
  274. {
  275. Q_D(const ctkCheckableModelHelper);
  276. return d->Model.isNull() ? 0 : d->Model.data();
  277. }
  278. //-----------------------------------------------------------------------------
  279. void ctkCheckableModelHelper::setModel(QAbstractItemModel *newModel)
  280. {
  281. Q_D(ctkCheckableModelHelper);
  282. QAbstractItemModel *current = this->model();
  283. if (current == newModel)
  284. {
  285. return;
  286. }
  287. if(current)
  288. {
  289. this->disconnect(
  290. current, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
  291. this, SLOT(onHeaderDataChanged(Qt::Orientation,int,int)));
  292. this->disconnect(
  293. current, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  294. this, SLOT(onDataChanged(QModelIndex,QModelIndex)));
  295. this->disconnect(
  296. current, SIGNAL(columnsInserted(QModelIndex,int,int)),
  297. this, SLOT(onColumnsInserted(QModelIndex,int,int)));
  298. this->disconnect(
  299. current, SIGNAL(rowsInserted(QModelIndex,int,int)),
  300. this, SLOT(onRowsInserted(QModelIndex,int,int)));
  301. }
  302. d->Model = newModel;
  303. if(newModel)
  304. {
  305. this->connect(
  306. newModel, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
  307. this, SLOT(onHeaderDataChanged(Qt::Orientation,int,int)));
  308. if (d->PropagateDepth != 0)
  309. {
  310. this->connect(
  311. newModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  312. this, SLOT(onDataChanged(QModelIndex,QModelIndex)));
  313. }
  314. this->connect(
  315. newModel, SIGNAL(columnsInserted(QModelIndex,int,int)),
  316. this, SLOT(onColumnsInserted(QModelIndex,int,int)));
  317. this->connect(
  318. newModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
  319. this, SLOT(onRowsInserted(QModelIndex,int,int)));
  320. if (d->ForceCheckability)
  321. {
  322. foreach(QModelIndex index, newModel->match(newModel->index(0,0), Qt::CheckStateRole, QVariant(), -1,Qt::MatchRecursive))
  323. {
  324. d->forceCheckability(index);
  325. }
  326. d->forceCheckability(this->rootIndex());
  327. }
  328. this->updateHeadersFromItems();
  329. }
  330. }
  331. //-----------------------------------------------------------------------------
  332. QModelIndex ctkCheckableModelHelper::rootIndex()const
  333. {
  334. Q_D(const ctkCheckableModelHelper);
  335. return d->RootIndex;
  336. }
  337. //-----------------------------------------------------------------------------
  338. void ctkCheckableModelHelper::setRootIndex(const QModelIndex &index)
  339. {
  340. Q_D(ctkCheckableModelHelper);
  341. d->RootIndex = index;
  342. if (d->PropagateDepth != 0)
  343. {
  344. this->updateHeadersFromItems();
  345. }
  346. }
  347. //-----------------------------------------------------------------------------
  348. void ctkCheckableModelHelper::setPropagateDepth(int depth)
  349. {
  350. Q_D(ctkCheckableModelHelper);
  351. if (d->PropagateDepth == depth)
  352. {
  353. return;
  354. }
  355. d->PropagateDepth = depth;
  356. if (!this->model())
  357. {
  358. return;
  359. }
  360. if (depth != 0)
  361. {
  362. this->connect(
  363. this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  364. this, SLOT(onDataChanged(QModelIndex,QModelIndex)), Qt::UniqueConnection);
  365. this->updateHeadersFromItems();
  366. }
  367. else
  368. {
  369. this->disconnect(
  370. this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  371. this, SLOT(onDataChanged(QModelIndex,QModelIndex)));
  372. }
  373. }
  374. //-----------------------------------------------------------------------------
  375. int ctkCheckableModelHelper::propagateDepth()const
  376. {
  377. Q_D(const ctkCheckableModelHelper);
  378. return d->PropagateDepth;
  379. }
  380. //-----------------------------------------------------------------------------
  381. void ctkCheckableModelHelper::setForceCheckability(bool force)
  382. {
  383. Q_D(ctkCheckableModelHelper);
  384. if (d->ForceCheckability == force)
  385. {
  386. return;
  387. }
  388. d->ForceCheckability = force;
  389. if (this->model())
  390. {
  391. d->propagateCheckStateToChildren(this->rootIndex());
  392. }
  393. }
  394. //-----------------------------------------------------------------------------
  395. bool ctkCheckableModelHelper::forceCheckability()const
  396. {
  397. Q_D(const ctkCheckableModelHelper);
  398. return d->ForceCheckability;
  399. }
  400. //-----------------------------------------------------------------------------
  401. void ctkCheckableModelHelper::setDefaultCheckState(Qt::CheckState defaultCheckState)
  402. {
  403. Q_D(ctkCheckableModelHelper);
  404. d->DefaultCheckState = defaultCheckState;
  405. }
  406. //-----------------------------------------------------------------------------
  407. Qt::CheckState ctkCheckableModelHelper::defaultCheckState()const
  408. {
  409. Q_D(const ctkCheckableModelHelper);
  410. return d->DefaultCheckState;
  411. }
  412. //-----------------------------------------------------------------------------
  413. void ctkCheckableModelHelper::setHeaderCheckState(int section, Qt::CheckState checkState)
  414. {
  415. QAbstractItemModel *current = this->model();
  416. if(current == 0)
  417. {
  418. return;
  419. }
  420. current->setHeaderData(section, this->orientation(),
  421. static_cast<int>(checkState), Qt::CheckStateRole);
  422. }
  423. //-----------------------------------------------------------------------------
  424. void ctkCheckableModelHelper::setCheckState(const QModelIndex& index, Qt::CheckState checkState)
  425. {
  426. QAbstractItemModel *current = this->model();
  427. if(current == 0)
  428. {
  429. return;
  430. }
  431. current->setData(index, static_cast<int>(checkState), Qt::CheckStateRole);
  432. }
  433. //-----------------------------------------------------------------------------
  434. void ctkCheckableModelHelper::toggleCheckState(const QModelIndex& modelIndex)
  435. {
  436. // If the section is checkable, toggle the check state.
  437. if(!this->isCheckable(modelIndex))
  438. {
  439. return;
  440. }
  441. // I've no strong feeling to turn the state checked or unchecked when the
  442. // state is PartiallyChecked.
  443. this->setCheckState(modelIndex,
  444. this->checkState(modelIndex) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
  445. }
  446. //-----------------------------------------------------------------------------
  447. void ctkCheckableModelHelper::toggleHeaderCheckState(int section)
  448. {
  449. // If the section is checkable, toggle the check state.
  450. if(!this->isHeaderCheckable(section))
  451. {
  452. return;
  453. }
  454. // I've no strong feeling to turn the state checked or unchecked when the
  455. // state is PartiallyChecked.
  456. this->setHeaderCheckState(
  457. section, this->headerCheckState(section) == Qt::Checked ?
  458. Qt::Unchecked : Qt::Checked);
  459. }
  460. //-----------------------------------------------------------------------------
  461. void ctkCheckableModelHelper::onHeaderDataChanged(Qt::Orientation orient,
  462. int firstSection,
  463. int lastSection)
  464. {
  465. Q_D(ctkCheckableModelHelper);
  466. Q_UNUSED(firstSection);
  467. Q_UNUSED(lastSection);
  468. if(orient != this->orientation())
  469. {
  470. return;
  471. }
  472. bool oldItemsAreUpdating = d->ItemsAreUpdating;
  473. if (!d->ItemsAreUpdating)
  474. {
  475. d->ItemsAreUpdating = true;
  476. d->propagateCheckStateToChildren(this->rootIndex());
  477. }
  478. d->ItemsAreUpdating = oldItemsAreUpdating;
  479. }
  480. //-----------------------------------------------------------------------------
  481. void ctkCheckableModelHelper::updateHeadersFromItems()
  482. {
  483. Q_D(ctkCheckableModelHelper);
  484. QAbstractItemModel *currentModel = this->model();
  485. if (!currentModel)
  486. {
  487. return;
  488. }
  489. d->updateCheckState(QModelIndex());
  490. }
  491. //-----------------------------------------------------------------------------
  492. void ctkCheckableModelHelper::onDataChanged(const QModelIndex & topLeft,
  493. const QModelIndex & bottomRight)
  494. {
  495. Q_UNUSED(bottomRight);
  496. Q_D(ctkCheckableModelHelper);
  497. if(d->ItemsAreUpdating || d->PropagateDepth == 0)
  498. {
  499. return;
  500. }
  501. bool checkable = false;
  502. d->checkState(topLeft, &checkable);
  503. if (!checkable)
  504. {
  505. return;
  506. }
  507. d->ItemsAreUpdating = true;
  508. // TODO: handle topLeft "TO bottomRight"
  509. d->propagateCheckStateToChildren(topLeft);
  510. d->updateCheckState(topLeft.parent());
  511. d->ItemsAreUpdating = false;
  512. }
  513. //-----------------------------------------------------------------------------
  514. void ctkCheckableModelHelper::onColumnsInserted(const QModelIndex &parentIndex,
  515. int start, int end)
  516. {
  517. Q_D(ctkCheckableModelHelper);
  518. if (this->orientation() == Qt::Horizontal)
  519. {
  520. if (start == 0)
  521. {
  522. this->updateHeadersFromItems();
  523. }
  524. }
  525. else
  526. {
  527. if (d->ForceCheckability)
  528. {
  529. for (int i = start; i <= end; ++i)
  530. {
  531. QModelIndex index = this->model()->index(0, i, parentIndex);
  532. d->forceCheckability(index);
  533. }
  534. }
  535. this->onDataChanged(this->model()->index(0, start, parentIndex),
  536. this->model()->index(0, end, parentIndex));
  537. }
  538. }
  539. //-----------------------------------------------------------------------------
  540. void ctkCheckableModelHelper::onRowsInserted(const QModelIndex &parentIndex,
  541. int start, int end)
  542. {
  543. Q_D(ctkCheckableModelHelper);
  544. if (this->orientation() == Qt::Vertical)
  545. {
  546. if (start == 0)
  547. {
  548. this->updateHeadersFromItems();
  549. }
  550. }
  551. else
  552. {
  553. if (d->ForceCheckability)
  554. {
  555. for (int i = start; i <= end; ++i)
  556. {
  557. QModelIndex index = this->model()->index(i, 0, parentIndex);
  558. d->forceCheckability(index);
  559. }
  560. }
  561. this->onDataChanged(this->model()->index(start, 0, parentIndex),
  562. this->model()->index(end, 0, parentIndex));
  563. }
  564. }
  565. //-----------------------------------------------------------------------------
  566. bool ctkCheckableModelHelper::isHeaderCheckable(int section)const
  567. {
  568. if (!this->model())
  569. {
  570. qWarning() << "ctkCheckableModelHelper::isHeaderCheckable : Model has not been set";
  571. return (this->forceCheckability() && section == 0);
  572. }
  573. return !this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).isNull();
  574. }
  575. //-----------------------------------------------------------------------------
  576. bool ctkCheckableModelHelper::isCheckable(const QModelIndex& index)const
  577. {
  578. if (!this->model())
  579. {
  580. qWarning() << "ctkCheckableModelHelper::isCheckable : Model has not been set";
  581. return (this->forceCheckability() && index.column() == 0);
  582. }
  583. return !this->model()->data(index, Qt::CheckStateRole).isNull();
  584. }
  585. //-----------------------------------------------------------------------------
  586. Qt::CheckState ctkCheckableModelHelper::headerCheckState(int section)const
  587. {
  588. if (!this->model())
  589. {
  590. qWarning() << "ctkCheckableModelHelper::headerCheckState : Model has not been set";
  591. return this->defaultCheckState();
  592. }
  593. return static_cast<Qt::CheckState>(
  594. this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt());
  595. }
  596. //-----------------------------------------------------------------------------
  597. Qt::CheckState ctkCheckableModelHelper::checkState(const QModelIndex& index)const
  598. {
  599. if (!this->model())
  600. {
  601. qWarning() << "ctkCheckableModelHelper::checkState : Model has not been set";
  602. return this->defaultCheckState();
  603. }
  604. return static_cast<Qt::CheckState>(
  605. this->model()->data(index, Qt::CheckStateRole).toInt());
  606. }
  607. //-----------------------------------------------------------------------------
  608. bool ctkCheckableModelHelper::headerCheckState(int section, Qt::CheckState& checkState)const
  609. {
  610. bool checkable = false;
  611. if (!this->model())
  612. {
  613. qWarning() << "ctkCheckableModelHelper::headerCheckState : Model has not been set";
  614. return (this->forceCheckability() && section == 0);
  615. }
  616. checkState = static_cast<Qt::CheckState>(
  617. this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt(&checkable));
  618. return checkable;
  619. }
  620. //-----------------------------------------------------------------------------
  621. bool ctkCheckableModelHelper::checkState(const QModelIndex& index, Qt::CheckState& checkState)const
  622. {
  623. bool checkable = false;
  624. if (!this->model())
  625. {
  626. qWarning() << "ctkCheckableModelHelper::checkState : Model has not been set";
  627. return (this->forceCheckability() && index.column() == 0);
  628. }
  629. checkState = static_cast<Qt::CheckState>(
  630. this->model()->data(index, Qt::CheckStateRole).toInt(&checkable));
  631. return checkable;
  632. }