ctkVTKSliceViewTest1.cpp 4.5 KB

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