]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomNode.cpp
Initial revision
[creaImageIO.git] / src / creaImageIODicomNode.cpp
1 #include <creaMessageManager.h>
2 #include <creaImageIODicomNode.h>
3
4 #include <creaImageIODicomDatabase.h>
5 //#include <creaImageIOException.h>
6
7 #include <algorithm>
8
9 namespace creaImageIO
10 {
11   //=============================================================
12   const int DicomNode::Database = 0;
13   const int DicomNode::Patient = 1;
14   const int DicomNode::Study   = 2;
15   const int DicomNode::Series  = 3;
16   const int DicomNode::Image   = 4;
17   //=============================================================
18
19   const std::string DicomNodeTypeName[5] =
20     { "Database",
21       "Patient",
22       "Study",
23       "Series",
24       "Image"
25     };
26   const std::string DicomNodePluralTypeName[5] =
27     { "Databases",
28       "Patients",
29       "Studies",
30       "Series",
31       "Images"
32     };
33
34   //=============================================================
35   DicomNode::DicomNode(Type type, DicomDatabase* root, DicomNode* parent)
36     : mType(type),
37       mDicomDatabase(root),
38       mParent(parent),
39       mData(0),
40       mChildrenLoaded(false),
41       mNumberOfChildren(-1)
42   {
43     if (parent) 
44       {
45         parent->GetChildrenList().push_back(this);
46       }
47     if (root) 
48       {
49         DicomNodeTypeDescription::FieldDescriptionMapType::const_iterator i;
50         for (i  = GetTypeDescription().GetFieldDescriptionMap().begin();
51              i != GetTypeDescription().GetFieldDescriptionMap().end();
52              i++)
53           {
54             mFieldValueMap[i->first] = "----";
55           }
56       }
57   }
58   //=============================================================
59
60   //=============================================================
61   DicomNode::~DicomNode()
62   {
63     ChildrenListType::iterator i;
64     for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
65       {
66         delete *i;
67       }
68     if (mData) 
69       {
70         delete mData;
71         mData = 0;
72       }
73   }
74   //=============================================================
75
76   //=============================================================
77   const std::string& DicomNode::GetTypeName(const Type& t)
78   {
79     return DicomNodeTypeName[t];
80   }
81   //=============================================================
82   //=============================================================
83   const std::string& DicomNode::GetPluralTypeName(const Type& t)
84   {
85     return DicomNodePluralTypeName[t];
86   }
87   //=============================================================
88
89   //=============================================================
90   const std::string& DicomNode::GetTypeName() const
91   {
92     return DicomNodeTypeName[GetType()];
93   }
94   //=============================================================
95
96   //=============================================================
97   void DicomNode::RemoveChildrenFromList(DicomNode* node)
98   {
99     ChildrenListType::iterator i = find(GetChildrenList().begin(),
100                                         GetChildrenList().end(),
101                                         node);
102     if (i != GetChildrenList().end())
103       {
104         GetChildrenList().erase(i);
105       }
106   }
107   //=============================================================
108
109   //=============================================================
110   const DicomNodeTypeDescription& DicomNode::GetTypeDescription() const
111   {
112     return mDicomDatabase->GetDicomNodeTypeDescription(mType);
113   }
114   //=============================================================
115
116
117   //  Tree* DicomNode::GetTree() const { return mDicomDatabase->GetTree(); }
118   
119
120   //=============================================================
121   const Field::Value& DicomNode::GetFieldValue(const Field::Key& k) const
122   {
123     //    std::cout << "this = "<<(void*)this<<std::endl;
124     //    std::cout << "mFieldValueMap="<<(void*)(&mFieldValueMap)<<std::endl;
125     FieldValueMapType::const_iterator i = mFieldValueMap.find(k);
126     if (i==mFieldValueMap.end())
127       {
128         static Field::Value def("");
129         return def;
130         //      CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
131       }
132     return i->second;
133   }
134   //=============================================================
135
136   //=============================================================
137   const Field::Value& DicomNode::GetCleanFieldValue(const Field::Key& k) const
138   {
139     static std::string Clean("-");
140     FieldValueMapType::const_iterator i = mFieldValueMap.find(k);
141     if (i==mFieldValueMap.end())
142       {
143         creaError("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
144       }
145     if (i->second == "GDCM::Unfound")
146       {
147         return Clean;
148       }
149     return i->second;
150   }
151   //=============================================================
152
153
154   //=============================================================
155   void DicomNode::SetFieldValue(const Field::Key& k, const Field::Value& v)
156   {
157     FieldValueMapType::iterator i = mFieldValueMap.find(k);
158     if (i==mFieldValueMap.end())
159       {
160         std::cout << "DicomNode::SetFieldValue : no field with key '"<<k
161                   <<"'"<<std::endl;
162         creaError("DicomNode::SetFieldValue : no field with key '"<<k<<"'");
163       }
164     i->second = v;
165   }
166   //=============================================================
167
168   //=============================================================
169   void DicomNode::Print() const 
170   {
171     for (int i=0;i<GetType()+1;++i) std::cout << " ";
172     std::cout << "-> "<<GetLabel() << std::endl;
173     ChildrenListType::const_iterator i;
174     for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
175       {
176         (*i)->Print();
177       }
178  
179   }
180   //=============================================================
181
182   //=============================================================
183   std::string DicomNode::GetLabel() const 
184   {
185     std::string l;
186     switch (GetType())
187       {
188       case Database :
189         l += GetFieldValue("Name");
190         break;
191         //        + " (" + GetFieldValue("Location") + ")";
192      case Patient :
193         l += GetCleanFieldValue("A0010_0010")  // Name 
194           + " | " + GetCleanFieldValue("A0010_0040") + " | "  // Sex
195           + GetCleanFieldValue("A0010_0030"); // Birthday
196         break;
197      case Study :
198        l += GetCleanFieldValue("A0008_0020")  // Study Date
199          + " | " +  GetCleanFieldValue("A0008_1030"); // Study Description
200        break;
201       case Series :
202         l += GetCleanFieldValue("A0008_0060")  // Modality
203           + " | " +  GetCleanFieldValue("A0008_103E");  // Description
204         break;
205       case Image :
206         l += GetCleanFieldValue("A0004_1500"); // File Name
207         break;
208       }
209     return l;
210   }
211   //=============================================================
212
213   //=============================================================
214   int DicomNode::GetNumberOfChildren()
215   {
216     //    std::cout << "$$$ DicomNode::GetNumberOfChildren"<<std::endl;
217     if ( GetType() == DicomNode::Image ) return 0;
218     if ( GetChildrenList().size() > 0 ) return GetChildrenList().size();
219     if ( mNumberOfChildren == -1 ) 
220       {
221         mNumberOfChildren = 0;
222         if (GetDicomDatabase()!=0)
223           {
224             mNumberOfChildren = 
225               GetDicomDatabase()->DBQueryNumberOfChildren(this);
226           }
227       }
228     return mNumberOfChildren;
229   }
230   //=============================================================
231
232   /*
233   const Field::Description& DicomNode::GetFieldDescription(const Field::Key& k) 
234     const
235   {
236     
237     return GetTypeDescription().GetFieldDescription(k);
238     
239   }
240   */
241
242
243
244  //=====================================================================
245   void DicomNode::SortChildren(const LexicographicalDicomNodeComparator& c)
246   {
247     // Check 
248     /*
249     ChildrenListType::const_iterator i;
250     for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
251       {
252         std::cout << (*i)->GetLabel()<<std::endl;
253         if ((*i)==0) 
254           {
255             std::cout << "!!! SortChildren : child = "<<*i<<std::endl;
256             exit(0);
257           }
258       }
259     std::cout<<"=========================="<<std::endl;
260     */
261     std::sort(GetChildrenList().begin(),
262               GetChildrenList().end(),
263               c);
264   }
265  //=====================================================================
266
267
268
269
270
271 } // namespace creaImageIO