]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxDescriptorPanel.cpp
d192534b96c80c305b25d27a3b5d7f78a29c72d3
[creaImageIO.git] / src2 / creaImageIOWxDescriptorPanel.cpp
1 #include "creaImageIOWxDescriptorPanel.h"
2 #include <creaImageIOSystem.h>
3 #include <gdcmGlobal.h>
4 #include <gdcmDictSet.h>
5 #include <boost/algorithm/string.hpp>
6
7 #include <boost/algorithm/string.hpp>
8
9 namespace creaImageIO
10 {
11   // CTor
12                    
13         WxDescriptorPanel::WxDescriptorPanel(wxWindow *parent, const std::string path)
14                 : wxDialog(parent, -1,_T("Descriptor Creation"), wxDefaultPosition, wxSize(550,550)) , m_path(path)
15 {
16
17   
18     GimmickDebugMessage(1,"WxDescriptorPanel::WxDescriptorPanel"
19                         <<std::endl);
20
21         lv = 0;
22         ownatt["FullFileName"]      = "Full_File_Name";
23         ownatt["FullFileDirectory"] = "Full_File_Directory";
24         
25
26         // START BUTTONS
27         wxButton *NewDescriptor = new wxButton(this, -1,_T("Create a new descriptor"), wxPoint(10,7) );
28         Connect( NewDescriptor->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnNew ); 
29
30         wxButton *LoadDescriptor = new wxButton(this, -1,_T("Load a descriptor"), wxPoint(150,7) );
31         Connect( LoadDescriptor->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnLoad ); 
32         
33         wxStaticLine *line1 = new wxStaticLine(this, -1, wxPoint(5,40), wxSize(540,2));
34
35         // LEVEL
36         wxStaticText * LevelText=new wxStaticText(this,-1,_T(" Level: "), wxPoint(5,50));
37         LevelCtrl=new wxTextCtrl(this, ID_GR_CTRL,_T("patient"), wxPoint(50,50), wxSize(50,25));
38         wxButton *addLevel = new wxButton(this, ID_LEVEL_ADD,_T("add a level"), wxPoint(150,50) );
39         Connect( addLevel->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnAddLevel ); 
40
41         wxStaticLine *line2 = new wxStaticLine(this, -1, wxPoint(5,75), wxSize(540,2));
42
43         // ATTRIBUTES
44
45         wxStaticText * GR=new wxStaticText(this,-1,_T(" DICOM Group: "), wxPoint(5,110));
46         GRCtrl=new wxTextCtrl(this, ID_GR_CTRL,_T("0x0010"), wxPoint(82,110), wxSize(50,25));
47         Connect( GRCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED , (wxObjectEventFunction) &WxDescriptorPanel::OnDicomAttribute ); 
48         
49         wxStaticText * EL=new wxStaticText(this,-1,_T(" DICOM Element: "), wxPoint(140,110));
50         ELCtrl=new wxTextCtrl(this, ID_EL_CTRL,_T("0x0010"), wxPoint(230,110), wxSize(50,25));
51         Connect( ELCtrl->GetId(), wxEVT_COMMAND_TEXT_UPDATED , (wxObjectEventFunction) &WxDescriptorPanel::OnDicomAttribute ); 
52
53         
54         wxString choices[3];
55         choices[0] = _T("Unknow Attribute");
56         std::map<std::string, std::string>::iterator it_att =ownatt.begin();
57         for(int i = 1; it_att != ownatt.end(); it_att++, i++)
58         {
59                 choices[i] = crea::std2wx(it_att->second);
60         }
61         
62
63         AttributeCombo  = new wxComboBox(this, ID_ATTRIBUTE_CTRL,_T(""),wxPoint(300,110), wxSize(120,25),3,choices, wxCB_READONLY);
64         AttributeCombo->SetSelection(0);
65         
66
67         wxButton *addAttribute = new wxButton(this, ID_ATTRIBUTE_ADD,_T("add an attribute"), wxPoint(440,110) );
68         Connect( addAttribute->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnAddAttribute ); 
69
70         wxStaticLine *line3 = new wxStaticLine(this, -1, wxPoint(5,140), wxSize(540,2));
71
72         // RESULT
73
74         ResultCtrl=new wxTextCtrl(this, ID_EL_CTRL,_T(""), wxPoint(5,150), wxSize(250,310), wxTE_READONLY| wxMac | wxTE_MULTILINE | wxTE_RICH );
75         wxButton *RemoveCtrl = new wxButton(this, ID_REMOVE_ADD,_T("Remove an entry"), wxPoint(280,200) );
76         Connect( RemoveCtrl->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnRemove ); 
77
78         wxStaticLine *line4 = new wxStaticLine(this, -1, wxPoint(5,470), wxSize(540,2));
79         // VALIDATION BUTTON
80         wxButton *Ok = new wxButton(this, -1,_T("OK"), wxPoint(10,480) );
81         Connect( Ok->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnOK ); 
82
83         wxButton *Apply = new wxButton(this, -1,_T("APPLY"), wxPoint(150,480) );
84         Connect( Apply->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxObjectEventFunction) &WxDescriptorPanel::OnApply ); 
85         
86         wxButton *Cancel = new wxButton(this, wxID_CANCEL,_T("CANCEL"), wxPoint(250,480) );
87 //      Connect( Cancel->GetId(), wxEVT_COMMAND_BUTTON_CLICKED , (wxCloseEvent) &wxWindow::Close ); 
88
89         Layout(); 
90         CreateDescriptor(0);
91   }
92
93   /// Destructor
94   WxDescriptorPanel::~WxDescriptorPanel()
95   {
96     GimmickDebugMessage(1,"WxCustomizeConfigPanel::~WxCustomizeConfigPanel"
97                         <<std::endl);
98   }
99
100     //////////////////////////////////////////////////////////
101   // Add an attribute  //
102   // @param event : Wxevent  //
103   // @return : -                                                  //
104   //////////////////////////////////////////////////
105   void WxDescriptorPanel::OnAddAttribute(wxCommandEvent& event)
106   {
107           std::string name_lv;
108           std::string name_att;
109           if (AttributeCombo->GetSelection() == 0)
110           {
111              name_att = "D" + crea::wx2std(GRCtrl->GetValue()) + "_" + crea::wx2std(ELCtrl->GetValue());
112           }
113           else
114           {     
115              wxString wd = AttributeCombo->GetValue();
116              std::string st = crea::wx2std(wd);
117              name_att = OwnAttribute(st);
118           }
119           onAddAttribute(crea::wx2std(AttributeCombo->GetValue()), name_att);
120   }
121   //////////////////////////////////////////////////////////
122   // add an attribute  //
123   // @param att :  attribute //
124   // @param name_att :  's name //
125   // @param level : level to add the attribute  //
126   // @return : -                                                  //
127   //////////////////////////////////////////////////
128   void WxDescriptorPanel::onAddAttribute( const std::string &att, const std::string &name_att,std::string level )
129   {
130           if(lv == 0)
131           {
132                   wxMessageBox(_T("Need a level first!"),crea::std2wx("WARNING"),wxOK,this);
133           }
134           else
135           {
136                 if( !att.empty() )
137                 {
138                 // Find Name of level
139                         if(level.empty())
140                         {
141                                 level = findLevel();
142                         }
143
144                         if (!addAtribute(level, name_att))
145                         {
146                                 wxMessageBox(_T("Attribute already used in this level"),crea::std2wx("WARNING"),wxOK,this);
147                         }
148                         else
149                         {
150                                 ResultCtrl->SetInsertionPoint(InsertPt);
151                 for (int i = 1; i<=lv;i++)
152                                 { 
153                                    ResultCtrl->WriteText(_T("   "));
154                                 }
155                                 ResultCtrl->WriteText(_T("| - "));
156                                 ResultCtrl->WriteText(crea::std2wx(att));
157                                 wxTextAttr ResultAttr(ResultCtrl->GetDefaultStyle());
158                                 ResultAttr.SetTextColour(*wxWHITE);
159                                 ResultCtrl->SetDefaultStyle(ResultAttr);
160                                 std::string text = " ";
161                                 ResultCtrl->WriteText(crea::std2wx(" " + name_att));
162                                 ResultAttr.SetTextColour(*wxBLACK);
163                                 ResultCtrl->SetDefaultStyle(ResultAttr);
164                                 ResultCtrl->WriteText(_T("\n"));
165                         }
166                         InsertPt = ResultCtrl->GetInsertionPoint();
167                 }
168           }
169   }
170   
171     //////////////////////////////////////////////////////////
172   // add a level //
173   // @param event : Wxevent  //
174   // @return : -                                                  //
175   //////////////////////////////////////////////////
176   void WxDescriptorPanel::OnAddLevel(wxCommandEvent& event)
177   {
178           if( !LevelCtrl->GetValue().IsEmpty() )
179           {
180                   onAddLevel(crea::wx2std(LevelCtrl->GetValue()));
181           }
182   }
183
184     //////////////////////////////////////////////////////////
185   // add a level  //
186   // @param level : level's name   //
187   // @return : -                                                  //
188   //////////////////////////////////////////////////
189   void WxDescriptorPanel::onAddLevel(const std::string &level)
190   {
191                   if(addLevel(level))
192                   {
193                           wxMessageBox(_T("Level already used"),crea::std2wx(("WARNING")),wxOK,this);
194                           return;
195                   }
196                   
197                    lv++;
198                    ResultCtrl->SetInsertionPoint(InsertPt);
199                    for (int i = 1; i<lv;i++)
200                    { 
201                            ResultCtrl->WriteText(_T("   "));
202                    }
203                    if(lv>1)
204                    {    ResultCtrl->WriteText(_T("| \n"));
205                                 for (int i = 1; i<lv;i++)
206                                 { 
207                                         ResultCtrl->WriteText(_T("   "));
208                                 }
209                                 ResultCtrl->WriteText(_T("|_"));
210                    }
211                    
212                         wxTextAttr ResultAttr(ResultCtrl->GetDefaultStyle());
213                         ResultAttr.SetTextColour(*wxRED);
214                         ResultCtrl->SetDefaultStyle(ResultAttr);
215                         ResultCtrl->WriteText(crea::std2wx(level));
216                         ResultAttr.SetTextColour(*wxBLACK);
217                         ResultCtrl->SetDefaultStyle(ResultAttr);
218                         ResultCtrl->WriteText(_T("\n"));
219                         InsertPt = ResultCtrl->GetInsertionPoint();
220           
221   }
222
223     //////////////////////////////////////////////////////////
224   // Find a DICOM attribute from group and element values //
225   // @param event : Wxevent  //
226   // @return : -                                                  //
227   //////////////////////////////////////////////////
228   void WxDescriptorPanel::OnDicomAttribute(wxCommandEvent& event)
229   {
230           int i = 0;
231           if(!GRCtrl->GetValue().IsEmpty() && !ELCtrl->GetValue().IsEmpty() 
232                   && GRCtrl->GetValue().Len() == 6 && ELCtrl->GetValue().Len() == 6 && AttributeCombo->GetSelection() == 0)
233           {
234
235                   std::string gr = crea::wx2std(GRCtrl->GetValue());
236                   std::string el = crea::wx2std(ELCtrl->GetValue());
237                   std::stringstream val;
238         
239                   unsigned short group;
240                   unsigned short element;
241                   val <<   std::dec << gr ;
242                   val >> std::hex >> group;
243                   val.clear();
244                   val <<   std::dec << el ;
245                   val >> std::hex >> element;
246                         
247                  // Retrieve the name from gdcm dict
248                   GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(group, element);
249                  // AttributeCombo->Clear();
250                   if(entry)
251                   {
252                           AttributeCombo->Delete(0);
253                           AttributeCombo->Insert(crea::std2wx(entry->GetName()), 0);
254                   }
255                   else
256                   {
257                           AttributeCombo->Delete(0);
258                           AttributeCombo->Insert(_T("Unknown Attribute"),0);
259                   }
260                           AttributeCombo->SetSelection(0);
261                 
262           }
263     
264   }
265
266
267    //////////////////////////////////////////////////////////
268   // determine values for own attributes //
269   // @param name : attribute's name  //
270   // @param key : indicates key map or not  //
271   // @return : -                                                  //
272   //////////////////////////////////////////////////
273   std::string WxDescriptorPanel::OwnAttribute(const std::string name)
274   {
275           std::string result;
276
277           std::map<std::string, std::string>::iterator it_att = ownatt.begin();
278           for(; it_att != ownatt.end(); it_att++)
279           {
280                   if(it_att->second == name)
281                   {
282                           result = it_att->first.c_str();
283                           break;
284                   }
285           }
286           return result;
287   }
288         
289   //////////////////////////////////////////////////////////
290   // Find a level in function of position in Return Ctrl  //
291   // @param - :   //
292   // @return : -                                                  //
293   //////////////////////////////////////////////////
294   std::string WxDescriptorPanel::findLevel()
295   {
296           long column;
297           long line;
298           
299           ResultCtrl->PositionToXY( ResultCtrl->GetInsertionPoint(),&column, &line);
300           std::string tx(crea::wx2std(ResultCtrl->GetRange(0, ResultCtrl->XYToPosition(0,line+1))).c_str());
301           std::string::size_type level_pos_start = tx.rfind("|_");
302           if(level_pos_start == -1)
303           {
304                   level_pos_start = 0;
305           }
306           else
307           {
308                   level_pos_start += 2;
309           }
310
311           std::string::size_type level_pos_end = tx.find_first_of("\n",level_pos_start);
312           return  tx.substr(level_pos_start,level_pos_end - level_pos_start);
313   }
314
315   //////////////////////////////////////////////////////
316   // Remove an item                               //
317   // @param event : Wxevent  //
318   // @return : -                                                  //
319   //////////////////////////////////////////////////
320   void WxDescriptorPanel::OnRemove(wxCommandEvent& event)
321   {
322           long line;
323           long column;
324           long pos_start;
325           long pos_end;
326
327           pos_start = ResultCtrl->GetInsertionPoint();
328           ResultCtrl->PositionToXY( pos_start,&column, &line);
329           if (line == 0) 
330           {
331                   std::string name("root");
332                   RemoveLevel(name);
333                   ResultCtrl->Clear();
334                   lv = 0;
335           }
336           else
337           {
338                 wxString text = ResultCtrl->GetLineText(line);
339                 if ( text.Find(_T("|_")) == -1)
340                 {
341                   std::string level = findLevel();
342                   // find GR and EL values to remove
343                   std::string tx = crea::wx2std(text);
344                   std::string::size_type  EL_start_pos = tx.find_last_of(" ");
345                   RemoveAttribute(level, tx.substr(EL_start_pos+1,tx.size() - EL_start_pos));
346                   ResultCtrl->Remove( ResultCtrl->XYToPosition(0,line), ResultCtrl->XYToPosition(0,line+1));
347                 }
348                 else
349                 {       
350                          RemoveLevel(crea::wx2std(text.AfterFirst('_')));
351                          lv = text.Find(_T("|"))/3;
352                          pos_start= ResultCtrl->XYToPosition(0,line-1);
353                          ResultCtrl->SetInsertionPointEnd();
354                          pos_end = ResultCtrl->GetInsertionPoint();
355                          ResultCtrl->Remove(pos_start, pos_end);
356                 }
357           }
358
359   }
360    //////////////////////////////////////////////
361   // create a descriptor structure               //
362   // @param name : level's name to add           //
363   // @return : boolean result                    //
364   //////////////////////////////////////////////////
365    void WxDescriptorPanel::CreateDescriptor(int type)
366    {
367            if(type == 0) // First initialization
368            {
369                    outDscp.clear();
370                    outDscp += "<level>";
371                    outDscp += "\n";
372                    outDscp += "root";
373                    outDscp += "\n";
374                    outDscp += "O Name Name 4";
375                    outDscp += "\n";
376            }
377            if(type == 1)
378            {
379                    if(lv > 1)
380                    {
381                         outDscp += "O NumberOfChildren ";
382                         outDscp += crea::wx2std(LevelCtrl->GetValue());
383                         outDscp += "s";
384                         outDscp += "\n";
385                    }
386                    outDscp += "<level>";
387                    outDscp += "\n";
388                    outDscp += crea::wx2std(LevelCtrl->GetValue());
389                    outDscp += "\n";
390                    
391            }
392            if(type == 2)
393            {
394                    outDscp += "D";
395                    outDscp += " ";
396                    outDscp += crea::wx2std(GRCtrl->GetValue());
397                    outDscp += " ";
398                    outDscp += crea::wx2std(ELCtrl->GetValue());
399                    outDscp += " ";
400                    outDscp += "3";
401                    outDscp += "\n";
402            }
403            
404
405    }
406
407
408   //////////////////////////////////////////////////////
409   // add a level                                  //
410   // @param name : level's name to add  //
411   // @return : boolean result                                             //
412   //////////////////////////////////////////////////
413    bool WxDescriptorPanel::addLevel(const std::string &name)
414    {
415            bool bfound = false;
416            std::map<std::string, std::vector <std::string> >::iterator it_tree = DscpTree.begin();
417            for (;it_tree != DscpTree.end(); it_tree++)
418            {
419                    if(it_tree->first == name)
420                    {
421                            bfound = true;
422                            break;
423                    }
424            }
425            if(!bfound)
426            {
427                     lvlist[lv] = name;
428                         std::vector <std::string> branch;
429                         DscpTree[name] = branch;
430            }
431                 return bfound;
432    }
433
434   //////////////////////////////////////////////////////
435   // remove a level                               //
436   // @param name : level's name to remove  //
437   // @return : boolean result                                             //
438   //////////////////////////////////////////////////
439    bool WxDescriptorPanel::RemoveLevel(const std::string &name)
440    {
441            bool bresult = false;
442            std::map<int, std::string>::iterator it_list= lvlist.begin();
443            for(; it_list != lvlist.end(); it_list++)
444            {
445                    if(it_list->second == name)
446                    {
447                            break;
448                    }
449            }
450            std::map<int, std::string>::iterator it_list2 = it_list;
451            for(;it_list != lvlist.end(); it_list++)
452            {
453                         std::map<std::string, std::vector <std::string> >::iterator it_tree = DscpTree.begin();
454                         for (;it_tree != DscpTree.end(); it_tree++)
455                         {       
456                                 if(it_tree->first == name)
457                                 {
458                                         DscpTree.erase(it_tree);
459                                         break;
460                                 }
461                         }
462            }
463            lvlist.erase(it_list2, lvlist.end());
464            return bresult;
465    }
466
467
468   //////////////////////////////////////////////////////
469   // add an attribute in a level                                  //
470   // @param level : level's name to add attribute  //
471   // @param name : attribute's name                                       //
472   // @return : boolean result                                             //
473   //////////////////////////////////////////////////
474    bool WxDescriptorPanel::addAtribute(const std::string &level, const std::string &name)
475    {
476            bool bresult = true;
477            std::map<std::string, std::vector <std::string> >::iterator it_tree = DscpTree.begin();
478            for (;it_tree != DscpTree.end(); it_tree++)
479            {
480                    if (it_tree->first.c_str() == level)
481                    {
482                            std::vector<std::string>::iterator it_branch = it_tree->second.begin();
483                            for(;it_branch != it_tree->second.end(); it_branch++)
484                            { 
485                                    if(it_branch->c_str() == name)
486                                    {
487                                            bresult = false;
488                                    }
489                            }
490                            if(bresult)
491                            {
492                                         it_tree->second.push_back(name);
493                                         break;
494                            }
495                    }
496            }
497            return bresult;
498    }
499
500   //////////////////////////////////////////////////////
501   // remove an attribute from a level                             //
502   // @param level : level's name to remove attribute  //
503   // @param name : attribute's name                                       //
504   // @return : boolean result                                             //
505   //////////////////////////////////////////////////
506    bool WxDescriptorPanel::RemoveAttribute(const std::string &level, const std::string &name)
507    {
508               bool bresult = false;
509            std::map<std::string, std::vector <std::string> >::iterator it_tree = DscpTree.begin();
510            for (;it_tree != DscpTree.end(); it_tree++)
511            {
512                    if(it_tree->first == level)
513                    {
514                            std::vector<std::string>::iterator it_branch = it_tree->second.begin();
515                            cout << it_tree->second.size();
516                            for(;it_branch != it_tree->second.end(); it_branch++)
517                            {
518                                    if(it_branch->c_str() == name)
519                                    {
520                                            bresult = true;
521                                            it_tree->second.erase(it_branch);
522                                            break;
523                                    }
524                            }
525                    }
526            }
527            return bresult;
528    }
529
530   //////////////////////////////////////////////////
531   // create a new descriptor                                    //
532   // @param event : WxEvent                                     //
533   // @return : -                                                //
534   //////////////////////////////////////////////////
535    void WxDescriptorPanel::OnNew(wxCommandEvent &Event)
536    {
537            LevelCtrl->SetValue(_T("patient"));
538            ResultCtrl->Clear();
539            DscpTree.clear();
540            lv = 0;
541    }
542
543   //////////////////////////////////////////////////
544   // Load a descriptor file                                             //
545   // @param event : WxEvent                                             //
546   // @return : -                                                        //
547   //////////////////////////////////////////////////
548    void WxDescriptorPanel::OnLoad(wxCommandEvent &Event)
549    {
550             long style = wxOPEN | wxFILE_MUST_EXIST;
551            LevelCtrl->SetValue(_T("patient"));
552            ResultCtrl->Clear();
553            DscpTree.clear();
554            lv = 0;
555                 
556            std::string wc("*.dscp");
557            wxFileDialog* FD = new wxFileDialog( 0, 
558                                          _T("Select file"),
559                                          crea::std2wx(m_path),
560                                          _T(""),
561                                          crea::std2wx(wc),
562                                          style,
563                                          wxDefaultPosition);
564         if (FD->ShowModal()==wxID_OK)
565         {
566                 loadDescriptor(crea::wx2std(FD->GetPath()).c_str());
567         }
568         
569    }
570
571   //////////////////////////////////////////////////
572   // Save a descriptor                                                    //
573   // @param event : WxEvent                                               //
574   // @return : -                                                         //
575   //////////////////////////////////////////////////
576    void WxDescriptorPanel::OnOK(wxCommandEvent &Event)
577    {
578           saveDescriptor();
579           wxWindow::Close();
580    }
581    
582   /////////////////////////////////////////////////////
583   // Save a descriptor  and apply it (create a new DB//
584   // @param event : WxEvent                                             //
585   // @return : -                                                        //
586   /////////////////////////////////////////////////////
587    void WxDescriptorPanel::OnApply(wxCommandEvent &Event)
588    {
589                 m_DscpFile = saveDescriptor();
590                 wxWindow::Close();
591                 SetReturnCode(ID_DSCP_APPLY);
592    }
593
594    const std::string WxDescriptorPanel::saveDescriptor()
595    {
596            std::string file = "";
597                 long style = wxSAVE;
598                 std::string wc("*.dscp");
599                 wxFileDialog* FD = new wxFileDialog( 0, 
600                                                 _T("Select file"),
601                                                 _T(""),
602                                                 _T(""),
603                                                 crea::std2wx(wc),
604                                                 style,
605                                                 wxDefaultPosition);
606
607
608                 if (FD->ShowModal()==wxID_OK)
609                 {
610                         createDescriptorFile();
611                         file = crea::wx2std(FD->GetPath()).c_str();
612                         std::ofstream ofs(file.c_str());
613                         ofs.clear();
614                         ofs << outDscp;
615                         ofs.close();
616                 }
617                 return file.c_str();
618    }
619  
620    ///////////////////////////////////////////////////////
621    // Cancel action                                                             //
622    // @param event :    WxEvent                                                 //
623    // @return : -                                                               //
624    ///////////////////////////////////////////////////////
625
626    void WxDescriptorPanel::OnCancel(wxCommandEvent& event)
627    {
628    }
629    
630    ///////////////////////////////////////////////////////
631    // create  a descriptor      file                                            //
632    // @param - :                                                                //
633    // @return : -                                                               //
634    ///////////////////////////////////////////////////////
635    void WxDescriptorPanel::createDescriptorFile()
636    {
637                 
638                    outDscp.clear();
639                    outDscp += "<level>";
640                    outDscp += "\n";
641                    outDscp += "Root";
642                    outDscp += "\n";
643                    outDscp += "O Name Name 4";
644                    outDscp += "\n";
645                    std::map<std::string, std::vector <std::string> >::iterator it_tree = DscpTree.begin();
646                    std::map<int, std::string >::iterator it_lv_nb = lvlist.begin();
647                    std::map<int, std::string >::iterator it_lv = lvlist.begin();
648                    it_lv_nb++;
649                    for (;it_lv != lvlist.end(); it_lv++)
650                {
651                            outDscp +="<level>";
652                        outDscp += "\n";
653                            outDscp += it_lv->second.c_str();
654                              outDscp += "\n";
655                             if(it_lv_nb != lvlist.end())
656                            {
657                                    outDscp += "O NumberOfChildren ";
658                                    outDscp += it_lv_nb->second.c_str();
659                                    outDscp += "s";
660                                    outDscp += "\n";
661                                    it_lv_nb++;
662                            }
663                            std::vector<std::string>::iterator it_branch = DscpTree[it_lv->second.c_str()].begin();      
664                       for(;it_branch != DscpTree[it_lv->second.c_str()].end(); it_branch++)
665                           {      
666                                   std::string att = it_branch->c_str();
667                                   if(att[0] == 'D' && att[7] == '_' && att.size() == 14) 
668                                   {
669                                       outDscp += "D ";
670                                           outDscp += att.substr(1,6) + " "; // GR
671                                           outDscp += att.substr(8,6) + " ";// EL
672                                           outDscp += "3";
673                                           outDscp += "\n";
674                                   }
675                                   else
676                                   {
677                                           outDscp += "O ";
678                                           outDscp += it_branch->c_str();
679                                           outDscp += " ";
680                                           outDscp += ownatt[att];
681                                           outDscp += " ";
682                                           outDscp += "2";
683                                           outDscp += "\n";
684                                   }
685                           }
686
687                    }
688    }
689
690    
691    ///////////////////////////////////////////////////////
692    // load a descriptor                                                                 //
693    // @param i_name : file name to load                             //
694    // @return : -                                                                               //
695    /////////////////////////////////////////////////////
696    void WxDescriptorPanel::loadDescriptor(const std::string i_name)
697    {
698            std::ifstream i_file(i_name.c_str());
699            std::stringstream buffer;
700                 buffer << i_file.rdbuf();
701                 std::string line;
702                 std::string level;
703
704                 bool bname;
705                 int ilevel = -1;
706
707                 
708                 while(std::getline(buffer, line))
709                 {
710                         if(line =="<level>")
711                         {       //increment levels.
712                                 ilevel++;
713                                 bname = true;
714                         }
715                         else if(bname)
716                         {
717                                 // For each level, a name to describe it
718                                 level = line;
719                                 if(ilevel>0)
720                                 {
721                                         onAddLevel(level);
722                                 }
723                                 bname = false;
724                         }
725                         else
726                         { 
727                                 // split line to find all tags
728                                 std::vector<std::string> descriptors;
729                                 std::string separator = " ";
730                                 std::string::size_type last_pos = line.find_first_not_of(separator);
731                                 //find first separator
732                                 std::string::size_type pos = line.find_first_of(separator, last_pos);
733                                 while(std::string::npos != pos || std::string::npos != last_pos)
734                                 {
735                                         descriptors.push_back(line.substr(last_pos, pos - last_pos));
736                                         last_pos = line.find_first_not_of(separator, pos);
737                                         pos = line.find_first_of(separator, last_pos);
738                                 }
739                                 
740                                 // By default, the last tag is at zero and not recorded but if take in count
741                                 unsigned int flag = 0;
742                                 if(descriptors.size() == 4)
743                                 {
744                                         std::stringstream val;
745                                         val << std::dec << descriptors[3];
746                                         val>> flag;
747                                 }
748
749                                 // if Dicom tag, use "group" and "element" descriptor
750                                 if(descriptors[0] == "D")
751                                 {       std::stringstream val, val2;
752                                         unsigned short group;
753                                         unsigned short element;
754                                         val <<   std::dec << descriptors[1] ;
755                                         val >> std::hex >> group;
756                                         val2 << std::dec <<  descriptors[2];
757                                         val2 >> std::hex >> element;
758                                         std::string compose =  "D";
759                                         compose +=  descriptors[1];
760                                         compose += "_";
761                                         compose +=  descriptors[2];
762                                         GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(group, element);
763                                         if(ilevel>0)
764                                         {
765                                                 onAddAttribute( entry->GetName(),compose, level);
766                                         }
767                                 }
768                                 else if(descriptors[0].find("#") != -1)
769                                 {
770                                         // commented line continue to next line
771                                 }
772                                 else // "O" means if user's own tag.
773                                 {       
774                                         boost::algorithm::replace_all(descriptors[2],"_"," ");
775                                         if(ilevel>0 && descriptors[1] != "NumberOfChildren" )
776                                         {       
777                                                 onAddAttribute( descriptors[2].c_str(),descriptors[1].c_str(), level);
778                                         }
779                                 }
780                         }
781                 }
782    }
783    
784 //======================================================================
785   
786 //====================================================================== 
787
788 } // EO namespace creaImageIO
789
790