File: DicomTree.cpp

package info (click to toggle)
mriconvert 1%3A2.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,488 kB
  • sloc: cpp: 17,029; makefile: 11
file content (296 lines) | stat: -rw-r----- 7,562 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/// DicomTree.cpp
/**
*/

#if defined(__WXGTK__) || defined(__WXMOTIF__)
  #include <wx/wx.h>
#endif

#include <wx/wxprec.h>
#include <wx/treectrl.h>

#include "SeriesHandler.h"
#include "StringConvert.h"
#include "DicomTree.h"

using namespace jcs;

///
/**
*/
DicomTreeCtrl::DicomTreeCtrl(wxWindow* parent, wxWindowID id, long more_styles)
: jcsTreeCtrl(parent, id, wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS | 
       wxTR_LINES_AT_ROOT  | wxSUNKEN_BORDER | more_styles, "Dicom series")
{
}


///
/**
*/
DicomTreeCtrl::DicomTreeCtrl(wxWindow* parent, wxWindowID id) 
: jcsTreeCtrl(parent, id, wxTR_HIDE_ROOT | wxTR_HAS_BUTTONS | 
       wxTR_LINES_AT_ROOT  | wxSUNKEN_BORDER, "Dicom series")
{
}


///
/** Adds a series. Heirarchy is Subject->Study->Series->File.
    In many cases, DICOM is encoded with Unicode. File systems
    typically use UTF-8. The wxStuff also uses UTF-8, so 
    conversion from Unicode to UTF-8 is needed where the input 
    data is Unicode-encoded. This is the reason for the various
    invocations of From8BitData below.
    \param series A pointer to the SeriesHandler for the data
                  to be added.
*/
void
DicomTreeCtrl::AddSeries(SeriesHandler* series)
{
  // Processing for subject level of the heirarchy.
  std::string subject;
  if (!series->Find("PatientName", subject)) {
    subject = "no name";
  }

  //std::cout << subject << std::endl;
  
  wxTreeItemId subjectTreeId;
  if (!mTreeMap.count(subject)) {
  
    //std::string itemText("Subject name: ");
    wxString itemText(_("Subject name: "));
    itemText += wxString::From8BitData(subject.c_str()).wc_str();
    //itemText += wxString(wxString::From8BitData(subject.c_str()).wc_str(), wxConvLocal);
    //std::cout << itemText << std::endl;
    //itemText.append(subject);
    subjectTreeId = AppendItem(
      GetRootItem(),
      itemText,
      folder_icon, -1,
      new jcsTreeItemData(subject, dtSUBJECT)
    );

    SetItemImage(
      subjectTreeId, 
      open_folder_icon,
      wxTreeItemIcon_Expanded
    );

    mTreeMap[subject] = subjectTreeId;
  }
  else {
    subjectTreeId = mTreeMap[subject];
  }

  // Processing for the study level of the heirarchy.
  std::string study_uid;
  //if (!series->Find("StudyInstanceUid", study_uid)) {
  if (!series->Find("StudyInstanceUID", study_uid)) {
    study_uid = "no study id";
  }

  wxTreeItemId studyTreeId;
  if (!mTreeMap.count(study_uid)) {

    std::string number;
    //series->Find("StudyId", number);
    series->Find("StudyID", number);
    std::string desc;
    series->Find("StudyDescription", desc);
    std::string date;
    series->Find("StudyDate", date);

    wxString itemText(_("Study "));  // Use this form to compile with wxWidgets < 3.0.0
    itemText += wxString(number.c_str(), *wxConvCurrent);  // Use this form to compile with wxWidgets < 3.0.0
    itemText += wxString(_("     "));
    itemText += wxString(date.c_str(), *wxConvCurrent);
    itemText += wxString(_("     "));
    itemText += wxString(desc.c_str(), *wxConvCurrent);
    /**
    wxString itemText("Study ");
    itemText += wxString(number);
    itemText += wxString("     ");
    itemText += wxString(date);
    itemText += wxString("     ");
    itemText += wxString(desc);
    **/
    /**
    std::string itemText("Study ");
    itemText.append(number);
    itemText.append("     ");
    itemText.append(date);
    itemText.append("     ");
    itemText.append(desc);
    **/

    studyTreeId = AppendItem(
      subjectTreeId,
      itemText,
      folder_icon, -1, 
      new jcsTreeItemData(study_uid, dtSTUDY)
    );
    SetItemImage(
      studyTreeId, 
      open_folder_icon,
      wxTreeItemIcon_Expanded
    );

    mTreeMap[study_uid] = studyTreeId;
  }
  else {
    studyTreeId = mTreeMap[study_uid];
  }

  // Processing for the series level of the heirarchy.
  std::string series_uid = series->GetSeriesUid();

  wxTreeItemId seriesTreeId;

  if (!mTreeMap.count(series_uid)) {

    std::string number;
    if (!series->Find("SeriesNumber", number)) {
      number = "0";
    }
    number = jcs::itos(jcs::stoi(number));
    std::string protocol;
    series->Find("ProtocolName", protocol);
    std::string desc;
    series->Find("SeriesDescription", desc);
    std::string date;
    series->Find("SeriesDate", date);

    std::string itemText("Series ");
    itemText.append(number);
    itemText.append("     ");
    itemText.append(date);
    itemText.append("     ");
    itemText.append(protocol);
    if (protocol != desc) {
      itemText.append(" ");
      itemText.append(desc);
    }

    seriesTreeId = AppendItem(
      studyTreeId, 
      wxString(wxString::From8BitData(itemText.c_str()).wc_str(), wxConvLocal),
      folder_icon, -1, 
      new jcsTreeItemData(series_uid, dtSERIES)
    );
    SetItemImage(
      seriesTreeId, 
      open_folder_icon,
      wxTreeItemIcon_Expanded
    );

    mTreeMap[series_uid] = seriesTreeId;
  }
  else {
    seriesTreeId = mTreeMap[series_uid];
  }

  CollapseAndReset(seriesTreeId);
  
  // Processing for each file in a series.
  // File names and paths are typically encoded with UTF-8, so
  // no From8BitData processing is needed here.
  std::vector<std::string> names = series->GetFilenames();
  std::vector<std::string>::iterator it = names.begin();
  while (it != names.end()) {
    AppendItem(
      seriesTreeId,
      wxString(it->c_str(), wxConvLocal),
      file_icon, -1,
      new jcsTreeItemData(*it, dtFILE)
    );
    ++it;
  }
}


///
/**
*/
std::vector<std::string>
DicomTreeCtrl::GetChildSeries(wxTreeItemId branch)
{
  std::vector<std::string> v;

  jcsTreeItemData* item = (jcsTreeItemData*) GetItemData(branch);

  switch (item->GetItemType()) {

  case dtSERIES :

    v.push_back(item->GetUid());
    break;

  case dtSTUDY : 
  case dtSUBJECT : 
    
    if (ItemHasChildren(branch)) {
      int n_children = GetChildrenCount(branch, false);
      wxTreeItemIdValue cookie;
      wxTreeItemId child = GetFirstChild(branch, cookie);
      std::vector<std::string> v2 = GetChildSeries(child);
      v.insert(v.end(), v2.begin(), v2.end());

      for (int i = 1; i < n_children; ++i) {
        child = GetNextChild(branch, cookie);
        v2 = GetChildSeries(child);
        v.insert(v.end(), v2.begin(), v2.end());
      }
    }
    break;

  default : 
    break;
  }

  return v;
}


///
/**
*/
std::vector<std::string>
DicomTreeCtrl::GetFilenames()
{
  std::vector<std::string> filenames;

  jcsTreeMap::iterator it = mTreeMap.begin();
  jcsTreeMap::iterator end = mTreeMap.end();

  while (it != end) {
    wxTreeItemId item = (*it).second;
    if (GetItemType(item) == dtFILE) {
      filenames.push_back(static_cast<const char*>(GetItemText(item).mb_str(wxConvLocal)));
    }
    ++it;
  }
  return filenames;
}


///
/**
*/
std::vector<wxString> 
DicomTreeCtrl::GetFilesInSeries(const wxTreeItemId item)
{
  std::vector<wxString> files;
  wxTreeItemId local_item = item;
  if (GetItemType(local_item) == dtFILE) {
    wxTreeItemId parent = GetItemParent(local_item);
    wxTreeItemIdValue cookie;
    local_item = GetFirstChild(parent, cookie);
    while (local_item.IsOk()) {
      files.push_back(GetItemText(local_item));
      local_item = GetNextChild(parent, cookie);
    }
  }
  return files;
}