1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #include "vvToolConvert.h"
21 #include "clitkImageConvertGenericFilter.h"
23 #include <QApplication>
24 #include <QMessageBox>
25 #include <vtkImageData.h>
27 //------------------------------------------------------------------------------
28 // Create the tool and automagically (I like this word) insert it in
29 // the main window menu.
30 ADD_TOOL(vvToolConvert);
31 //------------------------------------------------------------------------------
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;
39 //------------------------------------------------------------------------------
40 vvToolConvert::vvToolConvert(vvMainWindowBase * parent, Qt::WindowFlags f):
41 vvToolBase<vvToolConvert>(parent)
44 //------------------------------------------------------------------------------
47 //------------------------------------------------------------------------------
48 vvToolConvert::~vvToolConvert()
51 //------------------------------------------------------------------------------
54 //------------------------------------------------------------------------------
55 void vvToolConvert::Initialize()
57 SetToolName("Convert");
58 SetToolMenuName("Convert with WidgetBase");
59 SetToolIconFilename(":/common/icons/ducky.png");
60 SetToolTip("Make 'foo' on an image.");
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")));
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");
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");
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())),
87 m->addAction(mMapOfPixelType[s]);
90 CREATOR(vvToolConvert)->addMenuToContextMenu(m);
92 //------------------------------------------------------------------------------
95 //------------------------------------------------------------------------------
96 void vvToolConvert::show()
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();
103 int index = mMainWindowBase->GetSlicerManagerCurrentIndex();
104 vvSlicerManager * m = mMainWindowBase->GetSlicerManagers()[index];
105 assert(m != NULL); // Should no occur
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);
115 if (filter->IsWarningOccur()) {
116 QApplication::restoreOverrideCursor();
117 QMessageBox::warning(mMainWindowBase, "Warning", filter->GetWarning().c_str());
118 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
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();
128 //------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------
132 void vvToolConvert::apply()
136 //------------------------------------------------------------------------------