1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvToolStructureSetManager.h"
20 #include "vvImageReader.h"
21 #include "vvStructureSetActor.h"
23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <vtkLookupTable.h>
26 #include <vtkRenderWindow.h>
28 //------------------------------------------------------------------------------
29 // Create the tool and automagically (I like this word) insert it in
30 // the main window menu.
31 ADD_TOOL(vvToolStructureSetManager);
32 //------------------------------------------------------------------------------
34 //------------------------------------------------------------------------------
35 vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent, Qt::WindowFlags f)
36 :vvToolWidgetBase(parent, f),
37 vvToolBase<vvToolStructureSetManager>(parent),
38 Ui::vvToolStructureSetManager() {
40 Ui_vvToolStructureSetManager::setupUi(mToolWidget);
42 mCurrentStructureSet = NULL;
44 mDefaultLUTColor = vtkLookupTable::New();
45 for(unsigned int i=0; i<mDefaultLUTColor->GetNumberOfTableValues(); i++) {
46 double r = (rand()/(RAND_MAX+1.0));
47 double v = (rand()/(RAND_MAX+1.0));
48 double b = (rand()/(RAND_MAX+1.0));
49 mDefaultLUTColor->SetTableValue(i, r, v, b);
50 // std::cout << "mDefaultLUTColor->SetTableValue(" << i << ", " << r << ", " << v << ", " << b << ");" << std::endl;
52 #include "vvDefaultLut.h"
55 AddInputSelector("Select image");
57 //------------------------------------------------------------------------------
60 //------------------------------------------------------------------------------
61 vvToolStructureSetManager::~vvToolStructureSetManager() {
62 DD("vvToolStructureSetManager DESTRUCTOR");
64 //------------------------------------------------------------------------------
67 //------------------------------------------------------------------------------
68 void vvToolStructureSetManager::Initialize() {
69 SetToolName("StructureSetManager");
70 SetToolMenuName("StructureSet");
71 SetToolIconFilename(":/common/icons/ducky.png");
72 SetToolTip("Display Structure Set.");
73 SetToolExperimental(true);
75 //------------------------------------------------------------------------------
78 //------------------------------------------------------------------------------
79 void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m) {
80 // Hide the input selector
84 splitter->setSizes(s);
86 connect(mOpenComboBox, SIGNAL(activated(int)), this, SLOT(open(int)));
87 DD(mCurrentImage->GetNumberOfDimensions());
89 // To trigger the Render ??
90 // connect(m,SIGNAL(releasemouse()),this,SLOT(Render()));
92 // connect(m, SIGNAL(UpdateSlice(int, int)), SLOT(UpdateSlice(int, int)));
93 connect(m, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
95 //------------------------------------------------------------------------------
98 //------------------------------------------------------------------------------
99 void vvToolStructureSetManager::LeftButtonReleaseEvent(int slicer) {
100 DD("vvToolStructureSetManager::UpdateSlice");
104 for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
106 mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
109 //------------------------------------------------------------------------------
112 //------------------------------------------------------------------------------
113 void vvToolStructureSetManager::open(int type) {
116 case 0: openBinaryImage(); return; // Open binary image;
117 case 1: DD("TODO"); return; // Open DICOM RT
118 case 2: DD("TODO"); return; // Open mesh
119 default: std::cerr << "Error ????" << std::endl; exit(0);
122 //------------------------------------------------------------------------------
125 //------------------------------------------------------------------------------
126 void vvToolStructureSetManager::addRoiInTreeWidget(clitk::DicomRT_ROI * roi, QTreeWidgetItem * ww) {
127 QTreeWidgetItem * w = new QTreeWidgetItem(ww);
128 w->setText(0, QString("%1").arg(roi->GetROINumber()));
129 w->setText(1, QString("%1").arg(roi->GetName().c_str()));
130 QBrush brush(QColor(roi->GetDisplayColor()[0]*255, roi->GetDisplayColor()[1]*255, roi->GetDisplayColor()[2]*255));
131 brush.setStyle(Qt::SolidPattern);
132 for(int i=0; i<w->columnCount (); i++) {
133 w->setBackground(i, brush);
136 //------------------------------------------------------------------------------
139 //------------------------------------------------------------------------------
140 void vvToolStructureSetManager::addStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
142 QTreeWidgetItem * ss = new QTreeWidgetItem(mTree);
143 // ss->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsTristate);
144 ss->setText(0, QString("S%1").arg(index));
145 ss->setText(1, QString("%1").arg(s->GetLabel().c_str()));
148 const std::vector<clitk::DicomRT_ROI*> & rois = s->GetListOfROI();
149 for(unsigned int i=0; i<rois.size(); i++) {
151 addRoiInTreeWidget(rois[i], ss);
155 mStructureSetItemsList[index] = ss;
160 //------------------------------------------------------------------------------
163 //------------------------------------------------------------------------------
164 int vvToolStructureSetManager::addStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
166 // Create actor for this SS
167 vvStructureSetActor * mStructureSetActor = new vvStructureSetActor;
168 mStructureSetActor->SetStructureSet(mStructureSet);
169 mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
171 // Insert in lists and get index
172 mStructureSetsList.push_back(mStructureSet);
173 mStructureSetActorsList.push_back(mStructureSetActor);
174 int index = mStructureSetsList.size()-1;
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 void vvToolStructureSetManager::openBinaryImage() {
184 DD("openBinaryImage");
185 // Select current StructureSet (or create)
187 if (mCurrentStructureSet == NULL) {
188 if (mStructureSetsList.size() == 0) { // Create a default SS
189 clitk::DicomRT_StructureSet * mStructureSet = new clitk::DicomRT_StructureSet;
190 index = addStructureSet(mStructureSet);
193 else { // Get first SS
196 // TODO -> SET THIS SS AS CURRENT
197 mCurrentStructureSet = mStructureSetsList[index];
198 mCurrentStructureSetActor = mStructureSetActorsList[index];
201 index = mCurrentStructureSetIndex;
203 DD(mCurrentStructureSet->GetName());
206 QString Extensions = "Images files ( *.mhd *.hdr *.his)";
207 Extensions += ";;All Files (*)";
208 QStringList filename =
209 QFileDialog::getOpenFileNames(this,tr("Open binary image"),
210 mMainWindowBase->GetInputPathName(),Extensions);
211 if (filename.size() == 0) return;
213 for(int i=0; i<filename.size(); i++) {
214 DD(filename[i].toStdString());
217 //init the progress events
218 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
219 vvImageReader * mReader = new vvImageReader;
220 std::vector<std::string> filenames;
221 filenames.push_back(filename[i].toStdString());
222 mReader->SetInputFilenames(filenames);
223 mReader->Update(IMAGE);
224 QApplication::restoreOverrideCursor();
226 if (mReader->GetLastError().size() != 0) {
227 std::cerr << "Error while reading " << filename[i].toStdString() << std::endl;
228 QString error = "Cannot open file \n";
229 error += mReader->GetLastError().c_str();
230 QMessageBox::information(this,tr("Reading problem"),error);
234 vvImage::Pointer binaryImage = mReader->GetOutput();
238 int dim = mCurrentImage->GetNumberOfDimensions();
240 int bin_dim = binaryImage->GetNumberOfDimensions();
242 if (dim < bin_dim) { ////////// TO CHANGE FOR 3D/4D
243 std::ostringstream os;
244 os << "Error. Loaded binary image is " << bin_dim
245 << "D while selected image is " << dim << "D" << std::endl;
246 QMessageBox::information(this,tr("Reading problem"),os.str().c_str());
250 // Add a new roi to the structure
251 int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename[i].toStdString());
254 // Change color NEED DEFAULT COLOR LIST
255 DD(mDefaultLUTColor->GetNumberOfTableValues ());
256 if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
257 double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
261 mCurrentStructureSet->GetROI(n)->SetDisplayColor(color[0], color[1], color[2]);
264 // Add a new roi actor
265 mCurrentStructureSetActor->CreateNewROIActor(n);
268 // Update the TreeWidget
269 addStructureSetInTreeWidget(index, mCurrentStructureSet);
271 //------------------------------------------------------------------------------
274 //------------------------------------------------------------------------------
275 void vvToolStructureSetManager::apply() {
278 //------------------------------------------------------------------------------