]> Creatis software - clitk.git/blob - vv/vvMainWindow.cxx
760fa758558712aa798d07847b614a2ffd5f569f
[clitk.git] / vv / vvMainWindow.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 <algorithm>
20 #include <QMessageBox>
21 #include <QInputDialog>
22 #include <QTimer>
23 #include "QTreePushButton.h"
24
25 // VV include
26 #include "vvMainWindow.h"
27 #include "vvHelpDialog.h"
28 #include "vvDocumentation.h"
29 #include "vvProgressDialog.h"
30 #include "vvQDicomSeriesSelector.h"
31 #include "vvSlicerManager.h"
32 #include "clitkImageCommon.h"
33 #include "vvSlicer.h"
34 #include "vvInteractorStyleNavigator.h"
35 #include "vvImageWriter.h"
36 #include "vvSegmentationDialog.h"
37 #include "vvSurfaceViewerDialog.h"
38 #include "vvDeformationDialog.h"
39 #include "vvImageWarp.h"
40 #include "vvUtils.h"
41 #include "vvMidPosition.h"
42 #include "vvMesh.h"
43 #include "vvStructSelector.h"
44 #include "vvMeshReader.h"
45 #include "vvConstants.h"
46 #include "clitkConfiguration.h"
47
48 // ITK include
49 #include "itkImage.h"
50 #include "itkImageFileReader.h"
51 #include "itkByteSwapper.h"
52 #include "itkCommand.h"
53 #include "itkNumericSeriesFileNames.h"
54
55 // VTK include
56 #include "vtkImageData.h"
57 #include "vtkImageActor.h"
58 #include "vtkCornerAnnotation.h"
59 #include "vtkRenderWindow.h"
60 #include "vtkRenderWindowInteractor.h"
61 #include "vtkRenderer.h"
62 #include "vtkRendererCollection.h"
63 #include "vtkWindowToImageFilter.h"
64 #include "vtkBMPWriter.h"
65 #include "vtkTIFFWriter.h"
66 #include "vtkPNMWriter.h"
67 #include "vtkPNGWriter.h"
68 #include "vtkJPEGWriter.h"
69 #include "vtkMatrix4x4.h"
70 #include "vtkTransform.h"
71
72 // Standard includes
73 #include <iostream>
74 #include <sstream>
75 #include <iomanip>
76
77 #define COLUMN_TREE 0
78 #define COLUMN_UL_VIEW 1
79 #define COLUMN_UR_VIEW 2
80 #define COLUMN_DL_VIEW 3
81 #define COLUMN_DR_VIEW 4
82 #define COLUMN_CLOSE_IMAGE 5
83 #define COLUMN_RELOAD_IMAGE 6
84 #define COLUMN_IMAGE_NAME 7
85
86 #define EXTENSIONS "Images ( *.bmp *.png *.jpeg *.jpg *.tif *.mhd *.hdr *.vox *.his *.xdr *.SCAN )"
87
88 /*Data Tree values
89   0,Qt::UserRole full filename
90   1,Qt::CheckStateRole checkbutton UL View
91   1,Qt::UserRole overlay, fusion or vector
92   2,Qt::CheckStateRole checkbutton UR View
93   3,Qt::CheckStateRole checkbutton DL View
94   4,Qt::CheckStateRole checkbutton DR View
95   5,0  short filename
96   5,Qt::UserRole mSlicerManager id*/
97
98 //------------------------------------------------------------------------------
99 vvMainWindow::vvMainWindow():vvMainWindowBase()
100 {
101   setupUi(this); // this sets up the GUI
102
103   mInputPathName = "";
104   mMenuTools = menuTools;
105   //  mMenuSegmentation = menuSegmentation;
106   mContextMenu = &contextMenu;
107   mMenuExperimentalTools = menuExperimental;
108   mMainWidget = this;
109   mCurrentTime = -1;
110   mCurrentSelectedImageId = "";
111   mCurrentPickedImageId = "";
112   mCurrentPickedImageIndex = 0;
113
114   //Init the contextMenu
115   this->setContextMenuPolicy(Qt::CustomContextMenu);
116   contextActions.resize(0);
117   QAction* actionOpen_new_image = contextMenu.addAction(QIcon(QString::fromUtf8(":/common/icons/fileopen.png")),
118                                   tr("O&pen new Image"));
119   actionOpen_new_image->setShortcut(QKeySequence(tr("Ctrl+O")));
120   connect(actionOpen_new_image,SIGNAL(triggered()),this,SLOT(OpenImages()));
121   contextActions.push_back(actionOpen_new_image);
122   contextMenu.addSeparator();
123
124   QAction* actionClose_Image = contextMenu.addAction(QIcon(QString::fromUtf8(":/common/icons/exit.png")),
125                                tr("Close Current Image"));
126   connect(actionClose_Image,SIGNAL(triggered()),this,SLOT(CloseImage()));
127   contextActions.push_back(actionClose_Image);
128
129   QAction* actionReload_image = contextMenu.addAction(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")),
130                                 tr("Reload Current Image"));
131   connect(actionReload_image,SIGNAL(triggered()),this,SLOT(ReloadImage()));
132   contextActions.push_back(actionReload_image);
133
134   QAction* actionSave_image = contextMenu.addAction(QIcon(QString::fromUtf8(":/common/icons/filesave.png")),
135                               tr("Save Current Image"));
136   connect(actionSave_image,SIGNAL(triggered()),this,SLOT(SaveAs()));
137   contextActions.push_back(actionSave_image);
138
139   contextMenu.addSeparator();
140
141   contextMenu.addAction(actionAdd_VF_to_current_Image);
142   contextActions.push_back(actionAdd_VF_to_current_Image);
143
144   QAction* actionAdd_Overlay_to_current_Image = menuOverlay->addAction(QIcon(QString::fromUtf8(":/common/icons/GPSup.png")),
145       tr("Add overlay image to current image"));
146   contextMenu.addAction(actionAdd_Overlay_to_current_Image);
147   contextActions.push_back(actionAdd_Overlay_to_current_Image);
148
149   connect(actionAdd_Overlay_to_current_Image,SIGNAL(triggered()), this,SLOT(SelectOverlayImage()));
150
151   contextMenu.addAction(actionAdd_fusion_image);
152   connect(actionAdd_fusion_image,SIGNAL(triggered()),this,SLOT(SelectFusionImage()));
153   contextActions.push_back(actionAdd_fusion_image);
154
155   // TRIAL DS
156   /*
157   QMenu * m = new QMenu(menubar);
158   m->setTitle("TOTO");
159   //  m->setObjectName(QString::fromUtf8("TOTOTO"));
160   contextMenu.addMenu(m);
161   QAction * a = m->addAction(QIcon(QString::fromUtf8(":/common/icons/GPSup.png")),
162                      tr("BIDON"));
163   QAction * b = m->addAction(QIcon(QString::fromUtf8(":/common/icons/GPSup.png")),
164                      tr("BIDON2"));
165   m->addAction(a);
166   m->addAction(b);
167   connect(a,SIGNAL(triggered()),this,SLOT(AddFusionImage()));
168   */
169
170   //init the DataTree
171   mSlicerManagers.resize(0);
172
173   QStringList header;
174   header.append("");
175   header.append("TL");
176   header.append("TR");
177   header.append("BL");
178   header.append("BR");
179   header.append("");
180   header.append("");
181   header.append("Name");
182
183   DataTree->setHeaderLabels(header);
184   DataTree->resizeColumnToContents(COLUMN_TREE);
185   DataTree->resizeColumnToContents(COLUMN_UL_VIEW);
186   DataTree->resizeColumnToContents(COLUMN_UR_VIEW);
187   DataTree->resizeColumnToContents(COLUMN_DL_VIEW);
188   DataTree->resizeColumnToContents(COLUMN_DR_VIEW);
189   DataTree->resizeColumnToContents(COLUMN_CLOSE_IMAGE);
190   DataTree->resizeColumnToContents(COLUMN_RELOAD_IMAGE);
191   DataTree->resizeColumnToContents(COLUMN_IMAGE_NAME);
192
193   viewMode = 1;
194   documentation = new vvDocumentation();
195   help_dialog = new vvHelpDialog();
196   dicomSeriesSelector = new vvDicomSeriesSelector();
197
198   inverseButton->setEnabled(0);
199   actionAdd_Overlay_to_current_Image->setEnabled(0);
200   actionSave_As->setEnabled(0);
201   actionAdd_VF_to_current_Image->setEnabled(0);
202   actionAdd_fusion_image->setEnabled(0);
203
204   //init the sliders
205   verticalSliders.push_back(NOVerticalSlider);
206   verticalSliders.push_back(NEVerticalSlider);
207   verticalSliders.push_back(SOVerticalSlider);
208   verticalSliders.push_back(SEVerticalSlider);
209
210   for (int i =0; i < 4; i++)
211     verticalSliders[i]->hide();
212
213   horizontalSliders.push_back(NOHorizontalSlider);
214   horizontalSliders.push_back(NEHorizontalSlider);
215   horizontalSliders.push_back(SOHorizontalSlider);
216   horizontalSliders.push_back(SEHorizontalSlider);
217
218   for (int i =0; i < 4; i++)
219     horizontalSliders[i]->hide();
220
221
222   connect(NOVerticalSlider,SIGNAL(valueChanged(int)),this,SLOT(NOVerticalSliderChanged()));
223   connect(NEVerticalSlider,SIGNAL(valueChanged(int)),this,SLOT(NEVerticalSliderChanged()));
224   connect(SOVerticalSlider,SIGNAL(valueChanged(int)),this,SLOT(SOVerticalSliderChanged()));
225   connect(SEVerticalSlider,SIGNAL(valueChanged(int)),this,SLOT(SEVerticalSliderChanged()));
226
227   connect(NOHorizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(NOHorizontalSliderMoved()));
228   connect(NEHorizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(NEHorizontalSliderMoved()));
229   connect(SOHorizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(SOHorizontalSliderMoved()));
230   connect(SEHorizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(SEHorizontalSliderMoved()));
231
232   //connect everything
233   connect(actionCompute_mid_position_image,SIGNAL(triggered()),this,SLOT(ComputeMidPosition()));
234   connect(actionDeformable_Registration,SIGNAL(triggered()),this,SLOT(ComputeDeformableRegistration()));
235   connect(actionWarp_image_with_vector_field,SIGNAL(triggered()),this,SLOT(WarpImage()));
236   connect(actionLoad_images,SIGNAL(triggered()),this,SLOT(OpenImages()));
237   connect(actionOpen_Dicom,SIGNAL(triggered()),this,SLOT(OpenDicom()));
238   connect(actionOpen_Dicom_Struct,SIGNAL(triggered()),this,SLOT(OpenDCStructContour()));
239   connect(actionOpen_VTK_contour,SIGNAL(triggered()),this,SLOT(OpenVTKContour()));
240   connect(actionOpen_Multiple_Images_As_One,SIGNAL(triggered()),this,SLOT(MergeImages()));
241   connect(actionSlice_Image_As_Multiple_Images,SIGNAL(triggered()),this,SLOT(SliceImages()));
242   connect(actionOpen_Image_With_Time,SIGNAL(triggered()),this,SLOT(OpenImageWithTime()));
243   connect(actionMerge_images_as_n_dim_t, SIGNAL(triggered()), this, SLOT(MergeImagesWithTime()));
244   connect(actionSave_As,SIGNAL(triggered()),this,SLOT(SaveAs()));
245   connect(actionExit,SIGNAL(triggered()),this,SLOT(close()));
246   connect(actionAdd_VF_to_current_Image,SIGNAL(triggered()),this,SLOT(OpenField()));
247   connect(actionNavigation_Help,SIGNAL(triggered()),this,SLOT(ShowHelpDialog()));
248   connect(actionDocumentation,SIGNAL(triggered()),this,SLOT(ShowDocumentation()));
249
250   ///////////////////////////////////////////////
251   connect(actionSegmentation,SIGNAL(triggered()),this,SLOT(SegmentationOnCurrentImage()));
252   connect(actionSurface_Viewer,SIGNAL(triggered()),this,SLOT(SurfaceViewerLaunch()));
253   ///////////////////////////////////////////////
254
255   actionNorth_East_Window->setEnabled(0);
256   actionNorth_West_Window->setEnabled(0);
257   actionSouth_East_Window->setEnabled(0);
258   actionSouth_West_Window->setEnabled(0);
259
260   connect(actionNorth_East_Window,SIGNAL(triggered()),this,SLOT(SaveNEScreenshot()));
261   connect(actionNorth_West_Window,SIGNAL(triggered()),this,SLOT(SaveNOScreenshot()));
262   connect(actionSouth_East_Window,SIGNAL(triggered()),this,SLOT(SaveSEScreenshot()));
263   connect(actionSouth_West_Window,SIGNAL(triggered()),this,SLOT(SaveSOScreenshot()));
264
265   connect(DataTree,SIGNAL(itemSelectionChanged()),this,SLOT(ImageInfoChanged()));
266   connect(DataTree,SIGNAL(itemClicked(QTreeWidgetItem*, int)),this,
267           SLOT(DisplayChanged(QTreeWidgetItem*, int)));
268
269   connect(viewButton,SIGNAL(clicked()),this, SLOT(ChangeViewMode()) );
270   connect(windowSpinBox,SIGNAL(editingFinished()),this,SLOT(WindowLevelEdited()));
271   connect(levelSpinBox,SIGNAL(editingFinished()),this,SLOT(WindowLevelEdited()));
272   connect(colorMapComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(UpdateColorMap()));
273   connect(presetComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(UpdateWindowLevel()));
274   connect(inverseButton,SIGNAL(clicked()),this,SLOT(SwitchWindowLevel()));
275   connect(applyWindowLevelToAllButton,SIGNAL(clicked()),this,SLOT(ApplyWindowLevelToAllImages()));
276
277
278   connect(this,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(ShowContextMenu(QPoint)));
279
280   connect(linkPanel,SIGNAL(addLink(QString,QString)),this,SLOT(AddLink(QString,QString)));
281   connect(linkPanel,SIGNAL(removeLink(QString,QString)),this,SLOT(RemoveLink(QString,QString)));
282   connect(overlayPanel,SIGNAL(VFPropertyUpdated(int,int,int,int,double,double,double)),this,SLOT(SetVFProperty(int,int,int,int,double,double,double)));
283   connect(overlayPanel,SIGNAL(OverlayPropertyUpdated(int)),this,SLOT(SetOverlayProperty(int)));
284   connect(overlayPanel,SIGNAL(FusionPropertyUpdated(int,int,double,double)),
285           this,SLOT(SetFusionProperty(int,int,double,double)));
286   connect(landmarksPanel,SIGNAL(UpdateRenderWindows()),this,SLOT(UpdateRenderWindows()));
287
288   playMode = 0;//pause
289   mFrameRate = 10;
290   playButton->setEnabled(0);
291   frameRateLabel->setEnabled(0);
292   frameRateSpinBox->setEnabled(0);
293   connect(playButton, SIGNAL(clicked()),this,SLOT(PlayPause()));
294   connect(frameRateSpinBox, SIGNAL(valueChanged(int)),this,SLOT(ChangeFrameRate(int)));
295
296   goToCursorPushButton->setEnabled(0);
297   connect(goToCursorPushButton, SIGNAL(clicked()),this,SLOT(GoToCursor()));
298
299   NOViewWidget->hide();
300   NEViewWidget->hide();
301   SOViewWidget->hide();
302   SEViewWidget->hide();
303
304   //Recently opened files
305   std::list<std::string> recent_files = GetRecentlyOpenedImages();
306   if ( !recent_files.empty() ) {
307     QMenu * rmenu = new QMenu("Recently opened files...");
308     rmenu->setIcon(QIcon(QString::fromUtf8(":/common/icons/open.png")));
309     menuFile->insertMenu(actionOpen_Image_With_Time,rmenu);
310     menuFile->insertSeparator(actionOpen_Image_With_Time);
311     for (std::list<std::string>::iterator i = recent_files.begin(); i!=recent_files.end(); i++) {
312       QAction* current=new QAction(QIcon(QString::fromUtf8(":/common/icons/open.png")),
313                                    (*i).c_str(),this);
314       rmenu->addAction(current);
315       connect(current,SIGNAL(triggered()),this,SLOT(OpenRecentImage()));
316     }
317   }
318
319   // Adding all new tools (insertion in the menu)
320   vvToolManager::GetInstance()->InsertToolsInMenu(this);
321
322   if (!CLITK_EXPERIMENTAL)
323     menuExperimental->menuAction()->setVisible(false);
324
325
326   QTimer * timerMemory = new QTimer(this);
327   //timerMemory->setInterval(5);
328   connect(timerMemory, SIGNAL(timeout()), this, SLOT(UpdateMemoryUsage()));
329   timerMemory->start(2000);
330   
331 }
332 //------------------------------------------------------------------------------
333
334
335 //------------------------------------------------------------------------------
336 void vvMainWindow::UpdateMemoryUsage()
337 {
338   //  clitk::PrintMemory(true);
339   if (clitk::GetMemoryUsageInMb() == 0) infoPanel->setMemoryInMb("NA");
340   else infoPanel->setMemoryInMb(QString::number(clitk::GetMemoryUsageInMb())+" MiB");
341 }
342 //------------------------------------------------------------------------------
343
344
345 //------------------------------------------------------------------------------
346 void vvMainWindow::ComputeMidPosition()
347 {
348   bool ok;
349   int index=GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
350   int ref = QInputDialog::getInteger(this,"Chose reference phase","Reference phase",0,0,\
351                                      mSlicerManagers[index]->GetImage()->GetVTKImages().size()-1,1,&ok);
352   if (ok) {
353     vvMidPosition midp;
354     midp.slicer_manager = mSlicerManagers[index];
355     midp.reference_image_index = ref;
356     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
357     midp.Update();
358     if (midp.error)
359       QMessageBox::warning(this, "Error computing midposition image",midp.error_message.c_str());
360     else {
361       QFileInfo info(midp.slicer_manager->GetFileName().c_str());
362       AddImage(midp.output,info.path().toStdString()+"/"+info.completeBaseName().toStdString()+"_midposition.mhd");
363     }
364     QApplication::restoreOverrideCursor();
365   }
366 }
367 //------------------------------------------------------------------------------
368
369
370 //------------------------------------------------------------------------------
371 void vvMainWindow::AddContour(int image_index, vvMesh::Pointer contour, bool propagation)
372 {
373   QTreeWidgetItem *item = new QTreeWidgetItem();
374   item->setData(0,Qt::UserRole,"filename.vtk");
375   item->setData(1,Qt::UserRole,tr("contour"));
376   QBrush brush;
377   brush.setColor(QColor(contour->r*255,contour->g*255,contour->b*255));
378   brush.setStyle(Qt::SolidPattern);
379   item->setData(COLUMN_IMAGE_NAME,Qt::BackgroundRole,brush);
380   item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,contour->structure_name.c_str());
381
382   for (int j = 1; j <= 4; j++)
383     item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(image_index)->data(j,Qt::CheckStateRole));
384
385   QTreePushButton* cButton = new QTreePushButton;
386   cButton->setItem(item);
387   cButton->setColumn(COLUMN_CLOSE_IMAGE);
388   cButton->setToolTip(tr("close image"));
389   cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
390   connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
391           this,SLOT(CloseImage(QTreeWidgetItem*, int)));
392
393   QTreePushButton* rButton = new QTreePushButton;
394   rButton->setItem(item);
395   rButton->setColumn(COLUMN_RELOAD_IMAGE);
396   rButton->setToolTip(tr("reload image"));
397   rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
398   rButton->setEnabled(false);
399   //Not implemented
400   //connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
401   //this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
402
403   DataTree->topLevelItem(image_index)->setExpanded(1);
404   DataTree->topLevelItem(image_index)->addChild(item);
405   DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
406   DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
407   QString id = DataTree->topLevelItem(image_index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
408   item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
409
410   UpdateTree();
411   mSlicerManagers[image_index]->AddContour(contour,propagation);
412   mSlicerManagers[image_index]->Render();
413 }
414 //------------------------------------------------------------------------------
415
416
417 //------------------------------------------------------------------------------
418 void vvMainWindow::OpenVTKContour()
419 {
420   if (mSlicerManagers.size() > 0) {
421     QString Extensions = "Images ( *.vtk *.obj)";
422     Extensions += ";;All Files (*)";
423     QString file = QFileDialog::getOpenFileName(this,tr("Open vtkPolyData"),mInputPathName,Extensions);
424     if (file.isNull())
425       return;
426     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
427     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
428     vvMeshReader reader;
429     reader.SetImage(mSlicerManagers[index]->GetImage());
430     reader.SetModeToVTK();
431     reader.SetFilename(file.toStdString());
432     reader.Update();
433     AddContour(index,reader.GetOutput()[0],false);
434     QApplication::restoreOverrideCursor();
435   }
436 }
437 //------------------------------------------------------------------------------
438
439
440 //------------------------------------------------------------------------------
441 void vvMainWindow::OpenDCStructContour()
442 {
443   if (mSlicerManagers.size() > 0) {
444     QString Extensions = "Dicom Files ( *.dcm RS*)";
445     Extensions += ";;All Files (*)";
446     QString file = QFileDialog::getOpenFileName(this,tr("Merge Images"),mInputPathName,Extensions);
447     if (file.isNull())
448       return;
449     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
450     vvMeshReader reader;
451     reader.SetFilename(file.toStdString());
452     vvStructSelector selector;
453     selector.SetStructures(reader.GetROINames());
454     if (!mSlicerManagers[index]->GetVF().IsNull())
455       selector.EnablePropagationCheckBox();
456     if (selector.exec()) {
457       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
458       reader.SetSelectedItems(selector.getSelectedItems());
459       reader.SetImage(mSlicerManagers[index]->GetImage());
460       if (selector.PropagationEnabled())
461         reader.SetPropagationVF(mSlicerManagers[index]->GetVF());
462       reader.Update();
463       std::vector<vvMesh::Pointer> contours=reader.GetOutput();
464       for (std::vector<vvMesh::Pointer>::iterator i=contours.begin();
465            i!=contours.end(); i++)
466         AddContour(index,*i,selector.PropagationEnabled());
467       QApplication::restoreOverrideCursor();
468     }
469   }
470 }
471 //------------------------------------------------------------------------------
472
473
474 //------------------------------------------------------------------------------
475 void vvMainWindow::ComputeDeformableRegistration()
476 {
477   if (mSlicerManagers.size() > 0) {
478     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
479     vvDeformationDialog dialog(index,mSlicerManagers);
480     if (dialog.exec()) {
481       std::string base_name=itksys::SystemTools::GetFilenameWithoutExtension(mSlicerManagers[dialog.GetInputFileIndex()]->GetFileName());
482       AddField(dialog.GetOutput(),dialog.getFieldFile(),dialog.GetInputFileIndex());
483       WarpImage(dialog.GetSelectedSlicer(),dialog.GetReferenceFrameIndex());
484     } else
485       std::cout << "Error or user cancellation while computing deformation field..." << std::endl;
486   } else QMessageBox::information(this, "Need to open image","You must open an image first.");
487 }
488 //------------------------------------------------------------------------------
489
490
491 //------------------------------------------------------------------------------
492 void vvMainWindow::WarpImage()
493 {
494   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
495   if (!mSlicerManagers[index]->GetVF().IsNull()) {
496     bool ok;
497     int ref = QInputDialog::getInteger(this,"Chose reference phase","Reference phase",0,0,\
498                                        mSlicerManagers[index]->GetImage()->GetVTKImages().size()-1,1,&ok);
499     if (ok) {
500       WarpImage(mSlicerManagers[index],ref);
501     }
502   } else
503     QMessageBox::warning(this,tr("No vector field"),tr("Sorry, can't warp without a vector field"));
504 }
505 //------------------------------------------------------------------------------
506
507
508 //------------------------------------------------------------------------------
509 void vvMainWindow::WarpImage(vvSlicerManager* selected_slicer,int reference_phase)
510 {
511   if (!selected_slicer->GetVF().IsNull()) {
512     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
513     QFileInfo info(selected_slicer->GetFileName().c_str());
514     vvImageWarp warp(selected_slicer->GetImage(),selected_slicer->GetVF(),
515                      reference_phase,this);
516     if (warp.ComputeWarpedImage()) {
517       AddImage(warp.GetWarpedImage(),info.path().toStdString()+"/"+info.completeBaseName().toStdString()+"_warped.mhd");
518       AddImage(warp.GetDiffImage()  ,info.path().toStdString()+"/"+info.completeBaseName().toStdString()+"_diff.mhd");
519       AddImage(warp.GetJacobianImage()  ,info.path().toStdString()+"/"+info.completeBaseName().toStdString()+"_jacobian.mhd");
520       QApplication::restoreOverrideCursor();
521     } else {
522       QApplication::restoreOverrideCursor();
523       QMessageBox::warning(this,tr("Different spacings"),tr("The vector field and image spacings must be the same in order to warp."));
524     }
525   } else
526     QMessageBox::warning(this,tr("No vector field"),tr("Sorry, can't warp without a vector field."));
527 }
528 //------------------------------------------------------------------------------
529
530
531 //------------------------------------------------------------------------------
532 vvMainWindow::~vvMainWindow()
533 {
534   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
535     if (mSlicerManagers[i] != NULL)
536       delete mSlicerManagers[i];
537   }
538 }
539 //------------------------------------------------------------------------------
540
541 //------------------------------------------------------------------------------
542 QTabWidget * vvMainWindow::GetTab()
543 {
544   return tabWidget;
545 }
546 //------------------------------------------------------------------------------
547
548
549 //------------------------------------------------------------------------------
550 void vvMainWindow::MergeImages()
551 {
552   QString Extensions = EXTENSIONS;
553   Extensions += ";;All Files (*)";
554   QStringList files = QFileDialog::getOpenFileNames(this,tr("Merge Images"),mInputPathName,Extensions);
555   if (files.isEmpty())
556     return;
557   mInputPathName = itksys::SystemTools::GetFilenamePath(files[0].toStdString()).c_str();
558   std::vector<std::string> vector;
559
560   unsigned int currentDim = 0;
561   std::vector<double> currentSpacing;
562   std::vector<int> currentSize;
563   std::vector<double> currentOrigin;
564
565   for (int i = 0; i < files.size(); i++) {
566     itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
567                                          files[i].toStdString().c_str(), itk::ImageIOFactory::ReadMode);
568     reader->SetFileName(files[i].toStdString().c_str());
569     reader->ReadImageInformation();
570     if (reader)        {
571       //NOViewWidget->hide();
572       //NEViewWidget->hide();
573       //SOViewWidget->hide();
574       //SEViewWidget->hide();
575       if (i == 0)
576         currentDim = reader->GetNumberOfDimensions();
577       bool IsOk = true;
578       for (unsigned int j = 0; j < currentDim; j++) {
579         if (i == 0) {
580           if (j == 0) {
581             currentSpacing.resize(currentDim);
582             currentSize.resize(currentDim);
583             currentOrigin.resize(currentDim);
584           }
585           currentOrigin[j] = reader->GetOrigin(j);
586           currentSpacing[j] = reader->GetSpacing(j);
587           currentSize[j] = reader->GetDimensions(j);
588         } else if (currentDim != reader->GetNumberOfDimensions()
589                    || currentSpacing[j] != reader->GetSpacing(j)
590                    || currentSize[j] != (int)reader->GetDimensions(j)
591                    || currentOrigin[j] != reader->GetOrigin(j)) {
592           QString error = "Cannot read file (too different from others ";
593           error += files[i].toStdString().c_str();
594           QMessageBox::information(this,tr("Reading problem"),error);
595           IsOk = false;
596           break;
597         }
598       }
599       if (IsOk)
600         vector.push_back(files[i].toStdString());
601     }
602   }
603   if (vector.size() > 0)
604     LoadImages(vector, MERGED);
605 }
606 //------------------------------------------------------------------------------
607
608 //------------------------------------------------------------------------------
609 void vvMainWindow::SliceImages()
610 {
611   QString Extensions = EXTENSIONS;
612   Extensions += ";;All Files (*)";
613
614   QStringList files = QFileDialog::getOpenFileNames(this,tr("Slice Images"),mInputPathName,Extensions);
615   if (files.isEmpty())
616     return;
617   mInputPathName = itksys::SystemTools::GetFilenamePath(files[0].toStdString()).c_str();
618   std::vector<std::string> vector;
619   for (int i = 0; i < files.size(); i++)
620     vector.push_back(files[i].toStdString());
621   LoadImages(vector, SLICED);
622 }
623 //------------------------------------------------------------------------------
624
625 //------------------------------------------------------------------------------
626 void vvMainWindow::MergeImagesWithTime()
627 {
628   QString Extensions = EXTENSIONS;
629   Extensions += ";;All Files (*)";
630   QStringList files = QFileDialog::getOpenFileNames(this,tr("Merge Images With Time"),mInputPathName,Extensions);
631   if (files.isEmpty())
632     return;
633   mInputPathName = itksys::SystemTools::GetFilenamePath(files[0].toStdString()).c_str();
634   std::vector<std::string> vector;
635
636   unsigned int currentDim = 0;
637   std::vector<double> currentSpacing;
638   std::vector<int> currentSize;
639   std::vector<double> currentOrigin;
640
641   for (int i = 0; i < files.size(); i++) {
642     itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
643                                          files[i].toStdString().c_str(), itk::ImageIOFactory::ReadMode);
644     if (reader) {
645       reader->SetFileName(files[i].toStdString().c_str());
646       reader->ReadImageInformation();
647       if (i == 0)
648         currentDim = reader->GetNumberOfDimensions();
649       bool IsOk = true;
650       for (unsigned int j = 0; j < currentDim; j++) {
651         if (i == 0) {
652           if (j == 0) {
653             currentSpacing.resize(currentDim);
654             currentSize.resize(currentDim);
655             currentOrigin.resize(currentDim);
656           }
657           currentOrigin[j] = reader->GetOrigin(j);
658           currentSpacing[j] = reader->GetSpacing(j);
659           currentSize[j] = reader->GetDimensions(j);
660         } else if (currentDim != reader->GetNumberOfDimensions()
661                    || currentSpacing[j] != reader->GetSpacing(j)
662                    || currentSize[j] != (int)reader->GetDimensions(j)
663                    || currentOrigin[j] != reader->GetOrigin(j)) {
664           QString error = "Cannot read file (too different from others ";
665           error += files[i].toStdString().c_str();
666           QMessageBox::information(this,tr("Reading problem"),error);
667           IsOk = false;
668           break;
669         }
670       }
671       if (IsOk)
672         vector.push_back(files[i].toStdString());
673     } else {
674       QString error = "Cannot read file info for ";
675       error += files[i].toStdString().c_str();
676       error += "\n";
677       error += "Maybe you're trying to open an image in an unsupported format?\n";
678       QMessageBox::information(this,tr("Reading problem"),error);
679     }
680   }
681   sort(vector.begin(),vector.end());
682   if (vector.size() > 1)
683     LoadImages(vector, MERGEDWITHTIME);
684   else
685     QMessageBox::warning(this,tr("Reading problem"),"You need to select at least two images to merge images with time.\nIf you only want to open one image, please use the \"Open Image\" function.");
686 }
687 //------------------------------------------------------------------------------
688
689
690 //------------------------------------------------------------------------------
691 void vvMainWindow::OpenDicom()
692 {
693   std::vector<std::string> files;
694
695   std::cout << "dicomSeriesSelector " << std::endl;
696   if (dicomSeriesSelector->exec() == QDialog::Accepted) {
697     files = *(dicomSeriesSelector->GetFilenames());
698     LoadImages(files,DICOM);
699   }
700 }
701 //------------------------------------------------------------------------------
702
703 //------------------------------------------------------------------------------
704 void vvMainWindow::OpenImages()
705 {
706   QString Extensions = EXTENSIONS;
707   Extensions += ";;All Files (*)";
708
709   QStringList files = QFileDialog::getOpenFileNames(this,tr("Load Images"),mInputPathName,Extensions);
710   if (files.isEmpty())
711     return;
712   mInputPathName = itksys::SystemTools::GetFilenamePath(files[0].toStdString()).c_str();
713   std::vector<std::string> vector;
714   for (int i = 0; i < files.size(); i++)
715     vector.push_back(files[i].toStdString());
716   LoadImages(vector, IMAGE);
717 }
718 //------------------------------------------------------------------------------
719 void vvMainWindow::OpenRecentImage()
720 {
721   QAction * caller = qobject_cast<QAction*>(sender());
722   std::vector<std::string> images;
723   images.push_back(caller->text().toStdString());
724   mInputPathName = itksys::SystemTools::GetFilenamePath(images[0]).c_str();
725   LoadImages(images,IMAGE);
726 }
727 //------------------------------------------------------------------------------
728
729
730 //------------------------------------------------------------------------------
731 void vvMainWindow::OpenImageWithTime()
732 {
733   QString Extensions = EXTENSIONS;
734   Extensions += ";;All Files (*)";
735
736   QStringList files = QFileDialog::getOpenFileNames(this,tr("Load Images With Time"),mInputPathName,Extensions);
737   if (files.isEmpty())
738     return;
739   mInputPathName = itksys::SystemTools::GetFilenamePath(files[0].toStdString()).c_str();
740   std::vector<std::string> vector;
741   for (int i = 0; i < files.size(); i++) {
742     vector.push_back(files[i].toStdString());
743   }
744   LoadImages(vector, IMAGEWITHTIME);
745 }
746 //------------------------------------------------------------------------------
747
748
749 //------------------------------------------------------------------------------
750 void vvMainWindow::LoadImages(std::vector<std::string> files, LoadedImageType filetype)
751 {
752   //Separate the way to open images and dicoms
753   int fileSize;
754   if (filetype == IMAGE || filetype == IMAGEWITHTIME)
755     fileSize = files.size();
756   else
757     fileSize = 1;
758
759   // For SLICED, we need the number of slices (ndim and #slices)
760   std::vector<unsigned int> nSlices;
761   nSlices.resize(files.size());
762   std::fill(nSlices.begin(), nSlices.end(), 1);
763   if (filetype == SLICED) {
764     for (int i = 0; i < fileSize; i++) {
765       itk::ImageIOBase::Pointer header = clitk::readImageHeader(files[i]);
766       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
767       if (!header) {
768         nSlices[i] = 0;
769         QString error = "Cannot open file \n";
770         error += files[i].c_str();
771         QMessageBox::information(this,tr("Reading problem"),error);
772         return;
773       }
774       if (header->GetNumberOfDimensions() < 3) {
775         nSlices[i] = 0;
776         QString error = "Dimension problem. Cannot slice \n";
777         error += files[i].c_str();
778         QMessageBox::information(this,tr("Reading problem"),error);
779         return;
780       }
781       nSlices[i] = header->GetDimensions( header->GetNumberOfDimensions()-1 );
782     }
783   }
784
785   //Only add to the list of recently opened files when a single file is opened,
786   //to avoid polluting the list of recently opened files
787   if (files.size() == 1) {
788     QFileInfo finfo=tr(files[0].c_str());
789     AddToRecentlyOpenedImages(finfo.absoluteFilePath().toStdString());
790   }
791   //init the progress events
792   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
793   vvProgressDialog progress("Opening " + files[0],fileSize>1);
794   qApp->processEvents();
795
796   int numberofsuccesulreads=0;
797   //open images as 1 or multiples
798   for (int i = 0; i < fileSize; i++) {
799
800     progress.SetText("Opening " + files[i]);
801     progress.SetProgress(i,fileSize);
802     qApp->processEvents();
803
804     for (unsigned int j = 0; j < nSlices[i]; j++) {
805       //read the image and put it in mSlicerManagers
806       vvSlicerManager* imageManager = new vvSlicerManager(4);
807       qApp->processEvents();
808
809       bool SetImageSucceed=false;
810
811       // Change filename if an image with the same already exist
812       int number = GetImageDuplicateFilenameNumber(files[i] + std::string("_slice"));
813
814       if (filetype == IMAGE || filetype == IMAGEWITHTIME || filetype == SLICED)
815         SetImageSucceed = imageManager->SetImage(files[i],filetype, number, j);
816       else {
817         SetImageSucceed = imageManager->SetImages(files,filetype, number);
818       }
819       if (!SetImageSucceed) {
820         QApplication::restoreOverrideCursor();
821         QString error = "Cannot open file \n";
822         error += imageManager->GetLastError().c_str();
823         QMessageBox::information(this,tr("Reading problem"),error);
824         delete imageManager;
825       } else {
826         mSlicerManagers.push_back(imageManager);
827
828         //create an item in the tree with good settings
829         QTreeWidgetItem *item = new QTreeWidgetItem();
830         item->setData(0,Qt::UserRole,files[i].c_str());
831         QFileInfo fileinfo(imageManager->GetFileName().c_str()); //Do not show the path
832         item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
833         item->setToolTip(COLUMN_IMAGE_NAME,fileinfo.absoluteFilePath());
834         qApp->processEvents();
835
836         //Create the buttons for reload and close
837         qApp->processEvents();
838         QTreePushButton* cButton = new QTreePushButton;
839         cButton->setItem(item);
840         cButton->setColumn(COLUMN_CLOSE_IMAGE);
841         cButton->setToolTip(tr("close image"));
842         cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
843         connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
844                 this,SLOT(CloseImage(QTreeWidgetItem*, int)));
845
846         QTreePushButton* rButton = new QTreePushButton;
847         rButton->setItem(item);
848         rButton->setColumn(COLUMN_RELOAD_IMAGE);
849         rButton->setToolTip(tr("reload image"));
850         rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
851         connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
852                 this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
853
854         DataTree->addTopLevelItem(item);
855         DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
856         DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
857
858         //set the id of the image
859         QString id = files[i].c_str() + QString::number(mSlicerManagers.size()-1);
860         item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
861         mSlicerManagers.back()->SetId(id.toStdString());
862
863         linkPanel->addImage(imageManager->GetFileName(), id.toStdString());
864
865         connect(mSlicerManagers.back(), SIGNAL(currentImageChanged(std::string)),
866                 this,SLOT(CurrentImageChanged(std::string)));
867         connect(mSlicerManagers.back(), SIGNAL(currentPickedImageChanged(std::string)),
868                 this, SLOT(CurrentPickedImageChanged(std::string)));
869         connect(mSlicerManagers.back(), SIGNAL(UpdatePosition(int, double, double, double, double, double, double, double)),
870                 this,SLOT(MousePositionChanged(int,double, double, double, double, double, double, double)));
871         connect(mSlicerManagers.back(), SIGNAL(UpdateVector(int, double, double, double, double)),
872                 this, SLOT(VectorChanged(int,double,double,double, double)));
873         connect(mSlicerManagers.back(), SIGNAL(UpdateOverlay(int, double, double)),
874                 this, SLOT(OverlayChanged(int,double,double)));
875         connect(mSlicerManagers.back(), SIGNAL(UpdateFusion(int, double)),
876                 this, SLOT(FusionChanged(int,double)));
877         connect(mSlicerManagers.back(), SIGNAL(UpdateWindows(int, int, int)),
878                 this,SLOT(WindowsChanged(int, int, int)));
879         connect(mSlicerManagers.back(), SIGNAL(WindowLevelChanged(double, double,int, int)),
880                 this,SLOT(WindowLevelChanged(double, double, int, int)));
881         connect(mSlicerManagers.back(), SIGNAL(UpdateSlice(int,int)),
882                 this,SLOT(UpdateSlice(int,int)));
883         connect(mSlicerManagers.back(), SIGNAL(UpdateTSlice(int, int)),
884                 this,SLOT(UpdateTSlice(int, int)));
885         connect(mSlicerManagers.back(), SIGNAL(UpdateSliceRange(int,int,int,int,int)),
886                 this,SLOT(UpdateSliceRange(int,int,int,int,int)));
887         connect(mSlicerManagers.back(), SIGNAL(UpdateLinkManager(std::string,int,double,double,double,int)),
888                 this,SLOT(UpdateLinkManager(std::string,int,double,double,double,int)));
889         connect(mSlicerManagers.back(), SIGNAL(UpdateLinkedNavigation(std::string,vvSlicerManager*)),
890                 this,SLOT(UpdateLinkedNavigation(std::string,vvSlicerManager*)));
891         connect(mSlicerManagers.back(), SIGNAL(ChangeImageWithIndexOffset(vvSlicerManager*,int,int)),
892                 this,SLOT(ChangeImageWithIndexOffset(vvSlicerManager*,int,int)));
893         connect(mSlicerManagers.back(),SIGNAL(LandmarkAdded()),landmarksPanel,SLOT(AddPoint()));
894         InitSlicers();
895         numberofsuccesulreads++;
896       }
897     }
898   }
899   if (numberofsuccesulreads) {
900     NOViewWidget->show();
901     NEViewWidget->show();
902     SOViewWidget->show();
903     SEViewWidget->show();
904     UpdateTree();
905     InitDisplay();
906     ShowLastImage();
907
908     // Try to guess default WindowLevel
909     double range[2];
910     mSlicerManagers.back()->GetImage()->GetFirstVTKImageData()->GetScalarRange(range);
911     if ((range[0] == 0) && (range[1] == 1)) {
912       presetComboBox->setCurrentIndex(5);// binary
913     } else {
914       // TODO
915     }
916   }
917   QApplication::restoreOverrideCursor();
918 }
919 //------------------------------------------------------------------------------
920
921 //------------------------------------------------------------------------------
922 void vvMainWindow::UpdateTree()
923 {
924   DataTree->resizeColumnToContents(COLUMN_TREE);
925   DataTree->resizeColumnToContents(COLUMN_UL_VIEW);
926   DataTree->resizeColumnToContents(COLUMN_UR_VIEW);
927   DataTree->resizeColumnToContents(COLUMN_DL_VIEW);
928   DataTree->resizeColumnToContents(COLUMN_DR_VIEW);
929   DataTree->resizeColumnToContents(COLUMN_IMAGE_NAME);
930   DataTree->resizeColumnToContents(COLUMN_CLOSE_IMAGE);
931   DataTree->resizeColumnToContents(COLUMN_RELOAD_IMAGE);
932 }
933 //------------------------------------------------------------------------------
934
935 //------------------------------------------------------------------------------
936 void vvMainWindow::CurrentImageChanged(std::string id)
937 {
938   if (id == mCurrentSelectedImageId) return; // Do nothing
939   int selected = 0;
940   for (int i = 0; i < DataTree->topLevelItemCount(); i++) {
941     if (DataTree->topLevelItem(i)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString().toStdString() == id) {
942       selected = i;
943     } else {
944       DataTree->topLevelItem(i)->setSelected(0);
945     }
946     for (int child = 0; child < DataTree->topLevelItem(i)->childCount(); child++)
947       DataTree->topLevelItem(i)->child(child)->setSelected(0);
948
949   }
950   DataTree->topLevelItem(selected)->setSelected(1);
951   mCurrentSelectedImageId = id;
952   emit SelectedImageHasChanged(mSlicerManagers[selected]);
953 }
954 //------------------------------------------------------------------------------
955
956 //------------------------------------------------------------------------------
957 void vvMainWindow::CurrentPickedImageChanged(std::string id)
958 {
959   if (id == mCurrentPickedImageId) return; // Do nothing
960   int selected = 0;
961   for (int i = 0; i < DataTree->topLevelItemCount(); i++) {
962     if (DataTree->topLevelItem(i)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString().toStdString() == id) {
963       selected = i;
964     } else {
965       DataTree->topLevelItem(i)->setSelected(0);
966     }
967     for (int child = 0; child < DataTree->topLevelItem(i)->childCount(); child++)
968       DataTree->topLevelItem(i)->child(child)->setSelected(0);
969
970   }
971   DataTree->topLevelItem(selected)->setSelected(1);
972   mCurrentPickedImageId = id;
973   mCurrentPickedImageIndex = selected;
974 }
975 //------------------------------------------------------------------------------
976
977 //------------------------------------------------------------------------------
978 void vvMainWindow::ImageInfoChanged()
979 {
980   contextActions[6]->setEnabled(1);
981   contextActions[5]->setEnabled(1);
982   actionSave_As->setEnabled(1);
983   actionAdd_VF_to_current_Image->setEnabled(1);
984   actionAdd_fusion_image->setEnabled(1);
985   actionNorth_East_Window->setEnabled(1);
986   actionNorth_West_Window->setEnabled(1);
987   actionSouth_East_Window->setEnabled(1);
988   actionSouth_West_Window->setEnabled(1);
989   inverseButton->setEnabled(1);
990
991   goToCursorPushButton->setEnabled(1);
992
993   if (DataTree->selectedItems().size()) {
994     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
995
996     colorMapComboBox->setEnabled(1);
997     for (int i = 0; i < DataTree->topLevelItem(index)->childCount(); i++) {
998       if (DataTree->topLevelItem(index)->child(i)->data(1,Qt::UserRole).toString() == "overlay" ||
999           DataTree->topLevelItem(index)->child(i)->data(1,Qt::UserRole).toString() == "fusion") {
1000         colorMapComboBox->setEnabled(0);
1001         break;
1002       }
1003     }
1004
1005     std::vector<double> origin;
1006     std::vector<double> inputSpacing;
1007     std::vector<int> inputSize;
1008     std::vector<double> sizeMM;
1009     vtkSmartPointer<vtkMatrix4x4> transformation;
1010     int dimension=0;
1011     QString pixelType;
1012     QString inputSizeInBytes;
1013     QString image = DataTree->selectedItems()[0]->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1014
1015     if (mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetVTKImages().size() > 1 || playMode == 1) {
1016       playButton->setEnabled(1);
1017       frameRateLabel->setEnabled(1);
1018       frameRateSpinBox->setEnabled(1);
1019     } else {
1020       playButton->setEnabled(0);
1021       frameRateLabel->setEnabled(0);
1022       frameRateSpinBox->setEnabled(0);
1023     }
1024
1025     //read image header
1026     int NPixel = 1;
1027
1028     vvImage::Pointer imageSelected;
1029     if (DataTree->topLevelItem(index) == DataTree->selectedItems()[0]) {
1030       imageSelected = mSlicerManagers[index]->GetSlicer(0)->GetImage();
1031       dimension = imageSelected->GetNumberOfDimensions();
1032       origin.resize(dimension);
1033       inputSpacing.resize(dimension);
1034       inputSize.resize(dimension);
1035       sizeMM.resize(dimension);
1036       pixelType = mSlicerManagers[index]->GetImage()->GetScalarTypeAsITKString().c_str();
1037       for (int i = 0; i < dimension; i++) {
1038         origin[i] = imageSelected->GetOrigin()[i];
1039         inputSpacing[i] = imageSelected->GetSpacing()[i];
1040         inputSize[i] = imageSelected->GetSize()[i];
1041         sizeMM[i] = inputSize[i]*inputSpacing[i];
1042         NPixel *= inputSize[i];
1043       }
1044       inputSizeInBytes = GetSizeInBytes(imageSelected->GetActualMemorySize()*1000);
1045     } else if (DataTree->selectedItems()[0]->data(1,Qt::UserRole).toString() == "vector") {
1046       imageSelected = mSlicerManagers[index]->GetSlicer(0)->GetVF();
1047       dimension = imageSelected->GetNumberOfDimensions();
1048       origin.resize(dimension);
1049       inputSpacing.resize(dimension);
1050       inputSize.resize(dimension);
1051       sizeMM.resize(dimension);
1052       pixelType = mSlicerManagers[index]->GetVF()->GetScalarTypeAsITKString().c_str();
1053       for (int i = 0; i < dimension; i++) {
1054         origin[i] = imageSelected->GetOrigin()[i];
1055         inputSpacing[i] = imageSelected->GetSpacing()[i];
1056         inputSize[i] = imageSelected->GetSize()[i];
1057         sizeMM[i] = inputSize[i]*inputSpacing[i];
1058         NPixel *= inputSize[i];
1059       }
1060       inputSizeInBytes = GetSizeInBytes(imageSelected->GetActualMemorySize()*1000);
1061     } else if (DataTree->selectedItems()[0]->data(1,Qt::UserRole).toString() == "overlay") {
1062       imageSelected = mSlicerManagers[index]->GetSlicer(0)->GetOverlay();
1063       dimension = imageSelected->GetNumberOfDimensions();
1064       origin.resize(dimension);
1065       inputSpacing.resize(dimension);
1066       inputSize.resize(dimension);
1067       sizeMM.resize(dimension);
1068       pixelType = mSlicerManagers[index]->GetImage()->GetScalarTypeAsITKString().c_str();
1069       for (int i = 0; i < dimension; i++) {
1070         origin[i] = imageSelected->GetOrigin()[i];
1071         inputSpacing[i] = imageSelected->GetSpacing()[i];
1072         inputSize[i] = imageSelected->GetSize()[i];
1073         sizeMM[i] = inputSize[i]*inputSpacing[i];
1074         NPixel *= inputSize[i];
1075       }
1076       inputSizeInBytes = GetSizeInBytes(imageSelected->GetActualMemorySize()*1000);
1077     } else if (DataTree->selectedItems()[0]->data(1,Qt::UserRole).toString() == "fusion") {
1078       imageSelected = mSlicerManagers[index]->GetSlicer(0)->GetFusion();
1079       dimension = imageSelected->GetNumberOfDimensions();
1080       origin.resize(dimension);
1081       inputSpacing.resize(dimension);
1082       inputSize.resize(dimension);
1083       sizeMM.resize(dimension);
1084       pixelType = mSlicerManagers[index]->GetImage()->GetScalarTypeAsITKString().c_str();
1085       for (int i = 0; i < dimension; i++) {
1086         origin[i] = imageSelected->GetOrigin()[i];
1087         inputSpacing[i] = imageSelected->GetSpacing()[i];
1088         inputSize[i] = imageSelected->GetSize()[i];
1089         sizeMM[i] = inputSize[i]*inputSpacing[i];
1090         NPixel *= inputSize[i];
1091       }
1092       inputSizeInBytes = GetSizeInBytes(imageSelected->GetActualMemorySize()*1000);
1093     }
1094
1095     QString dim = QString::number(dimension) + " (";
1096     dim += pixelType + ")";
1097
1098     infoPanel->setFileName(image);
1099     infoPanel->setDimension(dim);
1100     infoPanel->setSizePixel(GetVectorIntAsString(inputSize));
1101     infoPanel->setSizeMM(GetVectorDoubleAsString(sizeMM));
1102     infoPanel->setOrigin(GetVectorDoubleAsString(origin));
1103     infoPanel->setSpacing(GetVectorDoubleAsString(inputSpacing));
1104     infoPanel->setNPixel(QString::number(NPixel)+" ("+inputSizeInBytes+")");
1105     transformation = imageSelected->GetTransform()->GetMatrix();
1106     infoPanel->setTransformation(Get4x4MatrixDoubleAsString(transformation));
1107
1108     landmarksPanel->SetCurrentLandmarks(mSlicerManagers[index]->GetLandmarks(),
1109                                         mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetVTKImages().size());
1110     landmarksPanel->SetCurrentPath(mInputPathName.toStdString());
1111     landmarksPanel->SetCurrentImage(mSlicerManagers[index]->GetFileName().c_str());
1112
1113     overlayPanel->getCurrentImageName(mSlicerManagers[index]->GetFileName().c_str());
1114     for (int i = 0; i < 4; i++) {
1115       if (DataTree->selectedItems()[0]->data(i+1,Qt::CheckStateRole).toInt() > 0 || i == 3) {
1116         mSlicerManagers[index]->UpdateInfoOnCursorPosition(i);
1117         break;
1118       }
1119     }
1120     windowSpinBox->setValue(mSlicerManagers[index]->GetColorWindow());
1121     levelSpinBox->setValue(mSlicerManagers[index]->GetColorLevel());
1122     presetComboBox->setCurrentIndex(mSlicerManagers[index]->GetPreset());
1123     colorMapComboBox->setCurrentIndex(mSlicerManagers[index]->GetColorMap());
1124
1125     infoPanel->setFileName(image);
1126     infoPanel->setDimension(dim);
1127     infoPanel->setSizePixel(GetVectorIntAsString(inputSize));
1128     infoPanel->setSizeMM(GetVectorDoubleAsString(sizeMM));
1129     infoPanel->setOrigin(GetVectorDoubleAsString(origin));
1130     infoPanel->setSpacing(GetVectorDoubleAsString(inputSpacing));
1131     infoPanel->setNPixel(QString::number(NPixel)+" ("+inputSizeInBytes+")");
1132
1133     landmarksPanel->SetCurrentLandmarks(mSlicerManagers[index]->GetLandmarks(),
1134                                         mSlicerManagers[index]->GetSlicer(0)->GetImage()->GetVTKImages().size());
1135     landmarksPanel->SetCurrentPath(mInputPathName.toStdString());
1136     landmarksPanel->SetCurrentImage(mSlicerManagers[index]->GetFileName().c_str());
1137
1138     overlayPanel->getCurrentImageName(mSlicerManagers[index]->GetFileName().c_str());
1139     for (int i = 0; i < 4; i++) {
1140       if (DataTree->selectedItems()[0]->data(i+1,Qt::CheckStateRole).toInt() > 0 || i == 3) {
1141         mSlicerManagers[index]->UpdateInfoOnCursorPosition(i);
1142         break;
1143       }
1144     }
1145     windowSpinBox->setValue(mSlicerManagers[index]->GetColorWindow());
1146     levelSpinBox->setValue(mSlicerManagers[index]->GetColorLevel());
1147     presetComboBox->setCurrentIndex(mSlicerManagers[index]->GetPreset());
1148     colorMapComboBox->setCurrentIndex(mSlicerManagers[index]->GetColorMap());
1149
1150     if (mSlicerManagers[index]->GetSlicer(0)->GetVF()) {
1151       overlayPanel->getVFName(mSlicerManagers[index]->GetVFName().c_str());
1152       overlayPanel->getVFProperty(mSlicerManagers[index]->GetSlicer(0)->GetVFSubSampling(),
1153                                   mSlicerManagers[index]->GetSlicer(0)->GetVFScale(),
1154                                   mSlicerManagers[index]->GetSlicer(0)->GetVFLog());
1155     } else {
1156       overlayPanel->getVFName(mSlicerManagers[index]->GetVFName().c_str());
1157       overlayPanel->getVFProperty(-1,-1,-1);
1158     }
1159     if (mSlicerManagers[index]->GetSlicer(0)->GetOverlay()) {
1160       overlayPanel->getOverlayName(mSlicerManagers[index]->GetOverlayName().c_str());
1161       overlayPanel->getOverlayProperty(mSlicerManagers[index]->GetOverlayColor());
1162     } else {
1163       overlayPanel->getOverlayName(mSlicerManagers[index]->GetOverlayName().c_str());
1164       overlayPanel->getOverlayProperty(-1);
1165     }
1166     if (mSlicerManagers[index]->GetSlicer(0)->GetFusion()) {
1167       overlayPanel->getFusionName(mSlicerManagers[index]->GetFusionName().c_str());
1168       overlayPanel->getFusionProperty(mSlicerManagers[index]->GetFusionOpacity(),
1169                                       mSlicerManagers[index]->GetFusionColorMap(),
1170                                       mSlicerManagers[index]->GetFusionWindow(),
1171                                       mSlicerManagers[index]->GetFusionLevel());
1172     } else {
1173       overlayPanel->getFusionName(mSlicerManagers[index]->GetFusionName().c_str());
1174       overlayPanel->getFusionProperty(-1, -1,-1,-1);
1175     }
1176   }
1177 }
1178 //------------------------------------------------------------------------------
1179
1180 //------------------------------------------------------------------------------
1181 void vvMainWindow::ShowDocumentation()
1182 {
1183   documentation->show();
1184 }
1185 //------------------------------------------------------------------------------
1186 void vvMainWindow::ShowHelpDialog()
1187 {
1188   help_dialog->show();
1189 }
1190 //------------------------------------------------------------------------------
1191
1192 //------------------------------------------------------------------------------
1193 void vvMainWindow::ChangeViewMode()
1194 {
1195   QListIterator<int> it0(splitter_3->sizes());
1196   QListIterator<int> it1(splitter_3->sizes());
1197   int max0 = 0;
1198   int max1 = 1;
1199   while (it0.hasNext()) {
1200     max0 += it0.next();
1201   }
1202   while (it1.hasNext()) {
1203     max1 += it1.next();
1204   }
1205   QList<int> size0;
1206   QList<int> size1;
1207   if (viewMode == 1) {
1208     viewMode = 0;
1209     size0.push_back(max0);
1210     size0.push_back(0);
1211     size1.push_back(max1);
1212     size1.push_back(0);
1213     splitter_3->setSizes(size0);
1214     OSplitter->setSizes(size1);
1215     DataTree->setColumnHidden(2,1);
1216     DataTree->setColumnHidden(3,1);
1217     DataTree->setColumnHidden(4,1);
1218   } else {
1219     viewMode = 1;
1220     size0.push_back(int(max0/2));
1221     size0.push_back(int(max0/2));
1222     size1.push_back(int(max1/2));
1223     size1.push_back(int(max1/2));
1224     splitter_3->setSizes(size0);
1225     OSplitter->setSizes(size1);
1226     DataTree->setColumnHidden(2,0);
1227     DataTree->setColumnHidden(3,0);
1228     DataTree->setColumnHidden(4,0);
1229   }
1230 }
1231 //------------------------------------------------------------------------------
1232
1233 //------------------------------------------------------------------------------
1234 QString vvMainWindow::GetSizeInBytes(unsigned long size)
1235 {
1236   QString result = "";// QString::number(size);
1237   //result += " bytes (";
1238   if (size > 1000000000) {
1239     size /= 1000000000;
1240     result += QString::number(size);
1241     result += "Gb";//)";
1242   } else if (size > 1000000) {
1243     size /= 1000000;
1244     result += QString::number(size);
1245     result += "Mb";//)";
1246   } else if (size > 1000) {
1247     size /= 1000;
1248     result += QString::number(size);
1249     result += "kb";//)";
1250   }
1251   return result;
1252 }
1253 //------------------------------------------------------------------------------
1254
1255 //------------------------------------------------------------------------------
1256 QString vvMainWindow::Get4x4MatrixDoubleAsString(vtkSmartPointer<vtkMatrix4x4> matrix)
1257 {
1258   std::ostringstream strmatrix;
1259
1260   for (unsigned int i = 0; i < 4; i++) {
1261     for (unsigned int j = 0; j < 4; j++) {
1262       strmatrix.flags(ios::showpos);
1263       strmatrix.width(10);
1264       strmatrix.precision(3);
1265       strmatrix.setf(ios::fixed,ios::floatfield);
1266       strmatrix.fill(' ');
1267       strmatrix << std::left << matrix->GetElement(i, j);
1268       //strmatrix.width(10);
1269       strmatrix << " ";
1270     }
1271     strmatrix << std::endl;
1272   }
1273   QString result = strmatrix.str().c_str();
1274   return result;
1275 }
1276 //------------------------------------------------------------------------------
1277
1278 //------------------------------------------------------------------------------
1279 QString vvMainWindow::GetVectorDoubleAsString(std::vector<double> vectorDouble)
1280 {
1281   QString result;
1282   for (unsigned int i= 0; i < vectorDouble.size(); i++) {
1283     if (i != 0)
1284       result += " ";
1285     result += QString::number(vectorDouble[i]);
1286   }
1287   return result;
1288 }
1289 //------------------------------------------------------------------------------
1290
1291 //------------------------------------------------------------------------------
1292 QString vvMainWindow::GetVectorIntAsString(std::vector<int> vectorInt)
1293 {
1294   QString result;
1295   for (unsigned int i= 0; i < vectorInt.size(); i++) {
1296     if (i != 0)
1297       result += " ";
1298     result += QString::number(vectorInt[i]);
1299   }
1300   return result;
1301 }
1302 //------------------------------------------------------------------------------
1303
1304 //------------------------------------------------------------------------------
1305 int vvMainWindow::GetSlicerIndexFromItem(QTreeWidgetItem* item)
1306 {
1307   QString id = item->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
1308   for (int i = 0; i < DataTree->topLevelItemCount(); i++) {
1309     if (DataTree->topLevelItem(i)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString() == id)
1310       return i;
1311   }
1312   return -1;
1313 }
1314 //------------------------------------------------------------------------------
1315
1316 //------------------------------------------------------------------------------
1317 QTreeWidgetItem* vvMainWindow::GetItemFromSlicerManager(vvSlicerManager* sm)
1318 {
1319   QString id = sm->GetId().c_str();
1320   for (int i = 0; i < DataTree->topLevelItemCount(); i++) {
1321     if (DataTree->topLevelItem(i)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString() == id)
1322       return DataTree->topLevelItem(i);
1323   }
1324   return NULL;
1325 }
1326 //------------------------------------------------------------------------------
1327
1328 //------------------------------------------------------------------------------
1329 void vvMainWindow::DisplayChanged(QTreeWidgetItem *clicked_item, int column)
1330 {
1331   int index = GetSlicerIndexFromItem(clicked_item);
1332   if ( column >= COLUMN_CLOSE_IMAGE || column <= 0)
1333     return;
1334   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
1335     //Trick to avoid redoing twice the job for a key (sr)
1336     mSlicerManagers[i]->GetSlicer(column-1)->GetRenderWindow()-> GetInteractor()->SetKeySym("Crap");
1337
1338     QTreeWidgetItem* current_row=DataTree->topLevelItem(i);
1339     if (DataTree->topLevelItem(index) == current_row) {
1340       vvSlicer* clicked_slicer=mSlicerManagers[i]->GetSlicer(column-1);
1341       if (current_row == clicked_item) {
1342         //If we just activated a slicer
1343         if (current_row->data(column,Qt::CheckStateRole).toInt() > 0) {
1344           mSlicerManagers[i]->UpdateSlicer(column-1,clicked_item->data(column,Qt::CheckStateRole).toInt());
1345           mSlicerManagers[i]->UpdateInfoOnCursorPosition(column-1);
1346           DisplaySliders(i,column-1);
1347           std::map<std::string,int> overlay_counts;
1348           for (int child = 0; child < current_row->childCount(); child++) {
1349             std::string overlay_type =
1350               current_row->child(child)->data(1,Qt::UserRole).toString().toStdString();
1351             overlay_counts[overlay_type]++;
1352             current_row->child(child)->setData(column,Qt::CheckStateRole,
1353                                                current_row->data(column,Qt::CheckStateRole));
1354             clicked_slicer->SetActorVisibility(overlay_type,overlay_counts[overlay_type]-1,true);
1355           }
1356         } else { //We don't allow simply desactivating a slicer
1357           clicked_item->setData(column,Qt::CheckStateRole,2);
1358           return;
1359         }
1360       }
1361       //if we clicked on the vector(or overlay) and not the image
1362       else {
1363         if (clicked_item->data(column,Qt::CheckStateRole).toInt()) {
1364           current_row->setData(column,Qt::CheckStateRole,2);
1365           mSlicerManagers[i]->UpdateSlicer(column-1,2);
1366           mSlicerManagers[i]->UpdateInfoOnCursorPosition(column-1);
1367           DisplaySliders(i,column-1);
1368         }
1369         int vis = clicked_item->data(column,Qt::CheckStateRole).toInt();
1370         std::string overlay_type = clicked_item->data(1,Qt::UserRole).toString().toStdString();
1371         int overlay_index=0;
1372         for (int child = 0; child < current_row->childCount(); child++) {
1373           if (current_row->child(child)->data(1,Qt::UserRole).toString().toStdString() == overlay_type)
1374             overlay_index++;
1375           if (current_row->child(child) == clicked_item) break;
1376         }
1377         clicked_slicer->SetActorVisibility(
1378           clicked_item->data(1,Qt::UserRole).toString().toStdString(), overlay_index-1,vis);
1379       }
1380     } else if (current_row->data(column,Qt::CheckStateRole).toInt() > 0) {
1381       current_row->setData(column,Qt::CheckStateRole,0);
1382       mSlicerManagers[i]->UpdateSlicer(column-1,0);
1383       std::map<std::string,int> overlay_counts;
1384       for (int child = 0; child < current_row->childCount(); child++) {
1385         std::string overlay_type =
1386           current_row->child(child)->data(1,Qt::UserRole).toString().toStdString();
1387         overlay_counts[overlay_type]++;
1388         current_row->child(child)->setData(column,Qt::CheckStateRole,0);
1389         vvSlicer * current_slicer=mSlicerManagers[i]->GetSlicer(column-1);
1390         current_slicer->SetActorVisibility(overlay_type,overlay_counts[overlay_type]-1,false);
1391       }
1392     }
1393     //mSlicerManagers[i]->SetColorMap(-1);
1394     mSlicerManagers[i]->SetColorMap();
1395   }
1396   mSlicerManagers[index]->GetSlicer(column-1)->Render();
1397 }
1398 //------------------------------------------------------------------------------
1399
1400 void vvMainWindow::InitSlicers()
1401 {
1402   if (mSlicerManagers.size()) {
1403     mSlicerManagers.back()->GenerateDefaultLookupTable();
1404
1405     mSlicerManagers.back()->SetSlicerWindow(0,NOViewWidget->GetRenderWindow());
1406     mSlicerManagers.back()->SetSlicerWindow(1,NEViewWidget->GetRenderWindow());
1407     mSlicerManagers.back()->SetSlicerWindow(2,SOViewWidget->GetRenderWindow());
1408     mSlicerManagers.back()->SetSlicerWindow(3,SEViewWidget->GetRenderWindow());
1409   }
1410 }
1411
1412 //------------------------------------------------------------------------------
1413 void vvMainWindow::InitDisplay()
1414 {
1415   if (mSlicerManagers.size()) {
1416     //BE CAREFUL : this is absolutely necessary to set the interactor style
1417     //in order to have the same style instanciation for all SlicerManagers in
1418     // a same window
1419     for (int j = 0; j < 4; j++) {
1420       vvInteractorStyleNavigator* style = vvInteractorStyleNavigator::New();
1421       style->SetAutoAdjustCameraClippingRange(1);
1422       bool AlreadySelected = false;
1423       for (int i = 0; i < DataTree->topLevelItemCount(); i++) {
1424         mSlicerManagers[i]->SetInteractorStyleNavigator(j,style);
1425
1426         //select the image only if previous are not selected
1427         if (DataTree->topLevelItem(i)->data(j+1,Qt::CheckStateRole).toInt() > 1) {
1428           mSlicerManagers[i]->UpdateSlicer(j,1);
1429           AlreadySelected = true;
1430         } else if (i == DataTree->topLevelItemCount()-1 && !AlreadySelected) {
1431           if (DataTree->selectedItems().size() == 0)
1432             DataTree->topLevelItem(i)->setSelected(1);
1433           DataTree->topLevelItem(i)->setData(j+1,Qt::CheckStateRole,2);
1434           mSlicerManagers[i]->UpdateSlicer(j,1);
1435           DisplaySliders(i,j);
1436         } else {
1437           DataTree->topLevelItem(i)->setData(j+1,Qt::CheckStateRole,0);
1438           mSlicerManagers[i]->UpdateSlicer(j,0);
1439         }
1440       }
1441       style->Delete();
1442     }
1443   }
1444 }
1445 //------------------------------------------------------------------------------
1446
1447 //------------------------------------------------------------------------------
1448 void vvMainWindow::DisplaySliders(int slicer, int window)
1449 {
1450   int range[2];
1451   mSlicerManagers[slicer]->GetSlicer(window)->GetSliceRange(range);
1452   int position = mSlicerManagers[slicer]->GetSlicer(window)->GetSlice();
1453
1454   int tRange[2];
1455   tRange[0] = 0;
1456   tRange[1] = mSlicerManagers[slicer]->GetSlicer(window)->GetTMax();
1457   int tPosition = mSlicerManagers[slicer]->GetSlicer(window)->GetTSlice();
1458   bool showHorizontal = false;
1459   bool showVertical = false;
1460   if (mSlicerManagers[slicer]->GetSlicer(window)->GetImage()->GetNumberOfDimensions() > 3
1461       || (mSlicerManagers[slicer]->GetSlicer(window)->GetImage()->GetNumberOfDimensions() > 2
1462           && mSlicerManagers[slicer]->GetType() != IMAGEWITHTIME
1463           && mSlicerManagers[slicer]->GetType() != MERGEDWITHTIME))
1464     showVertical = true;
1465   if (mSlicerManagers[slicer]->GetSlicer(window)->GetImage()->GetNumberOfDimensions() > 3
1466       || mSlicerManagers[slicer]->GetType() == IMAGEWITHTIME
1467       || mSlicerManagers[slicer]->GetType() == MERGEDWITHTIME)
1468     showHorizontal = true;
1469
1470   if (showVertical)
1471     verticalSliders[window]->show();
1472   else
1473     verticalSliders[window]->hide();
1474   verticalSliders[window]->setRange(range[0],range[1]);
1475   verticalSliders[window]->setValue(position);
1476
1477   if (showHorizontal)
1478     horizontalSliders[window]->show();
1479   else
1480     horizontalSliders[window]->hide();
1481   horizontalSliders[window]->setRange(tRange[0],tRange[1]);
1482   horizontalSliders[window]->setValue(tPosition);
1483 }
1484 //------------------------------------------------------------------------------
1485
1486 //------------------------------------------------------------------------------
1487 void vvMainWindow::CloseImage(QTreeWidgetItem* item, int column)
1488 {
1489   int index = GetSlicerIndexFromItem(item);
1490
1491   if (DataTree->topLevelItem(index) != item) {
1492     QString warning = "Do you really want to close the overlay : ";
1493     warning += item->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1494     QMessageBox msgBox(QMessageBox::Warning, tr("Close Overlay"),
1495                        warning, 0, this);
1496     msgBox.addButton(tr("Close"), QMessageBox::AcceptRole);
1497     msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
1498     if (msgBox.exec() == QMessageBox::AcceptRole) {
1499       std::string overlay_type=item->data(1,Qt::UserRole).toString().toStdString();
1500       int overlay_index=0;
1501       for (int child = 0; child < DataTree->topLevelItem(index)->childCount(); child++) {
1502         if (DataTree->topLevelItem(index)->\
1503             child(child)->data(1,Qt::UserRole).toString().toStdString() == overlay_type)
1504           overlay_index++;
1505         if (DataTree->topLevelItem(index)->child(child) == item) break;
1506       }
1507       mSlicerManagers[index]->RemoveActor(overlay_type, overlay_index-1);
1508       mSlicerManagers[index]->SetColorMap(0);
1509       DataTree->topLevelItem(index)->takeChild(DataTree->topLevelItem(index)->indexOfChild(item));
1510       mSlicerManagers[index]->Render();
1511     }
1512   } else if (DataTree->topLevelItemCount() <= 1) {
1513     QString warning = "Do you really want to close the image : ";
1514     warning += item->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1515     warning += "\nThis is the last image, you're about to close vv !!!";
1516     QMessageBox msgBox(QMessageBox::Warning, tr("Close Image"),
1517                        warning, 0, this);
1518     msgBox.addButton(tr("Close vv"), QMessageBox::AcceptRole);
1519     msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
1520     if (msgBox.exec() == QMessageBox::AcceptRole) {
1521       this->close();
1522     }
1523   } else {
1524     QString warning = "Do you really want to close the image : ";
1525     warning += item->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1526     QMessageBox msgBox(QMessageBox::Warning, tr("Close Image"),
1527                        warning, 0, this);
1528     msgBox.addButton(tr("Close"), QMessageBox::AcceptRole);
1529     msgBox.addButton(tr("Cancel"), QMessageBox::RejectRole);
1530     if (msgBox.exec() == QMessageBox::AcceptRole) {
1531
1532       // Tell tools that we close an image
1533       emit AnImageIsBeingClosed(mSlicerManagers[index]);
1534
1535       std::vector<vvSlicerManager*>::iterator Manageriter = mSlicerManagers.begin();
1536       DataTree->takeTopLevelItem(index);
1537       for (int i = 0; i < index; i++) {
1538         Manageriter++;
1539       }
1540       linkPanel->removeImage(index);
1541       mSlicerManagers[index]->RemoveActors();
1542       delete mSlicerManagers[index];
1543       mSlicerManagers.erase(Manageriter);
1544
1545       //
1546       InitDisplay();
1547     }
1548   }
1549   ImageInfoChanged();
1550 }
1551 //------------------------------------------------------------------------------
1552
1553 //------------------------------------------------------------------------------
1554 void vvMainWindow::ReloadImage(QTreeWidgetItem* item, int column)
1555 {
1556   // int index = GetSlicerIndexFromItem(item);
1557   //   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
1558   //   if (item->data(1,Qt::UserRole).toString() == "vector")
1559   //     mSlicerManagers[index]->ReloadVF();
1560   //   else
1561   //     mSlicerManagers[index]->Reload();
1562
1563   //   QApplication::restoreOverrideCursor();
1564   int index = GetSlicerIndexFromItem(item);
1565   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
1566   QString role=item->data(1,Qt::UserRole).toString();
1567   if ( role == "vector")
1568     mSlicerManagers[index]->ReloadVF();
1569   else if (role == "overlay")
1570     mSlicerManagers[index]->ReloadOverlay();
1571   else if (role == "fusion")
1572     mSlicerManagers[index]->ReloadFusion();
1573   else
1574     mSlicerManagers[index]->Reload();
1575
1576   // Update view and info
1577   ImageInfoChanged();
1578   mSlicerManagers[index]->Render();
1579   QApplication::restoreOverrideCursor();
1580 }
1581 //------------------------------------------------------------------------------
1582
1583 //------------------------------------------------------------------------------
1584 void vvMainWindow::MousePositionChanged(int visibility,double x, double y, double z, double X, double Y, double Z , double value)
1585 {
1586   infoPanel->setCurrentInfo(visibility,x,y,z,X,Y,Z,value);
1587 }
1588 //------------------------------------------------------------------------------
1589
1590 //------------------------------------------------------------------------------
1591 void vvMainWindow::VectorChanged(int visibility,double x, double y, double z, double value)
1592 {
1593   overlayPanel->getCurrentVectorInfo(visibility,x,y,z,value);
1594 }
1595 //------------------------------------------------------------------------------
1596
1597 //------------------------------------------------------------------------------
1598 void vvMainWindow::OverlayChanged(int visibility, double valueOver, double valueRef)
1599 {
1600   overlayPanel->getCurrentOverlayInfo(visibility,valueOver, valueRef);
1601 }
1602 //------------------------------------------------------------------------------
1603
1604 //------------------------------------------------------------------------------
1605 void vvMainWindow::FusionChanged(int visibility, double value)
1606 {
1607   overlayPanel->getCurrentFusionInfo(visibility,value);
1608 }
1609 //------------------------------------------------------------------------------
1610
1611 //------------------------------------------------------------------------------
1612 void vvMainWindow::WindowsChanged(int window, int view, int slice)
1613 {
1614   infoPanel->setViews(window, view, slice);
1615 }
1616 //------------------------------------------------------------------------------
1617
1618 //------------------------------------------------------------------------------
1619 void vvMainWindow::WindowLevelChanged(double window, double level,int preset,int colormap)
1620 {
1621   windowSpinBox->setValue(window);
1622   levelSpinBox->setValue(level);
1623   colorMapComboBox->setCurrentIndex(colormap);
1624   presetComboBox->setCurrentIndex(preset);
1625 }
1626 //------------------------------------------------------------------------------
1627
1628 //------------------------------------------------------------------------------
1629 void vvMainWindow::WindowLevelEdited()
1630 {
1631   presetComboBox->setCurrentIndex(6);
1632   UpdateWindowLevel();
1633 }
1634 //------------------------------------------------------------------------------
1635
1636 //------------------------------------------------------------------------------
1637 void vvMainWindow::UpdateWindowLevel()
1638 {
1639   if (DataTree->selectedItems().size()) {
1640     if (presetComboBox->currentIndex() == 7) //For ventilation
1641       colorMapComboBox->setCurrentIndex(5);
1642     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1643     mSlicerManagers[index]->SetColorWindow(windowSpinBox->value());
1644     mSlicerManagers[index]->SetColorLevel(levelSpinBox->value());
1645     mSlicerManagers[index]->SetPreset(presetComboBox->currentIndex());
1646     mSlicerManagers[index]->Render();
1647     windowSpinBox->setValue(mSlicerManagers[index]->GetColorWindow());
1648     levelSpinBox->setValue(mSlicerManagers[index]->GetColorLevel());
1649   }
1650 }
1651 //------------------------------------------------------------------------------
1652
1653 //------------------------------------------------------------------------------
1654 void vvMainWindow::UpdateColorMap()
1655 {
1656   if (DataTree->selectedItems().size()) {
1657     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1658     mSlicerManagers[index]->SetColorMap(colorMapComboBox->currentIndex());
1659     mSlicerManagers[index]->Render();
1660   }
1661 }
1662 //------------------------------------------------------------------------------
1663 void vvMainWindow::SwitchWindowLevel()
1664 {
1665   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1666   int window = mSlicerManagers[index]->GetColorWindow();
1667   presetComboBox->setCurrentIndex(6);
1668   windowSpinBox->setValue(-window);
1669   UpdateWindowLevel();
1670 }
1671 //------------------------------------------------------------------------------
1672
1673 //------------------------------------------------------------------------------
1674 void vvMainWindow::ApplyWindowLevelToAllImages()
1675 {
1676   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
1677     if (mSlicerManagers[i] == NULL)
1678       continue;
1679     mSlicerManagers[i]->SetColorWindow(windowSpinBox->value());
1680     mSlicerManagers[i]->SetColorLevel(levelSpinBox->value());
1681     mSlicerManagers[i]->SetPreset(6);
1682     mSlicerManagers[i]->Render();
1683   }
1684 }
1685 //------------------------------------------------------------------------------
1686
1687 //------------------------------------------------------------------------------
1688 void vvMainWindow::UpdateLinkManager(std::string id, int slicer, double x, double y, double z, int temps)
1689 {
1690   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
1691     if (mSlicerManagers[i]->GetId() == id) {
1692       //mSlicerManagers[i]->SetTSlice(temps);
1693       mSlicerManagers[i]->GetSlicer(slicer)->SetCurrentPosition(x,y,z,temps);
1694       mSlicerManagers[i]->UpdateViews(0,slicer);
1695       break;
1696     }
1697   }
1698 }
1699 //------------------------------------------------------------------------------
1700
1701 //------------------------------------------------------------------------------
1702 void vvMainWindow::UpdateLinkedNavigation(std::string id, vvSlicerManager * sm)
1703 {
1704   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
1705     if (id == mSlicerManagers[i]->GetId()) {
1706       mSlicerManagers[i]->UpdateLinkedNavigation(sm->GetSlicer(0));
1707     }
1708   }
1709 }
1710 //------------------------------------------------------------------------------
1711
1712 //------------------------------------------------------------------------------
1713 void vvMainWindow::ShowContextMenu(QPoint point)
1714 {
1715   if (!DataTree->selectedItems().size()) {
1716     contextActions[1]->setEnabled(0);
1717     contextActions[2]->setEnabled(0);
1718     contextActions[3]->setEnabled(0);
1719     contextActions[4]->setEnabled(0);
1720     contextActions[5]->setEnabled(0);
1721     contextActions[6]->setEnabled(0);
1722   } else {
1723     int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1724     contextActions[1]->setEnabled(1);
1725     contextActions[2]->setEnabled(
1726       DataTree->itemWidget(DataTree->selectedItems()[0],
1727                            COLUMN_RELOAD_IMAGE)->isEnabled());
1728     contextActions[3]->setEnabled(1);
1729     contextActions[5]->setEnabled(1);
1730     contextActions[6]->setEnabled(1);
1731
1732     if (mSlicerManagers[index]->GetDimension() < 3)
1733       contextActions[4]->setEnabled(0);
1734     else
1735       contextActions[4]->setEnabled(1);
1736   }
1737   contextMenu.exec(QCursor::pos());
1738 }
1739 //------------------------------------------------------------------------------
1740
1741 //------------------------------------------------------------------------------
1742 void vvMainWindow::CloseImage()
1743 {
1744   CloseImage(DataTree->selectedItems()[0],0);
1745 }
1746 //------------------------------------------------------------------------------
1747
1748 //------------------------------------------------------------------------------
1749 void vvMainWindow::ReloadImage()
1750 {
1751   ReloadImage(DataTree->selectedItems()[0],0);
1752 }
1753 //------------------------------------------------------------------------------
1754
1755 //------------------------------------------------------------------------------
1756 void vvMainWindow::SelectOverlayImage()
1757 {
1758   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1759
1760   //check if one overlay image is added
1761   for (int child = 0; child < DataTree->topLevelItem(index)->childCount(); child++)
1762     if (DataTree->topLevelItem(index)->child(child)->data(1,Qt::UserRole).toString() == "overlay") {
1763       QString error = "Cannot add more than one compared image\n";
1764       error += "Please remove first ";
1765       error += DataTree->topLevelItem(index)->child(child)->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1766       QMessageBox::information(this,tr("Problem adding compared image !"),error);
1767       return;
1768     }
1769
1770   QString Extensions = EXTENSIONS;
1771   Extensions += ";;All Files (*)";
1772   QString file = QFileDialog::getOpenFileName(this,tr("Load Overlay image"),mInputPathName,Extensions);
1773   if (!file.isEmpty())
1774     AddOverlayImage(index,file);
1775 }
1776 //------------------------------------------------------------------------------
1777
1778 //------------------------------------------------------------------------------
1779 void vvMainWindow::AddOverlayImage(int index, QString file)
1780 {
1781
1782   mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
1783   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
1784                                        file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
1785   reader->SetFileName(file.toStdString().c_str());
1786   reader->ReadImageInformation();
1787   std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
1788   int dimension = reader->GetNumberOfDimensions();
1789   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
1790   vvProgressDialog progress("Opening " + file.toStdString());
1791   qApp->processEvents();
1792
1793   std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
1794   if (mSlicerManagers[index]->SetOverlay(file.toStdString(),dimension, component)) {
1795     //create an item in the tree with good settings
1796     QTreeWidgetItem *item = new QTreeWidgetItem();
1797     item->setData(0,Qt::UserRole,file.toStdString().c_str());
1798     item->setData(1,Qt::UserRole,tr("overlay"));
1799     QFileInfo fileinfo(file); //Do not show the path
1800     item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
1801     item->setToolTip(COLUMN_IMAGE_NAME,fileinfo.absoluteFilePath());
1802     qApp->processEvents();
1803
1804     for (int j = 1; j <= 4; j++) {
1805       item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
1806       mSlicerManagers[index]->GetSlicer(j-1)->SetActorVisibility("overlay",0,
1807           DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole).toInt());
1808     }
1809
1810     //Create the buttons for reload and close
1811     qApp->processEvents();
1812     QTreePushButton* cButton = new QTreePushButton;
1813     cButton->setItem(item);
1814     cButton->setColumn(COLUMN_CLOSE_IMAGE);
1815     cButton->setToolTip(tr("close image"));
1816     cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
1817     connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
1818             this,SLOT(CloseImage(QTreeWidgetItem*, int)));
1819
1820     QTreePushButton* rButton = new QTreePushButton;
1821     rButton->setItem(item);
1822     rButton->setColumn(COLUMN_RELOAD_IMAGE);
1823     rButton->setToolTip(tr("reload image"));
1824     rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
1825     connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
1826             this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
1827
1828     DataTree->topLevelItem(index)->setExpanded(1);
1829     DataTree->topLevelItem(index)->addChild(item);
1830     DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
1831     DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
1832
1833     //set the id of the image
1834     QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
1835     item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
1836     UpdateTree();
1837     qApp->processEvents();
1838     ImageInfoChanged();
1839     QApplication::restoreOverrideCursor();
1840   } else {
1841     QApplication::restoreOverrideCursor();
1842     QString error = "Cannot import the new image.\n";
1843     error += mSlicerManagers[index]->GetLastError().c_str();
1844     QMessageBox::information(this,tr("Problem reading image !"),error);
1845   }
1846 }
1847 //------------------------------------------------------------------------------
1848
1849
1850 //------------------------------------------------------------------------------
1851 void vvMainWindow::AddROI(int index, QString file)
1852 {
1853   /*
1854   // Get slice manager
1855
1856   // Load image
1857
1858   vvImageReader * mReader = new vvImageReader;
1859   mReader->SetInputFilename(filename.toStdString());
1860   mReader->Update(IMAGE);
1861   if (mReader->GetLastError().size() != 0) {
1862     std::cerr << "Error while reading " << filename.toStdString() << std::endl;
1863     QString error = "Cannot open file \n";
1864     error += mReader->GetLastError().c_str();
1865     QMessageBox::information(this,tr("Reading problem"),error);
1866     delete mReader;
1867     return;
1868   }
1869   vvImage::Pointer roi = mReader->GetOutput();
1870
1871   // Create roi in new tool
1872   vvToolStructureSetManager::AddImage(mCurrentSlicerManager, roi);
1873   */
1874 }
1875 //------------------------------------------------------------------------------
1876
1877 //------------------------------------------------------------------------------
1878 void vvMainWindow::SelectFusionImage()
1879 {
1880   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1881
1882   //check if one fusion image is added
1883   for (int child = 0; child < DataTree->topLevelItem(index)->childCount(); child++)
1884     if (DataTree->topLevelItem(index)->child(child)->data(1,Qt::UserRole).toString() == "fusion") {
1885       QString error = "Cannot add more than one fusion image\n";
1886       error += "Please remove first ";
1887       error += DataTree->topLevelItem(index)->child(child)->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1888       QMessageBox::information(this,tr("Problem adding fusion image !"),error);
1889       return;
1890     }
1891
1892   QString Extensions = EXTENSIONS;
1893   Extensions += ";;All Files (*)";
1894   QString file = QFileDialog::getOpenFileName(this,tr("Load Fusion image"),mInputPathName,Extensions);
1895   if (!file.isEmpty())
1896     AddFusionImage(index,file);
1897 }
1898 //------------------------------------------------------------------------------
1899
1900 //------------------------------------------------------------------------------
1901 void vvMainWindow::AddFusionImage(int index, QString file)
1902 {
1903   mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
1904   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(
1905                                        file.toStdString().c_str(), itk::ImageIOFactory::ReadMode);
1906   reader->SetFileName(file.toStdString().c_str());
1907   reader->ReadImageInformation();
1908   std::string component = reader->GetComponentTypeAsString(reader->GetComponentType());
1909   if (reader) {
1910     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
1911     vvProgressDialog progress("Opening fusion");
1912     qApp->processEvents();
1913
1914     std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
1915     if (mSlicerManagers[index]->SetFusion(file.toStdString(),
1916                                           reader->GetNumberOfDimensions(), component)) {
1917       //create an item in the tree with good settings
1918       QTreeWidgetItem *item = new QTreeWidgetItem();
1919       item->setData(0,Qt::UserRole,file.toStdString().c_str());
1920       item->setData(1,Qt::UserRole,tr("fusion"));
1921       QFileInfo fileinfo(filename.c_str()); //Do not show the path
1922       item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
1923       item->setToolTip(COLUMN_IMAGE_NAME,fileinfo.absoluteFilePath());
1924       qApp->processEvents();
1925
1926       for (int j = 1; j <= 4; j++) {
1927         item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
1928         mSlicerManagers[index]->GetSlicer(j-1)->SetActorVisibility("fusion",0,
1929             DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole).toInt());
1930       }
1931
1932       //Create the buttons for reload and close
1933       qApp->processEvents();
1934       QTreePushButton* cButton = new QTreePushButton;
1935       cButton->setItem(item);
1936       cButton->setColumn(COLUMN_CLOSE_IMAGE);
1937       cButton->setToolTip(tr("close image"));
1938       cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
1939       connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
1940               this,SLOT(CloseImage(QTreeWidgetItem*, int)));
1941
1942       QTreePushButton* rButton = new QTreePushButton;
1943       rButton->setItem(item);
1944       rButton->setColumn(COLUMN_RELOAD_IMAGE);
1945       rButton->setToolTip(tr("reload image"));
1946       rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
1947       connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
1948               this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
1949
1950       DataTree->topLevelItem(index)->setExpanded(1);
1951       DataTree->topLevelItem(index)->addChild(item);
1952       DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
1953       DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
1954
1955       //set the id of the image
1956       QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
1957       item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
1958       UpdateTree();
1959       qApp->processEvents();
1960       ImageInfoChanged();
1961       QApplication::restoreOverrideCursor();
1962     } else {
1963       QApplication::restoreOverrideCursor();
1964       QString error = "Cannot import the new image.\n";
1965       error += mSlicerManagers[index]->GetLastError().c_str();
1966       QMessageBox::information(this,tr("Problem reading image !"),error);
1967     }
1968   } else {
1969     QString error = "Cannot import the new image.\n";
1970     QMessageBox::information(this,tr("Problem reading image !"),error);
1971   }
1972 }
1973 //------------------------------------------------------------------------------
1974
1975
1976 //------------------------------------------------------------------------------
1977 void vvMainWindow::OpenField()
1978 {
1979   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
1980   //check if a vector field has already been added
1981   for (int child = 0; child < DataTree->topLevelItem(index)->childCount(); child++)
1982     if (DataTree->topLevelItem(index)->child(child)->data(1,Qt::UserRole).toString() == "vector") {
1983       QString error = "Cannot add more than one vector field\n";
1984       error += "Please remove first ";
1985       error += DataTree->topLevelItem(index)->child(child)->data(COLUMN_IMAGE_NAME,Qt::DisplayRole).toString();
1986       QMessageBox::information(this,tr("Problem adding vector field!"),error);
1987       return;
1988     }
1989
1990   QString Extensions = "Images ( *.mhd)";
1991   Extensions += ";;Images ( *.vf)";
1992   QString file = QFileDialog::getOpenFileName(this,tr("Load deformation field"),mInputPathName,Extensions);
1993   if (!file.isEmpty())
1994     AddField(file,index);
1995 }
1996 //------------------------------------------------------------------------------
1997
1998
1999 //------------------------------------------------------------------------------
2000 void vvMainWindow::AddFieldEntry(QString filename,int index,bool from_disk)
2001 {
2002   //create an item in the tree with good settings
2003   QTreeWidgetItem *item = new QTreeWidgetItem();
2004   item->setData(0,Qt::UserRole,filename.toStdString().c_str());
2005   item->setData(1,Qt::UserRole,tr("vector"));
2006   QFileInfo fileinfo(filename); //Do not show the path
2007   item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,fileinfo.fileName());
2008   item->setToolTip(COLUMN_IMAGE_NAME,fileinfo.absoluteFilePath());
2009   qApp->processEvents();
2010
2011   for (int j = 1; j <= 4; j++) {
2012     item->setData(j,Qt::CheckStateRole,DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole));
2013     mSlicerManagers[index]->GetSlicer(j-1)->SetActorVisibility("vector",0,
2014         DataTree->topLevelItem(index)->data(j,Qt::CheckStateRole).toInt());
2015   }
2016
2017   //Create the buttons for reload and close
2018   qApp->processEvents();
2019   QTreePushButton* cButton = new QTreePushButton;
2020   cButton->setItem(item);
2021   cButton->setColumn(COLUMN_CLOSE_IMAGE);
2022   cButton->setToolTip(tr("close vector field"));
2023   cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
2024   connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
2025           this,SLOT(CloseImage(QTreeWidgetItem*, int)));
2026
2027   QTreePushButton* rButton = new QTreePushButton;
2028   rButton->setItem(item);
2029   rButton->setColumn(COLUMN_RELOAD_IMAGE);
2030   rButton->setToolTip(tr("reload vector field"));
2031   rButton->setEnabled(from_disk);
2032   rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
2033   connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
2034           this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
2035
2036   DataTree->topLevelItem(index)->setExpanded(1);
2037   DataTree->topLevelItem(index)->addChild(item);
2038   DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
2039   DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
2040
2041   //set the id of the image
2042   QString id = DataTree->topLevelItem(index)->data(COLUMN_IMAGE_NAME,Qt::UserRole).toString();
2043   item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
2044   UpdateTree();
2045   qApp->processEvents();
2046   ImageInfoChanged();
2047   QApplication::restoreOverrideCursor();
2048 }
2049 //------------------------------------------------------------------------------
2050
2051
2052 //------------------------------------------------------------------------------
2053 void vvMainWindow::AddField(vvImage::Pointer vf,QString file,int index)
2054 {
2055   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2056   vvSlicerManager* imageManager = mSlicerManagers[index];
2057   if (imageManager->SetVF(vf,file.toStdString())) {
2058     AddFieldEntry(file,index,false);
2059   } else {
2060     QString error = "Cannot import the vector field for this image.\n";
2061     error += imageManager->GetLastError().c_str();
2062     QMessageBox::information(this,tr("Problem reading VF !"),error);
2063   }
2064   QApplication::restoreOverrideCursor();
2065 }
2066 //------------------------------------------------------------------------------
2067
2068
2069 //------------------------------------------------------------------------------
2070 void vvMainWindow::AddField(QString file,int index)
2071 {
2072   if (QFile::exists(file)) {
2073     mInputPathName = itksys::SystemTools::GetFilenamePath(file.toStdString()).c_str();
2074
2075     //init the progress events
2076     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2077     vvProgressDialog progress("Opening " + file.toStdString());
2078     qApp->processEvents();
2079
2080     //read the vector and put it in the current mSlicerManager
2081     vvSlicerManager* imageManager = mSlicerManagers[index];
2082     qApp->processEvents();
2083
2084     std::string filename = itksys::SystemTools::GetFilenameWithoutExtension(file.toStdString()).c_str();
2085     if (imageManager->SetVF(file.toStdString())) {
2086       imageManager->Render();
2087       AddFieldEntry(file,index,true);
2088     } else {
2089       QApplication::restoreOverrideCursor();
2090       QString error = "Cannot import the vector field for this image.\n";
2091       error += imageManager->GetLastError().c_str();
2092       QMessageBox::information(this,tr("Problem reading VF !"),error);
2093     }
2094   } else
2095     QMessageBox::information(this,tr("Problem reading VF !"),"File doesn't exist!");
2096
2097 }
2098 //------------------------------------------------------------------------------
2099
2100
2101 //------------------------------------------------------------------------------
2102 void vvMainWindow::SetVFProperty(int subsampling, int scale, int log, int width, double r, double g, double b)
2103 {
2104   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2105   if (mSlicerManagers[index]->GetSlicer(0)->GetVF()) {
2106     for (int i = 0; i < 4; i++) {
2107       mSlicerManagers[index]->GetSlicer(i)->SetVFSubSampling(subsampling);
2108       mSlicerManagers[index]->GetSlicer(i)->SetVFScale(scale);
2109       mSlicerManagers[index]->GetSlicer(i)->SetVFWidth(width);
2110       mSlicerManagers[index]->GetSlicer(i)->SetVFColor(r,g,b);
2111       if (log > 0)
2112         mSlicerManagers[index]->GetSlicer(i)->SetVFLog(1);
2113       else
2114         mSlicerManagers[index]->GetSlicer(i)->SetVFLog(0);
2115     }
2116   }
2117 }
2118 //------------------------------------------------------------------------------
2119
2120
2121 //------------------------------------------------------------------------------
2122 void vvMainWindow::SetOverlayProperty(int color)
2123 {
2124   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2125   if (mSlicerManagers[index]->GetSlicer(0)->GetOverlay()) {
2126     mSlicerManagers[index]->SetOverlayColor(color);
2127     mSlicerManagers[index]->SetColorMap(0);
2128     mSlicerManagers[index]->Render();
2129   }
2130 }
2131 //------------------------------------------------------------------------------
2132
2133 //------------------------------------------------------------------------------
2134 void vvMainWindow::SetFusionProperty(int opacity, int colormap,double window, double level)
2135 {
2136   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2137   if (mSlicerManagers[index]->GetSlicer(0)->GetFusion()) {
2138     mSlicerManagers[index]->SetFusionOpacity(opacity);
2139     mSlicerManagers[index]->SetFusionColorMap(colormap);
2140     mSlicerManagers[index]->SetFusionWindow(window);
2141     mSlicerManagers[index]->SetFusionLevel(level);
2142     mSlicerManagers[index]->SetColorMap(0);
2143     mSlicerManagers[index]->Render();
2144   }
2145 }
2146 //------------------------------------------------------------------------------
2147
2148 //------------------------------------------------------------------------------
2149 void vvMainWindow::SaveAs()
2150 {
2151   if (DataTree->selectedItems()[0]->data(1,Qt::UserRole).toString() == "vector") {
2152     QMessageBox::warning(this,tr("Unsupported type"),tr("Sorry, saving a vector field is unsupported for the moment"));
2153     return;
2154   }
2155
2156   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2157   int dimension = mSlicerManagers[index]->GetDimension();
2158   QStringList OutputListeFormat;
2159   OutputListeFormat.clear();
2160   if (dimension == 1) {
2161     OutputListeFormat.push_back(".mhd");
2162   }
2163   if (dimension == 2) {
2164     OutputListeFormat.push_back(".bmp");
2165     OutputListeFormat.push_back(".png");
2166     OutputListeFormat.push_back(".jpeg");
2167     OutputListeFormat.push_back(".tif");
2168     OutputListeFormat.push_back(".mhd");
2169     OutputListeFormat.push_back(".hdr");
2170     OutputListeFormat.push_back(".vox");
2171   } else if (dimension == 3) {
2172     OutputListeFormat.push_back(".mhd");
2173     OutputListeFormat.push_back(".hdr");
2174     OutputListeFormat.push_back(".vox");
2175   } else if (dimension == 4) {
2176     OutputListeFormat.push_back(".mhd");
2177   }
2178   QString Extensions = "AllFiles(*.*)";
2179   for (int i = 0; i < OutputListeFormat.count(); i++) {
2180     Extensions += ";;Images ( *";
2181     Extensions += OutputListeFormat[i];
2182     Extensions += ")";
2183   }
2184   QString fileName = QFileDialog::getSaveFileName(this,
2185                      tr("Save As"),
2186                      mSlicerManagers[index]->GetFileName().c_str(),
2187                      Extensions);
2188   if (!fileName.isEmpty()) {
2189     std::string fileformat = itksys::SystemTools::GetFilenameLastExtension(fileName.toStdString());
2190     if (OutputListeFormat.contains(
2191           fileformat.c_str())) {
2192       QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2193       std::string action = "Saving";
2194       vvProgressDialog progress("Saving "+fileName.toStdString());
2195       qApp->processEvents();
2196       vvImageWriter *writer = new vvImageWriter;
2197       writer->SetOutputFileName(fileName.toStdString());
2198       writer->SetInput(mSlicerManagers[index]->GetImage());
2199       writer->Update();
2200       QApplication::restoreOverrideCursor();
2201       if (writer->GetLastError().size()) {
2202         QString error = "Saving did not succeed\n";
2203         error += writer->GetLastError().c_str();
2204         QMessageBox::information(this,tr("Saving Problem"),error);
2205         SaveAs();
2206       }
2207     } else {
2208       QString error = fileformat.c_str();
2209       if (error.isEmpty())
2210         error += "no file format specified !";
2211       else
2212         error += " format unknown !!!\n";
2213       QMessageBox::information(this,tr("Saving Problem"),error);
2214       SaveAs();
2215     }
2216   }
2217 }
2218 //------------------------------------------------------------------------------
2219
2220
2221 //------------------------------------------------------------------------------
2222 void vvMainWindow::AddLink(QString image1,QString image2)
2223 {
2224   unsigned int sm1 = 0;
2225   unsigned int sm2 = 0;
2226   
2227   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2228     if (image1.toStdString() == mSlicerManagers[i]->GetId()) {
2229       mSlicerManagers[i]->AddLink(image2.toStdString());
2230       sm1 = i;
2231     }
2232     if (image2.toStdString() == mSlicerManagers[i]->GetId()) {
2233       mSlicerManagers[i]->AddLink(image1.toStdString());
2234       sm2 = i;
2235     }
2236   }
2237
2238   if (linkPanel->isLinkAll())   {
2239     emit UpdateLinkedNavigation(mSlicerManagers[sm1]->GetId(), mSlicerManagers[mCurrentPickedImageIndex]);
2240     emit UpdateLinkedNavigation(mSlicerManagers[sm2]->GetId(), mSlicerManagers[mCurrentPickedImageIndex]);
2241   }
2242   else {
2243     emit UpdateLinkedNavigation(mSlicerManagers[sm2]->GetId(), mSlicerManagers[sm1]);
2244   }
2245 }
2246
2247 //------------------------------------------------------------------------------
2248
2249
2250 //------------------------------------------------------------------------------
2251 void vvMainWindow::RemoveLink(QString image1,QString image2)
2252 {
2253   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2254     if (image1.toStdString() == mSlicerManagers[i]->GetId()) {
2255       mSlicerManagers[i]->RemoveLink(image2.toStdString());
2256     }
2257     if (image2.toStdString() == mSlicerManagers[i]->GetId()) {
2258       mSlicerManagers[i]->RemoveLink(image1.toStdString());
2259     }
2260   }
2261 }
2262 //------------------------------------------------------------------------------
2263
2264 //------------------------------------------------------------------------------
2265 void vvMainWindow::ChangeImageWithIndexOffset(vvSlicerManager *sm, int slicer, int offset)
2266 {
2267   int index = 0;
2268   while(sm != mSlicerManagers[index])
2269     index++;
2270   index = (index+offset) % mSlicerManagers.size();
2271
2272   QTreeWidgetItem* item = GetItemFromSlicerManager(mSlicerManagers[index]);
2273   //CurrentImageChanged(mSlicerManagers[index]->GetId()); //select new image
2274   item->setData(slicer+1,Qt::CheckStateRole,2);         //change checkbox
2275   DisplayChanged(item,slicer+1);
2276 }
2277 //------------------------------------------------------------------------------
2278
2279 void vvMainWindow::HorizontalSliderMoved(int value,int column, int slicer_index)
2280 {
2281   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2282     if (DataTree->topLevelItem(i)->data(column,Qt::CheckStateRole).toInt() > 1) {
2283       for (int j = 0; j < 4; j++) {
2284         mSlicerManagers[i]->SetTSliceInSlicer(value,j);
2285         //if (mSlicerManagers[i]->GetSlicer(j)->GetImageActor()->GetVisibility())
2286         //UpdateTSlice(j,value);
2287       }
2288       mSlicerManagers[i]->GetSlicer(slicer_index)->Render();
2289       break;
2290     }
2291   }
2292 }
2293 //------------------------------------------------------------------------------
2294
2295
2296 //------------------------------------------------------------------------------
2297 void vvMainWindow::NOHorizontalSliderMoved()
2298 {
2299   // if (mCurrentTime == NOHorizontalSlider->value()) return;
2300   HorizontalSliderMoved(NOHorizontalSlider->value(),COLUMN_UL_VIEW,0);
2301 //  mCurrentTime = NOHorizontalSlider->value();
2302 }
2303 //------------------------------------------------------------------------------
2304
2305
2306 //------------------------------------------------------------------------------
2307 void vvMainWindow::NEHorizontalSliderMoved()
2308 {
2309   // if (mCurrentTime == NEHorizontalSlider->value()) return;
2310   HorizontalSliderMoved(NEHorizontalSlider->value(),COLUMN_UR_VIEW,1);
2311 //  mCurrentTime = NEHorizontalSlider->value();
2312 }
2313 //------------------------------------------------------------------------------
2314
2315
2316 //------------------------------------------------------------------------------
2317 void vvMainWindow::SOHorizontalSliderMoved()
2318 {
2319   // if (mCurrentTime == SOHorizontalSlider->value()) return;
2320   HorizontalSliderMoved(SOHorizontalSlider->value(),COLUMN_DL_VIEW,2);
2321   // mCurrentTime = SOHorizontalSlider->value();
2322 }
2323 //------------------------------------------------------------------------------
2324
2325
2326 //------------------------------------------------------------------------------
2327 void vvMainWindow::SEHorizontalSliderMoved()
2328 {
2329   // if (mCurrentTime == SEHorizontalSlider->value()) return;
2330   HorizontalSliderMoved(SEHorizontalSlider->value(),COLUMN_DR_VIEW,3);
2331   // mCurrentTime = SEHorizontalSlider->value();
2332 }
2333 //------------------------------------------------------------------------------
2334
2335 //------------------------------------------------------------------------------
2336 void vvMainWindow::NOVerticalSliderChanged()
2337 {
2338   static int value=-1;
2339   if (value == NOVerticalSlider->value()) return;
2340   else value = NOVerticalSlider->value();
2341   //  int value = NOVerticalSlider->value();
2342   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2343     if (DataTree->topLevelItem(i)->data(COLUMN_UL_VIEW,Qt::CheckStateRole).toInt() > 1) {
2344       if (mSlicerManagers[i]->GetSlicer(0)->GetSlice() != value) {
2345         mSlicerManagers[i]->GetSlicer(0)->SetSlice(value);
2346         mSlicerManagers[i]->VerticalSliderHasChanged(0, value);
2347
2348         // If nor Update/Render -> slider not work
2349         // only render = ok navigation, but for contour Update needed but slower ?
2350
2351         mSlicerManagers[i]->UpdateSlice(0);  // <-- DS add this. Not too much update ? YES. but needed for ImageContour ...
2352         //mSlicerManagers[i]->GetSlicer(0)->Render(); // <-- DS add this, needed for contour, seems ok ? not too slow ?
2353       }
2354       break;
2355     }
2356   }
2357 }
2358 //------------------------------------------------------------------------------
2359
2360
2361 //------------------------------------------------------------------------------
2362 void vvMainWindow::NEVerticalSliderChanged()
2363 {
2364   static int value=-1;
2365   if (value == NEVerticalSlider->value()) return;
2366   else value = NEVerticalSlider->value();
2367   //  int value = NEVerticalSlider->value();
2368   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2369     if (DataTree->topLevelItem(i)->data(COLUMN_UR_VIEW,Qt::CheckStateRole).toInt() > 1) {
2370       if (mSlicerManagers[i]->GetSlicer(1)->GetSlice() != value) {
2371         mSlicerManagers[i]->GetSlicer(1)->SetSlice(value);
2372         mSlicerManagers[i]->VerticalSliderHasChanged(1, value);
2373         mSlicerManagers[i]->UpdateSlice(1);
2374         //mSlicerManagers[i]->GetSlicer(1)->Render(); // <-- DS add this, needed for contour, seems ok ? not too slow ?
2375       }
2376       break;
2377     }
2378   }
2379 }
2380 //------------------------------------------------------------------------------
2381
2382
2383 //------------------------------------------------------------------------------
2384 void vvMainWindow::SOVerticalSliderChanged()
2385 {
2386   static int value=-1;
2387   if (value == SOVerticalSlider->value()) return;
2388   else value = SOVerticalSlider->value();
2389   //int value = SOVerticalSlider->value();
2390   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2391     if (DataTree->topLevelItem(i)->data(COLUMN_DL_VIEW,Qt::CheckStateRole).toInt() > 1) {
2392       if (mSlicerManagers[i]->GetSlicer(2)->GetSlice() != value) {
2393         mSlicerManagers[i]->GetSlicer(2)->SetSlice(value);
2394         mSlicerManagers[i]->VerticalSliderHasChanged(2, value);
2395         mSlicerManagers[i]->UpdateSlice(2);
2396         //mSlicerManagers[i]->GetSlicer(2)->Render(); // <-- DS add this, needed for contour, seems ok ? not too slow ?
2397       }
2398       // else { DD("avoid SOVerticalSlider slicer update"); }
2399       break;
2400     }
2401   }
2402 }
2403 //------------------------------------------------------------------------------
2404
2405
2406 //------------------------------------------------------------------------------
2407 void vvMainWindow::SEVerticalSliderChanged()
2408 {
2409   static int value=-1;
2410   if (value == SEVerticalSlider->value()) return;
2411   else value = SEVerticalSlider->value();
2412   // int value = SEVerticalSlider->value();
2413   for (unsigned int i = 0; i < mSlicerManagers.size(); i++) {
2414     if (DataTree->topLevelItem(i)->data(COLUMN_DR_VIEW,Qt::CheckStateRole).toInt() > 1) {
2415       if (mSlicerManagers[i]->GetSlicer(3)->GetSlice() != value) {
2416         mSlicerManagers[i]->GetSlicer(3)->SetSlice(value);
2417         mSlicerManagers[i]->VerticalSliderHasChanged(3, value);
2418         mSlicerManagers[i]->UpdateSlice(3);
2419         //mSlicerManagers[i]->GetSlicer(3)->Render(); // <-- DS add this, needed for contour, seems ok ? not too slow ?
2420       }
2421       break;
2422     }
2423   }
2424 }
2425 //------------------------------------------------------------------------------
2426
2427
2428 //------------------------------------------------------------------------------
2429 void vvMainWindow::UpdateSlice(int slicer, int slice)
2430 {
2431   // DD("vvMainWindow::UpdateSlice");
2432 //   DD(slicer);
2433 //   DD(slice);
2434   if (slicer == 0) {
2435     //    if (slice != NOVerticalSlider->value())
2436     NOVerticalSlider->setValue(slice);
2437   } else {
2438     if (slicer == 1)
2439       NEVerticalSlider->setValue(slice);
2440     else {
2441       if (slicer == 2)
2442         SOVerticalSlider->setValue(slice);
2443       else {
2444         if (slicer == 3)
2445           SEVerticalSlider->setValue(slice);
2446       }
2447     }
2448   }
2449   // DD("vvMainWindow:UpdateSlice END");
2450 }
2451 //------------------------------------------------------------------------------
2452
2453
2454 //------------------------------------------------------------------------------
2455 void vvMainWindow::UpdateTSlice(int slicer, int slice)
2456 {
2457   switch (slicer) {
2458   case 0:
2459     NOHorizontalSlider->setValue(slice);
2460     break;
2461   case 1:
2462     NEHorizontalSlider->setValue(slice);
2463     break;
2464   case 2:
2465     SOHorizontalSlider->setValue(slice);
2466     break;
2467   case 3:
2468     SEHorizontalSlider->setValue(slice);
2469     break;
2470   }
2471 }
2472 //------------------------------------------------------------------------------
2473
2474
2475 //------------------------------------------------------------------------------
2476 void vvMainWindow::UpdateSliceRange(int slicer, int min, int max, int tmin, int tmax)
2477 {
2478   int position = int((min+max)/2);
2479   int tPosition = int((tmin+tmax)/2);
2480   if (slicer == 0) {
2481     NOVerticalSlider->setValue(position);
2482     NOVerticalSlider->setRange(min,max);
2483     NOHorizontalSlider->setRange(tmin,tmax);
2484     NOHorizontalSlider->setValue(tPosition);
2485   } else if (slicer == 1) {
2486     NEVerticalSlider->setValue(position);
2487     NEVerticalSlider->setRange(min,max);
2488     NEHorizontalSlider->setRange(tmin,tmax);
2489     NEHorizontalSlider->setValue(tPosition);
2490   } else if (slicer == 2) {
2491     SOVerticalSlider->setValue(position);
2492     SOVerticalSlider->setRange(min,max);
2493     SOHorizontalSlider->setRange(tmin,tmax);
2494     SOHorizontalSlider->setValue(tPosition);
2495   } else if (slicer == 3) {
2496     SEVerticalSlider->setValue(position);
2497     SEVerticalSlider->setRange(min,max);
2498     SEHorizontalSlider->setRange(tmin,tmax);
2499     SEHorizontalSlider->setValue(tPosition);
2500   }
2501 }
2502 //------------------------------------------------------------------------------
2503
2504
2505 //------------------------------------------------------------------------------
2506 void vvMainWindow::SaveNOScreenshot()
2507 {
2508   vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
2509   w2i->SetInput(NOViewWidget->GetRenderWindow());
2510   w2i->Update();
2511   SaveScreenshot(w2i->GetOutput());
2512   w2i->Delete();
2513 }
2514 //------------------------------------------------------------------------------
2515
2516
2517 //------------------------------------------------------------------------------
2518 void vvMainWindow::SaveNEScreenshot()
2519 {
2520   vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
2521   w2i->SetInput(NEViewWidget->GetRenderWindow());
2522   w2i->Update();
2523   SaveScreenshot(w2i->GetOutput());
2524   w2i->Delete();
2525 }
2526 //------------------------------------------------------------------------------
2527
2528
2529 //------------------------------------------------------------------------------
2530 void vvMainWindow::SaveSOScreenshot()
2531 {
2532   vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
2533   w2i->SetInput(SOViewWidget->GetRenderWindow());
2534   w2i->Update();
2535   SaveScreenshot(w2i->GetOutput());
2536   w2i->Delete();
2537 }
2538 //------------------------------------------------------------------------------
2539
2540
2541 //------------------------------------------------------------------------------
2542 void vvMainWindow::SaveSEScreenshot()
2543 {
2544   vtkWindowToImageFilter *w2i = vtkWindowToImageFilter::New();
2545   w2i->SetInput(SEViewWidget->GetRenderWindow());
2546   w2i->Update();
2547   SaveScreenshot(w2i->GetOutput());
2548   w2i->Delete();
2549 }
2550 //------------------------------------------------------------------------------
2551
2552
2553 //------------------------------------------------------------------------------
2554 void vvMainWindow::SaveScreenshot(vtkImageData* image)
2555 {
2556   QString Extensions = "Images( *.png);;";
2557   Extensions += "Images( *.jpg);;";
2558   Extensions += "Images( *.bmp);;";
2559   Extensions += "Images( *.tif);;";
2560   Extensions += "Images( *.ppm)";
2561   QString fileName = QFileDialog::getSaveFileName(this,
2562                      tr("Save As"),
2563                      itksys::SystemTools::GetFilenamePath(
2564                        mSlicerManagers[0]->GetFileName()).c_str(),
2565                      Extensions);
2566   if (!fileName.isEmpty()) {
2567     const char *ext = fileName.toStdString().c_str() + strlen(fileName.toStdString().c_str()) - 4;
2568     if (!strcmp(ext, ".bmp")) {
2569       vtkBMPWriter *bmp = vtkBMPWriter::New();
2570       bmp->SetInput(image);
2571       bmp->SetFileName(fileName.toStdString().c_str());
2572       bmp->Write();
2573       bmp->Delete();
2574     } else if (!strcmp(ext, ".tif")) {
2575       vtkTIFFWriter *tif = vtkTIFFWriter::New();
2576       tif->SetInput(image);
2577       tif->SetFileName(fileName.toStdString().c_str());
2578       tif->Write();
2579       tif->Delete();
2580     } else if (!strcmp(ext, ".ppm")) {
2581       vtkPNMWriter *pnm = vtkPNMWriter::New();
2582       pnm->SetInput(image);
2583       pnm->SetFileName(fileName.toStdString().c_str());
2584       pnm->Write();
2585       pnm->Delete();
2586     } else if (!strcmp(ext, ".png")) {
2587       vtkPNGWriter *png = vtkPNGWriter::New();
2588       png->SetInput(image);
2589       png->SetFileName(fileName.toStdString().c_str());
2590       png->Write();
2591       png->Delete();
2592     } else if (!strcmp(ext, ".jpg")) {
2593       vtkJPEGWriter *jpg = vtkJPEGWriter::New();
2594       jpg->SetInput(image);
2595       jpg->SetFileName(fileName.toStdString().c_str());
2596       jpg->Write();
2597       jpg->Delete();
2598     } else {
2599       QMessageBox::information(this,tr("Problem saving screenshot !"),tr("Cannot save image.\nPlease set a file extension !!!"));
2600     }
2601   }
2602
2603 }
2604 //------------------------------------------------------------------------------
2605
2606
2607 //------------------------------------------------------------------------------
2608 void vvMainWindow::GoToCursor()
2609 {
2610   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2611   for (int column = 1; column < 5; column++) {
2612     if (DataTree->selectedItems()[0]->data(column,Qt::CheckStateRole).toInt() > 1) {
2613       double* cursorPos = mSlicerManagers[index]->GetSlicer(column-1)->GetCursorPosition();
2614       mSlicerManagers[index]->GetSlicer(column-1)->SetCurrentPosition(
2615         cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
2616       mSlicerManagers[index]->UpdateViews(1,column-1);
2617       mSlicerManagers[index]->UpdateLinked(column-1);
2618       break;
2619     }
2620   }
2621 }
2622 //------------------------------------------------------------------------------
2623
2624 //------------------------------------------------------------------------------
2625 void vvMainWindow::PlayPause()
2626 {
2627   if (playMode) {
2628     playMode = 0;
2629     playButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/player_play.png")));
2630     ImageInfoChanged();
2631     return;
2632   } else {
2633     int image_number=DataTree->topLevelItemCount();
2634     bool has_temporal;
2635     for (int i=0; i<image_number; i++)
2636       if (mSlicerManagers[i]->GetImage()->GetVTKImages().size() > 1) {
2637         has_temporal=true;
2638         break;
2639       }
2640     if (has_temporal) {
2641       playMode = 1;
2642       playButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/player_pause.png")));
2643       QTimer::singleShot(1000/mFrameRate, this, SLOT(PlayNext()));
2644     }
2645   }
2646 }
2647 //------------------------------------------------------------------------------
2648
2649 //------------------------------------------------------------------------------
2650 void vvMainWindow::PlayNext()
2651 {
2652   if (playMode && !this->isHidden()) {
2653     int image_number=DataTree->topLevelItemCount();
2654     ///Only play one slicer per SM, and only if the SM is being displayed
2655     for (int i=0; i<image_number; i++)
2656       for (int j=0; j<4; j++)
2657         if (mSlicerManagers[i]->GetImage()->GetVTKImages().size() > 1 &&
2658             DataTree->topLevelItem(i)->data(j+1,Qt::CheckStateRole).toInt() > 0) {
2659           mSlicerManagers[i]->SetNextTSlice(j);
2660           break;
2661         }
2662     QTimer::singleShot(1000/mFrameRate, this, SLOT(PlayNext()));
2663   }
2664 }
2665 //------------------------------------------------------------------------------
2666
2667 void vvMainWindow::ShowLastImage()
2668 {
2669   if (mSlicerManagers.size() > 1) {
2670     QTreeWidgetItem * item=DataTree->topLevelItem(DataTree->topLevelItemCount()-1);
2671     CurrentImageChanged(mSlicerManagers.back()->GetId()); //select new image
2672     item->setData(1,Qt::CheckStateRole,2); //show the new image in the first panel
2673     DisplayChanged(item,1);
2674   }
2675 }
2676
2677 //------------------------------------------------------------------------------
2678 void vvMainWindow::UpdateRenderWindows()
2679 {
2680   if (NOViewWidget->GetRenderWindow()) NOViewWidget->GetRenderWindow()->Render();
2681   if (NEViewWidget->GetRenderWindow()) NEViewWidget->GetRenderWindow()->Render();
2682   if (SOViewWidget->GetRenderWindow()) SOViewWidget->GetRenderWindow()->Render();
2683   if (SEViewWidget->GetRenderWindow()) SEViewWidget->GetRenderWindow()->Render();
2684 }
2685 //------------------------------------------------------------------------------
2686
2687 //------------------------------------------------------------------------------
2688 void vvMainWindow::SegmentationOnCurrentImage()
2689 {
2690   int index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2691
2692   vvSegmentationDialog segmentation;
2693   segmentation.SetImage(mSlicerManagers[index]->GetSlicer(0)->GetImage());
2694   segmentation.exec();
2695 }
2696 //------------------------------------------------------------------------------
2697
2698 void vvMainWindow::SurfaceViewerLaunch()
2699 {
2700   vvSurfaceViewerDialog surfaceViewer;
2701   surfaceViewer.exec();
2702 }
2703 //------------------------------------------------------------------------------
2704
2705
2706 //------------------------------------------------------------------------------
2707 int vvMainWindow::GetImageDuplicateFilenameNumber(std::string filename)
2708 {
2709   int number=0;
2710   for(unsigned int l=0; l<mSlicerManagers.size(); l++) {
2711     vvSlicerManager * v = mSlicerManagers[l];
2712     if (v->GetBaseFileName() ==
2713         vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(filename))) {
2714       number = std::max(number, v->GetBaseFileNameNumber()+1);
2715     }
2716   }
2717   return number;
2718 }
2719 //------------------------------------------------------------------------------
2720
2721
2722 //------------------------------------------------------------------------------
2723 vvSlicerManager* vvMainWindow::AddImage(vvImage::Pointer image,std::string filename)
2724 {
2725   // Change filename if another image exist with the same name
2726   int number = GetImageDuplicateFilenameNumber(filename);
2727
2728   // Create new SliceManager
2729   vvSlicerManager* slicer_manager = new vvSlicerManager(4);
2730   slicer_manager->SetImage(image);//, IMAGE, number);
2731   //  filename = filename+"_"+clitk::toString(number);
2732   slicer_manager->SetFilename(filename, number);
2733   mSlicerManagers.push_back(slicer_manager);
2734
2735   //create an item in the tree with good settings
2736   QTreeWidgetItem *item = new QTreeWidgetItem();
2737   item->setData(0,Qt::UserRole,slicer_manager->GetFileName().c_str());//files[i].c_str());
2738   item->setData(COLUMN_IMAGE_NAME,Qt::DisplayRole,slicer_manager->GetFileName().c_str());//filename.c_str());
2739   qApp->processEvents();
2740
2741   for (int j = 1; j <= 4; j++) item->setData(j,Qt::CheckStateRole,1);
2742
2743   //Create the buttons for reload and close
2744   qApp->processEvents();
2745   QTreePushButton* cButton = new QTreePushButton;
2746   cButton->setItem(item);
2747   cButton->setColumn(COLUMN_CLOSE_IMAGE);
2748   cButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/exit.png")));
2749   connect(cButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
2750           this,SLOT(CloseImage(QTreeWidgetItem*, int)));
2751
2752   QTreePushButton* rButton = new QTreePushButton;
2753   rButton->setItem(item);
2754   rButton->setColumn(COLUMN_RELOAD_IMAGE);
2755   rButton->setIcon(QIcon(QString::fromUtf8(":/common/icons/rotateright.png")));
2756   rButton->setEnabled(0);
2757   connect(rButton,SIGNAL(clickedInto(QTreeWidgetItem*, int)),
2758           this,SLOT(ReloadImage(QTreeWidgetItem*, int)));
2759
2760   DataTree->addTopLevelItem(item);
2761   DataTree->setItemWidget(item, COLUMN_CLOSE_IMAGE, cButton);
2762   DataTree->setItemWidget(item, COLUMN_RELOAD_IMAGE, rButton);
2763
2764   //set the id of the image
2765   QString id = slicer_manager->GetFileName().c_str() + QString::number(mSlicerManagers.size()-1);
2766   item->setData(COLUMN_IMAGE_NAME,Qt::UserRole,id.toStdString().c_str());
2767   mSlicerManagers.back()->SetId(id.toStdString());
2768
2769   linkPanel->addImage(slicer_manager->GetFileName().c_str()// filename
2770                       , id.toStdString());
2771
2772   connect(mSlicerManagers.back(), SIGNAL(currentImageChanged(std::string)),
2773           this, SLOT(CurrentImageChanged(std::string)));
2774   connect(mSlicerManagers.back(), SIGNAL(currentPickedImageChanged(std::string)),
2775           this, SLOT(CurrentPickedImageChanged(std::string)));
2776   connect(mSlicerManagers.back(), SIGNAL(UpdatePosition(int, double, double, double, double, double, double, double)),
2777           this, SLOT(MousePositionChanged(int,double, double, double, double, double, double, double)));
2778   connect(mSlicerManagers.back(), SIGNAL(UpdateVector(int, double, double, double, double)),
2779           this, SLOT(VectorChanged(int,double,double,double, double)));
2780   connect(mSlicerManagers.back(), SIGNAL(UpdateOverlay(int, double, double)),
2781           this, SLOT(OverlayChanged(int,double,double)));
2782   connect(mSlicerManagers.back(), SIGNAL(UpdateFusion(int, double)),
2783           this, SLOT(FusionChanged(int,double)));
2784   connect(mSlicerManagers.back(), SIGNAL(UpdateWindows(int, int, int)),
2785           this,SLOT(WindowsChanged(int, int, int)));
2786   connect(mSlicerManagers.back(), SIGNAL(WindowLevelChanged(double, double,int, int)),
2787           this,SLOT(WindowLevelChanged(double, double, int, int)));
2788   connect(mSlicerManagers.back(), SIGNAL(UpdateSlice(int,int)),
2789           this,SLOT(UpdateSlice(int,int)));
2790   connect(mSlicerManagers.back(), SIGNAL(UpdateTSlice(int, int)),
2791           this,SLOT(UpdateTSlice(int, int)));
2792   connect(mSlicerManagers.back(), SIGNAL(UpdateSliceRange(int,int,int,int,int)),
2793           this,SLOT(UpdateSliceRange(int,int,int,int,int)));
2794   connect(mSlicerManagers.back(), SIGNAL(UpdateLinkManager(std::string,int,double,double,double,int)),
2795           this,SLOT(UpdateLinkManager(std::string,int,double,double,double,int)));
2796   connect(mSlicerManagers.back(), SIGNAL(UpdateLinkedNavigation(std::string,vvSlicerManager*)),
2797           this,SLOT(UpdateLinkedNavigation(std::string,vvSlicerManager*)));
2798   connect(mSlicerManagers.back(), SIGNAL(ChangeImageWithIndexOffset(vvSlicerManager*,int,int)),
2799           this,SLOT(ChangeImageWithIndexOffset(vvSlicerManager*,int,int)));
2800   connect(mSlicerManagers.back(), SIGNAL(LandmarkAdded()),landmarksPanel,SLOT(AddPoint()));
2801   UpdateTree();
2802   qApp->processEvents();
2803   InitSlicers();
2804   ShowLastImage();
2805   InitDisplay();
2806   qApp->processEvents();
2807
2808   // End
2809   ImageInfoChanged();
2810   return slicer_manager;
2811 }
2812 //------------------------------------------------------------------------------
2813
2814
2815 //------------------------------------------------------------------------------
2816 void vvMainWindow::UpdateCurrentSlicer()
2817 {
2818   int index = -1;
2819   if (DataTree->selectedItems().size() > 0) {
2820     index = GetSlicerIndexFromItem(DataTree->selectedItems()[0]);
2821   }
2822   mSlicerManagerCurrentIndex = index;
2823 }
2824 //------------------------------------------------------------------------------
2825