ctkCheckableHeaderViewTest1.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #if 0
  33. QFileSystemModel model;
  34. model.setRootPath(QDir::currentPath());
  35. #else
  36. QStandardItemModel model;
  37. QList<QStandardItem*> row0;
  38. row0 << new QStandardItem << new QStandardItem << new QStandardItem;
  39. row0[0]->setText("not user checkable");
  40. model.appendRow(row0);
  41. QList<QStandardItem*> row1;
  42. row1 << new QStandardItem << new QStandardItem << new QStandardItem;
  43. row1[0]->setCheckable(true);
  44. row1[0]->setText("checkable");
  45. model.appendRow(row1);
  46. QList<QStandardItem*> row2;
  47. row2 << new QStandardItem << new QStandardItem << new QStandardItem;
  48. row2[0]->setCheckable(true);
  49. row2[0]->setText("checkable");
  50. model.appendRow(row2);
  51. #endif
  52. QTableView table;
  53. table.setModel(&model);
  54. model.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole);
  55. QHeaderView* previousHeaderView = table.horizontalHeader();
  56. bool oldClickable = previousHeaderView->isClickable();
  57. ctkCheckableHeaderView* headerView = new ctkCheckableHeaderView(Qt::Horizontal, &table);
  58. headerView->setClickable(oldClickable);
  59. headerView->setMovable(previousHeaderView->isMovable());
  60. headerView->setHighlightSections(previousHeaderView->highlightSections());
  61. headerView->setPropagateToItems(true);
  62. // sets the model to the headerview
  63. table.setHorizontalHeader(headerView);
  64. if (headerView->isClickable() != oldClickable)
  65. {
  66. std::cerr << "ctkCheckableHeaderView::setClickable() failed: "
  67. << headerView->isClickable() << std::endl;
  68. return EXIT_FAILURE;
  69. }
  70. if (headerView->checkState(0) == Qt::Unchecked)
  71. {
  72. std::cerr << "ctkCheckableHeaderView::checkstate() failed: "
  73. << headerView->checkState(0) << std::endl;
  74. return EXIT_FAILURE;
  75. }
  76. Qt::CheckState checkstate;
  77. if (!headerView->checkState(0, checkstate))
  78. {
  79. std::cerr << "ctkCheckableHeaderView::checkstate() failed: "
  80. << headerView->checkState(0, checkstate) << std::endl;
  81. return EXIT_FAILURE;
  82. }
  83. QFocusEvent focus(QEvent::FocusIn,Qt::TabFocusReason);
  84. headerView->eventFilter(headerView, &focus);
  85. if (!headerView->propagateToItems())
  86. {
  87. std::cerr << "ctkCheckableHeaderView::propagateToItems() failed: "
  88. << headerView->propagateToItems() << std::endl;
  89. return EXIT_FAILURE;
  90. }
  91. headerView->setPropagateToItems(false);
  92. if (headerView->propagateToItems())
  93. {
  94. std::cerr << "ctkCheckableHeaderView::propagateToItems() failed: "
  95. << headerView->propagateToItems() << std::endl;
  96. return EXIT_FAILURE;
  97. }
  98. // uncheck the header
  99. headerView->toggleCheckState(0);
  100. if (headerView->checkState(0) != Qt::Unchecked)
  101. {
  102. std::cerr << "ctkCheckableHeaderView::toggleCheckState() failed: "
  103. << headerView->checkState(0) << std::endl;
  104. return EXIT_FAILURE;
  105. }
  106. // make sure it didn't uncheck the checkable items
  107. if (row0[0]->checkState() != Qt::Checked ||
  108. row1[0]->checkState() != Qt::Checked ||
  109. row2[0]->checkState() != Qt::Checked)
  110. {
  111. std::cerr << "ctkCheckableHeaderView::toggleCheckState() failed: "
  112. << row0[0]->checkState() << " "
  113. << row1[0]->checkState() << " "
  114. << row2[0]->checkState() << std::endl;
  115. return EXIT_FAILURE;
  116. }
  117. row0[0]->setCheckState(Qt::Unchecked);
  118. // make sure it didn't uncheck the checkable items
  119. if (headerView->checkState(0) != Qt::Unchecked ||
  120. row0[0]->checkState() != Qt::Unchecked ||
  121. row1[0]->checkState() != Qt::Checked ||
  122. row2[0]->checkState() != Qt::Checked)
  123. {
  124. std::cerr << "QStandardItem::setCheckState() failed: "
  125. << headerView->checkState(0) << " "
  126. << row0[0]->checkState() << " "
  127. << row1[0]->checkState() << " "
  128. << row2[0]->checkState() << std::endl;
  129. return EXIT_FAILURE;
  130. }
  131. headerView->setPropagateToItems(true);
  132. if (!headerView->propagateToItems() ||
  133. headerView->checkState(0) != Qt::PartiallyChecked ||
  134. row0[0]->checkState() != Qt::Unchecked ||
  135. row1[0]->checkState() != Qt::Checked ||
  136. row2[0]->checkState() != Qt::Checked)
  137. {
  138. std::cerr << "ctkCheckableHeaderView::setPropagateToItems() failed: "
  139. << headerView->checkState(0) << " "
  140. << row0[0]->checkState() << " "
  141. << row1[0]->checkState() << " "
  142. << row2[0]->checkState() << std::endl;
  143. return EXIT_FAILURE;
  144. }
  145. row0[0]->setCheckState(Qt::Checked);
  146. if (headerView->checkState(0) != Qt::Checked ||
  147. row0[0]->checkState() != Qt::Checked ||
  148. row1[0]->checkState() != Qt::Checked ||
  149. row2[0]->checkState() != Qt::Checked)
  150. {
  151. std::cerr << "QStandardItem::setCheckState() failed: "
  152. << headerView->checkState(0) << " "
  153. << row0[0]->checkState() << " "
  154. << row1[0]->checkState() << " "
  155. << row2[0]->checkState() << std::endl;
  156. return EXIT_FAILURE;
  157. }
  158. row1[0]->setCheckState(Qt::Unchecked);
  159. // make sure it didn't uncheck the checkable items
  160. if (headerView->checkState(0) != Qt::PartiallyChecked ||
  161. row0[0]->checkState() != Qt::Checked ||
  162. row1[0]->checkState() != Qt::Unchecked ||
  163. row2[0]->checkState() != Qt::Checked)
  164. {
  165. std::cerr << "QStandardItem::setCheckState() failed: "
  166. << headerView->checkState(0) << " "
  167. << row0[0]->checkState() << " "
  168. << row1[0]->checkState() << " "
  169. << row2[0]->checkState() << std::endl;
  170. return EXIT_FAILURE;
  171. }
  172. row1[0]->setCheckState(Qt::Unchecked);
  173. // make sure it didn't uncheck the checkable items
  174. if (headerView->checkState(0) != Qt::PartiallyChecked ||
  175. row0[0]->checkState() != Qt::Checked ||
  176. row1[0]->checkState() != Qt::Unchecked ||
  177. row2[0]->checkState() != Qt::Checked)
  178. {
  179. std::cerr << "QStandardItem::setCheckState() failed: "
  180. << headerView->checkState(0) << " "
  181. << row0[0]->checkState() << " "
  182. << row1[0]->checkState() << " "
  183. << row2[0]->checkState() << std::endl;
  184. return EXIT_FAILURE;
  185. }
  186. row0[0]->setCheckState(Qt::Unchecked);
  187. row2[0]->setCheckState(Qt::Unchecked);
  188. // make sure the header is now unchecked
  189. if (headerView->checkState(0) != Qt::Unchecked ||
  190. row0[0]->checkState() != Qt::Unchecked ||
  191. row1[0]->checkState() != Qt::Unchecked ||
  192. row2[0]->checkState() != Qt::Unchecked)
  193. {
  194. std::cerr << "QStandardItem::setCheckState() failed: "
  195. << headerView->checkState(0) << " "
  196. << row0[0]->checkState() << " "
  197. << row1[0]->checkState() << " "
  198. << row2[0]->checkState() << std::endl;
  199. return EXIT_FAILURE;
  200. }
  201. headerView->setCheckState(0, Qt::Checked);
  202. if (headerView->checkState(0) != Qt::Checked ||
  203. row0[0]->checkState() != Qt::Checked ||
  204. row1[0]->checkState() != Qt::Checked ||
  205. row2[0]->checkState() != Qt::Checked)
  206. {
  207. std::cerr << "ctkCheckableHeaderView::setCheckState() failed: "
  208. << headerView->checkState(0) << " "
  209. << row0[0]->checkState() << " "
  210. << row1[0]->checkState() << " "
  211. << row2[0]->checkState() << std::endl;
  212. return EXIT_FAILURE;
  213. }
  214. table.show();
  215. //table.raise();
  216. if (argc < 2 || QString(argv[1]) != "-I" )
  217. {
  218. QTimer::singleShot(500, &app, SLOT(quit()));
  219. }
  220. return app.exec();
  221. }