ctkCheckableModelHelper.cpp 21 KB

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