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