ctkCheckableHeaderViewEventTranslatorPlayerTest1.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <QAction>
  16. #include <QApplication>
  17. #include <QDebug>
  18. #include <QSignalSpy>
  19. #include <QStandardItemModel>
  20. #include <QTableView>
  21. #include <QTimer>
  22. #include <QTreeView>
  23. // CTK includes
  24. #include "ctkCallback.h"
  25. #include "ctkConfig.h"
  26. #include "ctkCheckableHeaderView.h"
  27. #include "ctkCheckableHeaderViewEventPlayer.h"
  28. #include "ctkCheckableHeaderViewEventTranslator.h"
  29. #include "ctkEventTranslatorPlayerWidget.h"
  30. // QtTesting includes
  31. #include "pqTestUtility.h"
  32. // STD includes
  33. #include <cstdlib>
  34. #include <iostream>
  35. namespace
  36. {
  37. QSignalSpy* Spy1;
  38. //-----------------------------------------------------------------------------
  39. void checkFinalWidgetState(void* data)
  40. {
  41. QWidget* parentWidget = reinterpret_cast<QWidget*>(data);
  42. QList<ctkCheckableHeaderView*> widget = parentWidget->findChildren<ctkCheckableHeaderView*>();
  43. if( widget.count() > 0)
  44. {
  45. CTKCOMPARE(widget[0]->checkState(0), Qt::PartiallyChecked);
  46. CTKCOMPARE(widget[0]->checkState(1), Qt::Unchecked);
  47. CTKCOMPARE(Spy1->count(), 8);
  48. }
  49. else
  50. {
  51. QApplication::exit(EXIT_FAILURE);
  52. }
  53. }
  54. }
  55. //-----------------------------------------------------------------------------
  56. int ctkCheckableHeaderViewEventTranslatorPlayerTest1(int argc, char * argv [] )
  57. {
  58. QApplication app(argc, argv);
  59. QString xmlDirectory = CTK_SOURCE_DIR "/Libs/Widgets/Testing/Cpp/";
  60. // ------------------------
  61. ctkEventTranslatorPlayerWidget etpWidget;
  62. pqTestUtility* testUtility = new pqTestUtility(&etpWidget);
  63. etpWidget.setTestUtility(testUtility);
  64. etpWidget.addWidgetEventTranslator(new ctkCheckableHeaderViewEventTranslator);
  65. etpWidget.addWidgetEventPlayer(new ctkCheckableHeaderViewEventPlayer);
  66. // Test case 1
  67. QStandardItemModel model;
  68. QStringList headers;
  69. headers << "Title 1" << "Title 2" << "Title 3";
  70. model.setHorizontalHeaderLabels(headers);
  71. QList<QStandardItem*> row0;
  72. row0 << new QStandardItem << new QStandardItem << new QStandardItem;
  73. row0[0]->setText("not user checkable");
  74. model.appendRow(row0);
  75. QList<QStandardItem*> row1;
  76. row1 << new QStandardItem << new QStandardItem << new QStandardItem;
  77. row1[0]->setCheckable(true);
  78. row1[0]->setText("checkable");
  79. model.appendRow(row1);
  80. QList<QStandardItem*> row2;
  81. row2 << new QStandardItem << new QStandardItem << new QStandardItem;
  82. row2[0]->setCheckable(true);
  83. row2[0]->setText("checkable");
  84. model.appendRow(row2);
  85. QTableView table;
  86. table.setModel(&model);
  87. // Header is checked by default
  88. model.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole);
  89. QHeaderView* previousHeaderView = table.horizontalHeader();
  90. bool oldClickable = previousHeaderView->isClickable();
  91. ctkCheckableHeaderView* headerView =
  92. new ctkCheckableHeaderView(Qt::Horizontal, &table);
  93. headerView->setClickable(oldClickable);
  94. headerView->setMovable(previousHeaderView->isMovable());
  95. headerView->setHighlightSections(previousHeaderView->highlightSections());
  96. // propagatetoitems is true by default
  97. //headerView->setPropagateToItems(true);
  98. // sets the model to the headerview
  99. table.setHorizontalHeader(headerView);
  100. // Header is checked by default
  101. model.setHeaderData(1, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole);
  102. QSignalSpy spy1(headerView, SIGNAL(sectionPressed(int)));
  103. Spy1 = &spy1;
  104. etpWidget.addTestCase(&table,
  105. xmlDirectory + "ctkCheckableHeaderViewEventTranslatorPlayerTest1.xml",
  106. &checkFinalWidgetState);
  107. // ------------------------
  108. if (!app.arguments().contains("-I"))
  109. {
  110. QTimer::singleShot(0, &etpWidget, SLOT(play()));
  111. }
  112. etpWidget.show();
  113. return app.exec();
  114. }