ctkDynamicSpacerTest1.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <QApplication>
  16. #include <QPushButton>
  17. #include <QSignalSpy>
  18. #include <QStyle>
  19. #include <QTimer>
  20. #include <QVBoxLayout>
  21. // CTK includes
  22. #include "ctkDynamicSpacer.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. int ctkDynamicSpacerTest1(int argc, char * argv [] )
  28. {
  29. QApplication app(argc, argv);
  30. QWidget topLevel;
  31. ctkDynamicSpacer* spacer1 = new ctkDynamicSpacer;
  32. QPushButton* button= new QPushButton;
  33. button->setCheckable(true);
  34. ctkDynamicSpacer* spacer2 = new ctkDynamicSpacer;
  35. QVBoxLayout* layout = new QVBoxLayout;
  36. layout->addWidget(spacer1);
  37. layout->addWidget(button);
  38. layout->addWidget(spacer2);
  39. topLevel.setLayout(layout);
  40. if (spacer1->activeSizePolicy() !=
  41. QSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred))
  42. {
  43. std::cerr << "ctkDynamicSpacer: wrong default values" << std::endl;
  44. return EXIT_FAILURE;
  45. }
  46. spacer1->setActiveSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  47. if (spacer1->activeSizePolicy() !=
  48. QSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding))
  49. {
  50. std::cerr << "ctkDynamicSpacer::setActiveSizePolicy failed" << std::endl;
  51. return EXIT_FAILURE;
  52. }
  53. spacer1->setInactiveSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  54. if (spacer1->inactiveSizePolicy() !=
  55. QSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum))
  56. {
  57. std::cerr << "ctkDynamicSpacer::setInactiveSizePolicy failed" << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. spacer2->setInactiveSizePolicy(spacer1->inactiveSizePolicy());
  61. spacer2->setActiveSizePolicy(spacer1->activeSizePolicy());
  62. QObject::connect(button, SIGNAL(toggled(bool)), spacer1, SLOT(setActive(bool)));
  63. QObject::connect(button, SIGNAL(toggled(bool)), spacer2, SLOT(setInactive(bool)));
  64. spacer1->setPalette(QPalette(Qt::red));
  65. spacer2->setPalette(QPalette(Qt::blue));
  66. spacer1->setAutoFillBackground(true);
  67. spacer2->setAutoFillBackground(true);
  68. topLevel.resize(50, 300);
  69. topLevel.show();
  70. QApplication::processEvents();
  71. // both inactive, they don't grow
  72. if (spacer1->height() != 0 || spacer2->height() != 0)
  73. {
  74. std::cerr << "ctkDynamicSpacer failed 1: " << spacer1->height() << " "
  75. << spacer2->height() << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. button->toggle();
  79. // need to repaint the widgets to query their size
  80. QApplication::processEvents();
  81. if (spacer1->height() <= 0 || spacer2->height() != 0)
  82. {
  83. std::cerr << "ctkDynamicSpacer failed 2: " << spacer1->height() << " "
  84. << spacer2->height() << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. button->toggle();
  88. // need to repaint the widgets to query their size
  89. QApplication::processEvents();
  90. if (spacer1->height() != 0 || spacer2->height() <= 0)
  91. {
  92. std::cerr << "ctkDynamicSpacer failed 3: " << spacer1->height() << " "
  93. << spacer2->height() << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. if (argc < 2 || QString(argv[1]) != "-I" )
  97. {
  98. QTimer::singleShot(200, &app, SLOT(quit()));
  99. }
  100. return app.exec();
  101. }