ctkVTKMagnifyWidgetTest2.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <QCursor>
  17. #include <QHBoxLayout>
  18. #include <QIcon>
  19. #include <QSignalSpy>
  20. #include <QStyle>
  21. #include <QTimer>
  22. // CTK includes
  23. #include "ctkVTKMagnifyWidget.h"
  24. #include "ctkCommandLineParser.h"
  25. #include "ctkVTKSliceView.h"
  26. // VTK includes
  27. #include <vtkImageReader2Factory.h>
  28. #include <vtkImageReader2.h>
  29. #include <vtkImageData.h>
  30. #include <vtkSmartPointer.h>
  31. // STD includes
  32. #include <cstdlib>
  33. #include <iostream>
  34. //-----------------------------------------------------------------------------
  35. bool imageCompare(ctkVTKMagnifyWidget * magnify, QString baselineDirectory,
  36. QString baselineFilename)
  37. {
  38. QImage output = QPixmap::grabWidget(magnify).toImage();
  39. QImage baseline(baselineDirectory + "/" + baselineFilename);
  40. return output == baseline;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // (Used to create baselines, not during testing).
  44. void imageSave(ctkVTKMagnifyWidget * magnify, QString baselineDirectory,
  45. QString baselineFilename)
  46. {
  47. QImage output = QPixmap::grabWidget(magnify).toImage();
  48. output.save(baselineDirectory + "/" + baselineFilename);
  49. }
  50. //-----------------------------------------------------------------------------
  51. bool runBaselineTest(int time, QApplication& app, ctkVTKMagnifyWidget * magnify,
  52. QWidget * underWidget, bool shouldBeUnder,
  53. QString baselineDirectory, QString testName,
  54. QString testNumber, QString errorMessage)
  55. {
  56. QTimer::singleShot(time, &app, SLOT(quit()));
  57. if (app.exec() == EXIT_FAILURE)
  58. {
  59. std::cerr << "ctkVTKMagnifyWidget exec failed when "
  60. << qPrintable(errorMessage) << std::endl;
  61. return false;
  62. }
  63. if (underWidget->underMouse() != shouldBeUnder)
  64. {
  65. std::cerr << "ctkMagnifyWidget mouse position failed when "
  66. << qPrintable(errorMessage) << std::endl;
  67. return false;
  68. }
  69. QString baselineFilename
  70. = "ctkVTKMagnifyWidgetTest2" + testNumber + testName + ".png";
  71. if (!imageCompare(magnify, baselineDirectory, baselineFilename))
  72. {
  73. std::cerr << "ctkVTKMagnifyWidget baseline comparison failed when "
  74. << qPrintable(errorMessage) << "." << std::endl;
  75. return false;
  76. }
  77. return true;
  78. }
  79. //-----------------------------------------------------------------------------
  80. int ctkVTKMagnifyWidgetTest2(int argc, char * argv [] )
  81. {
  82. QApplication app(argc, argv);
  83. // Command line parser
  84. ctkCommandLineParser parser;
  85. parser.addArgument("", "-D", QVariant::String);
  86. parser.addArgument("", "-V", QVariant::String);
  87. parser.addArgument("", "-I", QVariant::String);
  88. parser.addArgument("", "-T", QVariant::String);
  89. parser.addArgument("", "-S", QVariant::String);
  90. parser.addArgument("", "-M", QVariant::String);
  91. bool ok = false;
  92. QHash<QString, QVariant> parsedArgs = parser.parseArguments(app.arguments(), &ok);
  93. if (!ok)
  94. {
  95. std::cerr << qPrintable(parser.errorString()) << std::endl;
  96. return EXIT_FAILURE;
  97. }
  98. QString dataDirectory = parsedArgs["-D"].toString();
  99. QString baselineDirectory = parsedArgs["-V"].toString();
  100. QString testType = parsedArgs["-T"].toString();
  101. bool interactive = parsedArgs["-I"].toBool();
  102. int size = parsedArgs["-S"].toInt();
  103. double magnification = parsedArgs["-M"].toDouble();
  104. // Create the parent widget
  105. QWidget parentWidget;
  106. QHBoxLayout layout(&parentWidget);
  107. // Magnify widget parameters (we want an odd widget size and odd bullsEye)
  108. bool showCursor = true;
  109. QPen cursorPen(QPen(Qt::yellow));
  110. cursorPen.setJoinStyle(Qt::MiterJoin);
  111. ctkCursorPixmapWidget::CursorType cursorType
  112. = ctkCursorPixmapWidget::BullsEyeCursor;
  113. double bullsEyeWidth = magnification + 2;
  114. QColor marginColor = Qt::magenta;
  115. // Create the magnify widget
  116. ctkVTKMagnifyWidget * magnify = new ctkVTKMagnifyWidget(&parentWidget);
  117. magnify->setMinimumSize(size,size);
  118. magnify->setMaximumSize(size,size);
  119. magnify->setShowCursor(showCursor);
  120. magnify->setCursorPen(cursorPen);
  121. magnify->setCursorType(cursorType);
  122. magnify->setBullsEyeWidth(bullsEyeWidth);
  123. magnify->setMarginColor(marginColor);
  124. magnify->setMagnification(magnification);
  125. layout.addWidget(magnify);
  126. // Test magnify widget parameters
  127. if (magnify->showCursor() != showCursor)
  128. {
  129. std::cerr << "ctkVTKMagnifyWidget:setShowCursor failed. "
  130. << magnify->showCursor() << std::endl;
  131. return EXIT_FAILURE;
  132. }
  133. if (magnify->cursorPen() != cursorPen)
  134. {
  135. std::cerr << "ctkVTKMagnifyWidget:setCursorPen failed. "
  136. << qPrintable(magnify->cursorPen().color().name()) << std::endl;
  137. return EXIT_FAILURE;
  138. }
  139. if (magnify->cursorType() != cursorType)
  140. {
  141. std::cerr << "ctkVTKMagnifyWidget:setCursorType failed. "
  142. << magnify->cursorType() << std::endl;
  143. return EXIT_FAILURE;
  144. }
  145. if (magnify->bullsEyeWidth() != bullsEyeWidth)
  146. {
  147. std::cerr << "ctkVTKMagnifyWidget:setBullsEyeWidth failed. "
  148. << magnify->bullsEyeWidth() << std::endl;
  149. return EXIT_FAILURE;
  150. }
  151. if (magnify->marginColor() != marginColor)
  152. {
  153. std::cerr << "ctkVTKMagnifyWidget:setMarginColor failed. "
  154. << qPrintable(magnify->marginColor().name()) << std::endl;
  155. return EXIT_FAILURE;
  156. }
  157. if (magnify->magnification() != magnification)
  158. {
  159. std::cerr << "ctkVTKMagnifyWidget:setMagnification failed. "
  160. << magnify->magnification() << std::endl;
  161. return EXIT_FAILURE;
  162. }
  163. // The remainder is interactive, so abort now if command line args specify otherwise
  164. if (interactive)
  165. {
  166. return EXIT_SUCCESS;
  167. }
  168. // Add observed ctkVTKSliceViews (there are three, and the first two are observed)
  169. QList<ctkVTKSliceView *> allSliceViews;
  170. int numSliceViews = 3;
  171. for (int i = 0; i < numSliceViews; i++)
  172. {
  173. allSliceViews.append(new ctkVTKSliceView(&parentWidget));
  174. layout.addWidget(allSliceViews[i]);
  175. }
  176. // Observe the first two widgets
  177. magnify->observe(allSliceViews[0]->VTKWidget());
  178. magnify->observe(allSliceViews[1]->VTKWidget());
  179. if (!magnify->isObserved(allSliceViews[0]->VTKWidget()) ||
  180. !magnify->isObserved(allSliceViews[1]->VTKWidget()) ||
  181. magnify->isObserved(allSliceViews[2]->VTKWidget()) ||
  182. magnify->numberObserved() != 2)
  183. {
  184. std::cerr << "ctkVTKMagnifyWidget:observe(QVTKWidget*) failed. "
  185. << "Number observed = " << magnify->numberObserved() << std::endl;
  186. return EXIT_FAILURE;
  187. }
  188. QString imageFilename = dataDirectory + "/" + "computerIcon.png";
  189. // Instanciate the reader factory
  190. vtkSmartPointer<vtkImageReader2Factory> imageFactory =
  191. vtkSmartPointer<vtkImageReader2Factory>::New();
  192. // Instanciate an image reader
  193. vtkSmartPointer<vtkImageReader2> imageReader;
  194. imageReader.TakeReference(imageFactory->CreateImageReader2(imageFilename.toLatin1()));
  195. if (!imageReader)
  196. {
  197. std::cerr << "Failed to instanciate image reader using: "
  198. << qPrintable(imageFilename) << std::endl;
  199. return EXIT_FAILURE;
  200. }
  201. // Read image
  202. imageReader->SetFileName(imageFilename.toLatin1());
  203. imageReader->Update();
  204. vtkSmartPointer<vtkImageData> image = imageReader->GetOutput();
  205. // Setup the slice views
  206. for (int i = 0; i < numSliceViews; i++)
  207. {
  208. allSliceViews[i]->setRenderEnabled(true);
  209. allSliceViews[i]->setMinimumSize(350,350);
  210. allSliceViews[i]->setImageData(image);
  211. allSliceViews[i]->setHighlightedBoxColor(Qt::yellow);
  212. allSliceViews[i]->scheduleRender();
  213. }
  214. int time = 200;
  215. // Get cursor points of interest, used in the following tests
  216. parentWidget.move(0,0);
  217. parentWidget.show();
  218. QTimer::singleShot(time, &app, SLOT(quit()));
  219. if (app.exec() == EXIT_FAILURE)
  220. {
  221. std::cerr << "ctkVTKMagnifyWidget:show failed the first time." << std::endl;
  222. return EXIT_FAILURE;
  223. }
  224. QPoint insideSlice0 = allSliceViews[0]->mapToGlobal(
  225. QPoint(allSliceViews[0]->width()/2 + 100, allSliceViews[0]->height()/2 + 100));
  226. QPoint outside = parentWidget.mapToGlobal(
  227. QPoint(parentWidget.width(), parentWidget.height()));
  228. QPoint insideSlice1 = allSliceViews[1]->mapToGlobal(
  229. QPoint(allSliceViews[1]->width()/2 - 50, allSliceViews[1]->height() - 50));
  230. QPoint insideSlice1edge = allSliceViews[1]->mapToGlobal(
  231. QPoint(allSliceViews[1]->width() - 5, allSliceViews[1]->height() - 100));
  232. QPoint insideSlice2 = allSliceViews[2]->mapToGlobal(
  233. QPoint(allSliceViews[2]->width()/2, allSliceViews[2]->height()/2));
  234. QPoint insideSlice0bottomRightCorner = allSliceViews[0]->mapToGlobal(
  235. QPoint(allSliceViews[0]->width()-1, allSliceViews[0]->height()-1));
  236. QPoint insideSlice0topLeftCorner = allSliceViews[0]->mapToGlobal(
  237. QPoint(0,0));
  238. parentWidget.hide();
  239. // Make sure the magnify widget magnifies right away when shown with the cursor inside
  240. // an observed QVTKWidget
  241. QCursor::setPos(insideSlice0);
  242. parentWidget.show();
  243. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  244. baselineDirectory, testType, "a",
  245. "magnify widget first shown with cursor inside observed widget"))
  246. {
  247. return EXIT_FAILURE;
  248. }
  249. parentWidget.hide();
  250. // Make sure the magnify widget shows blank right away when shown with the cursor
  251. // outside the observed QVTKWidgets
  252. QCursor::setPos(outside);
  253. parentWidget.show();
  254. if (!runBaselineTest(time, app, magnify, &parentWidget, false,
  255. baselineDirectory, testType, "b",
  256. "magnify widget first shown with cursor outside observed widget"))
  257. {
  258. return EXIT_FAILURE;
  259. }
  260. // Test magnification after move to allSliceViews[1]
  261. QCursor::setPos(insideSlice1);
  262. if (!runBaselineTest(time, app, magnify, allSliceViews[1], true,
  263. baselineDirectory, testType, "c",
  264. "cursor moved inside 2nd observed widget the first time"))
  265. {
  266. return EXIT_FAILURE;
  267. }
  268. // Test magnification after move within allSliceViews[1] close to border
  269. QCursor::setPos(insideSlice1edge);
  270. if (!runBaselineTest(time, app, magnify, allSliceViews[1], true,
  271. baselineDirectory, testType, "d",
  272. "cursor moved inside 2nd observed widget the second time"))
  273. {
  274. return EXIT_FAILURE;
  275. }
  276. // Test magnification after move outside an observed widget (should be blank)
  277. QCursor::setPos(insideSlice2);
  278. if (!runBaselineTest(time, app, magnify, allSliceViews[2], true,
  279. baselineDirectory, testType, "e",
  280. "cursor moved inside unobserved widget"))
  281. {
  282. return EXIT_FAILURE;
  283. }
  284. // Test magnification after move back inside an observed widget (at extreme bottom
  285. // right corner)
  286. QCursor::setPos(insideSlice0bottomRightCorner);
  287. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  288. baselineDirectory, testType, "f",
  289. "cursor moved to bottom-right corner of observed widget"))
  290. {
  291. return EXIT_FAILURE;
  292. }
  293. // Test magnification after move back inside an observed widget (at extreme top left
  294. // corner)
  295. QCursor::setPos(insideSlice0topLeftCorner);
  296. if (!runBaselineTest(time, app, magnify, allSliceViews[0], true,
  297. baselineDirectory, testType, "g",
  298. "cursor moved to top-left corner of observed widget"))
  299. {
  300. return EXIT_FAILURE;
  301. }
  302. return EXIT_SUCCESS;
  303. }