ctkVTKWidgetsUtilsTestGrabWidget.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <QDialog>
  17. #include <QFrame>
  18. #include <QLabel>
  19. #include <QTimer>
  20. #include <QVBoxLayout>
  21. // CTK includes
  22. #include "ctkVTKOpenGLNativeWidget.h"
  23. #include "ctkVTKRenderView.h"
  24. #include "ctkVTKWidgetsUtils.h"
  25. #include "ctkWidgetsUtils.h"
  26. // STD includes
  27. #include <cstdlib>
  28. #include <iostream>
  29. //-----------------------------------------------------------------------------
  30. int ctkVTKWidgetsUtilsTestGrabWidget(int argc, char * argv [] )
  31. {
  32. #if CTK_USE_QVTKOPENGLWIDGET
  33. QSurfaceFormat format = ctkVTKOpenGLNativeWidget::defaultFormat();
  34. format.setSamples(0);
  35. QSurfaceFormat::setDefaultFormat(format);
  36. #endif
  37. QApplication app(argc, argv);
  38. QFrame parentWidget;
  39. parentWidget.setFrameStyle(QFrame::Panel | QFrame::Raised);
  40. parentWidget.setLineWidth(2);
  41. ctkVTKRenderView vtkWidget(&parentWidget);
  42. QVBoxLayout* layout = new QVBoxLayout(&parentWidget);
  43. layout->addWidget(&vtkWidget);
  44. parentWidget.setLayout(layout);
  45. parentWidget.resize(200, 200);
  46. parentWidget.show();
  47. // Add a dialog to cover vtkWidget to test if grabWidget works even when the
  48. // opengl view is covered.
  49. QDialog dialog(0);
  50. dialog.move(parentWidget.pos());
  51. dialog.show();
  52. QImage screenshot =
  53. ctk::grabVTKWidget(&parentWidget);
  54. if (QColor(screenshot.pixel(100, 100)) != QColor(Qt::black))
  55. {
  56. std::cout << "Failed to grab ctkVTKOpenGLNativeWidget, pixel at (100,100)="
  57. << screenshot.pixel(100, 100) << " " << QColor(Qt::black).rgb() << std::endl;
  58. return EXIT_FAILURE;
  59. }
  60. QLabel screenshotLabel;
  61. screenshotLabel.setPixmap(QPixmap::fromImage(screenshot));
  62. screenshotLabel.show();
  63. if (argc < 2 || QString(argv[1]) != "-I")
  64. {
  65. QTimer::singleShot(100, &app, SLOT(quit()));
  66. }
  67. return app.exec();
  68. }