]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeHandlerImageAdder.h
#2500 creaImageIO Bug New Normal - Boost signals deprecated -> signals2 (cleanning...
[creaImageIO.git] / src / creaImageIOTreeHandlerImageAdder.h
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
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
9 #
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.
16 #
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
21 #  liability. 
22 #
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 # ------------------------------------------------------------------------
26 */
27
28 #ifndef __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
29 #define __creaImageIOTreeHandlerImageAdder_h_INCLUDED__
30
31 #include <creaImageIOTreeHandler.h>
32 #include <creaImageIOSynchron.h>
33 #include <creaImageIOImageReader.h>
34 //#include <creaImageIOImageWriter.h>
35
36 // Signal/slot mechanism for progress events
37 #include <boost/signals2/signal.hpp>
38 #include <boost/bind.hpp>
39 #if defined(USE_GDCM2)
40 #include "creaImageIODicomScanner.h"
41 #endif
42
43 namespace creaImageIO
44 {
45         /**
46         * \ingroup Model
47         */
48
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)
51
52   class TreeHandlerImageAdder
53   {
54           
55   public:
56     //====================================================================
57     /// Ctor
58     TreeHandlerImageAdder(TreeHandler* tree);
59     /// Dtor
60     ~TreeHandlerImageAdder();
61     /// Sets the TreeHandler
62     void SetTreeHandler(TreeHandler* tree) { mTreeHandler = tree;}
63         
64         /// Sets the synchronizer
65         void SetSynchronizer(Synchronizer* s){mSynchronizer=s;}
66         /// Sets the synchronizer
67         void SetCurrentDatabase(std::string cur){mCurrentDB=cur;}
68     //====================================================================
69
70     //====================================================================
71     /// Structure which holds progress information
72     /// To stop the image adder use SetStop()
73     class Progress
74     {
75     public:
76       Progress() { Reset(); }
77       ~Progress() {}
78       
79       void Reset() 
80       {
81         mStop = false;
82         mNumberScannedFiles = 0;
83         mNumberScannedDirs = 0;
84         mNumberHandledFiles = 0;
85         mNumberAddedFiles = 0;
86       }
87
88       int GetNumberScannedFiles() const { return mNumberScannedFiles; }
89       int GetNumberScannedDirs() const { return mNumberScannedDirs; }
90       int GetNumberHandledFiles() const { return mNumberHandledFiles; }
91       int GetNumberAddedFiles() const { return mNumberAddedFiles; }
92
93       void IncNumberScannedFiles() { mNumberScannedFiles++; }
94       void IncNumberScannedDirs() { mNumberScannedDirs++; }
95       void IncNumberHandledFiles() { mNumberHandledFiles++; }
96       void IncNumberAddedFiles() { mNumberAddedFiles++; }
97
98       void SetStop() { mStop = true; }
99       bool GetStop() const { return mStop; }
100
101     private:
102       bool mStop;
103       int mNumberScannedFiles;
104       int mNumberScannedDirs;
105       int mNumberHandledFiles;
106       int mNumberAddedFiles;
107     };
108     //=============================================
109
110     //=============================================
111     const Progress& GetProgress() const { return mProgress; }
112     //=============================================
113
114     //=============================================
115     typedef boost::signals2::signal<void (Progress&)>  ProgressSignalType;
116     typedef ProgressSignalType::slot_function_type ProgressCallbackType;
117     //=============================================
118
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    //==================================================================
129
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, 
137                        bool recurse);
138
139
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);
143 #endif
144
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);
149
150     void remove( tree::Node* i_node);
151
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, 
156                                                         bool recursive, 
157                                                         bool repair,
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  );
164         
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, 
171                 tree::Node*& node);
172
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);
175  
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);
188
189         /// Test if an attribute is referenced for a given treeHandler
190         const std::string isAttributeExist(const std::string i_attr);
191   
192   
193  
194     //====================================================================
195
196   private:
197
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 );
202
203     /// Recursive method which does the main job for AddDirectory
204     void AddDirectoryRecursor( const std::string& directory, 
205                                bool recurse,
206                            const std::string &addKey);
207
208     TreeHandler* mTreeHandler;
209         Synchronizer* mSynchronizer;
210     ImageReader mReader;
211         //ImageWriter mWriter;
212         std::string mCurrentDB;
213     
214     Progress mProgress;
215     ProgressSignalType mProgressSignal;
216
217
218   };
219   // EO class TreeHandlerImageAdder
220   //=======================================================================
221
222
223  
224
225
226 } // EO namespace creaImageIO
227
228 #include <iostream>
229 inline std::ostream& operator << ( std::ostream& o, 
230                             const creaImageIO::TreeHandlerImageAdder::Progress& p)
231 {
232   o << p.GetNumberScannedFiles() << " files - "
233     << p.GetNumberScannedDirs() << " dirs - "
234     << p.GetNumberHandledFiles() << " handled -"
235     << p.GetNumberAddedFiles() << " added";
236   return o;
237 }
238
239 // EOF
240 #endif  
241