ctkVTKSliceViewTest2.cpp 4.7 KB

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