]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeHandlerImageAdder.h
9800a7a01bcca2475aa61da92f915cfa50fa7af1
[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
123     void remove( tree::Node* i_node);
124
125         /// Synchronizes the DB and disk by repeating the operations the user has done and returns a report
126         std::string Synchronize(bool repair, bool checkAttributes);
127         ///Recursively checks if the directory is synchronized and optionally the state of the attributes
128         void CheckSyncDirectory(const std::string &dirpath, 
129                                                         bool recursive, 
130                                                         bool repair,
131                                                         bool checkAttributes,
132                                                         std::vector<std::string> &i_ignorefiles,
133                                                         std::vector<std::string> & attsModified,
134                                                         std::vector<std::string> & newfiles);
135         ///Copies the files indicated in the vector and updates all databases
136         void CopyFiles(const std::vector<std::string>& filenames, const std::string directory  );
137         
138         ///Saves as the files indicated in the vector in a specific directory
139         void SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images);
140         ///Finds the node that matches the specified parameters
141         void FindNode(tree::Node* parent, int level, 
142                 const std::string& searchParam, 
143                 const std::string& searchVal, 
144                 tree::Node*& node);
145
146         ///Finds the nodes that partially match the searchVal
147         void FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node);
148  
149         ///Checks the attributes of the database against the ones in disk
150         void CheckAttributes(bool repair, std::string& file, std::vector<std::string>& attsModified);
151         ///Deletes the drive with the given name (use for all databases except maintenance and timestamp)
152         void DeleteDriveFromMainDB(const std::string& drive);
153         ///Deletes the drive with the given name (use for maintenance and timestamp databases)
154         void DeleteDriveFromOtherDB(const std::string& drive);
155         ///Edits the given field and sets the new parameters
156         void EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val);
157         ///Returns the demanded attributes for the given file
158         void GetAttributes(const std::vector<std::string>& params, 
159           const std::string& filename, 
160           std::vector<std::string>& results);
161
162         /// Test if an attribute is referenced for a given treeHandler
163         const std::string isAttributeExist(const std::string i_attr);
164   
165   
166  
167     //====================================================================
168
169   private:
170
171     /// Adds a single file to the TreeHandler 
172     /// **WITHOUT** testing wether it is handled or not 
173     /// hence you have to call IsHandledFile before using AddFile!
174     void AddFile( const std::string& filename );
175
176     /// Recursive method which does the main job for AddDirectory
177     void AddDirectoryRecursor( const std::string& directory, 
178                                bool recurse,
179                            const std::string &addKey);
180
181     TreeHandler* mTreeHandler;
182         Synchronizer* mSynchronizer;
183     ImageReader mReader;
184         //ImageWriter mWriter;
185         std::string mCurrentDB;
186     
187     Progress mProgress;
188     ProgressSignalType mProgressSignal;
189
190
191   };
192   // EO class TreeHandlerImageAdder
193   //=======================================================================
194
195
196  
197
198
199 } // EO namespace creaImageIO
200
201 #include <iostream>
202 inline std::ostream& operator << ( std::ostream& o, 
203                             const creaImageIO::TreeHandlerImageAdder::Progress& p)
204 {
205   o << p.GetNumberScannedFiles() << " files - "
206     << p.GetNumberScannedDirs() << " dirs - "
207     << p.GetNumberHandledFiles() << " handled -"
208     << p.GetNumberAddedFiles() << " added";
209   return o;
210 }
211
212 // EOF
213 #endif  
214