ctkDoubleSliderTest1.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <QString>
  17. #include <QTimer>
  18. // CTK includes
  19. #include "ctkDoubleSlider.h"
  20. // STD includes
  21. #include <cstdlib>
  22. #include <iostream>
  23. //-----------------------------------------------------------------------------
  24. int ctkDoubleSliderTest1(int argc, char * argv [] )
  25. {
  26. QApplication app(argc, argv);
  27. ctkDoubleSlider slider;
  28. std::cout << "Test setValue(18.54)" << std::endl;
  29. slider.setValue(18.54);
  30. if (!qFuzzyCompare(slider.value(), 18.54))
  31. {
  32. std::cerr << "ctkDoubleSlider::setValue failed." << slider.value() << std::endl;
  33. return EXIT_FAILURE;
  34. }
  35. std::cout << "Test setMinimum(5.21)" << std::endl;
  36. slider.setMinimum(5.21);
  37. if (!qFuzzyCompare(slider.minimum(), 5.21) ||
  38. !qFuzzyCompare(slider.value(),18.54))
  39. {
  40. std::cerr << "ctkDoubleSlider::setMinimum failed." << slider.value() << std::endl;
  41. return EXIT_FAILURE;
  42. }
  43. std::cout << "Test setMaximum(1340.2)" << std::endl;
  44. slider.setMaximum(1340.2);
  45. if (!qFuzzyCompare(slider.maximum(), 1340.2) ||
  46. !qFuzzyCompare(slider.value(), 18.54))
  47. {
  48. std::cerr << "ctkDoubleSlider::setMinimum failed." << slider.value() << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. std::cout << "Test setSingleStep(0.15)" << std::endl;
  52. slider.setSingleStep(0.15);
  53. if (!qFuzzyCompare(slider.singleStep(), 0.15) ||
  54. !qFuzzyCompare(slider.value(), 18.54) ||
  55. !qFuzzyCompare(slider.sliderPosition(), 18.54))
  56. {
  57. std::cerr << "ctkDoubleSlider::setSingleStep failed."
  58. << "Step:" << slider.singleStep()
  59. << " Val:" << slider.value()
  60. << " Pos:" << slider.sliderPosition() << std::endl;
  61. return EXIT_FAILURE;
  62. }
  63. std::cout << "Test setSingleStep(0.33)" << std::endl;
  64. slider.setSingleStep(0.33);
  65. if (!qFuzzyCompare(slider.singleStep(), 0.33) ||
  66. !qFuzzyCompare(slider.value(), 18.54) ||
  67. !qFuzzyCompare(slider.sliderPosition(), 18.54))
  68. {
  69. std::cerr << "ctkDoubleSlider::setSingleStep failed."
  70. << slider.value() << " "
  71. << slider.sliderPosition() << std::endl;
  72. return EXIT_FAILURE;
  73. }
  74. std::cout << "Test Step Add" << std::endl;
  75. slider.triggerAction (QAbstractSlider::SliderSingleStepAdd);
  76. if (!qFuzzyCompare(slider.value(), 18.87))
  77. {
  78. std::cerr << "ctkDoubleSlider:: Step Add failed" << slider.value() << std::endl;
  79. return EXIT_FAILURE;
  80. }
  81. std::cout << "Test Step Sub" << std::endl;
  82. slider.triggerAction (QAbstractSlider::SliderSingleStepSub);
  83. if (!qFuzzyCompare(slider.value(), 18.54))
  84. {
  85. std::cerr << "ctkDoubleSlider:: Step Sub failed" << slider.value() << std::endl;
  86. return EXIT_FAILURE;
  87. }
  88. std::cout << "Test setMaximum" << std::endl;
  89. slider.setMaximum(15.08);
  90. if (!qFuzzyCompare(slider.maximum(), 15.08) ||
  91. !qFuzzyCompare(slider.value(), 15.08))
  92. {
  93. std::cerr << "ctkDoubleSlider:: setMaximum failed" << slider.value() << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. std::cout << "Test setMaximum" << std::endl;
  97. slider.setMaximum(5100000.333333);
  98. if (!qFuzzyCompare(slider.maximum(), 5100000.333333) ||
  99. !qFuzzyCompare(slider.value(), 15.08))
  100. {
  101. std::cerr << "ctkDoubleSlider:: setMaximum failed" << slider.value() << std::endl;
  102. return EXIT_FAILURE;
  103. }
  104. std::cout << "Test setValue close to previous value" << std::endl;
  105. slider.setValue(15.081);
  106. if (!qFuzzyCompare(slider.value(), 15.081))
  107. {
  108. std::cerr << "ctkDoubleSlider:: setValue failed" << slider.value() << std::endl;
  109. return EXIT_FAILURE;
  110. }
  111. std::cout << "Test setSingleStep" << std::endl;
  112. slider.setSingleStep(1.);
  113. if (!qFuzzyCompare(slider.value(),15.081) ||
  114. !qFuzzyCompare(slider.sliderPosition(), 15.081))
  115. {
  116. std::cerr << "ctkDoubleSlider:: setSingleStep failed: val" << slider.value() << " pos:" << slider.sliderPosition() << std::endl;
  117. return EXIT_FAILURE;
  118. }
  119. std::cout << "Test setSingleStep " << std::endl;
  120. slider.setSingleStep(0.01);
  121. if (!qFuzzyCompare(slider.value(),15.081) ||
  122. !qFuzzyCompare(slider.sliderPosition(), 15.081))
  123. {
  124. std::cerr << "ctkDoubleSlider:: setSingleStep failed: val" << slider.value() << " pos:" << slider.sliderPosition() << std::endl;
  125. return EXIT_FAILURE;
  126. }
  127. std::cout << "Test reach max" << std::endl;
  128. slider.setMaximum(510.333333);
  129. slider.setValue(1000);
  130. if (!qFuzzyCompare(slider.maximum(), 510.333333) ||
  131. !qFuzzyCompare(slider.value(), 510.333333))
  132. {
  133. std::cerr << "ctkDoubleSlider::setValue(1000) failed"
  134. << " val:" << slider.value()
  135. << " pos: " << slider.sliderPosition() << std::endl;
  136. return EXIT_FAILURE;
  137. }
  138. std::cout << "Test change range" << std::endl;
  139. slider.setRange(-500.1, 10.21);
  140. if (!qFuzzyCompare(slider.maximum(), 10.21) ||
  141. !qFuzzyCompare(slider.value(), 10.21) ||
  142. !qFuzzyCompare(slider.sliderPosition(), 10.21))
  143. {
  144. std::cerr << "ctkDoubleSlider::setRange(-400.2, 10.21) failed"
  145. << " val:" << slider.value()
  146. << " pos: " << slider.sliderPosition() << std::endl;
  147. return EXIT_FAILURE;
  148. }
  149. slider.setSingleStep(0.15);
  150. if (!qFuzzyCompare(slider.value(), 10.21) ||
  151. !qFuzzyCompare(slider.sliderPosition(), 10.21))
  152. {
  153. std::cerr << "ctkDoubleSlider::setSingleStep(0.15) failed"
  154. << " val:" << slider.value()
  155. << " pos: " << slider.sliderPosition() << std::endl;
  156. return EXIT_FAILURE;
  157. }
  158. // FIXME
  159. // slider.setSliderPosition(10.3333);
  160. // if (!qFuzzyCompare(slider.value(), 10.3333) ||
  161. // !qFuzzyCompare(slider.sliderPosition(), 10.3333))
  162. // {
  163. // std::cerr << "ctkDoubleSlider::setSliderPosition(10.3333) failed"
  164. // << " val:" << slider.value()
  165. // << " pos: " << slider.sliderPosition() << std::endl;
  166. // return EXIT_FAILURE;
  167. // }
  168. // slider.setTracking(false);
  169. // slider.setSliderPosition(10.123456789);
  170. // if (!qFuzzyCompare(slider.value(), 10.3333) ||
  171. // !qFuzzyCompare(slider.sliderPosition(), 10.123456789))
  172. // {
  173. // std::cerr << "ctkDoubleSlider::setSliderPosition(10.123456789) failed"
  174. // << " val:" << slider.value()
  175. // << " pos: " << slider.sliderPosition() << std::endl;
  176. // return EXIT_FAILURE;
  177. // }
  178. slider.setPageStep(1.);
  179. if (slider.pageStep() != 1.)
  180. {
  181. std::cerr << "ctkDoubleSlider::setPageStep(1.) failed"
  182. << " val: " << slider.pageStep() << std::endl;
  183. return EXIT_FAILURE;
  184. }
  185. slider.show();
  186. if (argc < 2 || QString(argv[1]) != "-I" )
  187. {
  188. QTimer::singleShot(100, &app, SLOT(quit()));
  189. }
  190. return app.exec();
  191. }