]> Creatis software - clitk.git/blob - vv/vvQPacsConnection.cxx
pacs only if clitk_use_system_gdcm is defined
[clitk.git] / vv / vvQPacsConnection.cxx
1 #include "vvQPacsConnection.h"
2 #include "gdcmCompositeNetworkFunctions.h"
3 #include <QtGui/qlistview.h>
4 #include <qfile.h>
5 #include <QDate>
6 #include <QIcon>
7 #include <QDateTime>
8 #include "vvPacsSettingsDialog.h"
9 #include "vvUtils.h"
10
11
12 vvQPacsConnection::vvQPacsConnection(QWidget *i_parent)
13         :QDialog(i_parent)
14 {
15         ui.setupUi(this);
16         setWindowTitle(QString::fromUtf8("PACS CONNECTIONHHHH"));
17         createTreeView();
18         ui.tabFilter->setTabText(0,QString(tr("Modality")));
19         ui.tabFilter->setTabText(1,QString(tr("Date")));
20
21         ui. tabNetwork->setTabText(0,QString(tr("Network")));
22         ui. tabNetwork->setTabText(1,QString(tr("Configuration")));
23         //ui.setChecked(true);
24         //ui.on_check_ModAll_clicked(true);
25         QIcon icon;
26         icon.addFile("basket_download.png",QSize(),QIcon::Normal,QIcon::Off);
27         ui.importButton->setIcon(icon);
28         icon.addFile("file_upload.png",QSize(),QIcon::Normal,QIcon::Off);
29         // exportButton->setIcon(icon);
30         icon.addFile("email_forward.png",QSize(),QIcon::Normal,QIcon::Off);
31         icon.addFile("edit.png",QSize(),QIcon::Normal,QIcon::Off);
32         ui.optionsButton->setIcon(icon);
33         icon.addFile("bullet_info.png",QSize(),QIcon::Normal,QIcon::Off);
34         ui.helpButton->setIcon(icon);
35
36         ui.networkCombo->addItems(getDicomServers());
37         connect(ui.networkCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(connectServer(int)));
38         connect(ui.scanButton,SIGNAL(clicked()),this,SLOT(on_scanButton_clicked()));
39         update();
40 }
41
42 void vvQPacsConnection::refreshNetworks()
43 {
44         ui.networkCombo->clear();
45         ui.networkCombo->addItems(getDicomServers());
46         //ui.networkCombo->show();
47 }
48
49 void vvQPacsConnection::on_clearButton_clicked()
50 {
51         Patientmodel->removeRows(0, Patientmodel->rowCount(),QModelIndex());
52         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
53         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
54         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
55 }
56
57 void vvQPacsConnection::on_scanButton_clicked()
58 {
59
60         manageStudiesFilter(true);
61         bool didItWork = gdcm::CompositeNetworkFunctions::CEcho(m_adress.c_str(), atoi(m_port.c_str()), "CREATIS", m_nickname.c_str() );
62         std::vector< std::pair<gdcm::Tag, std::string> > keys = getKeys();
63
64         gdcm::EQueryLevel theLevel = gdcm::eStudy;
65         gdcm::ERootType theRoot  = gdcm::eStudyRootType;//ePatientRootType;
66         
67         std::vector<gdcm::DataSet> theDataSet;
68         theLevel = gdcm::ePatient;
69         theRoot  = gdcm::ePatientRootType;//ePatientRootType;
70         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,getPatientKeys(""));
71         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet,    "CREATIS", m_nickname.c_str());
72         std::vector<gdcm::DataSet>::iterator it_ds = theDataSet.begin();
73
74         for(; it_ds != theDataSet.end(); it_ds++)
75         {
76                 QList<QStandardItem *> items;
77                 const gdcm::DataSet ds = (*it_ds);
78                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
79                 int ind = 0;
80                 for(; it_key != keys.end(); it_key++, ind++)
81                 {
82                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
83                         QStandardItem *item = new QStandardItem;
84                         const gdcm::ByteValue *bv = (de).GetByteValue();
85                         if( !de.IsEmpty() )
86                         {
87                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
88                                 item->setText(tr(buffer.c_str()));
89                         }
90                         else
91                         {
92                                 item->setText(tr(""));
93                         }
94                         if(ind ==0)
95                         {
96                                 item->setCheckable(true);
97                         }
98                         items.push_back(item);
99                 }
100                 Patientmodel->appendRow(items);
101         }
102 }
103
104
105 void vvQPacsConnection::on_optionsButton_clicked()
106 {
107         vvPacsSettingsDialog *dg  = new vvPacsSettingsDialog(this);
108         dg->show();
109 }
110
111 void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandardItemModel *i_model, std::vector< std::pair<gdcm::Tag, std::string> > keys)
112 {
113         gdcm::Tag tagdb(0x0008,0x0020);
114         std::vector<gdcm::DataSet>::iterator it_ds = i_ds.begin();
115         for(; it_ds != i_ds.end(); it_ds++)
116         {
117                 QList<QStandardItem *> items;
118                 const gdcm::DataSet ds = (*it_ds);
119                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
120                 int ind = 0;
121                 for(; it_key != keys.end(); it_key++, ind++)
122                 {
123                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
124                         QStandardItem *item = new QStandardItem;
125                         const gdcm::ByteValue *bv = (de).GetByteValue();
126                         if( !de.IsEmpty() )
127                         {
128                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
129                                 /*if((*it_key).first == tagdb)
130                                 {
131                                 QDate date;
132                                 date.fromString(tr(buffer.c_str()),"yyyy'MM'd");
133                                 item->setText(date.toString());
134                                 }
135                                 else
136                                 {*/
137                                 item->setText(tr(buffer.c_str()));
138                                 //}
139                         }
140                         else
141                         {
142                                 item->setText(tr(""));
143                         }
144                         if(ind ==0)
145                         {
146                                 item->setCheckable(true);
147                         }
148                         items.push_back(item);
149                 }
150                 i_model->appendRow(items);
151         }
152
153 }
154
155
156 void vvQPacsConnection::createTreeView()
157 {
158         // Patient Tree View
159         Patientmodel = new QStandardItemModel(0,2,this); 
160         QStringList Patientlist;
161         Patientlist.push_back(tr("PATIENT NAME"));
162         Patientlist.push_back(tr("PATIENT ID"));
163         Patientmodel->setHorizontalHeaderLabels(Patientlist);
164         ui.patientTreeView->setModel(Patientmodel);
165         ui.patientTreeView->setEnabled(true);
166         connect(ui.patientTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectStudies(QModelIndex)));
167
168         // Study Tree View
169         Studymodel = new QStandardItemModel(0,3,this); 
170         QStringList Studylist;
171         Studylist.push_back(tr("DESCRIPTION"));
172         Studylist.push_back(tr("DATE"));
173         Studylist.push_back(tr("HOUR"));
174         Studymodel->setHorizontalHeaderLabels(Studylist);
175         ui.studyTreeView->setModel(Studymodel);
176         connect(ui.studyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectSeries(QModelIndex)));
177
178
179         // Series Tree View
180         Seriesmodel = new QStandardItemModel(0,2,this); 
181         QStringList Serieslist;
182         Serieslist.push_back(tr("MODALITY"));
183         Serieslist.push_back(tr("DESCRIPTION"));
184         Serieslist.push_back(tr("no. accept."));
185         Seriesmodel->setHorizontalHeaderLabels(Serieslist);
186         ui.seriesTreeView->setModel(Seriesmodel);
187         connect(ui.seriesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImages(QModelIndex)));
188
189         // Images Tree View
190         Imagesmodel = new QStandardItemModel(0,1,this); 
191         QStringList Imageslist;
192         Imageslist.push_back(tr("instance number"));
193         Imagesmodel->setHorizontalHeaderLabels(Imageslist);
194         ui.imagesTreeView->setModel(Imagesmodel);
195 }
196
197 void vvQPacsConnection::selectStudies(const QModelIndex &index)
198 {
199         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
200         QVariant elt= Patientmodel->data(index.sibling(index.row(),1));
201         m_patient=elt.toString().toStdString();
202         manageSeriesFilter(true);
203         gdcm::EQueryLevel theLevel = gdcm::eStudy;
204         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
205         std::vector<gdcm::DataSet> theDataSet;
206         std::vector< std::pair<gdcm::Tag, std::string> > keys;
207
208         // Study Description
209         gdcm::Tag tagsd(0x0010,0x0020);
210         keys.push_back(std::make_pair(tagsd, m_patient));
211         // Study Description
212         gdcm::Tag tagsdc(0x0008,0x1030);
213         keys.push_back(std::make_pair(tagsdc, ""));
214         // Study date
215         gdcm::Tag tagdb(0x0008,0x0020);
216         keys.push_back(std::make_pair(tagdb, ""));
217         // Study Hour
218         gdcm::Tag tagsdh(0x0008,0x0030);
219         keys.push_back(std::make_pair(tagsdh, ""));
220         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
221         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
222         convertDataSet(theDataSet, Studymodel, getStudyKeys(""));
223 }
224
225
226 void vvQPacsConnection::selectSeries(const QModelIndex &index)
227 {
228         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
229         QVariant elt= Studymodel->data(index.sibling(index.row(),3));
230         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
231
232         //manageImagesFilter(true);
233         gdcm::EQueryLevel theLevel = gdcm::eSeries;
234         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
235         std::vector<gdcm::DataSet> theDataSet;
236         std::vector< std::pair<gdcm::Tag, std::string> > keys;
237
238         //Patient Unique key
239         keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0020), m_patient));
240
241         //Study Unique Key
242         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000d), elt.toString().toStdString()));
243
244
245         // Modality
246         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
247         // Description
248         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
249         // Acceptance NUmber????
250         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
251
252         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
253         keys.clear();
254         // Modality
255         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
256         // Description
257         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
258         // Acceptance NUmber????
259         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
260
261         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
262         convertDataSet(theDataSet, Seriesmodel, keys);
263 }
264
265 void vvQPacsConnection::selectImages(const QModelIndex &index)
266 {
267         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
268         QVariant elt= Seriesmodel->data(index.sibling(index.row(),2));
269         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
270
271         //manageImagesFilter(true);
272         gdcm::EQueryLevel theLevel = gdcm::eImage;
273         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
274         std::vector<gdcm::DataSet> theDataSet;
275         std::vector< std::pair<gdcm::Tag, std::string> > keys;
276
277         gdcm::Tag tagsd(0x0010,0x0020);
278         keys.push_back(std::make_pair(tagsd, m_patient));
279
280         gdcm::Tag tagss(0x0020,0x000e);
281         keys.push_back(std::make_pair(tagss, elt.toString().toStdString()));
282         //= getStudyKeys(elt.toString().toStdString());
283
284         // Study Description
285         gdcm::Tag tagsdc(0x0020,0x0013);
286         keys.push_back(std::make_pair(tagsdc, ""));
287
288
289         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
290         keys.clear();
291         // Study Description
292
293         keys.push_back(std::make_pair(tagsdc, ""));
294
295         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
296         convertDataSet(theDataSet, Imagesmodel, keys);
297 }
298
299
300 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
301 {
302         ui.text_PHYS->setEnabled(i_enable);
303         ui.text_SDESC->setEnabled(i_enable);
304         ui.dateTab->setEnabled(i_enable);
305
306 }
307
308 void vvQPacsConnection::manageSeriesFilter(bool i_enable)
309 {
310         ui.modalityTab->setEnabled(i_enable);
311 }
312
313 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getPatientKeys(const std::string i_val)
314 {
315         std::vector< std::pair<gdcm::Tag, std::string> > keys;
316         // Patient Name
317         gdcm::Tag tag(0x0010,0x0010);
318         keys.push_back(std::make_pair(tag, ""));
319
320         //// Patient ID
321         gdcm::Tag tagpid(0x0010,0x0020);
322         keys.push_back(std::make_pair(tagpid, i_val));
323         return keys;
324 }
325
326 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
327 {
328         std::vector< std::pair<gdcm::Tag, std::string> > keys;
329         // Study Description
330         gdcm::Tag tagsdc(0x0008,0x1030);
331         keys.push_back(std::make_pair(tagsdc, ""));
332         // Study date
333         gdcm::Tag tagdb(0x0008,0x0020);
334         keys.push_back(std::make_pair(tagdb, ""));
335         // Study Hour
336         gdcm::Tag tagsdh(0x0008,0x0030);
337         keys.push_back(std::make_pair(tagsdh, ""));
338         // Study Instance UID
339         gdcm::Tag tagsid(0x0020,0x000d);
340         keys.push_back(std::make_pair(tagsid, i_val));
341
342         return keys;
343 }
344
345
346 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getSeriesKeys(const std::string i_val)
347 {
348         std::vector< std::pair<gdcm::Tag, std::string> > keys;
349         // Modality
350         gdcm::Tag tagsm(0x0008,0x0060);
351         keys.push_back(std::make_pair(tagsm, ""));
352         // Study date
353         gdcm::Tag tagdb(0x0008,0x103e);
354         keys.push_back(std::make_pair(tagdb, ""));
355         // Study Hour
356         gdcm::Tag tagsdh(0x0020,0x000e);
357         keys.push_back(std::make_pair(tagsdh, ""));
358         // Study Instance UID
359         gdcm::Tag tagsid(0x0020,0x1209);
360         keys.push_back(std::make_pair(tagsid, i_val));
361
362         return keys;
363 }
364 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
365 {
366         std::vector< std::pair<gdcm::Tag, std::string> > keys;
367         // Patient Name
368         gdcm::Tag tag(0x0010,0x0010);
369         keys.push_back(std::make_pair(tag, ""));
370
371         //// Patient ID
372         gdcm::Tag tagpid(0x0010,0x0020);
373         keys.push_back(std::make_pair(tagpid, ""));
374
375         // Modality
376         gdcm::Tag tagmod(0x0008,0x0061);
377         keys.push_back(std::make_pair(tagmod, ""));
378
379         // date of birth
380         gdcm::Tag tagdb(0x0010,0x0030);
381         keys.push_back(std::make_pair(tagdb, ""));
382
383         // Study Date
384         gdcm::Tag tagsd(0x0020,0x000D);
385         keys.push_back(std::make_pair(tagsd, ""));
386
387         //// Study Time
388         //gdcm::Tag tagst(8,30);
389         //keys.push_back(std::make_pair(tagst, ""));
390
391         //// Study Description
392         //gdcm::Tag tagsdc(8,1030);
393         //keys.push_back(std::make_pair(tagsdc, ""));
394
395         //// Accession n°
396         //gdcm::Tag tagacc(8,50);
397         //keys.push_back(std::make_pair(tagacc, ""));
398
399         return keys;
400 }
401
402 void vvQPacsConnection::on_check_ModAll_clicked(bool state)
403 {
404         ui.check_MR->setEnabled(!state);
405         ui.check_CR->setEnabled(!state);
406         ui.check_OT->setEnabled(!state);
407         ui.check_RF->setEnabled(!state);
408         ui.check_SC->setEnabled(!state);
409         ui.check_CT->setEnabled(!state);
410         ui.check_US->setEnabled(!state);
411         ui.check_NM->setEnabled(!state);
412         ui.check_DR->setEnabled(!state);
413         ui.check_US->setEnabled(!state);
414         ui.check_NM->setEnabled(!state);
415         ui.check_DR->setEnabled(!state);
416         ui.check_SR->setEnabled(!state);
417         ui.check_XA->setEnabled(!state);
418         ui.check_MG->setEnabled(!state);
419         if(state)
420         {
421                 ui.check_MR->setChecked(state);
422                 ui.check_CR->setChecked(state);
423                 ui.check_OT->setChecked(state);
424                 ui.check_RF->setChecked(state);
425                 ui.check_SC->setChecked(state);
426                 ui.check_CT->setChecked(state);
427                 ui.check_US->setChecked(state);
428                 ui.check_NM->setChecked(state);
429                 ui.check_DR->setChecked(state);
430                 ui.check_US->setChecked(state);
431                 ui.check_NM->setChecked(state);
432                 ui.check_DR->setChecked(state);
433                 ui.check_SR->setChecked(state);
434                 ui.check_XA->setChecked(state);
435                 ui.check_MG->setChecked(state);
436         }
437
438 }
439
440 void vvQPacsConnection::connectServer(int index)
441 {
442         std::map < std::string, std:: string> values = getDicomServer(ui.networkCombo->currentText());
443         m_port = values["PORT"];
444         m_aetitle = values["AETITLE"];
445         m_adress= values["ADRESS"];
446         m_nickname = values["nickname"];
447         ui.AdressEdit->setText(QString(m_adress.c_str()));
448         ui.AETitleEdit->setText(QString(m_aetitle.c_str()));
449         ui.NameEdit->setText(QString(m_nickname.c_str()));
450         ui.PortEdit->setText(QString(m_port.c_str()));
451 }
452
453 void vvQPacsConnection::on_importButton_clicked()
454         {
455                 QModelIndex index;
456                 QVariant elt= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
457                 
458                 gdcm::EQueryLevel theLevel = gdcm::ePatient;
459                 gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
460                 std::vector<gdcm::DataSet> theDataSet;
461                 std::vector< std::pair<gdcm::Tag, std::string> > keys;
462                 
463                 // Study Description
464                 gdcm::Tag tagsdc(0x0010,0x0020);
465                 keys.push_back(std::make_pair(tagsdc, elt.toString().toStdString()));
466                                 gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
467         //      gdcm::CompositeNetworkFunctions::CFind("127.0.0.1", 5678, theQuery, theDataSet, "CREATIS", "CONQUESTSRV1");
468         
469                  bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()), theQuery, 0,
470        "CREATIS", m_nickname.c_str(), "d:\\temp_pacs_import\\" );
471         }