ctkSpinBoxTest1.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <QDebug>
  17. #include <QSignalSpy>
  18. #include <QTimer>
  19. // CTK includes
  20. #include "ctkSpinBox.h"
  21. // STD includes
  22. #include <cstdlib>
  23. #include <iostream>
  24. //-----------------------------------------------------------------------------
  25. int ctkSpinBoxTest1(int argc, char * argv [] )
  26. {
  27. QApplication app(argc, argv);
  28. ctkSpinBox spinBox;
  29. // ----------------------------------------
  30. // Regular SpinBox methods
  31. // ----------------------------------------
  32. qDebug()<<"Prefix:";
  33. spinBox.setPrefix("$");
  34. if (spinBox.prefix() != "$")
  35. {
  36. qDebug()<<"Prefix does not correspond. Got: "<<spinBox.prefix();
  37. return EXIT_FAILURE;
  38. }
  39. spinBox.setValue(3.0);
  40. if (spinBox.text() != "$3.00")
  41. {
  42. qDebug()<<"Text does not correspond. Got: "<<spinBox.text();
  43. return EXIT_FAILURE;
  44. }
  45. qDebug()<<"Suffix:";
  46. spinBox.setSuffix("#");
  47. if (spinBox.suffix() != "#")
  48. {
  49. qDebug()<<"suffix does not correspond. Got: "<<spinBox.suffix();
  50. return EXIT_FAILURE;
  51. }
  52. spinBox.setValue(8.69);
  53. if (spinBox.text() != "$8.69#")
  54. {
  55. qDebug()<<"Text does not correspond. Got: "<<spinBox.text();
  56. return EXIT_FAILURE;
  57. }
  58. qDebug()<<"cleanText:";
  59. if (spinBox.cleanText() != "8.69")
  60. {
  61. qDebug()<<"cleanText does not correspond. Got: "<<spinBox.cleanText();
  62. return EXIT_FAILURE;
  63. }
  64. qDebug()<<"SingleStep:";
  65. spinBox.setSingleStep(0.01);
  66. if (spinBox.singleStep() != 0.01)
  67. {
  68. qDebug()<<"singleStep does not correspond. Got: "<<spinBox.singleStep();
  69. return EXIT_FAILURE;
  70. }
  71. spinBox.stepUp();
  72. if (spinBox.value() != 8.70)
  73. {
  74. qDebug()<<"Value does not correspond. Got: "<<spinBox.value();
  75. return EXIT_FAILURE;
  76. }
  77. spinBox.setSingleStep(1.0);
  78. spinBox.stepDown();
  79. if (spinBox.displayedValue() != 7.7)
  80. {
  81. qDebug()<<"Value does not correspond. Got: "<<spinBox.value();
  82. return EXIT_FAILURE;
  83. }
  84. qDebug()<<"Minimum:";
  85. spinBox.setMinimum(9.1);
  86. if (spinBox.minimum() != 9.1)
  87. {
  88. qDebug()<<"minimum does not correspond. Got: "<<spinBox.minimum();
  89. return EXIT_FAILURE;
  90. }
  91. if (spinBox.value() != 9.1)
  92. {
  93. qDebug()<<"Value does not correspond. Got: "<<spinBox.value();
  94. return EXIT_FAILURE;
  95. }
  96. qDebug()<<"Maximum:";
  97. spinBox.setValue(18.34);
  98. spinBox.setMaximum(15);
  99. if (spinBox.maximum() != 15)
  100. {
  101. qDebug()<<"maximum does not correspond. Got: "<<spinBox.maximum();
  102. return EXIT_FAILURE;
  103. }
  104. if (spinBox.value() != 15)
  105. {
  106. qDebug()<<"Value does not correspond. Got: "<<spinBox.value();
  107. return EXIT_FAILURE;
  108. }
  109. qDebug()<<"Range:";
  110. spinBox.setRange(-3.6, 42);
  111. if (spinBox.maximum() != 42 || spinBox.minimum() != -3.6)
  112. {
  113. qDebug()<<"Range does not correspond. Got: "
  114. <<spinBox.minimum()<<" : "<<spinBox.minimum();
  115. return EXIT_FAILURE;
  116. }
  117. qDebug()<<"Decimals:";
  118. spinBox.setDecimals(6);
  119. if (spinBox.decimals() != 6)
  120. {
  121. qDebug()<<"Decimals does not correspond. Got: "<<spinBox.cleanText();
  122. return EXIT_FAILURE;
  123. }
  124. if (spinBox.cleanText() != "15.000000")
  125. {
  126. qDebug()<<"Decimals does not correspond. Got: "<<spinBox.cleanText();
  127. return EXIT_FAILURE;
  128. }
  129. qDebug()<<"Round:";
  130. spinBox.setDecimals(3);
  131. double roundedValue = spinBox.round(6.67899156);
  132. if (roundedValue != 6.679)
  133. {
  134. qDebug()<<"Round does not correspond. Got: "<<roundedValue;
  135. return EXIT_FAILURE;
  136. }
  137. QSignalSpy spy(&spinBox, SIGNAL(valueChanged(double)));
  138. qDebug()<<"SetValue:";
  139. spinBox.setValue(28.36);
  140. if (spinBox.value() != 28.36)
  141. {
  142. qDebug()<<"setValue does not correspond. Got: "<<spinBox.value() ;
  143. return EXIT_FAILURE;
  144. }
  145. spinBox.setValue(28.366917352);
  146. if (spinBox.value() != 28.367)
  147. {
  148. qDebug()<<"setValue does not correspond. Got: "<<spinBox.value() ;
  149. return EXIT_FAILURE;
  150. }
  151. qDebug()<<"SetValueIfDifferent:";
  152. spinBox.setValueIfDifferent(33.312587);
  153. if (spinBox.value() != 33.313)
  154. {
  155. qDebug()<<"setValueIfDifferent does not correspond. Got: "<<spinBox.value() ;
  156. return EXIT_FAILURE;
  157. }
  158. spinBox.setValueIfDifferent(33.312960134);
  159. if (spinBox.value() != 33.313)
  160. {
  161. qDebug()<<"setValueIfDifferent does not correspond. Got: "<<spinBox.value() ;
  162. return EXIT_FAILURE;
  163. }
  164. if (spy.count() != 3)
  165. {
  166. qDebug()<<"spy got wrong number of signal sent : "<<spy.count() ;
  167. return EXIT_FAILURE;
  168. }
  169. //-----------------------------------------------------------------------------
  170. spinBox.show();
  171. if (argc < 2 || QString(argv[1]) != "-I" )
  172. {
  173. QTimer::singleShot(100, &app, SLOT(quit()));
  174. }
  175. return app.exec();
  176. }