ctkCheckableModelHelperTest1.cpp 9.9 KB

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