ctkVTKMagnifyViewTest1.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <QSharedPointer>
  17. #include <QTimer>
  18. // CTK includes
  19. #include "ctkVTKMagnifyView.h"
  20. // VTK includes
  21. #include <QVTKWidget.h>
  22. #include <vtkNew.h>
  23. #include <vtkRenderer.h>
  24. #include <vtkRendererCollection.h>
  25. #include <vtkRenderWindow.h>
  26. // STD includes
  27. #include <cstdlib>
  28. #include <iostream>
  29. //-----------------------------------------------------------------------------
  30. int ctkVTKMagnifyViewTest1(int argc, char * argv [] )
  31. {
  32. QApplication app(argc, argv);
  33. ctkVTKMagnifyView magnify;
  34. // check default values
  35. if (!magnify.showCrosshair() ||
  36. magnify.crosshairPen().color() != magnify.palette().color(QPalette::Highlight) ||
  37. magnify.crosshairPen().width() != 0 ||
  38. magnify.crosshairPen().joinStyle() != Qt::MiterJoin ||
  39. magnify.crosshairType() != ctkCrosshairLabel::SimpleCrosshair ||
  40. magnify.marginColor() != magnify.palette().color(QPalette::Window) ||
  41. magnify.bullsEyeWidth() != 15 ||
  42. magnify.magnification() != 1.0 ||
  43. magnify.observeRenderWindowEvents() != true ||
  44. magnify.updateInterval() != 20 ||
  45. magnify.numberObserved() != 0)
  46. {
  47. std::cerr << "ctkVTKMagnifyView: Wrong default values. " << std::endl
  48. << " " << magnify.showCrosshair()
  49. << " " << qPrintable(magnify.crosshairPen().color().name())
  50. << " " << magnify.crosshairPen().width()
  51. << " " << static_cast<int>(magnify.crosshairPen().joinStyle())
  52. << " " << magnify.crosshairType()
  53. << " " << qPrintable(magnify.marginColor().name())
  54. << " " << magnify.bullsEyeWidth()
  55. << " " << magnify.magnification()
  56. << " " << magnify.observeRenderWindowEvents()
  57. << " " << magnify.updateInterval()
  58. << " " << magnify.numberObserved() << std::endl;
  59. return EXIT_FAILURE;
  60. }
  61. // Magnification
  62. magnify.setMagnification(10.5);
  63. if (magnify.magnification() != 10.5)
  64. {
  65. std::cerr << "ctkVTKMagnifyView:setMagnification failed. "
  66. << magnify.magnification() << std::endl;
  67. return EXIT_FAILURE;
  68. }
  69. // Observe render window events
  70. magnify.setObserveRenderWindowEvents(false);
  71. if (magnify.observeRenderWindowEvents() != false)
  72. {
  73. std::cerr << "ctkVTKMagnifyView:setObserveRenderWindowEvents failed. "
  74. << magnify.observeRenderWindowEvents() << std::endl;
  75. return EXIT_FAILURE;
  76. }
  77. // Update interval
  78. magnify.setUpdateInterval(0);
  79. if (magnify.updateInterval() != 0)
  80. {
  81. std::cerr << "ctkVTKMagnifyView:setUpdateInterval failed. "
  82. << magnify.updateInterval() << std::endl;
  83. return EXIT_FAILURE;
  84. }
  85. // Adding / removing observed QVTKWidgets
  86. QObject widgetParent;
  87. QList<QVTKWidget* > allVTKWidgets;
  88. QList<QSharedPointer<QVTKWidget> > widgetsToDelete;
  89. int numVTKWidgets = 3;
  90. for (int i = 0; i < numVTKWidgets; i++)
  91. {
  92. QVTKWidget* widget = new QVTKWidget();
  93. allVTKWidgets.append(widget);
  94. widgetsToDelete.append(QSharedPointer<QVTKWidget>(widget));
  95. vtkNew<vtkRenderer> renderer;
  96. widget->GetRenderWindow()->AddRenderer(renderer.GetPointer());
  97. double gray = static_cast<double>(i) / (numVTKWidgets-1);
  98. renderer->SetBackground( gray, gray, gray);
  99. renderer->SetBackground2( 0., 0., 1.);
  100. renderer->SetGradientBackground(true);
  101. widget->show();
  102. }
  103. // Observe one widget
  104. magnify.observe(allVTKWidgets[0]);
  105. if (!magnify.isObserved(allVTKWidgets[0]) || magnify.isObserved(allVTKWidgets[1]) ||
  106. magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 1)
  107. {
  108. std::cerr << "ctkVTKMagnifyView:observe(QVTKWidget*) failed. "
  109. << "Number observed = " << magnify.numberObserved() << std::endl;
  110. return EXIT_FAILURE;
  111. }
  112. // Observe two more widgets
  113. magnify.observe(allVTKWidgets.mid(1,2));
  114. if (!magnify.isObserved(allVTKWidgets[0]) || !magnify.isObserved(allVTKWidgets[1]) ||
  115. !magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 3)
  116. {
  117. std::cerr << "ctkVTKMagnifyView:observe(QList<QVTKWidget*>) failed. "
  118. << "Number observed = " << magnify.numberObserved() << std::endl;
  119. return EXIT_FAILURE;
  120. }
  121. // Re-observe a widget
  122. magnify.observe(allVTKWidgets[2]);
  123. if (!magnify.isObserved(allVTKWidgets[0]) || !magnify.isObserved(allVTKWidgets[1]) ||
  124. !magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 3)
  125. {
  126. std::cerr << "ctkVTKMagnifyView:observe(QVTKWidget*) failed on re-observe. "
  127. << "Number observed = " << magnify.numberObserved() << std::endl;
  128. return EXIT_FAILURE;
  129. }
  130. // Re-observe a list of widgets
  131. magnify.observe(allVTKWidgets.mid(0,2));
  132. if (!magnify.isObserved(allVTKWidgets[0]) || !magnify.isObserved(allVTKWidgets[1]) ||
  133. !magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 3)
  134. {
  135. std::cerr << "ctkVTKMagnifyView:observe(QList<QVTKWidget*>) failed on re-"
  136. << "observe. Number observed = " << magnify.numberObserved()
  137. << std::endl;
  138. return EXIT_FAILURE;
  139. }
  140. // Remove a widget
  141. magnify.remove(allVTKWidgets[2]);
  142. if (!magnify.isObserved(allVTKWidgets[0]) || !magnify.isObserved(allVTKWidgets[1]) ||
  143. magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 2)
  144. {
  145. std::cerr << "ctkVTKMagnifyView:remove(QVTKWidget*) failed. "
  146. << "Number observed = " << magnify.numberObserved() << std::endl;
  147. return EXIT_FAILURE;
  148. }
  149. // Re-remove a widget
  150. magnify.remove(allVTKWidgets[2]);
  151. if (!magnify.isObserved(allVTKWidgets[0]) || !magnify.isObserved(allVTKWidgets[1]) ||
  152. magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 2)
  153. {
  154. std::cerr << "ctkVTKMagnifyView:remove(QVTKWidget*) failed on re-remove. "
  155. << "Number observed = " << magnify.numberObserved() << std::endl;
  156. return EXIT_FAILURE;
  157. }
  158. // Remove a list of widgets
  159. magnify.remove(allVTKWidgets.mid(0,2));
  160. if (magnify.isObserved(allVTKWidgets[0]) || magnify.isObserved(allVTKWidgets[1]) ||
  161. magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 0)
  162. {
  163. std::cerr << "ctkVTKMagnifyView:remove(QList<QVTKWidget*>) failed. "
  164. << "Number observed = " << magnify.numberObserved() << std::endl;
  165. return EXIT_FAILURE;
  166. }
  167. // Re-remove a list of widgets
  168. magnify.remove(allVTKWidgets.mid(1,2));
  169. if (magnify.isObserved(allVTKWidgets[0]) || magnify.isObserved(allVTKWidgets[1]) ||
  170. magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 0)
  171. {
  172. std::cerr << "ctkVTKMagnifyView:remove(QList<QVTKWidget*>) failed on re-remove."
  173. << " Number observed = " << magnify.numberObserved() << std::endl;
  174. return EXIT_FAILURE;
  175. }
  176. // Observe a list of widgets of length one
  177. magnify.observe(allVTKWidgets.mid(1,1));
  178. if (magnify.isObserved(allVTKWidgets[0]) || !magnify.isObserved(allVTKWidgets[1]) ||
  179. magnify.isObserved(allVTKWidgets[2]) || magnify.numberObserved() != 1)
  180. {
  181. std::cerr << "ctkVTKMagnifyView:observe(QList<QVTKWidget*>) failed on lists of "
  182. << "length 1. Number observed = " << magnify.numberObserved()
  183. << std::endl;
  184. return EXIT_FAILURE;
  185. }
  186. magnify.show();
  187. if (argc < 2 || QString(argv[1]) != "-I" )
  188. {
  189. QTimer::singleShot(200, &app, SLOT(quit()));
  190. }
  191. return app.exec();
  192. }