ctkCheckableHeaderViewTest1.cpp 11 KB

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