ctkCheckableModelHelperTest1.cpp 14 KB

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