ctkCheckableModelHelperTest1.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.commontk.org/LICENSE
  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 <QDebug>
  16. #include <QApplication>
  17. #include <QFocusEvent>
  18. #include <QTableView>
  19. #include <QStandardItem>
  20. #include <QStandardItemModel>
  21. #include <QTimer>
  22. // CTK includes
  23. #include "ctkCheckableModelHelper.h"
  24. // STD includes
  25. #include <cstdlib>
  26. #include <iostream>
  27. //-----------------------------------------------------------------------------
  28. int ctkCheckableModelHelperTest1(int argc, char * argv [] )
  29. {
  30. QApplication app(argc, argv);
  31. QStandardItemModel model;
  32. QList<QStandardItem*> row0;
  33. row0 << new QStandardItem << new QStandardItem << new QStandardItem;
  34. row0[0]->setText("not user checkable");
  35. model.appendRow(row0);
  36. QList<QStandardItem*> row1;
  37. row1 << new QStandardItem << new QStandardItem << new QStandardItem;
  38. row1[0]->setCheckable(true);
  39. row1[0]->setText("checkable");
  40. model.appendRow(row1);
  41. QList<QStandardItem*> row2;
  42. row2 << new QStandardItem << new QStandardItem << new QStandardItem;
  43. row2[0]->setCheckable(true);
  44. row2[0]->setText("checkable");
  45. model.appendRow(row2);
  46. // items are unchecked by default
  47. if (row0[0]->checkState() != Qt::Unchecked ||
  48. row1[0]->checkState() != Qt::Unchecked ||
  49. row2[0]->checkState() != Qt::Unchecked)
  50. {
  51. std::cerr << "QStandardItem default failed: "
  52. << static_cast<int>(row0[0]->checkState()) << " "
  53. << static_cast<int>(row1[0]->checkState()) << " "
  54. << static_cast<int>(row2[0]->checkState()) << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. // Header is checked by default
  58. model.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole);
  59. ctkCheckableModelHelper* modelHelper =
  60. new ctkCheckableModelHelper(Qt::Horizontal);
  61. modelHelper->setModel(&model);
  62. // propagatetoitems is true by default
  63. //modelHelper->setPropagateToItems(true);
  64. // As propagateToItems is true, once the model is set to the modelHelper,
  65. // the checkable header is updated from the check state of all the items
  66. // all the items are unchecked by default, so the header becomes unchecked
  67. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  68. row0[0]->checkState() != Qt::Unchecked ||
  69. row1[0]->checkState() != Qt::Unchecked ||
  70. row2[0]->checkState() != Qt::Unchecked)
  71. {
  72. std::cerr << "ctkCheckableModelHelper::checkstate() failed: "
  73. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  74. << static_cast<int>(row0[0]->checkState()) << " "
  75. << static_cast<int>(row1[0]->checkState()) << " "
  76. << static_cast<int>(row2[0]->checkState()) << std::endl;
  77. return EXIT_FAILURE;
  78. }
  79. // Retrieve checkstate of the header
  80. Qt::CheckState checkstate;
  81. if (!modelHelper->headerCheckState(0, checkstate))
  82. {
  83. std::cerr << "ctkCheckableModelHelper::checkstate() failed: "
  84. << static_cast<int>(checkstate) << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. if (modelHelper->propagateDepth() == 0)
  88. {
  89. std::cerr << "ctkCheckableModelHelper::propagateDepth() failed: "
  90. << modelHelper->propagateDepth() << std::endl;
  91. return EXIT_FAILURE;
  92. }
  93. modelHelper->setPropagateDepth(0);
  94. if (modelHelper->propagateDepth() != 0)
  95. {
  96. std::cerr << "ctkCheckableModelHelper::propagateDepth() failed: "
  97. << modelHelper->propagateDepth() << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  101. row0[0]->checkState() != Qt::Unchecked ||
  102. row1[0]->checkState() != Qt::Unchecked ||
  103. row2[0]->checkState() != Qt::Unchecked)
  104. {
  105. std::cerr << "ctkCheckableModelHelper::propagateToItems() failed: "
  106. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  107. << static_cast<int>(row0[0]->checkState()) << " "
  108. << static_cast<int>(row1[0]->checkState()) << " "
  109. << static_cast<int>(row2[0]->checkState()) << std::endl;
  110. return EXIT_FAILURE;
  111. }
  112. // check the header
  113. modelHelper->setHeaderCheckState(0, Qt::Checked);
  114. // make sure it didn't uncheck the checkable items
  115. if (modelHelper->headerCheckState(0) != Qt::Checked ||
  116. row0[0]->checkState() != Qt::Unchecked ||
  117. row1[0]->checkState() != Qt::Unchecked ||
  118. row2[0]->checkState() != Qt::Unchecked)
  119. {
  120. std::cerr << __LINE__ << " ctkCheckableModelHelper::toggleCheckState() failed: "
  121. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  122. << static_cast<int>(row0[0]->checkState()) << " "
  123. << static_cast<int>(row1[0]->checkState()) << " "
  124. << static_cast<int>(row2[0]->checkState()) << std::endl;
  125. return EXIT_FAILURE;
  126. }
  127. row0[0]->setCheckState(Qt::Checked);
  128. // make sure it didn't uncheck the checkable items
  129. if (modelHelper->headerCheckState(0) != Qt::Checked ||
  130. row0[0]->checkState() != Qt::Checked ||
  131. row1[0]->checkState() != Qt::Unchecked ||
  132. row2[0]->checkState() != Qt::Unchecked)
  133. {
  134. std::cerr << "QStandardItem::setCheckState() failed: "
  135. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  136. << static_cast<int>(row0[0]->checkState()) << " "
  137. << static_cast<int>(row1[0]->checkState()) << " "
  138. << static_cast<int>(row2[0]->checkState()) << std::endl;
  139. return EXIT_FAILURE;
  140. }
  141. // The checkable header gets updated with the item check states
  142. modelHelper->setPropagateDepth(-1);
  143. if (modelHelper->propagateDepth() == 0 ||
  144. modelHelper->headerCheckState(0) != Qt::PartiallyChecked ||
  145. row0[0]->checkState() != Qt::Checked ||
  146. row1[0]->checkState() != Qt::Unchecked ||
  147. row2[0]->checkState() != Qt::Unchecked)
  148. {
  149. std::cerr << "ctkCheckableModelHelper::setPropagateToItems() failed: "
  150. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  151. << static_cast<int>(row0[0]->checkState()) << " "
  152. << static_cast<int>(row1[0]->checkState()) << " "
  153. << static_cast<int>(row2[0]->checkState()) << std::endl;
  154. return EXIT_FAILURE;
  155. }
  156. row0[0]->setCheckState(Qt::Unchecked);
  157. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  158. row0[0]->checkState() != Qt::Unchecked ||
  159. row1[0]->checkState() != Qt::Unchecked ||
  160. row2[0]->checkState() != Qt::Unchecked)
  161. {
  162. std::cerr << "QStandardItem::setCheckState() failed: "
  163. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  164. << static_cast<int>(row0[0]->checkState()) << " "
  165. << static_cast<int>(row1[0]->checkState()) << " "
  166. << static_cast<int>(row2[0]->checkState()) << std::endl;
  167. return EXIT_FAILURE;
  168. }
  169. row1[0]->setCheckState(Qt::Checked);
  170. // make sure it didn't uncheck the checkable items
  171. if (modelHelper->headerCheckState(0) != Qt::PartiallyChecked ||
  172. row0[0]->checkState() != Qt::Unchecked ||
  173. row1[0]->checkState() != Qt::Checked ||
  174. row2[0]->checkState() != Qt::Unchecked)
  175. {
  176. std::cerr << "QStandardItem::setCheckState() failed: "
  177. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  178. << static_cast<int>(row0[0]->checkState()) << " "
  179. << static_cast<int>(row1[0]->checkState()) << " "
  180. << static_cast<int>(row2[0]->checkState()) << std::endl;
  181. return EXIT_FAILURE;
  182. }
  183. row1[0]->setCheckState(Qt::Checked);
  184. // make sure it didn't check the checkable items
  185. if (modelHelper->headerCheckState(0) != Qt::PartiallyChecked ||
  186. row0[0]->checkState() != Qt::Unchecked ||
  187. row1[0]->checkState() != Qt::Checked ||
  188. row2[0]->checkState() != Qt::Unchecked)
  189. {
  190. std::cerr << "QStandardItem::setCheckState() failed: "
  191. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  192. << static_cast<int>(row0[0]->checkState()) << " "
  193. << static_cast<int>(row1[0]->checkState()) << " "
  194. << static_cast<int>(row2[0]->checkState()) << std::endl;
  195. return EXIT_FAILURE;
  196. }
  197. row0[0]->setCheckState(Qt::Checked);
  198. row2[0]->setCheckState(Qt::Checked);
  199. // make sure the header is now checked
  200. if (modelHelper->headerCheckState(0) != Qt::Checked ||
  201. row0[0]->checkState() != Qt::Checked ||
  202. row1[0]->checkState() != Qt::Checked ||
  203. row2[0]->checkState() != Qt::Checked)
  204. {
  205. std::cerr << "QStandardItem::setCheckState() failed: "
  206. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  207. << static_cast<int>(row0[0]->checkState()) << " "
  208. << static_cast<int>(row1[0]->checkState()) << " "
  209. << static_cast<int>(row2[0]->checkState()) << std::endl;
  210. return EXIT_FAILURE;
  211. }
  212. modelHelper->setHeaderCheckState(0, Qt::Unchecked);
  213. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  214. row0[0]->checkState() != Qt::Unchecked ||
  215. row1[0]->checkState() != Qt::Unchecked ||
  216. row2[0]->checkState() != Qt::Unchecked)
  217. {
  218. std::cerr << "ctkCheckableModelHelper::setCheckState() failed: "
  219. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  220. << static_cast<int>(row0[0]->checkState()) << " "
  221. << static_cast<int>(row1[0]->checkState()) << " "
  222. << static_cast<int>(row2[0]->checkState()) << std::endl;
  223. return EXIT_FAILURE;
  224. }
  225. return EXIT_SUCCESS;
  226. }