ctkVTKSliceViewTest2.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 <QTimer>
  17. #include <QDebug>
  18. #include <QVBoxLayout>
  19. #include <QHBoxLayout>
  20. #include <QSpinBox>
  21. // CTK includes
  22. #include "ctkCommandLineParser.h"
  23. #include "ctkVTKObjectEventsObserver.h"
  24. #include "ctkVTKSliceView.h"
  25. // VTK includes
  26. #include <vtkImageReader2Factory.h>
  27. #include <vtkImageReader2.h>
  28. #include <vtkImageData.h>
  29. #include <vtkSmartPointer.h>
  30. #include <vtkInteractorStyleImage.h>
  31. #include <vtkRenderWindowInteractor.h>
  32. #include <vtkLightBoxRendererManager.h>
  33. #if CTK_USE_QVTKOPENGLWIDGET
  34. #include <QVTKOpenGLWidget.h>
  35. #endif
  36. // STD includes
  37. #include <iostream>
  38. //-----------------------------------------------------------------------------
  39. int ctkVTKSliceViewTest2(int argc, char * argv [] )
  40. {
  41. #if CTK_USE_QVTKOPENGLWIDGET
  42. QSurfaceFormat format = QVTKOpenGLWidget::defaultFormat();
  43. QSurfaceFormat::setDefaultFormat(format);
  44. #endif
  45. QApplication app(argc, argv);
  46. // Test arguments
  47. QString filename = "HeadMRVolume.mhd";
  48. // Command line parser
  49. ctkCommandLineParser parser;
  50. parser.addArgument("", "-I", QVariant::Bool);
  51. parser.addArgument("", "-D", QVariant::String);
  52. bool ok = false;
  53. QHash<QString, QVariant> parsedArgs = parser.parseArguments(app.arguments(), &ok);
  54. if (!ok)
  55. {
  56. std::cerr << qPrintable(parser.errorString()) << std::endl;
  57. return EXIT_FAILURE;
  58. }
  59. bool interactive = parsedArgs["-I"].toBool();
  60. QString data_directory = parsedArgs["-D"].toString();
  61. QString imageFilename = data_directory + "/" + filename;
  62. // Instanciate the reader factory
  63. vtkSmartPointer<vtkImageReader2Factory> imageFactory =
  64. vtkSmartPointer<vtkImageReader2Factory>::New();
  65. // Instanciate an image reader
  66. vtkSmartPointer<vtkImageReader2> imageReader;
  67. imageReader.TakeReference(imageFactory->CreateImageReader2(imageFilename.toLatin1()));
  68. if (!imageReader)
  69. {
  70. std::cerr << "Failed to instanciate image reader using: "
  71. << qPrintable(imageFilename) << std::endl;
  72. return EXIT_FAILURE;
  73. }
  74. // Read image
  75. imageReader->SetFileName(imageFilename.toLatin1());
  76. #if (VTK_MAJOR_VERSION <= 5)
  77. imageReader->Update();
  78. vtkImageData* image = imageReader->GetOutput();
  79. #else
  80. imageReader->Update(); // XXX This shouldn't be needed. See issue #467
  81. vtkAlgorithmOutput* imagePort = imageReader->GetOutputPort();
  82. #endif
  83. // Top level widget
  84. QWidget widget;
  85. // .. and its associated layout
  86. QVBoxLayout * topLevelLayout = new QVBoxLayout(&widget);
  87. topLevelLayout->setContentsMargins(0, 0, 0, 0);
  88. // Horizontal layout to contain the spinboxes
  89. QHBoxLayout * spinBoxLayout = new QHBoxLayout;
  90. topLevelLayout->addLayout(spinBoxLayout);
  91. int defaultRowCount = 4;
  92. int defaultColumnCount = 3;
  93. // SpinBox to change number of row in lightBox
  94. QSpinBox * rowSpinBox = new QSpinBox;
  95. rowSpinBox->setRange(1, 10);
  96. rowSpinBox->setSingleStep(1);
  97. rowSpinBox->setValue(defaultRowCount);
  98. spinBoxLayout->addWidget(rowSpinBox);
  99. // SpinBox to change number of column in lightBox
  100. QSpinBox * columnSpinBox = new QSpinBox;
  101. columnSpinBox->setRange(1, 10);
  102. columnSpinBox->setSingleStep(1);
  103. columnSpinBox->setValue(defaultColumnCount);
  104. spinBoxLayout->addWidget(columnSpinBox);
  105. ctkVTKSliceView * sliceView = new ctkVTKSliceView;
  106. sliceView->setRenderEnabled(true);
  107. sliceView->setMinimumSize(600, 600);
  108. #if (VTK_MAJOR_VERSION <= 5)
  109. sliceView->setImageData(image);
  110. #else
  111. sliceView->setImageDataConnection(imagePort);
  112. #endif
  113. sliceView->setHighlightedBoxColor(QColor(Qt::yellow));
  114. sliceView->lightBoxRendererManager()->SetRenderWindowLayout(defaultRowCount, defaultColumnCount);
  115. sliceView->lightBoxRendererManager()->SetHighlighted(0, 0, true);
  116. sliceView->setCornerAnnotationText("CTK");
  117. sliceView->scheduleRender();
  118. topLevelLayout->addWidget(sliceView);
  119. // Set connection
  120. QObject::connect(rowSpinBox, SIGNAL(valueChanged(int)),
  121. sliceView, SLOT(setLightBoxRendererManagerRowCount(int)));
  122. QObject::connect(columnSpinBox, SIGNAL(valueChanged(int)),
  123. sliceView, SLOT(setLightBoxRendererManagerColumnCount(int)));
  124. ctkVTKObjectEventsObserver vtkObserver;
  125. vtkObserver.addConnection(sliceView->lightBoxRendererManager(), vtkCommand::ModifiedEvent,
  126. sliceView, SLOT(scheduleRender()));
  127. widget.show();
  128. // TODO Add image regression test
  129. if (!interactive)
  130. {
  131. QTimer::singleShot(1000, &app, SLOT(quit()));
  132. }
  133. return app.exec();
  134. }