ctkCheckableHeaderViewTest1.cpp 11 KB

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