]> Creatis software - clitk.git/blob - vv/vvToolStructureSetManager.cxx
- add future structure set manager
[clitk.git] / vv / vvToolStructureSetManager.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18
19 #include "vvToolStructureSetManager.h"
20 #include "vvImageReader.h"
21 #include "vvStructureSetActor.h"
22 #include "vvSlicer.h"
23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <vtkLookupTable.h>
26 #include <vtkRenderWindow.h>
27
28 //------------------------------------------------------------------------------
29 // Create the tool and automagically (I like this word) insert it in
30 // the main window menu.
31 ADD_TOOL(vvToolStructureSetManager);
32 //------------------------------------------------------------------------------
33
34 //------------------------------------------------------------------------------
35 vvToolStructureSetManager::vvToolStructureSetManager(vvMainWindowBase * parent, Qt::WindowFlags f)
36   :vvToolWidgetBase(parent, f), 
37    vvToolBase<vvToolStructureSetManager>(parent), 
38    Ui::vvToolStructureSetManager() {
39   // Setup the UI
40   Ui_vvToolStructureSetManager::setupUi(mToolWidget);
41   mTree->clear();
42   mCurrentStructureSet = NULL;
43   
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;
51   }
52   #include "vvDefaultLut.h"
53
54   // Add input selector
55   AddInputSelector("Select image");
56 }
57 //------------------------------------------------------------------------------
58
59
60 //------------------------------------------------------------------------------
61 vvToolStructureSetManager::~vvToolStructureSetManager() {
62   DD("vvToolStructureSetManager DESTRUCTOR");
63 }
64 //------------------------------------------------------------------------------
65
66
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);
74 }
75 //------------------------------------------------------------------------------
76
77
78 //------------------------------------------------------------------------------
79 void vvToolStructureSetManager::InputIsSelected(vvSlicerManager *m) {
80   // Hide the input selector
81   QList<int> s;
82   s.push_back(0);
83   s.push_back(1);
84   splitter->setSizes(s);
85   // Connect open menus
86   connect(mOpenComboBox, SIGNAL(activated(int)), this, SLOT(open(int)));
87   DD(mCurrentImage->GetNumberOfDimensions());
88
89   // To trigger the Render ??
90   //  connect(m,SIGNAL(releasemouse()),this,SLOT(Render()));
91   
92   //  connect(m, SIGNAL(UpdateSlice(int, int)), SLOT(UpdateSlice(int, int)));
93   connect(m, SIGNAL(LeftButtonReleaseSignal(int)), SLOT(LeftButtonReleaseEvent(int)));
94 }
95 //------------------------------------------------------------------------------
96
97
98 //------------------------------------------------------------------------------
99 void vvToolStructureSetManager::LeftButtonReleaseEvent(int slicer) {
100   DD("vvToolStructureSetManager::UpdateSlice");
101   //DD(slicer);
102   //DD(view);
103   //DD(slices);
104   for(int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
105     if (i != slicer);
106     mCurrentSlicerManager->GetSlicer(i)->GetRenderWindow()->Render();
107   }
108 }
109 //------------------------------------------------------------------------------
110
111
112 //------------------------------------------------------------------------------
113 void vvToolStructureSetManager::open(int type) {
114   DD(type);
115   switch (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);
120   }
121 }
122 //------------------------------------------------------------------------------
123
124
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);
134   }
135 }
136 //------------------------------------------------------------------------------
137
138
139 //------------------------------------------------------------------------------
140 void vvToolStructureSetManager::addStructureSetInTreeWidget(int index, clitk::DicomRT_StructureSet * s) {
141   // Main row item
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()));
146
147   // Insert ROI
148   const std::vector<clitk::DicomRT_ROI*> & rois = s->GetListOfROI();
149   for(unsigned int i=0; i<rois.size(); i++) {
150     DD(i);
151     addRoiInTreeWidget(rois[i], ss);
152   }
153   
154   // Insert in list
155   mStructureSetItemsList[index] = ss;
156   
157   // Connect
158   //  TODO
159 }
160 //------------------------------------------------------------------------------
161
162
163 //------------------------------------------------------------------------------
164 int vvToolStructureSetManager::addStructureSet(clitk::DicomRT_StructureSet * mStructureSet) {
165
166   // Create actor for this SS
167   vvStructureSetActor * mStructureSetActor = new vvStructureSetActor;
168   mStructureSetActor->SetStructureSet(mStructureSet);
169   mStructureSetActor->SetSlicerManager(mCurrentSlicerManager);
170
171   // Insert in lists and get index
172   mStructureSetsList.push_back(mStructureSet);
173   mStructureSetActorsList.push_back(mStructureSetActor);
174   int index = mStructureSetsList.size()-1;
175
176   // Return index
177   return index;
178 }
179 //------------------------------------------------------------------------------
180
181
182 //------------------------------------------------------------------------------
183 void vvToolStructureSetManager::openBinaryImage() {
184   DD("openBinaryImage");
185   // Select current StructureSet (or create)
186   int index;
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);
191       DD(index);
192     }
193     else { // Get first SS
194       index = 0;
195     }
196     // TODO -> SET THIS SS AS CURRENT
197     mCurrentStructureSet = mStructureSetsList[index];
198     mCurrentStructureSetActor = mStructureSetActorsList[index];
199   }
200   else {
201     index = mCurrentStructureSetIndex;
202   }
203   DD(mCurrentStructureSet->GetName());
204
205   // Open images
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;
212
213   for(int i=0; i<filename.size(); i++) {
214     DD(filename[i].toStdString());
215
216     // Open Image
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();
225     
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);
231       delete mReader;
232       return;
233     }
234     vvImage::Pointer binaryImage = mReader->GetOutput();
235   //  delete mReader;
236
237   // Check Dimension
238   int dim = mCurrentImage->GetNumberOfDimensions();
239   DD(dim);
240   int bin_dim = binaryImage->GetNumberOfDimensions();
241   DD(bin_dim);
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());
247     return;
248   }
249
250   // Add a new roi to the structure
251   int n = mCurrentStructureSet->AddBinaryImageAsNewROI(binaryImage, filename[i].toStdString());
252   DD(n);
253   
254   // Change color NEED DEFAULT COLOR LIST
255   DD(mDefaultLUTColor->GetNumberOfTableValues ());
256   if (n<mDefaultLUTColor->GetNumberOfTableValues ()) {
257     double * color = mDefaultLUTColor->GetTableValue(n % mDefaultLUTColor->GetNumberOfTableValues ());
258     DD(color[0]);
259     DD(color[1]);
260     DD(color[2]);
261     mCurrentStructureSet->GetROI(n)->SetDisplayColor(color[0], color[1], color[2]);
262   }
263   
264   // Add a new roi actor
265   mCurrentStructureSetActor->CreateNewROIActor(n);
266   }
267
268   // Update the TreeWidget
269   addStructureSetInTreeWidget(index, mCurrentStructureSet);
270 }
271 //------------------------------------------------------------------------------
272
273
274 //------------------------------------------------------------------------------
275 void vvToolStructureSetManager::apply() {
276   close();
277 }
278 //------------------------------------------------------------------------------