]> Creatis software - clitk.git/commitdiff
Add ReadTools and WriteTools
authorDavid Sarrut <david.sarrut@gmail.com>
Fri, 30 Mar 2012 13:17:48 +0000 (15:17 +0200)
committerDavid Sarrut <david.sarrut@gmail.com>
Fri, 30 Mar 2012 13:17:48 +0000 (15:17 +0200)
vv/vvReadState.cxx
vv/vvReadState.h
vv/vvSaveState.cxx
vv/vvSaveState.h

index dafcf3f4865f7f038d17f47820161a93d322bae4..7383ecdcd30a1e311a00b431a135c5ac5efdcbc4 100644 (file)
@@ -1,6 +1,26 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+  ===========================================================================**/
+
 #include "vvReadState.h"
 #include "vvMainWindow.h"
 #include "vvSlicerManager.h"
+#include "vvToolCreatorBase.h"
+#include "vvToolBaseBase.h"
 
 #include <qtreewidget.h>
 
 #include <cassert>
 #include <string>
 
+//------------------------------------------------------------------------------
 vvReadState::vvReadState() : m_XmlReader(new QXmlStreamReader), m_File(new QFile)
 {
   m_NumImages = 0;
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 vvReadState::~vvReadState()
 {
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 void vvReadState::Run(vvMainWindow* vvWindow, const std::string& file)
 {
   assert(vvWindow);
@@ -29,29 +56,38 @@ void vvReadState::Run(vvMainWindow* vvWindow, const std::string& file)
   m_XmlReader->setDevice(m_File.get());
   m_Window = vvWindow;
   QTreeWidget* tree = m_Window->GetTree();
+
+  // Get the number of images already loaded
   m_TreeItemCount = tree->topLevelItemCount();
 
+  // Read elements
   ReadGUI();
   ReadTree();
+  ReadTools();
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 void vvReadState::ReadTree()
 {
   std::string value;
   
-  while (!m_XmlReader->atEnd()) {
+  while (!m_XmlReader->isEndElement() || value != "Images") { 
     m_XmlReader->readNext();
     value = m_XmlReader->qualifiedName().toString().toStdString();
     if (m_XmlReader->isStartElement()) {
-      if (value == "Image") 
-        value = ReadImage();
+      if (value == "Image") value = ReadImage();
     } 
   }
   
   if (m_XmlReader->hasError())
     std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 std::string  vvReadState::ReadImage()
 {
   std::string value;
@@ -67,7 +103,6 @@ std::string  vvReadState::ReadImage()
   while (!m_XmlReader->isEndElement() || value != "Image") { 
     m_XmlReader->readNext();
     value = m_XmlReader->qualifiedName().toString().toStdString();
-    //std::cout << "Value = " << value << std::endl;
     if (m_XmlReader->isStartElement()) {
       if (value == "FileName") {
         files[0] = m_XmlReader->readElementText().toStdString();
@@ -91,7 +126,10 @@ std::string  vvReadState::ReadImage()
 
   return value;
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 std::string vvReadState::ReadFusion(int index)
 {
   std::string file, value;
@@ -137,7 +175,10 @@ std::string vvReadState::ReadFusion(int index)
   m_Window->ImageInfoChanged();
   return value;
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 std::string vvReadState::ReadOverlay(int index)
 {
   std::string file, value;
@@ -178,7 +219,10 @@ std::string vvReadState::ReadOverlay(int index)
   m_Window->ImageInfoChanged();
   return value;
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 std::string vvReadState::ReadVector(int index)
 {
   std::string file, value;
@@ -195,9 +239,64 @@ std::string vvReadState::ReadVector(int index)
   }
   return value;
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 void vvReadState::ReadGUI()
 {
 
 }
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvReadState::ReadTools()
+{
+  std::string value;
+  
+  while ((!m_XmlReader->hasError()) && (!m_XmlReader->isEndElement() || value != "Tools")) { 
+    m_XmlReader->readNext();
+    value = m_XmlReader->qualifiedName().toString().toStdString();
+    if (value != "Tools") {
+      if (m_XmlReader->isStartElement()) {
+        ReadTool(value);
+      } 
+    }
+  }
+  
+  if (m_XmlReader->hasError())
+    std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
+}
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvReadState::ReadTool(const std::string & toolname)
+{
+  // Find name into vvToolManager::GetInstance()->GetListOfTools();
+  vvToolCreatorBase * v = vvToolManager::GetInstance()->GetToolCreatorFromName(toolname.c_str());
+  if (v == NULL) {
+    std::cerr << "Error, I do not know the tool named '" << toolname << "' ; ignored." << std::endl;
+    std::string value="";
+    while (!m_XmlReader->isEndElement() || value != toolname) { 
+      m_XmlReader->readNext();
+      value = m_XmlReader->qualifiedName().toString().toStdString();
+      if (m_XmlReader->hasError()) {
+        std::cout << "Error " << m_XmlReader->error() << " XML " << std::endl;
+        return;
+      }
+    }
+    return;
+  }
 
+  // CreateTool
+  // std::vector<vvToolBaseBase*> & tools = v->GetListOfTool();
+  v->m_XmlReader = m_XmlReader;
+  v->mReadStateFlag = true;
+  v->mImageIndex = m_TreeItemCount;
+  v->MenuSpecificToolSlot();
+  v->mReadStateFlag = false;
+  m_XmlReader = v->m_XmlReader; // Need because auto_ptr operator= release on the right.
+}
+//------------------------------------------------------------------------------
index 8d6d5601e0cccd0727a858fd19d774effa83c6d5..04607f68790bfa4261145f0526abf7b55a774502 100644 (file)
@@ -21,10 +21,12 @@ protected:
   
   void ReadGUI();
   void ReadTree();
-  std::string  ReadImage();
-  std::string  ReadFusion(int index);
-  std::string  ReadOverlay(int index);
-  std::string  ReadVector(int index);
+  void ReadTools();
+  void ReadTool(const std::string & toolname);
+  std::string ReadImage();
+  std::string ReadFusion(int index);
+  std::string ReadOverlay(int index);
+  std::string ReadVector(int index);
 
   std::auto_ptr<QXmlStreamReader> m_XmlReader;
   std::auto_ptr<QFile> m_File;
index c1a7e496aa4dacc769832bef41f8e3139d5b050d..71e22ef0cff854e7305484255b83e23a0e0deba1 100644 (file)
@@ -1,5 +1,7 @@
 #include "vvSaveState.h"
 #include "vvMainWindow.h"
+#include "vvToolCreatorBase.h"
+#include "vvToolBaseBase.h"
 
 #include <QDir>
 #include <QFile>
 #include <cassert>
 #include <string>
 
+//------------------------------------------------------------------------------
 vvSaveState::vvSaveState() : m_XmlWriter(new QXmlStreamWriter), m_File(new QFile)
 {
 }
+//------------------------------------------------------------------------------
 
+//------------------------------------------------------------------------------
 vvSaveState::~vvSaveState()
 {
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 void vvSaveState::Run(vvMainWindow* vvWindow, const std::string& file)
 {
   assert(vvWindow);
@@ -36,16 +44,23 @@ void vvSaveState::Run(vvMainWindow* vvWindow, const std::string& file)
   SaveGlobals();
   SaveGUI();
   SaveTree();
+  SaveTools();
   m_XmlWriter->writeEndDocument();
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 void vvSaveState::SaveGlobals()
 {
   m_XmlWriter->writeStartElement("Globals");
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 void vvSaveState::SaveTree()
 {
   QTreeWidget* tree = m_Window->GetTree();
@@ -58,7 +73,10 @@ void vvSaveState::SaveTree()
   }
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 void vvSaveState::SaveImage(const QTreeWidgetItem* item, int index)
 {
   const vvSlicerManager * slicerManager = m_Window->GetSlicerManagers()[index];
@@ -85,9 +103,13 @@ void vvSaveState::SaveImage(const QTreeWidgetItem* item, int index)
       SaveVector(item_child);
   }
 
+  // End
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 void vvSaveState::SaveFusion(const QTreeWidgetItem* item, const vvSlicerManager* vvManager)
 {
   m_XmlWriter->writeStartElement("Fusion");
@@ -100,7 +122,10 @@ void vvSaveState::SaveFusion(const QTreeWidgetItem* item, const vvSlicerManager*
   m_XmlWriter->writeTextElement("FusionLevel", QString::number(vvManager->GetFusionLevel()));
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 void vvSaveState::SaveOverlay(const QTreeWidgetItem* item, const vvSlicerManager* vvManager)
 {
   m_XmlWriter->writeStartElement("Overlay");
@@ -112,7 +137,10 @@ void vvSaveState::SaveOverlay(const QTreeWidgetItem* item, const vvSlicerManager
   m_XmlWriter->writeTextElement("OverlayColor", QString::number(vvManager->GetOverlayColor()));
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
+
 
+//------------------------------------------------------------------------------
 void vvSaveState::SaveVector(const QTreeWidgetItem* item)
 {
   m_XmlWriter->writeStartElement("Vector");
@@ -120,10 +148,36 @@ void vvSaveState::SaveVector(const QTreeWidgetItem* item)
   m_XmlWriter->writeTextElement("FileName", QDir::current().absoluteFilePath(filename.c_str()));
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
 
+
+//------------------------------------------------------------------------------
 void vvSaveState::SaveGUI()
 {
   m_XmlWriter->writeStartElement("GUI");
   m_XmlWriter->writeEndElement();
 }
+//------------------------------------------------------------------------------
+
+
+//------------------------------------------------------------------------------
+void vvSaveState::SaveTools()
+{
 
+  m_XmlWriter->writeStartElement("Tools");
+  // Check if a tool is open and need to be store
+  std::vector<vvToolCreatorBase *> & v = vvToolManager::GetInstance()->GetListOfTools();
+  for(uint i=0; i<v.size(); i++) {
+    std::vector<vvToolBaseBase*> & t = v[i]->GetListOfTool();
+    for(uint j=0; j<t.size(); j++) {
+      std::string name = v[i]->mToolName.toStdString();
+      m_XmlWriter->writeStartElement(name.c_str());
+      m_XmlWriter->writeTextElement("ToolType_Index", QString::number(i));
+      m_XmlWriter->writeTextElement("Tool_Index", QString::number(j));
+      t[j]->SaveState(m_XmlWriter);
+      m_XmlWriter->writeEndElement();
+    }
+  }
+  m_XmlWriter->writeEndElement();
+}
+//------------------------------------------------------------------------------
index 299158029fe01ac783485b65f713c96e1e05130f..29a0e405803fc64fbec339bd6c69dc2855e21132 100644 (file)
@@ -23,6 +23,7 @@ protected:
   void SaveGlobals();
   void SaveGUI();
   void SaveTree();
+  void SaveTools();
   void SaveImage(const QTreeWidgetItem* item, int index);
   void SaveFusion(const QTreeWidgetItem* item, const vvSlicerManager* slicerManager);
   void SaveOverlay(const QTreeWidgetItem* item, const vvSlicerManager* slicerManager);