ctkButtonGroupTest1.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <QApplication>
  16. #include <QHBoxLayout>
  17. #include <QPushButton>
  18. #include <QSignalSpy>
  19. #include <QTimer>
  20. // CTK includes
  21. #include "ctkButtonGroup.h"
  22. #include "ctkCollapsibleButton.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. int ctkButtonGroupTest1(int argc, char * argv [] )
  28. {
  29. QApplication app(argc, argv);
  30. QWidget widget(0);
  31. widget.show();
  32. QHBoxLayout* layout = new QHBoxLayout(&widget);
  33. QPushButton* button1 = new QPushButton(&widget);
  34. layout->addWidget(button1);
  35. QPushButton* button2 = new QPushButton(&widget);
  36. layout->addWidget(button2);
  37. QPushButton* button3 = new QPushButton(&widget);
  38. layout->addWidget(button3);
  39. ctkCollapsibleButton* button4 = new ctkCollapsibleButton(&widget);
  40. layout->addWidget(button4);
  41. widget.setLayout(layout);
  42. button1->setCheckable(true);
  43. button2->setCheckable(true);
  44. button3->setCheckable(false);
  45. button4->setCheckable(true);
  46. button2->setChecked(true);
  47. button4->setChecked(true);
  48. ctkButtonGroup buttonGroup(0);
  49. buttonGroup.addButton(button1);
  50. buttonGroup.addButton(button2);
  51. buttonGroup.addButton(button3);
  52. buttonGroup.addButton(button4);
  53. if (!button4->isChecked() || button2->isChecked())
  54. {
  55. std::cerr << "ctkButtonGroup::addButton failed"
  56. << button2->isChecked() << " " << button4->isChecked()
  57. << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. // Click #1: check button1
  61. button1->click();
  62. if (!button1->isChecked() || button4->isChecked())
  63. {
  64. std::cerr << "ctkButtonGroup::click1 failed"
  65. << button1->isChecked() << " " << button4->isChecked()
  66. << std::endl;
  67. return EXIT_FAILURE;
  68. }
  69. // Click #2: uncheck button1
  70. button1->click();
  71. if (button1->isChecked() || button4->isChecked())
  72. {
  73. std::cerr << "ctkButtonGroup::click2 failed"
  74. << button1->isChecked() << " " << button4->isChecked()
  75. << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. // Click #3: check button1
  79. button1->click();
  80. if (!button1->isChecked() || button4->isChecked())
  81. {
  82. std::cerr << "ctkButtonGroup::click3 failed"
  83. << button1->isChecked() << " " << button4->isChecked()
  84. << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. // Click #4: check button2
  88. button2->click();
  89. if (!button2->isChecked() || button1->isChecked())
  90. {
  91. std::cerr << "ctkButtonGroup::click4 failed"
  92. << button2->isChecked() << " " << button1->isChecked()
  93. << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. // Click #5: click button3 keep check on button2
  97. button3->click();
  98. if (!button2->isChecked() || button3->isChecked())
  99. {
  100. std::cerr << "ctkButtonGroup::click5 failed"
  101. << button2->isChecked() << " " << button3->isChecked()
  102. << std::endl;
  103. return EXIT_FAILURE;
  104. }
  105. // Click #6: uncheck button2
  106. button2->click();
  107. if (button2->isChecked())
  108. {
  109. std::cerr << "ctkButtonGroup::click6 failed"
  110. << button2->isChecked() << std::endl;
  111. return EXIT_FAILURE;
  112. }
  113. qRegisterMetaType<QAbstractButton*>("QAbstractButton*");
  114. QSignalSpy spy(&buttonGroup, SIGNAL(buttonClicked(QAbstractButton*)));
  115. QSignalSpy spyInt(&buttonGroup, SIGNAL(buttonClicked(int)));
  116. button1->click();
  117. if (spy.count() != 1 || spyInt.count() != 1)
  118. {
  119. std::cerr << "ctkButtonGroup::click7 failed"
  120. << button1->isChecked() << ", "
  121. << spy.count() << "clicks" << std::endl;
  122. return EXIT_FAILURE;
  123. }
  124. button4->click();
  125. if (spy.count() != 2 || spyInt.count() != 2)
  126. {
  127. std::cerr << "ctkButtonGroup::click8 failed"
  128. << button4->isChecked() << ", "
  129. << spy.count() << "clicks" << std::endl;
  130. return EXIT_FAILURE;
  131. }
  132. button4->click();
  133. if (spy.count() != 3 || spyInt.count() != 3)
  134. {
  135. std::cerr << "ctkButtonGroup::click9 failed"
  136. << button4->isChecked() << ", "
  137. << spy.count() << "clicks" << std::endl;
  138. return EXIT_FAILURE;
  139. }
  140. QTimer autoExit;
  141. if (argc < 2 || QString(argv[1]) != "-I")
  142. {
  143. QObject::connect(&autoExit, SIGNAL(timeout()), &app, SLOT(quit()));
  144. autoExit.start(500);
  145. }
  146. return app.exec();
  147. }