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()
76 while (!m_XmlReader->isEndElement() || value != "Images") {
77 m_XmlReader->readNext();
78 value = m_XmlReader->qualifiedName().toString().toStdString();
79 if (m_XmlReader->isStartElement()) {
80 if (value == "Image") value = ReadImage();
84 if (m_XmlReader->hasError())
85 std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
87 //------------------------------------------------------------------------------
90 //------------------------------------------------------------------------------
91 std::string vvReadState::ReadImage()
94 int current_index = -1;
95 std::vector<std::string> files(1);
97 QXmlStreamAttributes attributes = m_XmlReader->attributes();
98 if (!m_XmlReader->hasError())
99 current_index = attributes.value("Index").toString().toInt();
101 current_index += m_TreeItemCount;
103 while (!m_XmlReader->isEndElement() || value != "Image") {
104 m_XmlReader->readNext();
105 value = m_XmlReader->qualifiedName().toString().toStdString();
106 if (m_XmlReader->isStartElement()) {
107 if (value == "FileName") {
108 files[0] = m_XmlReader->readElementText().toStdString();
109 if (!m_XmlReader->hasError()) {
110 m_Window->LoadImages(files, vvImageReader::IMAGE);
113 else if (current_index >= 0) {
114 if (value == "Fusion")
115 value = ReadFusion(current_index);
116 else if (value == "Overlay")
117 value = ReadOverlay(current_index);
118 else if (value == "Vector")
119 value = ReadVector(current_index);
124 if (!m_XmlReader->hasError())
129 //------------------------------------------------------------------------------
132 //------------------------------------------------------------------------------
133 std::string vvReadState::ReadFusion(int index)
135 std::string file, value;
138 vvSlicerManager* slicerManager = m_Window->GetSlicerManagers()[index];
139 while (!m_XmlReader->isEndElement() || value != "Fusion") {
140 m_XmlReader->readNext();
141 value = m_XmlReader->qualifiedName().toString().toStdString();
142 if (m_XmlReader->isStartElement()) {
143 if (value == "FileName") {
144 file = m_XmlReader->readElementText().toStdString();
145 if (!m_XmlReader->hasError())
146 m_Window->AddFusionImage(index, file.c_str());
148 if (value == "FusionOpacity") {
149 vali = m_XmlReader->readElementText().toInt();
150 if (!m_XmlReader->hasError())
151 slicerManager->SetFusionOpacity(vali);
153 if (value == "FusionThresholdOpacity") {
154 vali = m_XmlReader->readElementText().toInt();
155 if (!m_XmlReader->hasError())
156 slicerManager->SetFusionThresholdOpacity(vali);
158 if (value == "FusionColorMap") {
159 vali = m_XmlReader->readElementText().toInt();
160 if (!m_XmlReader->hasError())
161 slicerManager->SetFusionColorMap(vali);
163 if (value == "FusionWindow") {
164 vald = m_XmlReader->readElementText().toDouble();
165 if (!m_XmlReader->hasError())
166 slicerManager->SetFusionWindow(vald);
168 if (value == "FusionLevel") {
169 vald = m_XmlReader->readElementText().toDouble();
170 if (!m_XmlReader->hasError())
171 slicerManager->SetFusionLevel(vald);
175 m_Window->ImageInfoChanged();
178 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 std::string vvReadState::ReadOverlay(int index)
184 std::string file, value;
187 vvSlicerManager* slicerManager = m_Window->GetSlicerManagers()[index];
188 while (!m_XmlReader->isEndElement() || value != "Overlay") {
189 m_XmlReader->readNext();
190 value = m_XmlReader->qualifiedName().toString().toStdString();
191 if (m_XmlReader->isStartElement()) {
192 if (value == "FileName") {
193 file = m_XmlReader->readElementText().toStdString();
194 if (!m_XmlReader->hasError())
195 m_Window->AddOverlayImage(index, file.c_str());
197 if (value == "OverlayColorWindow") {
198 vald = m_XmlReader->readElementText().toDouble();
199 if (!m_XmlReader->hasError())
200 slicerManager->SetOverlayColorWindow(vald);
202 if (value == "OverlayColorLevel") {
203 vald = m_XmlReader->readElementText().toDouble();
204 if (!m_XmlReader->hasError())
205 slicerManager->SetOverlayColorLevel(vald);
207 if (value == "LinkOverlayWindowLevel") {
208 vali = m_XmlReader->readElementText().toInt();
209 if (!m_XmlReader->hasError())
210 slicerManager->SetLinkOverlayWindowLevel(vali);
212 if (value == "OverlayColor") {
213 vali = m_XmlReader->readElementText().toInt();
214 if (!m_XmlReader->hasError())
215 slicerManager->SetOverlayColor(vali);
219 m_Window->ImageInfoChanged();
222 //------------------------------------------------------------------------------
225 //------------------------------------------------------------------------------
226 std::string vvReadState::ReadVector(int index)
228 std::string file, value;
229 while (!m_XmlReader->isEndElement() || value != "Vector") {
230 m_XmlReader->readNext();
231 value = m_XmlReader->qualifiedName().toString().toStdString();
232 if (m_XmlReader->isStartElement()) {
233 if (value == "FileName") {
234 file = m_XmlReader->readElementText().toStdString();
235 if (!m_XmlReader->hasError())
236 m_Window->AddField(file.c_str(), index);
242 //------------------------------------------------------------------------------
245 //------------------------------------------------------------------------------
246 void vvReadState::ReadGUI()
250 //------------------------------------------------------------------------------
253 //------------------------------------------------------------------------------
254 void vvReadState::ReadTools()
258 while ((!m_XmlReader->hasError()) && (!m_XmlReader->isEndElement() || value != "Tools")) {
259 m_XmlReader->readNext();
260 value = m_XmlReader->qualifiedName().toString().toStdString();
261 if (value != "Tools") {
262 if (m_XmlReader->isStartElement()) {
268 if (m_XmlReader->hasError())
269 std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
271 //------------------------------------------------------------------------------
274 //------------------------------------------------------------------------------
275 void vvReadState::ReadTool(const std::string & toolname)
277 // Find name into vvToolManager::GetInstance()->GetListOfTools();
278 vvToolCreatorBase * v = vvToolManager::GetInstance()->GetToolCreatorFromName(toolname.c_str());
280 std::cerr << "Error, I do not know the tool named '" << toolname << "' ; ignored." << std::endl;
281 std::string value="";
282 while (!m_XmlReader->isEndElement() || value != toolname) {
283 m_XmlReader->readNext();
284 value = m_XmlReader->qualifiedName().toString().toStdString();
285 if (m_XmlReader->hasError()) {
286 std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
294 // std::vector<vvToolBaseBase*> & tools = v->GetListOfTool();
295 v->m_XmlReader = m_XmlReader;
296 v->mReadStateFlag = true;
297 v->mImageIndex = m_TreeItemCount;
298 v->MenuSpecificToolSlot();
299 v->mReadStateFlag = false;
300 m_XmlReader = v->m_XmlReader; // Need because auto_ptr operator= release on the right.
302 //------------------------------------------------------------------------------