ctkMatrixWidgetTest2.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <QHBoxLayout>
  17. #include <QMouseEvent>
  18. #include <QTimer>
  19. // CTK includes
  20. #include "ctkMatrixWidget.h"
  21. // STD includes
  22. #include <iostream>
  23. //-----------------------------------------------------------------------------
  24. int ctkMatrixWidgetTest2(int argc, char * argv [] )
  25. {
  26. QApplication app(argc, argv);
  27. QWidget topLevel;
  28. QHBoxLayout* layout = new QHBoxLayout(&topLevel);
  29. ctkMatrixWidget matrixWidget;
  30. // 4x4 by default, if not anymore, change the documentation
  31. if (matrixWidget.rowCount() != 4 ||
  32. matrixWidget.columnCount() != 4)
  33. {
  34. std::cerr << "Default constructor doesn't create a 4x4 matrix" << std::endl;
  35. return EXIT_FAILURE;
  36. }
  37. for (int i = 0; i != 4; ++i)
  38. {
  39. for (int j = 0; j != 4; ++j)
  40. {
  41. if ((i == j && matrixWidget.value(i,j) != 1.) ||
  42. (i != j && matrixWidget.value(i,j) != 0.))
  43. {
  44. std::cerr << "Not an identity matrix: (" << i << "," << j << ") = "
  45. << matrixWidget.value(i,j) << std::endl;
  46. return EXIT_FAILURE;
  47. }
  48. }
  49. }
  50. matrixWidget.setValue(2,3, 15.352);
  51. if (matrixWidget.value(2,3) != 15.352)
  52. {
  53. std::cerr << "ctkMatrixWidget::setValue() failed: "
  54. << matrixWidget.value(2,3) << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. // resize the matrix:
  58. matrixWidget.setRowCount(8);
  59. if (matrixWidget.rowCount() != 8 ||
  60. matrixWidget.columnCount() != 4)
  61. {
  62. std::cerr << "ctkMatrixWidget::setRowCount() failed "
  63. << matrixWidget.rowCount() << std::endl;
  64. return EXIT_FAILURE;
  65. }
  66. matrixWidget.setColumnCount(6);
  67. if (matrixWidget.rowCount() != 8 ||
  68. matrixWidget.columnCount() != 6)
  69. {
  70. std::cerr << "ctkMatrixWidget::setColumnCount() failed: "
  71. << matrixWidget.columnCount() << std::endl;
  72. return EXIT_FAILURE;
  73. }
  74. matrixWidget.setEditable(false);
  75. if (matrixWidget.isEditable())
  76. {
  77. std::cerr << "ctkMatrixWidget::setEditable() failed" << std::endl;
  78. return EXIT_FAILURE;
  79. }
  80. matrixWidget.setMinimum(0.5);
  81. if (matrixWidget.minimum() != 0.5 ||
  82. matrixWidget.value(1,0) != 0.5 )
  83. {
  84. std::cerr << "ctkMatrixWidget::setMinimum() failed:"
  85. << matrixWidget.value(1,0) << std::endl;
  86. return EXIT_FAILURE;
  87. }
  88. matrixWidget.setMaximum(0.7);
  89. if (matrixWidget.maximum() != 0.7 ||
  90. matrixWidget.value(1,1) != 0.7 )
  91. {
  92. std::cerr << "ctkMatrixWidget::setMaximum() failed:"
  93. << matrixWidget.value(1,1) << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. matrixWidget.setMinimum(40.);
  97. if (matrixWidget.minimum() != 40. ||
  98. matrixWidget.maximum() != 40. ||
  99. matrixWidget.value(2,2) != 40. ||
  100. matrixWidget.value(1,2) != 40.)
  101. {
  102. std::cerr << "ctkMatrixWidget::setMinimum() failed:"
  103. << matrixWidget.maximum() << std::endl;
  104. return EXIT_FAILURE;
  105. }
  106. matrixWidget.setRowCount(3);
  107. matrixWidget.setColumnCount(3);
  108. if (matrixWidget.minimum() != 40. ||
  109. matrixWidget.maximum() != 40. ||
  110. matrixWidget.value(2,2) != 40. ||
  111. matrixWidget.value(1,2) != 40.)
  112. {
  113. std::cerr << "ctkMatrixWidget::setMinimum() failed:"
  114. << matrixWidget.maximum() << std::endl;
  115. return EXIT_FAILURE;
  116. }
  117. matrixWidget.setRange(250., 10.);
  118. if (matrixWidget.minimum() != 10. ||
  119. matrixWidget.maximum() != 250. ||
  120. matrixWidget.value(0,0) != 40. ||
  121. matrixWidget.value(2,1) != 40.)
  122. {
  123. std::cerr << "ctkMatrixWidget::setRange() failed:"
  124. << matrixWidget.minimum() << " "
  125. << matrixWidget.maximum() << std::endl;
  126. return EXIT_FAILURE;
  127. }
  128. QVector<double> items;
  129. items.push_back(200.);
  130. items.push_back(201.);
  131. items.push_back(202.);
  132. items.push_back(203.);
  133. items.push_back(204.);
  134. items.push_back(205.);
  135. items.push_back(206.);
  136. items.push_back(207.);
  137. items.push_back(208.);
  138. matrixWidget.setValues(items);
  139. if (matrixWidget.value(2,1) != 207.)
  140. {
  141. std::cerr << "ctkMatrixWidget::setValues() failed:"
  142. << matrixWidget.value(2,1) << std::endl;
  143. return EXIT_FAILURE;
  144. }
  145. matrixWidget.identity();
  146. if (matrixWidget.value(0,0) != 10. ||
  147. matrixWidget.value(1,0) != 10.)
  148. {
  149. std::cerr << "ctkMatrixWidget::identity() failed:"
  150. << matrixWidget.value(0,0) << " "
  151. << matrixWidget.value(1,0) <<std::endl;
  152. return EXIT_FAILURE;
  153. }
  154. matrixWidget.setRange(-100, 100.);
  155. matrixWidget.identity();
  156. if (matrixWidget.value(1,1) != 1. ||
  157. matrixWidget.value(0,2) != 0.)
  158. {
  159. std::cerr << "ctkMatrixWidget::identity() failed:"
  160. << matrixWidget.value(1,1) << " "
  161. << matrixWidget.value(0,2) <<std::endl;
  162. return EXIT_FAILURE;
  163. }
  164. matrixWidget.setSingleStep(1.);
  165. if (matrixWidget.singleStep() != 1.)
  166. {
  167. std::cerr << "ctkMatrixWidget::setSingleStep() failed:"
  168. << matrixWidget.singleStep() << std::endl;
  169. }
  170. matrixWidget.setDecimals(5);
  171. if (matrixWidget.decimals() != 5)
  172. {
  173. std::cerr << "ctkMatrixWidget::setDecimals() failed:"
  174. << matrixWidget.decimals() << std::endl;
  175. }
  176. matrixWidget.setDecimals(-1);
  177. if (matrixWidget.decimals() != 0)
  178. {
  179. std::cerr << "ctkMatrixWidget::setDecimals() failed:"
  180. << matrixWidget.decimals() << std::endl;
  181. }
  182. matrixWidget.setEditable(true);
  183. layout->addWidget(&matrixWidget);
  184. topLevel.setLayout(layout);
  185. topLevel.show();
  186. topLevel.resize(300, 401);
  187. if (argc < 2 || QString(argv[1]) != "-I" )
  188. {
  189. QTimer::singleShot(200, &app, SLOT(quit()));
  190. }
  191. return app.exec();
  192. }