]> Creatis software - creaImageIO.git/blob - src/creaImageIOGimmickView.h
thread bug correction
[creaImageIO.git] / src / creaImageIOGimmickView.h
1 #ifndef __creaImageIOGimmickView_h_INCLUDED__
2 #define __creaImageIOGimmickView_h_INCLUDED__
3
4 #include <creaImageIOGimmick.h>
5 #include <creaImageIOTreeView.h>
6 #include <creaImageIOSystem.h>
7 #include <creaImageIOImagePointerHolder.h>
8
9 //#include <map>
10 #include <vtkImageData.h>
11 #if defined (USE_WXWIDGETS)
12 #include <creaImageIOMultiThreadImageReader.h>
13 #endif
14
15 #if defined(USE_QT4)
16 #include <creaImageIOQMultiThreadImageReader.h>
17 #endif
18
19
20 // Signal/slot mechanism for progress events
21 #include <boost/signal.hpp>
22 #include <boost/bind.hpp>
23
24 #define GIMMICK_NO_IMAGE_SELECTION 0
25 #define GIMMICK_2D_IMAGE_SELECTION 2
26 #define GIMMICK_3D_IMAGE_SELECTION 3
27 #define GIMMICK_4D_IMAGE_SELECTION 4
28
29 #define NATIVE 0
30 #define _2D    2
31 #define _3D    3
32
33 namespace creaImageIO
34 {
35         typedef std::map<std::string, std::string> mapInfoDicom;
36
37         typedef struct 
38         { 
39                 vtkImageData *img;
40                 //std::map<std::string, std::string> infos;
41                 mapInfoDicom infos; 
42         } OutStrGimmick;
43
44
45         /**
46         * \ingroup View
47         */
48          
49         class ImageExtent;
50     //=====================================================================
51     
52     //=====================================================================
53     ///Abstract class that handles views, attributes and previews (GUI) for Gimmick.
54         class GimmickView: public MultiThreadImageReaderUser
55     {
56     public:
57       /// Ctor
58       GimmickView(boost::shared_ptr<Gimmick>, int number_of_threads = 0 );
59       /// Virtual destructor
60       virtual ~GimmickView();
61       /// Initializes the view : 
62       /// Creates the TreeViews for all the TreeHandler of the Controller
63       /// 
64       virtual void Initialize();
65
66       /// Type of map from View name to TreeView* 
67       /// (This map is equivalent for Views of the TreeHandlerMap of Gimmick)
68       typedef std::map<std::string, TreeView*> TreeViewMapType;
69       
70       /// Returns the TreeViewMap (ref)
71       TreeViewMapType& GetTreeViewMap() { return mTreeViewMap; }
72       /// Returns the TreeViewMap (const ref)
73       const TreeViewMapType& GetTreeViewMap() const
74       { return mTreeViewMap; }
75
76       /// Finalize 
77       virtual void Finalize();
78
79           ///Returns the maximal priority
80       int GetMaximalPriority(){return mReader.GetMaximalPriority();}
81
82       ///Adds the selected Images to the given vector
83       virtual void GetSelectedImagesInVector(std::vector<vtkImageData*>& s, int dim)
84         { GimmickError("INTERNAL ERROR : GetSelectedImagesInVector not implemented"); }
85
86       ///Adds the selected Images to the given vector and validates to see if they comply with the given parameter (4D)
87       virtual void GetSelectedImages(std::vector<vtkImageData*>& s, int dim) 
88           { GimmickError("INTERNAL ERROR : GetSelectedImages not implemented"); }
89
90       virtual void GetSelectedFiles(std::vector<std::string>& s)
91       { GimmickError("INTERNAL ERROR : GetSelectedFiles not implemented"); }
92
93       virtual void GetImages(int dim, std::vector<std::string> files, std::vector<vtkImageData*>& s) 
94       { GimmickError("INTERNAL ERROR : GetImages not implemented"); }
95
96       virtual void OnSelectionChange(const std::vector<tree::Node*>& s, bool isSelection, int selection, bool mProcess)
97       { GimmickError("INTERNAL ERROR : OnSelectionChange not implemented"); }
98
99            virtual void ClearSelection()
100       { GimmickError("INTERNAL ERROR : ClearSelection not implemented"); }
101
102           ///Adds a file to ignore
103           virtual void AddIgnoreFile(tree::Node* toRemove)
104           { GimmickError("INTERNAL ERROR : AddIgnoreFile not implemented"); }
105       
106           ///Copies selected files
107           virtual void CopyFiles(const std::vector<std::string>& filenames)
108           { GimmickError("INTERNAL ERROR : CopyFiles not implemented"); }
109
110           ///Edits the fields of a given node
111           virtual void CreateEditFieldsDialog(tree::Node* node, std::vector<std::string> names, std::vector<std::string> keys)
112           { GimmickError("INTERNAL ERROR : EditFields not implemented"); }
113       
114           /// Anonymize or de-anonymize data
115           virtual void Anonymize(std::vector<std::string> i_filenames, int type)
116           { GimmickError("INTERNAL ERROR : Anonymize not implemented"); }
117
118           /// Display all Dicom Tags
119           virtual void DumpTags(const std::string filename)
120           {GimmickError("INTERNAL ERROR : DumpTags not implemented"); }
121           
122             ///Edits the fields of a given node
123           virtual void ExportToStorage(const std::vector<std::string> keys)
124           { GimmickError("INTERNAL ERROR : ExportToStorage not implemented"); }
125       
126            ///Copies selected files
127           virtual void SaveAs(const std::vector<std::string>& filenames)
128           { GimmickError("INTERNAL ERROR : SaveAs not implemented"); }
129
130           /// No selected image
131           bool NoValidateSelected();
132
133       ///Validates the dimension compliance of the images with the maximum and minimum given, and between their sizes
134       bool ValidateSelected (tree::Node* sel, int min_dim, int max_dim);
135
136       ///Reads the vector of images, builds it in the dimension required and returns them in the supplied vector.
137           void ReadImagesNotThreaded(std::vector<vtkImageData*>& s,std::vector<std::string> files, int dim);
138
139       ///Reads the vector of images, builds it in the dimension required and returns them in the supplied vector.
140           void ReadImagesNotThreadedInVector(std::vector<vtkImageData*>& s,std::vector<std::string> files, int dim);
141
142 ///Requests the reading of an image with priority and index in the 
143       /// current selection (-1 if not in selection)
144       //void RequestReading(tree::Node* n, int prio, int selection_index , ImagePointerHolder *p);
145           void RequestReading(tree::Node* n, int prio, int selection_index , boost::shared_ptr<ImagePointerHolder> p);
146
147       ///Obtains the message of the state
148       std::string GetMessage(){return mMess;}
149
150      ///Obtains the message of the state
151       void SetMessage(std::string mess){mMess=mess;}
152
153           ///Resets the data of the extent and begins a new selection
154           void ResetExtent();
155
156       /// Create the tree views 
157       void CreateTreeViews();
158
159           /// Create a tree view with a given name
160           void CreateSingleTreeView(std::string &i_name);
161
162       /// Create the tree view for TreeHandler provided
163       virtual void CreateTreeView( TreeHandler* ) 
164       { GimmickError("INTERNAL ERROR : CreateTreeView not implemented"); }
165
166       /// Updates the TreeView of given name from level l to bottom
167       /// (calls the virtual method TreeView::UpdateLevel(l))
168       virtual void UpdateTreeViewLevel(const std::string&, int l);
169
170           // Multi-thread image reader callback
171           void OnMultiThreadImageReaderEvent(const std::string& filename,
172                                        MultiThreadImageReaderUser::EventType t,
173                                        vtkImageData* image);
174
175       vtkImageData* GetDefaultImage() { return mReader.GetImage(""); }
176
177       //=============================================
178       typedef boost::signal<void (bool)>  ValidationSignalType;
179       typedef ValidationSignalType::slot_function_type ValidationCallbackType;
180       //=============================================
181
182      //==================================================================
183       /// Adds the function f to the list of functions to call 
184       /// when the addition progresses.
185       /// f is of type ProgressCallbackType which is:
186       /// void (*ProgressCallbackType)(Progress&)
187       /// To pass a member function 'f' of an instance 'c' of a class 'C' 
188       /// as callback you have to 'bind' it, i.e. call:
189       /// ConnectValidationObserver ( boost::bind( &C::f , c, _1 ) );
190       void ConnectValidationObserver(ValidationCallbackType callback);
191      //==================================================================
192
193         void modifyValidationSignal(bool ivalid);
194
195         
196         void readImages(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
197                          OutputAttr i_attr, int i_dim, double i_zspc);
198                         bool isSingle(const std::string i_file);
199
200         void stopReader();
201     private:
202
203
204         typedef void (*readXD)(std::vector<OutStrGimmick>&,  std::vector<std::string>, OutputAttr, double);
205
206         void getAttributes(const std::string i_file, std::map<std::string, std::string> &o_infos, OutputAttr i_attr);
207
208         virtual void readImages2(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
209                          OutputAttr i_attr, double i_zspc);
210
211         virtual void readImages3(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
212                          OutputAttr i_attr, double i_zspc);
213
214         virtual void readImages4(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
215                          OutputAttr i_attr);
216
217         virtual void readImages1(std::vector<OutStrGimmick>& o_output, std::vector<std::string> im,
218                          OutputAttr i_attr);
219
220
221       /// Controller which manages the interaction with the model
222       boost::shared_ptr< Gimmick> mGimmick;
223
224       /// The views 
225       TreeViewMapType mTreeViewMap;
226
227       /// The message that results from the validation
228       std::string mMess;
229
230       /// Multi-thread image reader
231       MultiThreadImageReader mReader;
232
233       /// Internal type of image reading event
234       /// If the image pointer is non null then the image is available (loaded)
235       /// else it has been unloaded
236       struct ImageEventType
237       {
238         ImageEventType( tree::Node* no = 0,   
239                         int sel_index = -1)
240 //                      ImagePointerHolder* ph= 0)
241           : node(no), index(sel_index){}//,  pointerHolder(ph){}
242         ImageEventType(vtkImageData* im )
243           : image(im) {}
244         tree::Node* node;
245         vtkImageData* image;
246                 int index;
247                 boost::shared_ptr<ImagePointerHolder> pointerHolder;
248       };
249       typedef std::map<std::string,ImageEventType> ImageEventTypeMap;
250       /// Map of images' names to ImageEventType
251       /// Used to associated a filename to a the data of a request
252       ImageEventTypeMap mImageEventMap;
253
254       // queue of image event 
255       typedef std::deque<ImageEventType> ImageEventQueueType;
256
257       //ImageEventQueueType mImageEventQueue;
258
259           ///The current image extent
260           boost::shared_ptr<ImageExtent> mImageExtent;
261
262           ///The validation signal
263           ValidationSignalType mValidationSignal;
264
265           ///Boolean that determines if the selection is valid
266           bool valid;
267
268           ///Boolean that determines if the reader has been started
269           bool mReaderStarted;
270
271         };
272   // EO class GimmickView
273     //=====================================================================
274
275  
276 } // EO namespace creaImageIO
277
278 // EOF
279 #endif