]> Creatis software - creaImageIO.git/blob - src/creaImageIOWxDumpPanel.cpp
05b2da963c6649d8aa6bfb3ba59d922c3b89722e
[creaImageIO.git] / src / creaImageIOWxDumpPanel.cpp
1 #include <creaImageIOWxDumpPanel.h>
2 #include <creaImageIOSystem.h>
3 #include <creaImageIOGimmick.h>
4 #if defined(USE_GDCM)
5 #include <gdcmGlobal.h>
6 #include <gdcmDictSet.h>
7 #include "gdcmFile.h"
8 #include "gdcmDocument.h"
9 #include "gdcmFileHelper.h"
10 #endif
11
12 #if defined(USE_GDCM2)
13 #include "gdcmGlobal.h"
14 #include "gdcmFile.h"
15 #include "gdcmDictPrinter.h"
16 #include "gdcmPrinter.h"
17 #include "gdcmReader.h"
18 #endif
19 #include "icons/save.xpm"
20
21 namespace creaImageIO
22 {
23         // CTor
24    WxDumpPanel::WxDumpPanel(wxWindow *parent,  std::string i_filename)
25     : wxDialog(parent, -1,_T("DICOM TAGS"), wxDefaultPosition, wxSize(550,580)), filename(i_filename)
26    {
27         int size = 16;
28         mIcon = new wxImageList(size,size,true);
29         mIcon->Add(wxBitmap(wxBitmap(wxIcon(save_xpm)).ConvertToImage().Rescale(size, size)));
30         wxToolBar *mToolBar = new wxToolBar(this,-1,wxDefaultPosition,wxDefaultSize);
31         mToolBar->AddTool( DUMP_SAVE_ID,_T("Save"), mIcon->GetBitmap(0), _T("Save Dicom Tags in text file"));
32         mToolBar->Realize();
33         DumpText = new wxTextCtrl( this, wxID_ANY,_T(""), wxPoint(5,30), wxSize(520,510), wxTE_READONLY| wxMac | wxTE_MULTILINE | wxTE_RICH );
34         Layout(); 
35         Print();
36         }
37
38         // DTor
39         WxDumpPanel::~WxDumpPanel(){}
40
41         ///////////////////////////////////////////////////
42         /// Display in a control Text all dicom tags
43         ///////////////////////////////////////////////////
44         void WxDumpPanel::Print()
45         {
46            std::stringstream os;
47            std::string result = "";
48            if ( !filename.empty()) // ====== Deal with a single file ======
49            {
50
51 #if defined(USE_GDCM)
52                    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
53                    f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
54                    f->SetFileName( filename );
55                    f->SetMaxSizeLoadEntry(0xffff);
56                    f->Load();
57                    GDCM_NAME_SPACE::FileHelper *fh = GDCM_NAME_SPACE::FileHelper::New(f);
58                       f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ |GDCM_NAME_SPACE::LD_NOSHADOW); 
59                    fh->SetPrintLevel( 0 );
60                    fh->Print(os);
61                  
62                    std::string line;
63                    while(std::getline(os, line))
64                    {
65                           result +=clean(line.c_str());
66                           result += "\n";
67                    }
68                    
69
70 #endif
71 #if defined(USE_GDCM2)
72                   gdcm::Reader reader;
73                   reader.SetFileName( filename.c_str() );
74                   if (reader.Read())
75                   {
76                           gdcm::Printer printer;
77                           printer.SetFile ( reader.GetFile() );
78                           printer.SetColor( 0 );
79                           printer.Print( os );
80                           result = os.str();
81                   }
82 #endif
83                   DumpText->SetValue(crea::std2wx(result));                
84
85                 }
86         }
87
88
89         const std::string WxDumpPanel::clean(const std::string &i_line)
90         {
91                 
92                   if (i_line.substr(4,1) == "|")
93                            {
94                                    std::string tag;
95                                    std::string line;
96                                    std:string signification;
97                                    std::string value;
98                                    std::string resultat;
99
100                                    tag = "(" + i_line.substr(0,9) + ")";
101                                    line = i_line.substr(14,i_line.size()-10);
102                                    int pos1 = line.find_first_of("[");
103                                    int pos2 = line.find_first_of("]");
104                                    signification = line.substr(pos1+1, pos2-pos1-1);
105                                    line = line.substr(pos2+1);
106                                     pos1 = line.find_first_of("[");
107                                     pos2 = line.find_first_of("]");
108                                    value = line.substr(pos1+1, pos2-pos1-1);
109                                    resultat = tag + " " + signification + ": " +value;
110                                   return resultat;
111                            }
112                    else
113                    {
114                            return i_line;
115                    }
116         }
117
118 ///////////////////////////////////////////////////     
119 /// wxEvent to save Dicom Tags in a text file    //
120 ///////////////////////////////////////////////////
121         void WxDumpPanel::SaveInfos(wxCommandEvent& event)
122         {
123         wxFileDialog* FD = new wxFileDialog( 0,_T("Select file"), _T(""), _T(""),
124                                                crea::std2wx("*.txt"), wxOPEN, wxDefaultPosition);
125         if (FD->ShowModal()==wxID_OK)
126                 {
127                         wxBusyCursor busy;
128                         std::ofstream ofs(crea::wx2std(FD->GetPath()).c_str());
129                         ofs.clear();
130                         ofs << crea::wx2std(DumpText->GetValue());;
131                         ofs.close();
132                 }
133                 Close();
134         }
135
136 ////////////////////////////////////////////////////////////
137 ////////////////////////////////////////////////////////////
138 BEGIN_EVENT_TABLE(WxDumpPanel, wxDialog)
139     EVT_TOOL(DUMP_SAVE_ID, WxDumpPanel::SaveInfos)
140 END_EVENT_TABLE()
141 }
142