ctkRangeWidgetTest1.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. // CTK includes
  17. #include "ctkRangeWidget.h"
  18. // STD includes
  19. #include <cstdlib>
  20. #include <iostream>
  21. //-----------------------------------------------------------------------------
  22. bool checkSlider(const ctkRangeWidget& slider)
  23. {
  24. return slider.minimum() <= slider.minimumValue() &&
  25. slider.minimumValue() <= slider.maximumValue() &&
  26. slider.maximumValue() <= slider.maximum();
  27. }
  28. //-----------------------------------------------------------------------------
  29. bool checkSlider(const ctkRangeWidget& slider,
  30. double min, double minVal, double maxVal, double max)
  31. {
  32. return qFuzzyCompare(slider.minimum(), min) &&
  33. qFuzzyCompare(slider.minimumValue(), minVal) &&
  34. qFuzzyCompare(slider.maximumValue(), maxVal) &&
  35. qFuzzyCompare(slider.maximum(), max);
  36. }
  37. //-----------------------------------------------------------------------------
  38. int ctkRangeWidgetTest1(int argc, char * argv [] )
  39. {
  40. QApplication app(argc, argv);
  41. ctkRangeWidget sliderSpinBox;
  42. if (!checkSlider(sliderSpinBox))
  43. {
  44. std::cerr << "ctkRangeSlider:: 0) "
  45. << sliderSpinBox.minimum() << " "
  46. << sliderSpinBox.minimumValue() << " "
  47. << sliderSpinBox.maximumValue() << " "
  48. << sliderSpinBox.maximum() << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. // the first part of the tests infer 2 decimals
  52. if (sliderSpinBox.decimals() != 2)
  53. {
  54. std::cerr << "ctkRangeWidget::decimals default value failed."
  55. << sliderSpinBox.decimals() << std::endl;
  56. return EXIT_FAILURE;
  57. }
  58. sliderSpinBox.setValues(32.12,75.38);
  59. if (!checkSlider(sliderSpinBox, 0., 32.12, 75.38, 99.))
  60. {
  61. std::cerr << "ctkRangeWidget:: 1) setValues"
  62. << sliderSpinBox.minimum() << " "
  63. << sliderSpinBox.minimumValue() << " "
  64. << sliderSpinBox.maximumValue() << " "
  65. << sliderSpinBox.maximum() << std::endl;
  66. return EXIT_FAILURE;
  67. }
  68. sliderSpinBox.setMinimum(10.0123);
  69. if (!checkSlider(sliderSpinBox, 10.01, 32.12, 75.38, 99.))
  70. {
  71. std::cerr << "ctkRangeWidget:: 2) setMinimum "
  72. << sliderSpinBox.minimum() << " "
  73. << sliderSpinBox.minimumValue() << " "
  74. << sliderSpinBox.maximumValue() << " "
  75. << sliderSpinBox.maximum() << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. sliderSpinBox.setMaximum(2050.9876);
  79. if (!checkSlider(sliderSpinBox, 10.01, 32.12, 75.38, 2050.99))
  80. {
  81. std::cerr << "ctkRangeWidget:: 3) setMaximum "
  82. << sliderSpinBox.minimum() << " "
  83. << sliderSpinBox.minimumValue() << " "
  84. << sliderSpinBox.maximumValue() << " "
  85. << sliderSpinBox.maximum() << std::endl;
  86. return EXIT_FAILURE;
  87. }
  88. std::cout << "1" << std::endl;
  89. sliderSpinBox.setSingleStep(0.1);
  90. if (!qFuzzyCompare(sliderSpinBox.singleStep(), 0.1) ||
  91. !checkSlider(sliderSpinBox, 10.01, 32.12, 75.38, 2050.99))
  92. {
  93. std::cerr << "ctkRangeWidget:: 4) SetSingleStep"
  94. << sliderSpinBox.minimum() << " "
  95. << sliderSpinBox.minimumValue() << " "
  96. << sliderSpinBox.maximumValue() << " "
  97. << sliderSpinBox.maximum() << std::endl;
  98. return EXIT_FAILURE;
  99. }
  100. std::cout << "2" << std::endl;
  101. sliderSpinBox.setDecimals(1);
  102. if (sliderSpinBox.decimals() != 1 ||
  103. !checkSlider(sliderSpinBox, 10.0, 32.1, 75.4, 2051.0))
  104. {
  105. std::cerr << "ctkRangeWidget:: 5) SetDecimals"
  106. << sliderSpinBox.minimum() << " "
  107. << sliderSpinBox.minimumValue() << " "
  108. << sliderSpinBox.maximumValue() << " "
  109. << sliderSpinBox.maximum() << std::endl;
  110. return EXIT_FAILURE;
  111. }
  112. std::cout << "3" << std::endl;
  113. sliderSpinBox.setMaximumValue(77.777);
  114. if (!checkSlider(sliderSpinBox, 10.0, 32.1, 77.8, 2051.0))
  115. {
  116. std::cerr << "ctkRangeWidget:: 6) SetMaximumValue"
  117. << sliderSpinBox.minimum() << " "
  118. << sliderSpinBox.minimumValue() << " "
  119. << sliderSpinBox.maximumValue() << " "
  120. << sliderSpinBox.maximum() << std::endl;
  121. return EXIT_FAILURE;
  122. }
  123. std::cout << "4" << std::endl;
  124. sliderSpinBox.setTickInterval(0.1);
  125. if (!qFuzzyCompare(sliderSpinBox.tickInterval(), 0.1) ||
  126. !checkSlider(sliderSpinBox, 10.0, 32.1, 77.8, 2051.0))
  127. {
  128. std::cerr << "ctkRangeWidget:: 6) setTickInterval"
  129. << sliderSpinBox.minimum() << " "
  130. << sliderSpinBox.minimumValue() << " "
  131. << sliderSpinBox.maximumValue() << " "
  132. << sliderSpinBox.maximum() << std::endl;
  133. return EXIT_FAILURE;
  134. }
  135. std::cout << "5" << std::endl;
  136. sliderSpinBox.setMinimum(80.5678);
  137. if (!qFuzzyCompare(sliderSpinBox.tickInterval(), 0.1) ||
  138. !checkSlider(sliderSpinBox, 80.6, 80.6, 80.6, 2051.0))
  139. {
  140. std::cerr << "ctkRangeWidget:: 6) setMinimum"
  141. << sliderSpinBox.minimum() << " "
  142. << sliderSpinBox.minimumValue() << " "
  143. << sliderSpinBox.maximumValue() << " "
  144. << sliderSpinBox.maximum() << std::endl;
  145. return EXIT_FAILURE;
  146. }
  147. std::cout << "6" << std::endl;
  148. //sliderSpinBox.reset();
  149. sliderSpinBox.setSpinBoxAlignment(Qt::AlignRight);
  150. if (sliderSpinBox.spinBoxAlignment() != Qt::AlignRight ||
  151. !checkSlider(sliderSpinBox, 80.6, 80.6, 80.6, 2051.0))
  152. {
  153. std::cerr << "ctkRangeWidget:: 7) setSpinBoxAlignment"
  154. << sliderSpinBox.minimum() << " "
  155. << sliderSpinBox.minimumValue() << " "
  156. << sliderSpinBox.maximumValue() << " "
  157. << sliderSpinBox.maximum() << std::endl;
  158. return EXIT_FAILURE;
  159. }
  160. std::cout << "7" << std::endl;
  161. sliderSpinBox.setAutoSpinBoxWidth(false);
  162. if (sliderSpinBox.isAutoSpinBoxWidth() != false ||
  163. !checkSlider(sliderSpinBox, 80.6, 80.6, 80.6, 2051.0))
  164. {
  165. std::cerr << "ctkRangeWidget:: 8) setAutoSpinBoxWidth"
  166. << sliderSpinBox.minimum() << " "
  167. << sliderSpinBox.minimumValue() << " "
  168. << sliderSpinBox.maximumValue() << " "
  169. << sliderSpinBox.maximum() << std::endl;
  170. return EXIT_FAILURE;
  171. }
  172. std::cout << "8" << std::endl;
  173. sliderSpinBox.setPrefix("$");
  174. if (sliderSpinBox.prefix() != "$" ||
  175. !checkSlider(sliderSpinBox, 80.6, 80.6, 80.6, 2051.0))
  176. {
  177. std::cerr << "ctkRangeWidget:: 8) setPrefix"
  178. << sliderSpinBox.minimum() << " "
  179. << sliderSpinBox.minimumValue() << " "
  180. << sliderSpinBox.maximumValue() << " "
  181. << sliderSpinBox.maximum() << std::endl;
  182. return EXIT_FAILURE;
  183. }
  184. std::cout << "9" << std::endl;
  185. sliderSpinBox.setSuffix("mm");
  186. if (sliderSpinBox.suffix() != "mm" ||
  187. !checkSlider(sliderSpinBox, 80.6, 80.6, 80.6, 2051.0))
  188. {
  189. std::cerr << "ctkRangeWidget:: 9) setSuffix"
  190. << sliderSpinBox.minimum() << " "
  191. << sliderSpinBox.minimumValue() << " "
  192. << sliderSpinBox.maximumValue() << " "
  193. << sliderSpinBox.maximum() << std::endl;
  194. return EXIT_FAILURE;
  195. }
  196. std::cout << "10" << std::endl;
  197. sliderSpinBox.setDecimals(0);
  198. if (!checkSlider(sliderSpinBox, 81., 81., 81., 2051.))
  199. {
  200. std::cerr << "ctkRangeWidget:: 10) setDecimals"
  201. << sliderSpinBox.minimum() << " "
  202. << sliderSpinBox.minimumValue() << " "
  203. << sliderSpinBox.maximumValue() << " "
  204. << sliderSpinBox.maximum() << std::endl;
  205. return EXIT_FAILURE;
  206. }
  207. // FIXME check that the correct signals are sent.
  208. return EXIT_SUCCESS;
  209. }