1 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
2 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
4 #include <creaImageIOTreeHandler.h>
5 #include <creaImageIOSynchron.h>
6 #include <creaImageIOImageReader.h>
7 //#include <creaImageIOImageWriter.h>
9 // Signal/slot mechanism for progress events
10 #include <boost/signal.hpp>
11 #include <boost/bind.hpp>
12 #if defined(USE_GDCM2)
13 #include "creaImageIODicomScanner.h"
22 //=======================================================================
23 /// 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)
25 class TreeHandlerImageAdder
29 //====================================================================
31 TreeHandlerImageAdder(TreeHandler* tree);
33 ~TreeHandlerImageAdder();
34 /// Sets the TreeHandler
35 void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
37 /// Sets the synchronizer
38 void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
39 /// Sets the synchronizer
40 void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
41 //====================================================================
43 //====================================================================
44 /// Structure which holds progress information
45 /// To stop the image adder use SetStop()
49 Progress() { Reset(); }
55 mNumberScannedFiles = 0;
56 mNumberScannedDirs = 0;
57 mNumberHandledFiles = 0;
58 mNumberAddedFiles = 0;
61 int GetNumberScannedFiles() const { return mNumberScannedFiles; }
62 int GetNumberScannedDirs() const { return mNumberScannedDirs; }
63 int GetNumberHandledFiles() const { return mNumberHandledFiles; }
64 int GetNumberAddedFiles() const { return mNumberAddedFiles; }
66 void IncNumberScannedFiles() { mNumberScannedFiles++; }
67 void IncNumberScannedDirs() { mNumberScannedDirs++; }
68 void IncNumberHandledFiles() { mNumberHandledFiles++; }
69 void IncNumberAddedFiles() { mNumberAddedFiles++; }
71 void SetStop() { mStop = true; }
72 bool GetStop() const { return mStop; }
76 int mNumberScannedFiles;
77 int mNumberScannedDirs;
78 int mNumberHandledFiles;
79 int mNumberAddedFiles;
81 //=============================================
83 //=============================================
84 const Progress& GetProgress() const { return mProgress; }
85 //=============================================
87 //=============================================
88 typedef boost::signal<void (Progress&)> ProgressSignalType;
89 typedef ProgressSignalType::slot_function_type ProgressCallbackType;
90 //=============================================
92 //==================================================================
93 /// Adds the function f to the list of functions to call
94 /// when the addition progresses.
95 /// f is of type ProgressCallbackType which is:
96 /// void (*ProgressCallbackType)(Progress&)
97 /// To pass a member function 'f' of an instance 'c' of a class 'C'
98 /// as callback you have to 'bind' it, i.e. call:
99 /// ConnectProgressObserver ( boost::bind( &C::f , c, _1 ) );
100 void ConnectProgressObserver(ProgressCallbackType callback);
101 //==================================================================
103 //====================================================================
104 /// Returns if the file can be read or not
105 bool IsHandledFile( const std::string& filename);
106 /// Adds a list of files to the TreeHandler
107 void AddFiles( const std::vector<std::string>& filename );
108 /// (Recursively) adds the files of a directory to the TreeHandler
109 void AddDirectory( const std::string& directory,
113 #if defined(USE_GDCM2)
114 void AddDirectoryRecursorScanner(const std::string &dirpath,
115 bool recursive,const std::string &addKey, DicomImageScanner i_sc, bool b_loaded);
118 /// Removes a file from the databases
119 void RemoveFile(tree::Node* node);
120 /// Removes files from the databases
121 void RemoveFiles(const std::vector<tree::Node*>& nodes);
122 /// Synchronizes the DB and disk by repeating the operations the user has done and returns a report
123 std::string Synchronize(bool repair, bool checkAttributes);
124 ///Recursively checks if the directory is synchronized and optionally the state of the attributes
125 void CheckSyncDirectory(const std::string &dirpath,
128 bool checkAttributes,
129 std::vector<std::string> &i_ignorefiles,
130 std::vector<std::string> & attsModified,
131 std::vector<std::string> & newfiles);
132 ///Copies the files indicated in the vector and updates all databases
133 void CopyFiles(const std::vector<std::string>& filenames, const std::string directory );
135 ///Saves as the files indicated in the vector in a specific directory
136 void SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images);
137 ///Finds the node that matches the specified parameters
138 void FindNode(tree::Node* parent, int level,
139 const std::string& searchParam,
140 const std::string& searchVal,
143 ///Finds the nodes that partially match the searchVal
144 void FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node);
146 ///Checks the attributes of the database against the ones in disk
147 void CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified);
148 ///Deletes the drive with the given name (use for all databases except maintenance and timestamp)
149 void DeleteDriveFromMainDB(const std::string& drive);
150 ///Deletes the drive with the given name (use for maintenance and timestamp databases)
151 void DeleteDriveFromOtherDB(const std::string& drive);
152 ///Edits the given field and sets the new parameters
153 void EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val);
154 ///Returns the demanded attributes for the given file
155 void GetAttributes(const std::vector<std::string>& params,
156 const std::string& filename,
157 std::vector<std::string>& results);
159 /// Test if an attribute is referenced for a given treeHandler
160 const std::string isAttributeExist(const std::string i_attr);
164 //====================================================================
168 /// Adds a single file to the TreeHandler
169 /// **WITHOUT** testing wether it is handled or not
170 /// hence you have to call IsHandledFile before using AddFile!
171 void AddFile( const std::string& filename );
173 /// Recursive method which does the main job for AddDirectory
174 void AddDirectoryRecursor( const std::string& directory,
176 const std::string &addKey);
178 TreeHandler* mTreeHandler;
179 Synchronizer* mSynchronizer;
181 //ImageWriter mWriter;
182 std::string mCurrentDB;
185 ProgressSignalType mProgressSignal;
189 // EO class TreeHandlerImageAdder
190 //=======================================================================
196 } // EO namespace creaImageIO
199 inline std::ostream& operator << ( std::ostream& o,
200 const creaImageIO::TreeHandlerImageAdder::Progress& p)
202 o << p.GetNumberScannedFiles() << " files - "
203 << p.GetNumberScannedDirs() << " dirs - "
204 << p.GetNumberHandledFiles() << " handled -"
205 << p.GetNumberAddedFiles() << " added";