ctkCursorPixmapWidgetTest2.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <QIcon>
  17. #include <QSignalSpy>
  18. #include <QStyle>
  19. #include <QTimer>
  20. // CTK includes
  21. #include "ctkCursorPixmapWidget.h"
  22. #include "ctkCommandLineParser.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. //-----------------------------------------------------------------------------
  27. bool imageCompare(ctkCursorPixmapWidget& cursor, QString baselineDirectory,
  28. QString baselineFilename)
  29. {
  30. QImage output = QPixmap::grabWidget(&cursor).toImage();
  31. QImage baseline(baselineDirectory + "/" + baselineFilename);
  32. return output == baseline;
  33. }
  34. //-----------------------------------------------------------------------------
  35. // (Used to create baselines, not during testing).
  36. void imageSave(ctkCursorPixmapWidget& cursor, QString baselineDirectory,
  37. QString baselineFilename)
  38. {
  39. QImage output = QPixmap::grabWidget(&cursor).toImage();
  40. output.save(baselineDirectory + "/" + baselineFilename);
  41. }
  42. //-----------------------------------------------------------------------------
  43. bool runBaselineTest(int time, QApplication& app, ctkCursorPixmapWidget& cursor,
  44. QString baselineDirectory, QString baselineFilename,
  45. QString errorMessage)
  46. {
  47. QTimer::singleShot(time, &app, SLOT(quit()));
  48. if (app.exec() == EXIT_FAILURE)
  49. {
  50. std::cerr << "ctkCursorPixmapWidget exec failed "
  51. << qPrintable(errorMessage) << std::endl;
  52. return false;
  53. }
  54. if (!imageCompare(cursor, baselineDirectory, baselineFilename))
  55. {
  56. std::cerr << "ctkCursorPixmapWidget baseline comparison failed when "
  57. << qPrintable(errorMessage) << "." << std::endl;
  58. return false;
  59. }
  60. return true;
  61. }
  62. //-----------------------------------------------------------------------------
  63. int ctkCursorPixmapWidgetTest2(int argc, char * argv [] )
  64. {
  65. QApplication app(argc, argv);
  66. // Command line parser
  67. ctkCommandLineParser parser;
  68. parser.addArgument("", "-D", QVariant::String);
  69. parser.addArgument("", "-V", QVariant::String);
  70. parser.addArgument("", "-I", QVariant::String);
  71. bool ok = false;
  72. QHash<QString, QVariant> parsedArgs = parser.parseArguments(app.arguments(), &ok);
  73. if (!ok)
  74. {
  75. std::cerr << qPrintable(parser.errorString()) << std::endl;
  76. return EXIT_FAILURE;
  77. }
  78. QString dataDirectory = parsedArgs["-D"].toString();
  79. QString baselineDirectory = parsedArgs["-V"].toString();
  80. bool interactive = parsedArgs["-I"].toBool();
  81. // The remainder is interactive, so abort now if command line args specify otherwise
  82. if (interactive)
  83. {
  84. return EXIT_SUCCESS;
  85. }
  86. // Create the cursor widget
  87. ctkCursorPixmapWidget cursor;
  88. cursor.setCursorColor(Qt::yellow);
  89. cursor.setMarginColor(Qt::blue);
  90. int time = 200;
  91. QPixmap pixmap(dataDirectory + "/" + "computerIcon.png");
  92. // Basesize is always odd
  93. QSize baseSize = pixmap.size();
  94. if (pixmap.width() % 2 == 0)
  95. {
  96. baseSize.setWidth(baseSize.width()+1);
  97. }
  98. if (pixmap.height() % 2 == 0)
  99. {
  100. baseSize.setHeight(baseSize.height()+1);
  101. }
  102. cursor.setMinimumSize(baseSize);
  103. cursor.setPixmap(pixmap.scaled(baseSize));
  104. cursor.show();
  105. // Bullseye cursor
  106. cursor.setCursorType(ctkCursorPixmapWidget::BullsEyeCursor);
  107. cursor.setBullsEyeWidth(15);
  108. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  109. "ctkCursorPixmapWidgetTest2a.png",
  110. "using bulls-eye cursor (odd size, odd bullsEye)"))
  111. {
  112. return EXIT_FAILURE;
  113. }
  114. cursor.setBullsEyeWidth(14);
  115. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  116. "ctkCursorPixmapWidgetTest2b.png",
  117. "using bulls-eye cursor (odd size, even bullsEye)"))
  118. {
  119. return EXIT_FAILURE;
  120. }
  121. cursor.resize(baseSize.width()+1, baseSize.height()+1);
  122. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  123. "ctkCursorPixmapWidgetTest2c.png",
  124. "using bulls-eye cursor (even size, even bullsEye)"))
  125. {
  126. return EXIT_FAILURE;
  127. }
  128. cursor.setBullsEyeWidth(15);
  129. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  130. "ctkCursorPixmapWidgetTest2d.png",
  131. "using bulls-eye cursor (even size, odd bullsEye)"))
  132. {
  133. return EXIT_FAILURE;
  134. }
  135. cursor.resize(baseSize);
  136. // Cursor not shown
  137. cursor.setShowCursor(false);
  138. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  139. "ctkCursorPixmapWidgetTest2e.png",
  140. "show cursor false"))
  141. {
  142. return EXIT_FAILURE;
  143. }
  144. // Crosshair cursor
  145. cursor.setShowCursor(true);
  146. cursor.setCursorType(ctkCursorPixmapWidget::CrossHairCursor);
  147. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  148. "ctkCursorPixmapWidgetTest2f.png",
  149. "using cross-hair cursor (odd size)"))
  150. {
  151. return EXIT_FAILURE;
  152. }
  153. cursor.resize(baseSize.width()+1, baseSize.height()+1);
  154. if (!runBaselineTest(time, app, cursor, baselineDirectory,
  155. "ctkCursorPixmapWidgetTest2g.png",
  156. "using cross-hair cursor (even size)"))
  157. {
  158. return EXIT_FAILURE;
  159. }
  160. return EXIT_SUCCESS;
  161. }