ctkCheckableModelHelperTest1.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // CheckForce & Default model
  57. QStandardItemModel modelForce;
  58. modelForce.appendRow(row0);
  59. QModelIndex modelIndex;
  60. ctkCheckableModelHelper* modelHelperCF =
  61. new ctkCheckableModelHelper(Qt::Horizontal);
  62. modelHelperCF->setForceCheckability(true);
  63. if (!modelHelperCF->forceCheckability())
  64. {
  65. std::cerr << "ctkCheckableModelHelper::setForceCheckability() failed: "
  66. << static_cast<int>(modelHelperCF->forceCheckability()) << std::endl;
  67. return EXIT_FAILURE;
  68. }
  69. modelHelperCF->isCheckable(modelIndex);
  70. modelHelperCF->toggleCheckState(modelIndex);
  71. modelHelperCF->setForceCheckability(false);
  72. if (modelHelperCF->forceCheckability())
  73. {
  74. std::cerr << "ctkCheckableModelHelper::setForceCheckability() failed: "
  75. << static_cast<int>(modelHelperCF->forceCheckability()) << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. modelHelperCF->setCheckState(modelIndex, Qt::Unchecked);
  79. modelHelperCF->setDefaultCheckState(Qt::Checked);
  80. if (modelHelperCF->defaultCheckState() != Qt::Checked)
  81. {
  82. std::cerr << "ctkCheckableModelHelper::setDefaultCheckState() failed: "
  83. << static_cast<int>(modelHelperCF->defaultCheckState()) << std::endl;
  84. return EXIT_FAILURE;
  85. }
  86. modelHelperCF->setRootIndex(modelIndex);
  87. modelHelperCF->setForceCheckability(true);
  88. modelHelperCF->setCheckState(modelIndex, Qt::Checked);
  89. Qt::CheckState statutCheck = Qt::Checked;
  90. if (modelHelperCF->checkState(modelIndex, statutCheck))
  91. {
  92. std::cerr << "ctkCheckableModelHelper::setCheckState() failed: "
  93. << static_cast<int>(modelHelperCF->checkState(modelIndex, statutCheck)) << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. modelHelperCF->checkState(modelIndex);
  97. modelHelperCF->setModel(&modelForce);
  98. if (!modelHelperCF->model())
  99. {
  100. std::cerr << "ctkCheckableModelHelper::setModel() failed: "
  101. << "is null" << std::endl;
  102. return EXIT_FAILURE;
  103. }
  104. modelHelperCF->setCheckState(modelIndex, Qt::Checked);
  105. if (modelHelperCF->checkState(modelIndex, statutCheck))
  106. {
  107. std::cerr << "ctkCheckableModelHelper::setCheckState() failed: "
  108. << static_cast<int>(modelHelperCF->checkState(modelIndex, statutCheck)) << std::endl;
  109. return EXIT_FAILURE;
  110. }
  111. // Row & Column dummy insert
  112. QList<QStandardItem*> col0;
  113. col0 << new QStandardItem << new QStandardItem << new QStandardItem;
  114. col0[0]->setText("not user checkable");
  115. modelForce.appendColumn(col0);
  116. modelForce.appendRow(row1);
  117. delete modelHelperCF;
  118. ctkCheckableModelHelper* modelHelperRC =
  119. new ctkCheckableModelHelper(Qt::Vertical);
  120. modelHelperRC->setForceCheckability(true);
  121. modelHelperRC->setModel(&modelForce);
  122. modelForce.appendColumn(col0);
  123. modelForce.appendRow(row1);
  124. //Header is checked by default
  125. model.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole);
  126. ctkCheckableModelHelper* modelHelper =
  127. new ctkCheckableModelHelper(Qt::Horizontal);
  128. modelHelper->setModel(&model);
  129. // propagatetoitems is true by default
  130. //modelHelper->setPropagateToItems(true);
  131. modelHelper->toggleHeaderCheckState(-1);
  132. // As propagateToItems is true, once the model is set to the modelHelper,
  133. // the checkable header is updated from the check state of all the items
  134. // all the items are unchecked by default, so the header becomes unchecked
  135. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  136. row0[0]->checkState() != Qt::Unchecked ||
  137. row1[0]->checkState() != Qt::Unchecked ||
  138. row2[0]->checkState() != Qt::Unchecked)
  139. {
  140. std::cerr << "ctkCheckableModelHelper::checkstate() failed: "
  141. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  142. << static_cast<int>(row0[0]->checkState()) << " "
  143. << static_cast<int>(row1[0]->checkState()) << " "
  144. << static_cast<int>(row2[0]->checkState()) << std::endl;
  145. return EXIT_FAILURE;
  146. }
  147. // Retrieve checkstate of the header
  148. Qt::CheckState checkstate;
  149. if (!modelHelper->headerCheckState(0, checkstate))
  150. {
  151. std::cerr << "ctkCheckableModelHelper::checkstate() failed: "
  152. << static_cast<int>(checkstate) << std::endl;
  153. return EXIT_FAILURE;
  154. }
  155. if (modelHelper->propagateDepth() == 0)
  156. {
  157. std::cerr << "ctkCheckableModelHelper::propagateDepth() failed: "
  158. << modelHelper->propagateDepth() << std::endl;
  159. return EXIT_FAILURE;
  160. }
  161. modelHelper->setPropagateDepth(0);
  162. if (modelHelper->propagateDepth() != 0)
  163. {
  164. std::cerr << "ctkCheckableModelHelper::propagateDepth() failed: "
  165. << modelHelper->propagateDepth() << std::endl;
  166. return EXIT_FAILURE;
  167. }
  168. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  169. row0[0]->checkState() != Qt::Unchecked ||
  170. row1[0]->checkState() != Qt::Unchecked ||
  171. row2[0]->checkState() != Qt::Unchecked)
  172. {
  173. std::cerr << "ctkCheckableModelHelper::propagateToItems() failed: "
  174. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  175. << static_cast<int>(row0[0]->checkState()) << " "
  176. << static_cast<int>(row1[0]->checkState()) << " "
  177. << static_cast<int>(row2[0]->checkState()) << std::endl;
  178. return EXIT_FAILURE;
  179. }
  180. // check the header
  181. modelHelper->setHeaderCheckState(0, Qt::Checked);
  182. // make sure it didn't uncheck the checkable items
  183. if (modelHelper->headerCheckState(0) != Qt::Checked ||
  184. row0[0]->checkState() != Qt::Unchecked ||
  185. row1[0]->checkState() != Qt::Unchecked ||
  186. row2[0]->checkState() != Qt::Unchecked)
  187. {
  188. std::cerr << __LINE__ << " ctkCheckableModelHelper::toggleCheckState() failed: "
  189. << static_cast<int>(modelHelper->headerCheckState(0))
  190. << " " << static_cast<int>(row0[0]->checkState()) << " "
  191. << static_cast<int>(row1[0]->checkState()) << " "
  192. << static_cast<int>(row2[0]->checkState()) << std::endl;
  193. return EXIT_FAILURE;
  194. }
  195. row0[0]->setCheckState(Qt::Checked);
  196. // make sure it didn't uncheck the checkable items
  197. if (modelHelper->headerCheckState(0) != Qt::Checked ||
  198. row0[0]->checkState() != Qt::Checked ||
  199. row1[0]->checkState() != Qt::Unchecked ||
  200. row2[0]->checkState() != Qt::Unchecked)
  201. {
  202. std::cerr << "QStandardItem::setCheckState() failed: "
  203. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  204. << static_cast<int>(row0[0]->checkState()) << " "
  205. << static_cast<int>(row1[0]->checkState()) << " "
  206. << static_cast<int>(row2[0]->checkState()) << std::endl;
  207. return EXIT_FAILURE;
  208. }
  209. // The checkable header gets updated with the item check states
  210. modelHelper->setPropagateDepth(-1);
  211. if (modelHelper->propagateDepth() == 0 ||
  212. modelHelper->headerCheckState(0) != Qt::PartiallyChecked ||
  213. row0[0]->checkState() != Qt::Checked ||
  214. row1[0]->checkState() != Qt::Unchecked ||
  215. row2[0]->checkState() != Qt::Unchecked)
  216. {
  217. std::cerr << "ctkCheckableModelHelper::setPropagateToItems() 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. row0[0]->setCheckState(Qt::Unchecked);
  225. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  226. row0[0]->checkState() != Qt::Unchecked ||
  227. row1[0]->checkState() != Qt::Unchecked ||
  228. row2[0]->checkState() != Qt::Unchecked)
  229. {
  230. std::cerr << "QStandardItem::setCheckState() failed: "
  231. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  232. << static_cast<int>(row0[0]->checkState()) << " "
  233. << static_cast<int>(row1[0]->checkState()) << " "
  234. << static_cast<int>(row2[0]->checkState()) << std::endl;
  235. return EXIT_FAILURE;
  236. }
  237. row1[0]->setCheckState(Qt::Checked);
  238. // make sure it didn't uncheck the checkable items
  239. if (modelHelper->headerCheckState(0) != Qt::PartiallyChecked ||
  240. row0[0]->checkState() != Qt::Unchecked ||
  241. row1[0]->checkState() != Qt::Checked ||
  242. row2[0]->checkState() != Qt::Unchecked)
  243. {
  244. std::cerr << "QStandardItem::setCheckState() failed: "
  245. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  246. << static_cast<int>(row0[0]->checkState()) << " "
  247. << static_cast<int>(row1[0]->checkState()) << " "
  248. << static_cast<int>(row2[0]->checkState()) << std::endl;
  249. return EXIT_FAILURE;
  250. }
  251. row1[0]->setCheckState(Qt::Checked);
  252. // make sure it didn't check the checkable items
  253. if (modelHelper->headerCheckState(0) != Qt::PartiallyChecked ||
  254. row0[0]->checkState() != Qt::Unchecked ||
  255. row1[0]->checkState() != Qt::Checked ||
  256. row2[0]->checkState() != Qt::Unchecked)
  257. {
  258. std::cerr << "QStandardItem::setCheckState() failed: "
  259. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  260. << static_cast<int>(row0[0]->checkState()) << " "
  261. << static_cast<int>(row1[0]->checkState()) << " "
  262. << static_cast<int>(row2[0]->checkState()) << std::endl;
  263. return EXIT_FAILURE;
  264. }
  265. row0[0]->setCheckState(Qt::Checked);
  266. row2[0]->setCheckState(Qt::Checked);
  267. // make sure the header is now checked
  268. if (modelHelper->headerCheckState(0) != Qt::Checked ||
  269. row0[0]->checkState() != Qt::Checked ||
  270. row1[0]->checkState() != Qt::Checked ||
  271. row2[0]->checkState() != Qt::Checked)
  272. {
  273. std::cerr << "QStandardItem::setCheckState() failed: "
  274. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  275. << static_cast<int>(row0[0]->checkState()) << " "
  276. << static_cast<int>(row1[0]->checkState()) << " "
  277. << static_cast<int>(row2[0]->checkState()) << std::endl;
  278. return EXIT_FAILURE;
  279. }
  280. modelHelper->setHeaderCheckState(0, Qt::Unchecked);
  281. if (modelHelper->headerCheckState(0) != Qt::Unchecked ||
  282. row0[0]->checkState() != Qt::Unchecked ||
  283. row1[0]->checkState() != Qt::Unchecked ||
  284. row2[0]->checkState() != Qt::Unchecked)
  285. {
  286. std::cerr << "ctkCheckableModelHelper::setCheckState() failed: "
  287. << static_cast<int>(modelHelper->headerCheckState(0)) << " "
  288. << static_cast<int>(row0[0]->checkState()) << " "
  289. << static_cast<int>(row1[0]->checkState()) << " "
  290. << static_cast<int>(row2[0]->checkState()) << std::endl;
  291. return EXIT_FAILURE;
  292. }
  293. delete modelHelper;
  294. return EXIT_SUCCESS;
  295. }