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