2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
28 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
29 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
31 #include <creaImageIOTreeHandler.h>
32 #include <creaImageIOSynchron.h>
33 #include <creaImageIOImageReader.h>
34 //#include <creaImageIOImageWriter.h>
36 // Signal/slot mechanism for progress events
37 #include <boost/signal.hpp>
38 #include <boost/bind.hpp>
39 #if defined(USE_GDCM2)
40 #include "creaImageIODicomScanner.h"
49 //=======================================================================
50 /// Object which can add images files to a TreeHandler. Is able to parse (recursively) a part of a filesystem to look for known images and load their attributes in order to add the images to a Tree (submission via a TreeHandler::AddBranch)
52 class TreeHandlerImageAdder
56 //====================================================================
58 TreeHandlerImageAdder(TreeHandler* tree);
60 ~TreeHandlerImageAdder();
61 /// Sets the TreeHandler
62 void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
64 /// Sets the synchronizer
65 void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
66 /// Sets the synchronizer
67 void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
68 //====================================================================
70 //====================================================================
71 /// Structure which holds progress information
72 /// To stop the image adder use SetStop()
76 Progress() { Reset(); }
82 mNumberScannedFiles = 0;
83 mNumberScannedDirs = 0;
84 mNumberHandledFiles = 0;
85 mNumberAddedFiles = 0;
88 int GetNumberScannedFiles() const { return mNumberScannedFiles; }
89 int GetNumberScannedDirs() const { return mNumberScannedDirs; }
90 int GetNumberHandledFiles() const { return mNumberHandledFiles; }
91 int GetNumberAddedFiles() const { return mNumberAddedFiles; }
93 void IncNumberScannedFiles() { mNumberScannedFiles++; }
94 void IncNumberScannedDirs() { mNumberScannedDirs++; }
95 void IncNumberHandledFiles() { mNumberHandledFiles++; }
96 void IncNumberAddedFiles() { mNumberAddedFiles++; }
98 void SetStop() { mStop = true; }
99 bool GetStop() const { return mStop; }
103 int mNumberScannedFiles;
104 int mNumberScannedDirs;
105 int mNumberHandledFiles;
106 int mNumberAddedFiles;
108 //=============================================
110 //=============================================
111 const Progress& GetProgress() const { return mProgress; }
112 //=============================================
114 //=============================================
115 typedef boost::signal<void (Progress&)> ProgressSignalType;
116 typedef ProgressSignalType::slot_function_type ProgressCallbackType;
117 //=============================================
119 //==================================================================
120 /// Adds the function f to the list of functions to call
121 /// when the addition progresses.
122 /// f is of type ProgressCallbackType which is:
123 /// void (*ProgressCallbackType)(Progress&)
124 /// To pass a member function 'f' of an instance 'c' of a class 'C'
125 /// as callback you have to 'bind' it, i.e. call:
126 /// ConnectProgressObserver ( boost::bind( &C::f , c, _1 ) );
127 void ConnectProgressObserver(ProgressCallbackType callback);
128 //==================================================================
130 //====================================================================
131 /// Returns if the file can be read or not
132 bool IsHandledFile( const std::string& filename);
133 /// Adds a list of files to the TreeHandler
134 void AddFiles( const std::vector<std::string>& filename );
135 /// (Recursively) adds the files of a directory to the TreeHandler
136 void AddDirectory( const std::string& directory,
140 #if defined(USE_GDCM2)
141 void AddDirectoryRecursorScanner(const std::string &dirpath,
142 bool recursive,const std::string &addKey, DicomImageScanner i_sc, bool b_loaded);
145 /// Removes a file from the databases
146 void RemoveFile(tree::Node* node);
147 /// Removes files from the databases
148 void RemoveFiles(const std::vector<tree::Node*>& nodes);
150 void remove( tree::Node* i_node);
152 /// Synchronizes the DB and disk by repeating the operations the user has done and returns a report
153 std::string Synchronize(bool repair, bool checkAttributes);
154 ///Recursively checks if the directory is synchronized and optionally the state of the attributes
155 void CheckSyncDirectory(const std::string &dirpath,
158 bool checkAttributes,
159 std::vector<std::string> &i_ignorefiles,
160 std::vector<std::string> & attsModified,
161 std::vector<std::string> & newfiles);
162 ///Copies the files indicated in the vector and updates all databases
163 void CopyFiles(const std::vector<std::string>& filenames, const std::string directory );
165 ///Saves as the files indicated in the vector in a specific directory
166 void SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images);
167 ///Finds the node that matches the specified parameters
168 void FindNode(tree::Node* parent, int level,
169 const std::string& searchParam,
170 const std::string& searchVal,
173 ///Finds the nodes that partially match the searchVal
174 void FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node);
176 ///Checks the attributes of the database against the ones in disk
177 void CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified);
178 ///Deletes the drive with the given name (use for all databases except maintenance and timestamp)
179 void DeleteDriveFromMainDB(const std::string& drive);
180 ///Deletes the drive with the given name (use for maintenance and timestamp databases)
181 void DeleteDriveFromOtherDB(const std::string& drive);
182 ///Edits the given field and sets the new parameters
183 void EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val);
184 ///Returns the demanded attributes for the given file
185 void GetAttributes(const std::vector<std::string>& params,
186 const std::string& filename,
187 std::vector<std::string>& results);
189 /// Test if an attribute is referenced for a given treeHandler
190 const std::string isAttributeExist(const std::string i_attr);
194 //====================================================================
198 /// Adds a single file to the TreeHandler
199 /// **WITHOUT** testing wether it is handled or not
200 /// hence you have to call IsHandledFile before using AddFile!
201 void AddFile( const std::string& filename );
203 /// Recursive method which does the main job for AddDirectory
204 void AddDirectoryRecursor( const std::string& directory,
206 const std::string &addKey);
208 TreeHandler* mTreeHandler;
209 Synchronizer* mSynchronizer;
211 //ImageWriter mWriter;
212 std::string mCurrentDB;
215 ProgressSignalType mProgressSignal;
219 // EO class TreeHandlerImageAdder
220 //=======================================================================
226 } // EO namespace creaImageIO
229 inline std::ostream& operator << ( std::ostream& o,
230 const creaImageIO::TreeHandlerImageAdder::Progress& p)
232 o << p.GetNumberScannedFiles() << " files - "
233 << p.GetNumberScannedDirs() << " dirs - "
234 << p.GetNumberHandledFiles() << " handled -"
235 << p.GetNumberAddedFiles() << " added";