ctkCheckableComboBoxTest1.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <QTimer>
  18. #if (QT_VERSION < 0x50000)
  19. #include <QCleanlooksStyle>
  20. #endif
  21. // CTK includes
  22. #include <ctkCheckableComboBox.h>
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. int ctkCheckableComboBoxTest1(int argc, char * argv [] )
  28. {
  29. // QCleanlooksStyle is the only style that doesn't show the checkboxes by
  30. // default. Test it with it
  31. #if (QT_VERSION < 0x50000)
  32. QApplication::setStyle(new QCleanlooksStyle);
  33. #endif
  34. QApplication app(argc, argv);
  35. ctkCheckableComboBox comboBox;
  36. comboBox.addItem("toto");
  37. comboBox.addItem("tata");
  38. comboBox.addItem("titi");
  39. comboBox.addItem(comboBox.style()->standardIcon(QStyle::SP_FileIcon),"tutu");
  40. if (comboBox.checkedIndexes().count() != 0 ||
  41. comboBox.allChecked() ||
  42. !comboBox.noneChecked())
  43. {
  44. std::cerr << "ctkCheckableComboBox has wrong default values\n"
  45. << " count:" << comboBox.checkedIndexes().count()
  46. << " all:" << comboBox.allChecked()
  47. << " none:" << comboBox.noneChecked() << std::endl;
  48. return EXIT_FAILURE;
  49. }
  50. int row = 0;
  51. QModelIndex firstIndex = comboBox.model()->index(row,0);
  52. comboBox.setCheckState(firstIndex, Qt::Checked);
  53. if (comboBox.checkState(firstIndex) != Qt::Checked)
  54. {
  55. std::cerr << "ctkCheckableComboBox::setCheckedState failed\n"
  56. << static_cast<int>(comboBox.checkState(firstIndex))
  57. << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. if (comboBox.checkedIndexes().count() != 1 ||
  61. comboBox.allChecked() ||
  62. comboBox.noneChecked())
  63. {
  64. std::cerr << "ctkCheckableComboBox::setCheckedState(first) failed\n"
  65. << " count:" << comboBox.checkedIndexes().count()
  66. << " all:" << comboBox.allChecked()
  67. << " none:" << comboBox.noneChecked() << std::endl;
  68. return EXIT_FAILURE;
  69. }
  70. // Check all the items
  71. QModelIndex nextIndex = firstIndex;
  72. while( (nextIndex = nextIndex.sibling(++row, 0)).isValid())
  73. {
  74. comboBox.setCheckState(nextIndex, Qt::Checked);
  75. }
  76. if (comboBox.checkedIndexes().count() != 4 ||
  77. !comboBox.allChecked() ||
  78. comboBox.noneChecked())
  79. {
  80. std::cerr << "ctkCheckableComboBox::setCheckedState(all) failed\n"
  81. << " count:" << comboBox.checkedIndexes().count()
  82. << " all:" << comboBox.allChecked()
  83. << " none:" << comboBox.noneChecked() << std::endl;
  84. return EXIT_FAILURE;
  85. }
  86. comboBox.show();
  87. if (argc < 2 || QString(argv[1]) != "-I" )
  88. {
  89. QTimer::singleShot(200, &app, SLOT(quit()));
  90. }
  91. return app.exec();
  92. }