]> Creatis software - clitk.git/blob - vv/vvQPacsConnection.cxx
simplify query/retrieve calls
[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
13 vvQPacsConnection::vvQPacsConnection(QWidget *i_parent)
14         :QDialog(i_parent)
15 {
16         ui.setupUi(this);
17         setWindowTitle(QString::fromUtf8("PACS CONNECTIONHHHH"));
18         createTreeView();
19         ui.tabFilter->setTabText(0,QString(tr("Modality")));
20         ui.tabFilter->setTabText(1,QString(tr("Date")));
21
22         ui. tabNetwork->setTabText(0,QString(tr("Network")));
23         ui. tabNetwork->setTabText(1,QString(tr("Configuration")));
24         ui.check_ModAll->setEnabled(true);
25         ui.networkCombo->addItem("");
26         ui.networkCombo->addItems(getDicomServers());
27         
28         // Connection   
29         connect(ui.networkCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(chooseServer(int)));
30         connect(ui.removeNetworkButton,SIGNAL(clicked()),this,SLOT(removeServer()));
31         connect(ui.NetworkButton,SIGNAL(clicked()),this,SLOT(modifyServer()));
32         
33         update();
34 }
35
36 // remote a Dicom Server in VV settings
37 void vvQPacsConnection::removeServer()
38 {
39         removeDicomServer(m_nickname);
40         ui.networkCombo->removeItem(ui.networkCombo->findText(QString(m_nickname.c_str())));
41         m_nickname="";
42         refreshNetworks();
43 }
44
45 // modify a Dicom Server in VV settings
46 void vvQPacsConnection::modifyServer()
47 {
48         AddDicomServer(ui.NameEdit->text().toStdString(),ui.AETitleEdit->text().toStdString(),ui.AdressEdit->text().toStdString(),ui.PortEdit->text().toStdString());
49         removeServer();
50 }
51
52 // refresh the list of Dicom Servers available from VV settings
53 void vvQPacsConnection::refreshNetworks()
54 {
55         ui.networkCombo->clear();
56         ui.networkCombo->addItem(QString());
57         ui.networkCombo->addItems(getDicomServers());
58         ui.NameEdit->setText(QString());
59         ui.AETitleEdit->setText(QString());
60         ui.AdressEdit->setText(QString());
61         ui.PortEdit->setText(QString());
62         ui.tabNetwork->setCurrentIndex(0);
63 }
64
65 void vvQPacsConnection::on_clearButton_clicked()
66 {
67         Patientmodel->removeRows(0, Patientmodel->rowCount(),QModelIndex());
68         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
69         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
70         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
71 }
72
73 void vvQPacsConnection::on_scanButton_clicked()
74 {
75         cleanTree();
76         manageStudiesFilter(true);
77
78         // test first if echo works
79         bool didItWork = gdcm::CompositeNetworkFunctions::CEcho(m_adress.c_str(), atoi(m_port.c_str()), getDicomClientAETitle().c_str(), m_nickname.c_str() );
80         if (didItWork)
81         {
82                 m_level =gdcm::ePatient;
83                 std::vector<gdcm::DataSet> theDataSet;
84                 f_query = getQueryPatient(ui.patientName->toPlainText().toStdString(),  ui.patientID->toPlainText().toStdString());
85
86                 bool cfindWork = gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
87                         gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys),
88                         theDataSet, getDicomClientAETitle().c_str()     , m_nickname.c_str());
89                 if( cfindWork)
90                 {
91                         convertDataSet(theDataSet,Patientmodel,getPatientKeys("",""));
92                 } // end cfindwork
93         } // end didItwork
94 }
95
96
97 /// show Options DialogBox to set a new Dicom Server
98 void vvQPacsConnection::on_optionsButton_clicked()
99 {
100         vvPacsSettingsDialog *dg  = new vvPacsSettingsDialog(this);
101         dg->show();
102 }
103
104 void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandardItemModel *i_model, std::vector< std::pair<gdcm::Tag, std::string> > keys)
105 {
106         std::vector<gdcm::DataSet>::iterator it_ds = i_ds.begin();
107         for(; it_ds != i_ds.end(); it_ds++)
108         {
109                 QList<QStandardItem *> items;
110                 const gdcm::DataSet ds = (*it_ds);
111                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
112                 int ind = 0;
113                 for(; it_key != keys.end(); it_key++, ind++)
114                 {
115                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
116                         QStandardItem *item = new QStandardItem;
117                         const gdcm::ByteValue *bv = (de).GetByteValue();
118                         if( !de.IsEmpty() )
119                         {
120                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
121                                 item->setText(tr(buffer.c_str()));
122                         }
123                         else
124                         {
125                                 item->setText(tr(""));
126                         }
127                         if(ind ==0)
128                         {
129                                 item->setCheckable(true);
130                         }
131                         items.push_back(item);
132                 }
133                 i_model->appendRow(items);
134         }
135 }
136
137 // TreeViews creation
138 void vvQPacsConnection::createTreeView()
139 {
140         // Patient Tree View
141         Patientmodel = new QStandardItemModel(0,2,this); 
142         QStringList Patientlist;
143         Patientlist.push_back(tr("PATIENT NAME"));
144         Patientlist.push_back(tr("PATIENT ID"));
145         Patientmodel->setHorizontalHeaderLabels(Patientlist);
146         ui.patientTreeView->setModel(Patientmodel);
147         ui.patientTreeView->setEnabled(true);
148         connect(ui.patientTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectStudies(QModelIndex)));
149
150         // Study Tree View
151         Studymodel = new QStandardItemModel(0,3,this); 
152         QStringList Studylist;
153         Studylist.push_back(tr("DESCRIPTION"));
154         Studylist.push_back(tr("DATE"));
155         Studylist.push_back(tr("HOUR"));
156         Studylist.push_back(tr("STUDY ID"));
157         Studymodel->setHorizontalHeaderLabels(Studylist);
158         ui.studyTreeView->setModel(Studymodel);
159         connect(ui.studyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectSeries(QModelIndex)));
160
161
162         // Series Tree View
163         Seriesmodel = new QStandardItemModel(0,2,this); 
164         QStringList Serieslist;
165         Serieslist.push_back(tr("MODALITY"));
166         Serieslist.push_back(tr("DESCRIPTION"));
167         Serieslist.push_back(tr("no. accept."));
168         Seriesmodel->setHorizontalHeaderLabels(Serieslist);
169         ui.seriesTreeView->setModel(Seriesmodel);
170         connect(ui.seriesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImages(QModelIndex)));
171
172         // Images Tree View
173         Imagesmodel = new QStandardItemModel(0,1,this); 
174         QStringList Imageslist;
175         Imageslist.push_back(tr("instance number"));
176         Imageslist.push_back(tr("sopuid"));
177         Imagesmodel->setHorizontalHeaderLabels(Imageslist);
178         ui.imagesTreeView->setModel(Imagesmodel);
179 }
180
181 // clean the different model Trees
182 void vvQPacsConnection::cleanTree()
183 {
184         Patientmodel->removeRows(0,Patientmodel->rowCount());
185         Studymodel->removeRows(0,Patientmodel->rowCount());
186         Seriesmodel->removeRows(0,Patientmodel->rowCount());
187         Imagesmodel->removeRows(0,Patientmodel->rowCount());
188
189 }
190
191 void vvQPacsConnection::selectStudies(const QModelIndex &index)
192 {
193
194                         
195         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
196         QVariant elt= Patientmodel->data(index.sibling(index.row(),1));
197         m_patient=elt.toString().toStdString();
198         manageSeriesFilter(true);
199         
200         std::vector<gdcm::DataSet> theDataSet;
201                  m_query = getQueryPatient("",m_patient);
202          f_query = getQueryforStudy(m_patient);
203                  
204                  
205         if (  gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
206                 gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys)
207                 , theDataSet, getDicomClientAETitle().c_str(), m_nickname.c_str()) )
208         {
209                 convertDataSet(theDataSet, Studymodel, getStudyKeys(""));
210         }
211 }
212
213
214
215
216 void vvQPacsConnection::selectSeries(const QModelIndex &index)
217 {
218         m_level =gdcm::eSeries;
219         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
220         QVariant elt= Studymodel->data(index.sibling(index.row(),3));
221         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
222
223
224         std::vector<gdcm::DataSet> theDataSet;
225         std::vector< std::pair<gdcm::Tag, std::string> > keys;
226
227         //Patient Unique key
228         keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0020), m_patient));
229
230         //Study Unique Key
231         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000d), elt.toString().toStdString()));
232
233
234         // Modality
235         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
236         // Description
237         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
238         // Acceptance NUmber????
239         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
240         f_query =getQueryforSeries(elt.toString().toStdString());
241         if ( gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
242                 gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys), theDataSet, 
243                  getDicomClientAETitle().c_str(), m_nickname.c_str()))
244         {
245                 keys.clear();
246         // Modality
247         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
248         // Description
249         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
250         // Acceptance NUmber????
251         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
252                 convertDataSet(theDataSet, Seriesmodel, getSeriesKeys(""));
253         }
254 }
255
256 void vvQPacsConnection::selectImages(const QModelIndex &index)
257 {
258         m_level = gdcm::eImage;
259         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
260         QVariant elt= Seriesmodel->data(index.sibling(index.row(),2));
261         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
262
263         //manageImagesFilter(true);
264         gdcm::EQueryLevel theLevel = gdcm::eImage;
265         gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
266         std::vector<gdcm::DataSet> theDataSet;
267         std::vector< std::pair<gdcm::Tag, std::string> > keys;
268
269         gdcm::Tag tagsd(0x0010,0x0020);
270         keys.push_back(std::make_pair(tagsd, m_patient));
271
272         gdcm::Tag tagss(0x0020,0x000e);
273         keys.push_back(std::make_pair(tagss, elt.toString().toStdString()));
274         //= getStudyKeys(elt.toString().toStdString());
275
276         // Image Description
277         gdcm::Tag tagsdc(0x0020,0x0013);
278         keys.push_back(std::make_pair(tagsdc, ""));
279                 gdcm::Tag tagsic(0x0008,0x0018);
280
281
282         keys.push_back(std::make_pair(tagsic, ""));
283
284         gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
285         keys.clear();
286         // SOP Instance UID 
287         
288
289
290         keys.push_back(std::make_pair(tagsdc, ""));
291 keys.push_back(std::make_pair(tagsic, ""));
292
293         gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
294         convertDataSet(theDataSet, Imagesmodel, keys);
295 }
296
297
298 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
299 {
300         ui.text_PHYS->setEnabled(i_enable);
301         ui.text_SDESC->setEnabled(i_enable);
302         ui.dateTab->setEnabled(i_enable);
303
304 }
305
306 void vvQPacsConnection::manageSeriesFilter(bool i_enable)
307 {
308         ui.modalityTab->setEnabled(i_enable);
309 }
310
311 vvQuery vvQPacsConnection::getQueryPatient(const std::string i_patname, const std::string i_patid)
312 {
313         vvQuery query;
314         query.theRoot = gdcm::ePatientRootType;
315         query.theLevel = gdcm::ePatient;
316         query.keys = getPatientKeys(i_patname, i_patid);
317         return query;
318
319 }
320
321 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getPatientKeys(const std::string i_patname, const std::string i_patid)
322 {
323         std::vector< std::pair<gdcm::Tag, std::string> > keys;
324         // Patient Name
325         gdcm::Tag tag(0x0010,0x0010);
326         keys.push_back(std::make_pair(tag, i_patname));
327
328         //// Patient ID
329         gdcm::Tag tagpid(0x0010,0x0020);
330         keys.push_back(std::make_pair(tagpid, i_patid));
331         return keys;
332 }
333
334 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
335 {
336         std::vector< std::pair<gdcm::Tag, std::string> > keys;
337         // Study Description
338         gdcm::Tag tagsdc(0x0008,0x1030);
339         keys.push_back(std::make_pair(tagsdc, ""));
340         // Study date
341         gdcm::Tag tagdb(0x0008,0x0020);
342         keys.push_back(std::make_pair(tagdb, ""));
343         // Study Hour
344         gdcm::Tag tagsdh(0x0008,0x0030);
345         keys.push_back(std::make_pair(tagsdh, ""));
346         // Study Instance UID
347         gdcm::Tag tagsid(0x0020,0x000d);
348         keys.push_back(std::make_pair(tagsid, i_val));
349
350         return keys;
351 }
352
353 vvQuery vvQPacsConnection::getQueryforSeries(const std::string study_id)
354 {
355         vvQuery query;
356         query.theRoot = gdcm::ePatientRootType;
357         query.theLevel = gdcm::eSeries;
358         query.keys = getSeriesKeys(study_id);
359         return query;
360 }
361 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getSeriesKeys(const std::string study_id)
362 {
363         std::vector< std::pair<gdcm::Tag, std::string> > keys;
364         // Modality
365         gdcm::Tag tagsm(0x0008,0x0060);
366         keys.push_back(std::make_pair(tagsm, ""));
367         // Study date
368         gdcm::Tag tagdb(0x0008,0x103e);
369         keys.push_back(std::make_pair(tagdb, ""));
370         // Study Hour
371         gdcm::Tag tagsdh(0x0020,0x000e);
372         keys.push_back(std::make_pair(tagsdh, ""));
373         // Study Instance UID
374         gdcm::Tag tagsid(0x0020,0x1209);
375         keys.push_back(std::make_pair(tagsid, study_id));
376
377         return keys;
378 }
379 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
380 {
381         std::vector< std::pair<gdcm::Tag, std::string> > keys;
382         // Patient Name
383         gdcm::Tag tag(0x0010,0x0010);
384         keys.push_back(std::make_pair(tag, ""));
385
386         //// Patient ID
387         gdcm::Tag tagpid(0x0010,0x0020);
388         keys.push_back(std::make_pair(tagpid, ""));
389
390         // Modality
391         gdcm::Tag tagmod(0x0008,0x0061);
392         keys.push_back(std::make_pair(tagmod, ""));
393
394         // date of birth
395         gdcm::Tag tagdb(0x0010,0x0030);
396         keys.push_back(std::make_pair(tagdb, ""));
397
398         // Study Date
399         gdcm::Tag tagsd(0x0020,0x000D);
400         keys.push_back(std::make_pair(tagsd, ""));
401
402         //// Study Time
403         //gdcm::Tag tagst(8,30);
404         //keys.push_back(std::make_pair(tagst, ""));
405
406         //// Study Description
407         //gdcm::Tag tagsdc(8,1030);
408         //keys.push_back(std::make_pair(tagsdc, ""));
409
410         //// Accession n°
411         //gdcm::Tag tagacc(8,50);
412         //keys.push_back(std::make_pair(tagacc, ""));
413
414         return keys;
415 }
416
417 void vvQPacsConnection::on_check_ModAll_clicked(bool state)
418 {
419         ui.check_MR->setEnabled(!state);
420         ui.check_CR->setEnabled(!state);
421         ui.check_OT->setEnabled(!state);
422         ui.check_RF->setEnabled(!state);
423         ui.check_SC->setEnabled(!state);
424         ui.check_CT->setEnabled(!state);
425         ui.check_US->setEnabled(!state);
426         ui.check_NM->setEnabled(!state);
427         ui.check_DR->setEnabled(!state);
428         ui.check_US->setEnabled(!state);
429         ui.check_NM->setEnabled(!state);
430         ui.check_DR->setEnabled(!state);
431         ui.check_SR->setEnabled(!state);
432         ui.check_XA->setEnabled(!state);
433         ui.check_MG->setEnabled(!state);
434         if(state)
435         {
436                 ui.check_MR->setChecked(state);
437                 ui.check_CR->setChecked(state);
438                 ui.check_OT->setChecked(state);
439                 ui.check_RF->setChecked(state);
440                 ui.check_SC->setChecked(state);
441                 ui.check_CT->setChecked(state);
442                 ui.check_US->setChecked(state);
443                 ui.check_NM->setChecked(state);
444                 ui.check_DR->setChecked(state);
445                 ui.check_US->setChecked(state);
446                 ui.check_NM->setChecked(state);
447                 ui.check_DR->setChecked(state);
448                 ui.check_SR->setChecked(state);
449                 ui.check_XA->setChecked(state);
450                 ui.check_MG->setChecked(state);
451         }
452
453 }
454
455 void vvQPacsConnection::chooseServer(int index)
456 {
457         std::map < std::string, std:: string> values = getDicomServer(ui.networkCombo->currentText());
458         m_port = values["PORT"];
459         m_aetitle = values["AETITLE"];
460         m_adress= values["ADRESS"];
461         m_nickname = values["nickname"];
462         ui.AdressEdit->setText(QString(m_adress.c_str()));
463         ui.AETitleEdit->setText(QString(m_aetitle.c_str()));
464         ui.NameEdit->setText(QString(m_nickname.c_str()));
465         ui.PortEdit->setText(QString(m_port.c_str()));
466 }
467
468 void vvQPacsConnection::on_importButton_clicked()
469         {
470         
471                 bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()),
472                         gdcm::CompositeNetworkFunctions::ConstructQuery(m_query.theRoot, m_query.theLevel ,m_query.keys,true), getDicomClientPort(),
473                          getDicomClientAETitle().c_str(), m_aetitle.c_str(),"D:\\move" );
474                 gdcm::Directory theDir;
475                 theDir.Load("D:\\move");
476            m_files =    theDir.GetFilenames();
477            accept();
478         }
479
480 std::vector <std::string> vvQPacsConnection::getFileNames()
481 {
482         std::vector <std::string> filenames;
483         gdcm::Directory::FilenamesType::iterator it = m_files.begin();
484         for (;it != m_files.end(); it++)
485                 filenames.push_back(it->c_str());
486         return filenames;
487 }
488 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::fillMoveKeys()
489 {
490         std::vector< std::pair<gdcm::Tag, std::string> > keys;
491         switch(m_level)
492         {
493         case gdcm::ePatient:
494                         //keys.push_back(getPatientKeys("",""));
495                         break;
496         }
497
498         return keys;
499 }
500
501 vvQuery vvQPacsConnection::getQueryforStudy(const  std::string patient_id)
502 {
503         vvQuery query;
504         query.keys = getQueryKeysforStudy( patient_id);
505         query.theRoot = gdcm::ePatientRootType;
506         query.theLevel = gdcm::ePatient;
507         return query;
508 }
509
510 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getQueryKeysforStudy(const std::string patient_id)
511 {
512         std::vector< std::pair<gdcm::Tag, std::string> > keys;
513
514         // Study Description
515         gdcm::Tag tagsd(0x0010,0x0020);
516         keys.push_back(std::make_pair(tagsd, ""));
517         // Study Description
518         gdcm::Tag tagsdc(0x0008,0x1030);
519         keys.push_back(std::make_pair(tagsdc, ""));
520         // Study date
521         gdcm::Tag tagdb(0x0008,0x0020);
522         keys.push_back(std::make_pair(tagdb, ""));
523         // Study Hour
524         gdcm::Tag tagsdh(0x0008,0x0030);
525         keys.push_back(std::make_pair(tagsdh, ""));
526
527         // Study Hour
528         gdcm::Tag tagsid(0x020,0x000d);
529         keys.push_back(std::make_pair(tagsid, patient_id.c_str()));
530
531
532         return keys;
533 }