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 "vvReadState.h"
20 #include "vvMainWindow.h"
21 #include "vvSlicerManager.h"
22 #include "vvToolCreatorBase.h"
23 #include "vvToolBaseBase.h"
25 #include <qtreewidget.h>
28 #include <QXmlStreamReader>
33 //------------------------------------------------------------------------------
34 vvReadState::vvReadState() : m_XmlReader(new QXmlStreamReader), m_File(new QFile)
38 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 vvReadState::~vvReadState()
45 //------------------------------------------------------------------------------
48 //------------------------------------------------------------------------------
49 void vvReadState::Run(vvMainWindow* vvWindow, const std::string& file)
54 m_File->setFileName(file.c_str());
55 m_File->open(QIODevice::ReadOnly);
56 m_XmlReader->setDevice(m_File.get());
58 QTreeWidget* tree = m_Window->GetTree();
60 // Get the number of images already loaded
61 m_TreeItemCount = tree->topLevelItemCount();
68 //------------------------------------------------------------------------------
71 //------------------------------------------------------------------------------
72 void vvReadState::ReadTree()
77 while (!m_XmlReader->isEndElement() || value != "Images") {
78 m_XmlReader->readNext();
79 value = m_XmlReader->qualifiedName().toString().toStdString();
80 if (m_XmlReader->isStartElement()) {
81 if (value == "Image") value = ReadImage();
86 while (!m_XmlReader->isEndElement() || value != "Links") {
87 m_XmlReader->readNext();
88 value = m_XmlReader->qualifiedName().toString().toStdString();
89 if (m_XmlReader->isStartElement()) {
90 if (value == "LinkedFrom") value = ReadLink();
94 if (m_XmlReader->hasError())
95 std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
97 //------------------------------------------------------------------------------
100 //------------------------------------------------------------------------------
101 std::string vvReadState::ReadImage()
104 int current_index = -1;
105 std::vector<std::string> files(1);
107 QXmlStreamAttributes attributes = m_XmlReader->attributes();
108 if (!m_XmlReader->hasError())
109 current_index = attributes.value("Index").toString().toInt();
111 current_index += m_TreeItemCount;
113 while (!m_XmlReader->isEndElement() || value != "Image") {
114 m_XmlReader->readNext();
115 value = m_XmlReader->qualifiedName().toString().toStdString();
116 if (m_XmlReader->isStartElement()) {
117 if (value == "FileName") {
118 files[0] = m_XmlReader->readElementText().toStdString();
119 if (!m_XmlReader->hasError()) {
120 m_Window->LoadImages(files, vvImageReader::IMAGE);
123 else if (current_index >= 0) {
124 vvSlicerManager* slicerManager = m_Window->GetSlicerManagers()[current_index];
125 if (value == "Preset") {
126 double vali = m_XmlReader->readElementText().toInt();
127 if (!m_XmlReader->hasError())
128 slicerManager->SetPreset(vali);
130 else if (value == "Window") {
131 double vald = m_XmlReader->readElementText().toDouble();
132 if (!m_XmlReader->hasError())
133 slicerManager->SetColorWindow(vald);
135 else if (value == "Level") {
136 double vald = m_XmlReader->readElementText().toDouble();
137 if (!m_XmlReader->hasError())
138 slicerManager->SetColorLevel(vald);
140 else if (value == "Fusion")
141 value = ReadFusion(current_index);
142 else if (value == "Overlay")
143 value = ReadOverlay(current_index);
144 else if (value == "Vector")
145 value = ReadVector(current_index);
150 if (!m_XmlReader->hasError())
155 //------------------------------------------------------------------------------
158 //------------------------------------------------------------------------------
159 std::string vvReadState::ReadFusion(int index)
161 std::string file, value;
164 vvSlicerManager* slicerManager = m_Window->GetSlicerManagers()[index];
165 while (!m_XmlReader->isEndElement() || value != "Fusion") {
166 m_XmlReader->readNext();
167 value = m_XmlReader->qualifiedName().toString().toStdString();
168 if (m_XmlReader->isStartElement()) {
169 if (value == "FileName") {
170 file = m_XmlReader->readElementText().toStdString();
171 if (!m_XmlReader->hasError()) {
172 // TODO: manage sequence of images
173 std::vector<std::string> vec;
174 vec.push_back(file.c_str());
175 m_Window->AddFusionImage(index, vec, vvImageReader::IMAGE);
178 if (value == "FusionOpacity") {
179 vali = m_XmlReader->readElementText().toInt();
180 if (!m_XmlReader->hasError())
181 slicerManager->SetFusionOpacity(vali);
183 if (value == "FusionThresholdOpacity") {
184 vali = m_XmlReader->readElementText().toInt();
185 if (!m_XmlReader->hasError())
186 slicerManager->SetFusionThresholdOpacity(vali);
188 if (value == "FusionColorMap") {
189 vali = m_XmlReader->readElementText().toInt();
190 if (!m_XmlReader->hasError())
191 slicerManager->SetFusionColorMap(vali);
193 if (value == "FusionWindow") {
194 vald = m_XmlReader->readElementText().toDouble();
195 if (!m_XmlReader->hasError())
196 slicerManager->SetFusionWindow(vald);
198 if (value == "FusionLevel") {
199 vald = m_XmlReader->readElementText().toDouble();
200 if (!m_XmlReader->hasError())
201 slicerManager->SetFusionLevel(vald);
205 m_Window->ImageInfoChanged();
208 //------------------------------------------------------------------------------
211 //------------------------------------------------------------------------------
212 std::string vvReadState::ReadOverlay(int index)
214 std::string file, value;
217 vvSlicerManager* slicerManager = m_Window->GetSlicerManagers()[index];
218 while (!m_XmlReader->isEndElement() || value != "Overlay") {
219 m_XmlReader->readNext();
220 value = m_XmlReader->qualifiedName().toString().toStdString();
221 if (m_XmlReader->isStartElement()) {
222 if (value == "FileName") {
223 file = m_XmlReader->readElementText().toStdString();
224 if (!m_XmlReader->hasError()) {
225 // TODO: manage sequence of images
226 std::vector<std::string> vec;
227 vec.push_back(file.c_str());
228 m_Window->AddOverlayImage(index, vec, vvImageReader::IMAGE);
231 if (value == "OverlayColorWindow") {
232 vald = m_XmlReader->readElementText().toDouble();
233 if (!m_XmlReader->hasError())
234 slicerManager->SetOverlayColorWindow(vald);
236 if (value == "OverlayColorLevel") {
237 vald = m_XmlReader->readElementText().toDouble();
238 if (!m_XmlReader->hasError())
239 slicerManager->SetOverlayColorLevel(vald);
241 if (value == "LinkOverlayWindowLevel") {
242 vali = m_XmlReader->readElementText().toInt();
243 if (!m_XmlReader->hasError())
244 slicerManager->SetLinkOverlayWindowLevel(vali);
246 if (value == "OverlayColor") {
247 vali = m_XmlReader->readElementText().toInt();
248 if (!m_XmlReader->hasError())
249 slicerManager->SetOverlayColor(vali);
253 m_Window->ImageInfoChanged();
256 //------------------------------------------------------------------------------
259 //------------------------------------------------------------------------------
260 std::string vvReadState::ReadVector(int index)
262 std::string file, value;
263 while (!m_XmlReader->isEndElement() || value != "Vector") {
264 m_XmlReader->readNext();
265 value = m_XmlReader->qualifiedName().toString().toStdString();
266 if (m_XmlReader->isStartElement()) {
267 if (value == "FileName") {
268 file = m_XmlReader->readElementText().toStdString();
269 if (!m_XmlReader->hasError())
270 m_Window->AddField(file.c_str(), index);
276 //------------------------------------------------------------------------------
279 //------------------------------------------------------------------------------
280 std::string vvReadState::ReadLink()
282 std::string id_from, id_to, value;
284 QXmlStreamAttributes attributes = m_XmlReader->attributes();
285 if (!m_XmlReader->hasError()) {
286 id_from = attributes.value("Id").toString().toStdString();
289 while (!m_XmlReader->isEndElement() || value != "LinkedFrom") {
290 m_XmlReader->readNext();
291 value = m_XmlReader->qualifiedName().toString().toStdString();
292 if (m_XmlReader->isStartElement()) {
293 if (value == "LinkedTo") {
294 id_to = m_XmlReader->readElementText().toStdString();
295 if (!m_XmlReader->hasError()) {
296 m_Window->AddLink(id_from.c_str(), id_to.c_str(), false);
304 //------------------------------------------------------------------------------
306 //------------------------------------------------------------------------------
307 void vvReadState::ReadGUI()
311 //------------------------------------------------------------------------------
314 //------------------------------------------------------------------------------
315 void vvReadState::ReadTools()
319 while ((!m_XmlReader->hasError()) && (!m_XmlReader->isEndElement() || value != "Tools")) {
320 m_XmlReader->readNext();
321 value = m_XmlReader->qualifiedName().toString().toStdString();
322 if (value != "Tools") {
323 if (m_XmlReader->isStartElement()) {
329 if (m_XmlReader->hasError())
330 std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
332 //------------------------------------------------------------------------------
335 //------------------------------------------------------------------------------
336 void vvReadState::ReadTool(const std::string & toolname)
338 // Find name into vvToolManager::GetInstance()->GetListOfTools();
339 vvToolCreatorBase * v = vvToolManager::GetInstance()->GetToolCreatorFromName(toolname.c_str());
341 std::cerr << "Error, I do not know the tool named '" << toolname << "' ; ignored." << std::endl;
342 std::string value="";
343 while (!m_XmlReader->isEndElement() || value != toolname) {
344 m_XmlReader->readNext();
345 value = m_XmlReader->qualifiedName().toString().toStdString();
346 if (m_XmlReader->hasError()) {
347 std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
355 // std::vector<vvToolBaseBase*> & tools = v->GetListOfTool();
356 v->m_XmlReader = m_XmlReader;
357 v->mReadStateFlag = true;
358 v->mImageIndex = m_TreeItemCount;
359 v->MenuSpecificToolSlot();
360 v->mReadStateFlag = false;
361 m_XmlReader = v->m_XmlReader; // Need because auto_ptr operator= release on the right.
363 //------------------------------------------------------------------------------