]> Creatis software - clitk.git/blob - vv/vvToolConvert.cxx
Fusion windows level is now 4 decimals
[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 //------------------------------------------------------------------------------
55 void vvToolConvert::Initialize()
56 {
57   SetToolName("Convert");
58   SetToolMenuName("Convert with WidgetBase");
59   SetToolIconFilename(":/common/icons/ducky.png");
60   SetToolTip("Make 'foo' on an image.");
61
62   // Create a menu to choose the convert image
63   QMenu * m = new QMenu();
64   m->setTitle("Convert to ");
65   m->setIcon(QIcon(QString::fromUtf8(":/common/icons/green-arrow.png")));
66
67   mListOfPixelTypeNames.push_back("char");
68   mListOfPixelTypeNames.push_back("unsigned_char");
69   mListOfPixelTypeNames.push_back("short");
70   mListOfPixelTypeNames.push_back("unsigned_short");
71   mListOfPixelTypeNames.push_back("int");
72   mListOfPixelTypeNames.push_back("float");
73   mListOfPixelTypeNames.push_back("double");
74
75   mListOfPixelTypeIcons.push_back(":/common/icons/1b.png");
76   mListOfPixelTypeIcons.push_back(":/common/icons/1b.png");
77   mListOfPixelTypeIcons.push_back(":/common/icons/2b.png");
78   mListOfPixelTypeIcons.push_back(":/common/icons/2b.png");
79   mListOfPixelTypeIcons.push_back(":/common/icons/4b.png");
80   mListOfPixelTypeIcons.push_back(":/common/icons/4b.png");
81   mListOfPixelTypeIcons.push_back(":/common/icons/8b.png");
82
83   for(unsigned int i=0; i<mListOfPixelTypeNames.size(); i++) {
84     std::string & s = mListOfPixelTypeNames[i];
85     mMapOfPixelType[s] = m->addAction(QIcon(QString::fromUtf8(mListOfPixelTypeIcons[i].c_str())),
86                                       tr(s.c_str()));
87     m->addAction(mMapOfPixelType[s]);
88   }
89
90   CREATOR(vvToolConvert)->addMenuToContextMenu(m);
91 }
92 //------------------------------------------------------------------------------
93
94
95 //------------------------------------------------------------------------------
96 void vvToolConvert::show()
97 {
98   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
99   // Get action menu name
100   QAction * cc = dynamic_cast<QAction*>(mSender);
101   std::string type = cc->text().toStdString();
102   // Get current image
103   int index = mMainWindowBase->GetSlicerManagerCurrentIndex();
104   vvSlicerManager * m = mMainWindowBase->GetSlicerManagers()[index];
105   assert(m != NULL); // Should no occur
106
107   // Create filter and run !
108   clitk::ImageConvertGenericFilter * filter = new clitk::ImageConvertGenericFilter;
109   filter->SetInputVVImage(m->GetImage());
110   filter->SetOutputPixelType(type);
111   filter->EnableDisplayWarning(false);
112   filter->Update();
113
114   // Manage warning
115   if (filter->IsWarningOccur()) {
116     QApplication::restoreOverrideCursor();
117     QMessageBox::warning(mMainWindowBase, "Warning", filter->GetWarning().c_str());
118     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
119   }
120
121   // Output
122   vvImage::Pointer output = filter->GetOutputVVImage();
123   std::ostringstream osstream;
124   osstream << "Convert_" << type << "_" << m->GetSlicer(0)->GetFileName() << ".mhd";
125   AddImage(output,osstream.str());
126   QApplication::restoreOverrideCursor();
127 }
128 //------------------------------------------------------------------------------
129
130
131 //------------------------------------------------------------------------------
132 void vvToolConvert::apply()
133 {
134   // nothing !!
135 }
136 //------------------------------------------------------------------------------
137
138