]> Creatis software - clitk.git/blob - vv/vvToolConvert.cxx
d63a4eaafffa9477d79bcffad8e75f276a805106
[clitk.git] / vv / vvToolConvert.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 "vvToolConvert.h"
20 #include "vvSlicer.h"
21 #include "clitkImageConvertGenericFilter.h"
22 #include <QMenu>
23 #include <QApplication>
24 #include <QMessageBox>
25 #include <vtkImageData.h>
26
27 //------------------------------------------------------------------------------
28 // Create the tool and automagically (I like this word) insert it in
29 // the main window menu.
30 ADD_TOOL(vvToolConvert);
31 //------------------------------------------------------------------------------
32
33 QAction * vvToolConvert::a = NULL;
34 QAction * vvToolConvert::b = NULL;
35 std::vector<std::string> vvToolConvert::mListOfPixelTypeNames;
36 std::vector<std::string> vvToolConvert::mListOfPixelTypeIcons;
37 std::map<std::string, QAction*> vvToolConvert::mMapOfPixelType;
38
39 //------------------------------------------------------------------------------
40 vvToolConvert::vvToolConvert(vvMainWindowBase * parent, Qt::WindowFlags f):
41   vvToolBase<vvToolConvert>(parent)
42 {
43 }
44 //------------------------------------------------------------------------------
45
46
47 //------------------------------------------------------------------------------
48 vvToolConvert::~vvToolConvert() {
49 }
50 //------------------------------------------------------------------------------
51
52
53 //------------------------------------------------------------------------------
54 void vvToolConvert::Initialize() {
55   SetToolName("Convert");
56   SetToolMenuName("Convert with WidgetBase");
57   SetToolIconFilename(":/common/icons/ducky.png");
58   SetToolTip("Make 'foo' on an image.");
59   
60   // Create a menu to choose the convert image
61   QMenu * m = new QMenu();
62   m->setTitle("Convert to ");
63   m->setIcon(QIcon(QString::fromUtf8(":/common/icons/green-arrow.png")));
64
65   mListOfPixelTypeNames.push_back("char");
66   mListOfPixelTypeNames.push_back("unsigned_char");
67   mListOfPixelTypeNames.push_back("short");
68   mListOfPixelTypeNames.push_back("unsigned_short");
69   mListOfPixelTypeNames.push_back("int");
70   mListOfPixelTypeNames.push_back("float");
71   mListOfPixelTypeNames.push_back("double");
72
73   mListOfPixelTypeIcons.push_back(":/common/icons/1b.png");
74   mListOfPixelTypeIcons.push_back(":/common/icons/1b.png");
75   mListOfPixelTypeIcons.push_back(":/common/icons/2b.png");
76   mListOfPixelTypeIcons.push_back(":/common/icons/2b.png");
77   mListOfPixelTypeIcons.push_back(":/common/icons/4b.png");
78   mListOfPixelTypeIcons.push_back(":/common/icons/4b.png");
79   mListOfPixelTypeIcons.push_back(":/common/icons/8b.png");
80
81   for(unsigned int i=0; i<mListOfPixelTypeNames.size(); i++) {
82     std::string & s = mListOfPixelTypeNames[i];
83     mMapOfPixelType[s] = m->addAction(QIcon(QString::fromUtf8(mListOfPixelTypeIcons[i].c_str())), 
84                                       tr(s.c_str()));
85     m->addAction(mMapOfPixelType[s]);
86   }
87
88   CREATOR(vvToolConvert)->addMenuToContextMenu(m);
89 }
90 //------------------------------------------------------------------------------
91
92
93 //------------------------------------------------------------------------------
94 void vvToolConvert::show() {
95   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
96   // Get action menu name
97   QAction * cc = dynamic_cast<QAction*>(mSender);
98   std::string type = cc->text().toStdString();
99   // Get current image
100   int index = mMainWindowBase->GetSlicerManagerCurrentIndex();
101   vvSlicerManager * m = mMainWindowBase->GetSlicerManagers()[index];
102   assert(m != NULL); // Should no occur
103   
104   // Create filter and run !
105   clitk::ImageConvertGenericFilter * filter = new clitk::ImageConvertGenericFilter;
106   filter->SetInputVVImage(m->GetImage());
107   filter->SetOutputPixelType(type);
108   filter->EnableDisplayWarning(false);
109   filter->Update();
110       
111   // Manage warning
112   if (filter->IsWarningOccur()) {
113     QApplication::restoreOverrideCursor();
114     QMessageBox::warning(mMainWindowBase, "Warning", filter->GetWarning().c_str());     
115     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
116   }
117   
118   // Output
119   vvImage::Pointer output = filter->GetOutputVVImage();
120   std::ostringstream osstream;
121   osstream << "Convert_" << type << "_" << m->GetSlicer(0)->GetFileName() << ".mhd";
122   AddImage(output,osstream.str()); 
123   QApplication::restoreOverrideCursor();
124 }
125 //------------------------------------------------------------------------------
126
127
128 //------------------------------------------------------------------------------
129 void vvToolConvert::apply() {
130   // nothing !!
131 }
132 //------------------------------------------------------------------------------
133
134