]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeHandlerImageAdder.h
move directory
[creaImageIO.git] / src / creaImageIOTreeHandlerImageAdder.h
1 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
2 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
3
4 #include <creaImageIOTreeHandler.h>
5 #include <creaImageIOSynchron.h>
6 #include <creaImageIOImageReader.h>
7 //#include <creaImageIOImageWriter.h>
8
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"
14 #endif
15
16 namespace creaImageIO
17 {
18         /**
19         * \ingroup Model
20         */
21
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)
24
25   class TreeHandlerImageAdder
26   {
27           
28   public:
29     //====================================================================
30     /// Ctor
31     TreeHandlerImageAdder(TreeHandler* tree);
32     /// Dtor
33     ~TreeHandlerImageAdder();
34     /// Sets the TreeHandler
35     void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
36         
37         /// Sets the synchronizer
38         void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
39         /// Sets the synchronizer
40         void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
41     //====================================================================
42
43     //====================================================================
44     /// Structure which holds progress information
45     /// To stop the image adder use SetStop()
46     class Progress
47     {
48     public:
49       Progress() { Reset(); }
50       ~Progress() {}
51       
52       void Reset() 
53       {
54         mStop = false;
55         mNumberScannedFiles = 0;
56         mNumberScannedDirs = 0;
57         mNumberHandledFiles = 0;
58         mNumberAddedFiles = 0;
59       }
60
61       int GetNumberScannedFiles() const { return mNumberScannedFiles; }
62       int GetNumberScannedDirs() const { return mNumberScannedDirs; }
63       int GetNumberHandledFiles() const { return mNumberHandledFiles; }
64       int GetNumberAddedFiles() const { return mNumberAddedFiles; }
65
66       void IncNumberScannedFiles() { mNumberScannedFiles++; }
67       void IncNumberScannedDirs() { mNumberScannedDirs++; }
68       void IncNumberHandledFiles() { mNumberHandledFiles++; }
69       void IncNumberAddedFiles() { mNumberAddedFiles++; }
70
71       void SetStop() { mStop = true; }
72       bool GetStop() const { return mStop; }
73
74     private:
75       bool mStop;
76       int mNumberScannedFiles;
77       int mNumberScannedDirs;
78       int mNumberHandledFiles;
79       int mNumberAddedFiles;
80     };
81     //=============================================
82
83     //=============================================
84     const Progress& GetProgress() const { return mProgress; }
85     //=============================================
86
87     //=============================================
88     typedef boost::signal<void (Progress&)>  ProgressSignalType;
89     typedef ProgressSignalType::slot_function_type ProgressCallbackType;
90     //=============================================
91
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    //==================================================================
102
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, 
110                        bool recurse);
111
112
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);
116 #endif
117
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, 
126                                                         bool recursive, 
127                                                         bool repair,
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  );
134         
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, 
141                 tree::Node*& node);
142
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);
145  
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);
158   
159   
160   
161  
162     //====================================================================
163
164   private:
165
166     /// Adds a single file to the TreeHandler 
167     /// **WITHOUT** testing wether it is handled or not 
168     /// hence you have to call IsHandledFile before using AddFile!
169     void AddFile( const std::string& filename );
170
171     /// Recursive method which does the main job for AddDirectory
172     void AddDirectoryRecursor( const std::string& directory, 
173                                bool recurse,
174                            const std::string &addKey);
175
176     TreeHandler* mTreeHandler;
177         Synchronizer* mSynchronizer;
178     ImageReader mReader;
179         //ImageWriter mWriter;
180         std::string mCurrentDB;
181     
182     Progress mProgress;
183     ProgressSignalType mProgressSignal;
184
185
186   };
187   // EO class TreeHandlerImageAdder
188   //=======================================================================
189
190
191  
192
193
194 } // EO namespace creaImageIO
195
196 #include <iostream>
197 inline std::ostream& operator << ( std::ostream& o, 
198                             const creaImageIO::TreeHandlerImageAdder::Progress& p)
199 {
200   o << p.GetNumberScannedFiles() << " files - "
201     << p.GetNumberScannedDirs() << " dirs - "
202     << p.GetNumberHandledFiles() << " handled -"
203     << p.GetNumberAddedFiles() << " added";
204   return o;
205 }
206
207 // EOF
208 #endif  
209