]> Creatis software - clitk.git/blob - vv/vvQPacsConnection.cxx
Dicom server connection
[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         connect(ui.optionsButton,SIGNAL(clicked()),this,SLOT(setNewPacs()));
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         //on_clearButton_clicked();
60         manageStudiesFilter(true);
61         //      #if defined (USE_GDCM2)
62         bool didItWork = gdcm::CompositeNetworkFunctions::CEcho(m_adress.c_str(), atoi(m_port.c_str()), "CREATIS", m_nickname.c_str() );
63         /*  tag.SetElement(8);
64         tag.SetGroup(8);*/
65         std::vector< std::pair<gdcm::Tag, std::string> > keys = getKeys();
66
67         //std::pair<gdcm::Tag, std::string> pa;
68         //pa.first = tag;
69         //pa.second = "";
70         ////keys.push_back(std::make_pair(tag, ""));
71         gdcm::EQueryLevel theLevel = gdcm::eStudy;
72         gdcm::ERootType theRoot  = gdcm::eStudyRootType;//ePatientRootType;
73         //gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
74
75
76         std::vector<gdcm::DataSet> theDataSet;
77
78         theLevel = gdcm::ePatient;
79         theRoot  = gdcm::ePatientRootType;//ePatientRootType;
80         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,getPatientKeys(""));
81
82         //      
83         //theDataSet.clear();
84         //  gdcm::CompositeNetworkFunctions::CFind(m_adress, m_port, theQuery, theDataSet,
85         //       "CREATIS", m_nickname);
86         //   keys.clear();
87         //   keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0010), ""));                      
88         //                      
89         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet,
90                 "CREATIS", m_nickname.c_str());
91         std::vector<gdcm::DataSet>::iterator it_ds = theDataSet.begin();
92         //
93         for(; it_ds != theDataSet.end(); it_ds++)
94         {
95
96                 QList<QStandardItem *> items;
97                 const gdcm::DataSet ds = (*it_ds);
98                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
99                 int ind = 0;
100                 for(; it_key != keys.end(); it_key++, ind++)
101                 {
102                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
103                         QStandardItem *item = new QStandardItem;
104                         const gdcm::ByteValue *bv = (de).GetByteValue();
105                         if( !de.IsEmpty() )
106                         {
107                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
108                                 item->setText(tr(buffer.c_str()));
109                         }
110                         else
111                         {
112                                 item->setText(tr(""));
113                         }
114                         if(ind ==0)
115                         {
116                                 item->setCheckable(true);
117                         }
118                         items.push_back(item);
119                 }
120                 Patientmodel->appendRow(items);
121         }
122         //   gdcm::Tag tagtest(0x0020,0x1208);
123         //   keys.clear();
124         //   keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0010), ""));
125         ////  keys.push_back(std::make_pair(tagtest, ""));
126         //  theLevel = gdcm::ePatient;
127         //  theRoot  = gdcm::ePatientRootType;//ePatientRootType;
128         //  theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
129         //
130         //      
131         //theDataSet.clear();
132         //  gdcm::CompositeNetworkFunctions::CFind(m_adress, m_port, theQuery, theDataSet,
133         //       "CREATIS",m_nickname);
134
135 }
136
137
138 void vvQPacsConnection::on_optionsButton_clicked()
139 {
140
141         vvPacsSettingsDialog *dg  = new vvPacsSettingsDialog(this);
142         dg->show();
143
144         /*QModelIndex index;
145         QVariant elt= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
146
147         gdcm::EQueryLevel theLevel = gdcm::ePatient;
148         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
149         std::vector<gdcm::DataSet> theDataSet;
150         std::vector< std::pair<gdcm::Tag, std::string> > keys;
151
152         // Study Description
153         gdcm::Tag tagsdc(0x0010,0x0020);
154         keys.push_back(std::make_pair(tagsdc, elt.toString().toStdString()));
155         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
156         gdcm::CompositeNetworkFunctions::CFind(m_adress, m_port, theQuery, theDataSet, "CREATIS", m_nickname);
157
158         bool didItWork =  gdcm::CompositeNetworkFunctions::CMove( m_adress, m_port, theQuery, 0,
159         "CREATIS", m_nickname,m_destination );*/
160 }
161
162 void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandardItemModel *i_model, std::vector< std::pair<gdcm::Tag, std::string> > keys)
163 {
164         gdcm::Tag tagdb(0x0008,0x0020);
165         std::vector<gdcm::DataSet>::iterator it_ds = i_ds.begin();
166         for(; it_ds != i_ds.end(); it_ds++)
167         {
168                 QList<QStandardItem *> items;
169                 const gdcm::DataSet ds = (*it_ds);
170                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
171                 int ind = 0;
172                 for(; it_key != keys.end(); it_key++, ind++)
173                 {
174                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
175                         QStandardItem *item = new QStandardItem;
176                         const gdcm::ByteValue *bv = (de).GetByteValue();
177                         if( !de.IsEmpty() )
178                         {
179                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
180                                 /*if((*it_key).first == tagdb)
181                                 {
182                                 QDate date;
183                                 date.fromString(tr(buffer.c_str()),"yyyy'MM'd");
184                                 item->setText(date.toString());
185                                 }
186                                 else
187                                 {*/
188                                 item->setText(tr(buffer.c_str()));
189                                 //}
190                         }
191                         else
192                         {
193                                 item->setText(tr(""));
194                         }
195                         if(ind ==0)
196                         {
197                                 item->setCheckable(true);
198                         }
199                         items.push_back(item);
200                 }
201                 i_model->appendRow(items);
202         }
203
204 }
205
206
207 void vvQPacsConnection::createTreeView()
208 {
209         // Patient Tree View
210         Patientmodel = new QStandardItemModel(0,2,this); 
211         QStringList Patientlist;
212         Patientlist.push_back(tr("PATIENT NAME"));
213         Patientlist.push_back(tr("PATIENT ID"));
214         Patientmodel->setHorizontalHeaderLabels(Patientlist);
215         ui.patientTreeView->setModel(Patientmodel);
216         ui.patientTreeView->setEnabled(true);
217         connect(ui.patientTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectStudies(QModelIndex)));
218
219         // Study Tree View
220         Studymodel = new QStandardItemModel(0,3,this); 
221         QStringList Studylist;
222         Studylist.push_back(tr("DESCRIPTION"));
223         Studylist.push_back(tr("DATE"));
224         Studylist.push_back(tr("HOUR"));
225         Studymodel->setHorizontalHeaderLabels(Studylist);
226         ui.studyTreeView->setModel(Studymodel);
227         connect(ui.studyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectSeries(QModelIndex)));
228
229
230         // Series Tree View
231         Seriesmodel = new QStandardItemModel(0,2,this); 
232         QStringList Serieslist;
233         Serieslist.push_back(tr("MODALITY"));
234         Serieslist.push_back(tr("DESCRIPTION"));
235         Serieslist.push_back(tr("no. accept."));
236         Seriesmodel->setHorizontalHeaderLabels(Serieslist);
237         ui.seriesTreeView->setModel(Seriesmodel);
238         connect(ui.seriesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImages(QModelIndex)));
239
240         // Images Tree View
241         Imagesmodel = new QStandardItemModel(0,1,this); 
242         QStringList Imageslist;
243         Imageslist.push_back(tr("instance number"));
244         Imagesmodel->setHorizontalHeaderLabels(Imageslist);
245         ui.imagesTreeView->setModel(Imagesmodel);
246
247         //model->setHeaderData(0, Qt::Horizontal,tr("test"),Qt::DisplayRole);
248         /*QFileSystemModel *filemodel = new   QFileSystemModel;
249         filemodel->insertColumn(1);
250         filemodel->setHeaderData(1, Qt::Horizontal,"test",0);*/
251
252 }
253
254 void vvQPacsConnection::selectStudies(const QModelIndex &index)
255 {
256         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
257         QVariant elt= Patientmodel->data(index.sibling(index.row(),1));
258         m_patient=elt.toString().toStdString();
259         manageSeriesFilter(true);
260         gdcm::EQueryLevel theLevel = gdcm::eStudy;
261         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
262         std::vector<gdcm::DataSet> theDataSet;
263         std::vector< std::pair<gdcm::Tag, std::string> > keys;
264
265         // Study Description
266         gdcm::Tag tagsd(0x0010,0x0020);
267         keys.push_back(std::make_pair(tagsd, m_patient));
268         // Study Description
269         gdcm::Tag tagsdc(0x0008,0x1030);
270         keys.push_back(std::make_pair(tagsdc, ""));
271         // Study date
272         gdcm::Tag tagdb(0x0008,0x0020);
273         keys.push_back(std::make_pair(tagdb, ""));
274         // Study Hour
275         gdcm::Tag tagsdh(0x0008,0x0030);
276         keys.push_back(std::make_pair(tagsdh, ""));
277         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
278         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
279         convertDataSet(theDataSet, Studymodel, getStudyKeys(""));
280 }
281
282
283 void vvQPacsConnection::selectSeries(const QModelIndex &index)
284 {
285         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
286         QVariant elt= Studymodel->data(index.sibling(index.row(),3));
287         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
288
289         //manageImagesFilter(true);
290         gdcm::EQueryLevel theLevel = gdcm::eSeries;
291         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
292         std::vector<gdcm::DataSet> theDataSet;
293         std::vector< std::pair<gdcm::Tag, std::string> > keys;
294
295         //Patient Unique key
296         keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0020), m_patient));
297
298         //Study Unique Key
299         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000d), elt.toString().toStdString()));
300
301
302         // Modality
303         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
304         // Description
305         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
306         // Acceptance NUmber????
307         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
308
309         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
310         keys.clear();
311         // Modality
312         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
313         // Description
314         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
315         // Acceptance NUmber????
316         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
317
318
319
320         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
321         convertDataSet(theDataSet, Seriesmodel, keys);
322 }
323
324 void vvQPacsConnection::selectImages(const QModelIndex &index)
325 {
326         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
327         QVariant elt= Seriesmodel->data(index.sibling(index.row(),2));
328         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
329
330         //manageImagesFilter(true);
331         gdcm::EQueryLevel theLevel = gdcm::eImage;
332         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
333         std::vector<gdcm::DataSet> theDataSet;
334         std::vector< std::pair<gdcm::Tag, std::string> > keys;
335
336         gdcm::Tag tagsd(0x0010,0x0020);
337         keys.push_back(std::make_pair(tagsd, m_patient));
338
339         gdcm::Tag tagss(0x0020,0x000e);
340         keys.push_back(std::make_pair(tagss, elt.toString().toStdString()));
341         //= getStudyKeys(elt.toString().toStdString());
342
343         // Study Description
344         gdcm::Tag tagsdc(0x0020,0x0013);
345         keys.push_back(std::make_pair(tagsdc, ""));
346
347
348         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
349         keys.clear();
350         // Study Description
351
352         keys.push_back(std::make_pair(tagsdc, ""));
353
354
355         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
356         convertDataSet(theDataSet, Seriesmodel, keys);
357 }
358
359
360 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
361 {
362         ui.text_PHYS->setEnabled(i_enable);
363         ui.text_SDESC->setEnabled(i_enable);
364         ui.dateTab->setEnabled(i_enable);
365
366 }
367
368 void vvQPacsConnection::manageSeriesFilter(bool i_enable)
369 {
370         ui.modalityTab->setEnabled(i_enable);
371 }
372
373 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getPatientKeys(const std::string i_val)
374 {
375         std::vector< std::pair<gdcm::Tag, std::string> > keys;
376         // Patient Name
377         gdcm::Tag tag(0x0010,0x0010);
378         keys.push_back(std::make_pair(tag, ""));
379
380         //// Patient ID
381         gdcm::Tag tagpid(0x0010,0x0020);
382         keys.push_back(std::make_pair(tagpid, i_val));
383         return keys;
384 }
385
386 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
387 {
388         std::vector< std::pair<gdcm::Tag, std::string> > keys;
389         // Study Description
390         gdcm::Tag tagsdc(0x0008,0x1030);
391         keys.push_back(std::make_pair(tagsdc, ""));
392         // Study date
393         gdcm::Tag tagdb(0x0008,0x0020);
394         keys.push_back(std::make_pair(tagdb, ""));
395         // Study Hour
396         gdcm::Tag tagsdh(0x0008,0x0030);
397         keys.push_back(std::make_pair(tagsdh, ""));
398         // Study Instance UID
399         gdcm::Tag tagsid(0x0020,0x000d);
400         keys.push_back(std::make_pair(tagsid, i_val));
401
402         return keys;
403 }
404
405
406 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getSeriesKeys(const std::string i_val)
407 {
408         std::vector< std::pair<gdcm::Tag, std::string> > keys;
409         // Modality
410         gdcm::Tag tagsm(0x0008,0x0060);
411         keys.push_back(std::make_pair(tagsm, ""));
412         // Study date
413         gdcm::Tag tagdb(0x0008,0x103e);
414         keys.push_back(std::make_pair(tagdb, ""));
415         // Study Hour
416         gdcm::Tag tagsdh(0x0020,0x000e);
417         keys.push_back(std::make_pair(tagsdh, ""));
418         // Study Instance UID
419         gdcm::Tag tagsid(0x0020,0x1209);
420         keys.push_back(std::make_pair(tagsid, i_val));
421
422         return keys;
423 }
424 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
425 {
426         std::vector< std::pair<gdcm::Tag, std::string> > keys;
427         // Patient Name
428         gdcm::Tag tag(0x0010,0x0010);
429         keys.push_back(std::make_pair(tag, ""));
430
431         //// Patient ID
432         gdcm::Tag tagpid(0x0010,0x0020);
433         keys.push_back(std::make_pair(tagpid, ""));
434
435         // Modality
436         gdcm::Tag tagmod(0x0008,0x0061);
437         keys.push_back(std::make_pair(tagmod, ""));
438
439         // date of birth
440         gdcm::Tag tagdb(0x0010,0x0030);
441         keys.push_back(std::make_pair(tagdb, ""));
442
443         // Study Date
444         gdcm::Tag tagsd(0x0020,0x000D);
445         keys.push_back(std::make_pair(tagsd, ""));
446
447         //// Study Time
448         //gdcm::Tag tagst(8,30);
449         //keys.push_back(std::make_pair(tagst, ""));
450
451         //// Study Description
452         //gdcm::Tag tagsdc(8,1030);
453         //keys.push_back(std::make_pair(tagsdc, ""));
454
455         //// Accession n°
456         //gdcm::Tag tagacc(8,50);
457         //keys.push_back(std::make_pair(tagacc, ""));
458
459         return keys;
460 }
461
462 void vvQPacsConnection::on_check_ModAll_clicked(bool state)
463 {
464         ui.check_MR->setEnabled(!state);
465         ui.check_CR->setEnabled(!state);
466         ui.check_OT->setEnabled(!state);
467         ui.check_RF->setEnabled(!state);
468         ui.check_SC->setEnabled(!state);
469         ui.check_CT->setEnabled(!state);
470         ui.check_US->setEnabled(!state);
471         ui.check_NM->setEnabled(!state);
472         ui.check_DR->setEnabled(!state);
473         ui.check_US->setEnabled(!state);
474         ui.check_NM->setEnabled(!state);
475         ui.check_DR->setEnabled(!state);
476         ui.check_SR->setEnabled(!state);
477         ui.check_XA->setEnabled(!state);
478         ui.check_MG->setEnabled(!state);
479         if(state)
480         {
481                 ui.check_MR->setChecked(state);
482                 ui.check_CR->setChecked(state);
483                 ui.check_OT->setChecked(state);
484                 ui.check_RF->setChecked(state);
485                 ui.check_SC->setChecked(state);
486                 ui.check_CT->setChecked(state);
487                 ui.check_US->setChecked(state);
488                 ui.check_NM->setChecked(state);
489                 ui.check_DR->setChecked(state);
490                 ui.check_US->setChecked(state);
491                 ui.check_NM->setChecked(state);
492                 ui.check_DR->setChecked(state);
493                 ui.check_SR->setChecked(state);
494                 ui.check_XA->setChecked(state);
495                 ui.check_MG->setChecked(state);
496         }
497
498 }
499
500 void vvQPacsConnection::connectServer(int index)
501 {
502         std::map < std::string, std:: string> values = getDicomServer(ui.networkCombo->currentText());
503         m_port = values["PORT"];
504         m_aetitle = values["AETITLE"];
505         m_adress= values["ADRESS"];
506         m_nickname = values["nickname"];
507         ui.AdressEdit->setText(QString(m_adress.c_str()));
508         ui.AETitleEdit->setText(QString(m_aetitle.c_str()));
509         ui.NameEdit->setText(QString(m_nickname.c_str()));
510         ui.PortEdit->setText(QString(m_port.c_str()));
511 }
512
513 void vvQPacsConnection::setNewPacs()
514 {
515         int i;
516         i =0;
517 }