]> Creatis software - clitk.git/blob - vv/vvQPacsConnection.cxx
add cmove for all levels
[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
107         std::vector<gdcm::DataSet>::iterator it_ds = i_ds.begin();
108         for(; it_ds != i_ds.end(); it_ds++)
109         {
110                 QList<QStandardItem *> items;
111                 const gdcm::DataSet ds = (*it_ds);
112                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
113                 int ind = 0;
114                 for(; it_key != keys.end(); it_key++, ind++)
115                 {
116                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
117                         QStandardItem *item = new QStandardItem;
118                         const gdcm::ByteValue *bv = (de).GetByteValue();
119                         if( !de.IsEmpty() )
120                         {
121                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
122                                 item->setText(tr(buffer.c_str()));
123                         }
124                         else
125                         {
126                                 item->setText(tr(""));
127                         }
128                         if(ind ==0)
129                         {
130                                 item->setCheckable(true);
131                         }
132                         items.push_back(item);
133                 }
134                 i_model->appendRow(items);
135         }
136 }
137
138 // TreeViews creation
139 void vvQPacsConnection::createTreeView()
140 {
141         // Patient Tree View
142         Patientmodel = new QStandardItemModel(0,2,this); 
143         QStringList Patientlist;
144         Patientlist.push_back(tr("PATIENT NAME"));
145         Patientlist.push_back(tr("PATIENT ID"));
146         Patientmodel->setHorizontalHeaderLabels(Patientlist);
147         ui.patientTreeView->setModel(Patientmodel);
148         ui.patientTreeView->setEnabled(true);
149         connect(ui.patientTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectStudies(QModelIndex)));
150
151         // Study Tree View
152         Studymodel = new QStandardItemModel(0,3,this); 
153         QStringList Studylist;
154         Studylist.push_back(tr("DESCRIPTION"));
155         Studylist.push_back(tr("DATE"));
156         Studylist.push_back(tr("HOUR"));
157         Studylist.push_back(tr("STUDY ID"));
158         Studymodel->setHorizontalHeaderLabels(Studylist);
159         ui.studyTreeView->setModel(Studymodel);
160         connect(ui.studyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectSeries(QModelIndex)));
161
162
163         // Series Tree View
164         Seriesmodel = new QStandardItemModel(0,2,this); 
165         QStringList Serieslist;
166         Serieslist.push_back(tr("MODALITY"));
167         Serieslist.push_back(tr("DESCRIPTION"));
168         Serieslist.push_back(tr("no. accept."));
169         Seriesmodel->setHorizontalHeaderLabels(Serieslist);
170         ui.seriesTreeView->setModel(Seriesmodel);
171         connect(ui.seriesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImages(QModelIndex)));
172
173         // Images Tree View
174         Imagesmodel = new QStandardItemModel(0,1,this); 
175         QStringList Imageslist;
176         Imageslist.push_back(tr("instance number"));
177         Imageslist.push_back(tr("sopuid"));
178         Imagesmodel->setHorizontalHeaderLabels(Imageslist);
179         ui.imagesTreeView->setModel(Imagesmodel);
180 }
181
182 // clean the different model Trees
183 void vvQPacsConnection::cleanTree()
184 {
185         Patientmodel->removeRows(0,Patientmodel->rowCount());
186         Studymodel->removeRows(0,Patientmodel->rowCount());
187         Seriesmodel->removeRows(0,Patientmodel->rowCount());
188         Imagesmodel->removeRows(0,Patientmodel->rowCount());
189
190 }
191
192 void vvQPacsConnection::selectStudies(const QModelIndex &index)
193 {
194
195                         
196         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
197         QVariant elt= Patientmodel->data(index.sibling(index.row(),1));
198         m_patient=elt.toString().toStdString();
199         manageSeriesFilter(true);
200         
201         std::vector<gdcm::DataSet> theDataSet;
202                  m_query = getQueryPatient("",m_patient);
203          f_query = getQueryforStudy(m_patient, false);
204                  
205                  
206         if (  gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
207                 gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys)
208                 , theDataSet, getDicomClientAETitle().c_str(), m_nickname.c_str()) )
209         {
210                 convertDataSet(theDataSet, Studymodel, getQueryKeysforStudy("",true));
211         }
212 }
213
214
215
216
217 void vvQPacsConnection::selectSeries(const QModelIndex &index)
218 {
219         m_level =gdcm::eSeries;
220         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
221         QVariant elt= Studymodel->data(index.sibling(index.row(),3));
222         QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
223
224
225         std::vector<gdcm::DataSet> theDataSet;
226         
227          m_query = getQueryforSeries(elt.toString().toStdString(),elt2.toString().toStdString(), false);
228         f_query =getQueryforSeries(elt.toString().toStdString(),elt2.toString().toStdString(), false);
229         if ( gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
230                 gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys), theDataSet, 
231                  getDicomClientAETitle().c_str(), m_nickname.c_str()))
232         {
233         
234                 convertDataSet(theDataSet, Seriesmodel, getSeriesKeys("","",true));
235         }
236 }
237
238 void vvQPacsConnection::selectImages(const QModelIndex &index)
239 {
240         m_level = gdcm::eImage;
241         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
242         QVariant elt3= Seriesmodel->data(index.sibling(index.row(),2));
243         QVariant elt2= Studymodel->data(index.sibling(ui.studyTreeView->selectionModel()->selectedRows().first().row(),3));
244         QVariant elt= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
245
246         //manageImagesFilter(true);
247         
248         std::vector<gdcm::DataSet> theDataSet;
249         std::vector< std::pair<gdcm::Tag, std::string> > keys;
250         f_query =getQueryforImages(elt.toString().toStdString(),elt2.toString().toStdString(), elt3.toString().toStdString(),false);
251                 m_query =getQueryforImages(elt.toString().toStdString(),elt2.toString().toStdString(), elt3.toString().toStdString(),false);
252  if(    gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
253                 gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys), theDataSet,  
254                 getDicomClientAETitle().c_str(), m_nickname.c_str()))
255  {
256         convertDataSet(theDataSet, Imagesmodel, getQueryKeysforImages("","","",true));
257  }
258 }
259
260
261 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
262 {
263         ui.text_PHYS->setEnabled(i_enable);
264         ui.text_SDESC->setEnabled(i_enable);
265         ui.dateTab->setEnabled(i_enable);
266
267 }
268
269 void vvQPacsConnection::manageSeriesFilter(bool i_enable)
270 {
271         ui.modalityTab->setEnabled(i_enable);
272 }
273
274 vvQuery vvQPacsConnection::getQueryPatient(const std::string i_patname, const std::string i_patid)
275 {
276         vvQuery query;
277         query.theRoot = gdcm::ePatientRootType;
278         query.theLevel = gdcm::ePatient;
279         query.keys = getPatientKeys(i_patname, i_patid);
280         return query;
281
282 }
283
284 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getPatientKeys(const std::string i_patname, const std::string i_patid)
285 {
286         std::vector< std::pair<gdcm::Tag, std::string> > keys;
287         // Patient Name
288         gdcm::Tag tag(0x0010,0x0010);
289         keys.push_back(std::make_pair(tag, i_patname));
290
291         //// Patient ID
292         gdcm::Tag tagpid(0x0010,0x0020);
293         keys.push_back(std::make_pair(tagpid, i_patid));
294         return keys;
295 }
296
297 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
298 {
299         std::vector< std::pair<gdcm::Tag, std::string> > keys;
300         // Study Description
301         gdcm::Tag tagsdc(0x0008,0x1030);
302         keys.push_back(std::make_pair(tagsdc, ""));
303         // Study date
304         gdcm::Tag tagdb(0x0008,0x0020);
305         keys.push_back(std::make_pair(tagdb, ""));
306         // Study Hour
307         gdcm::Tag tagsdh(0x0008,0x0030);
308         keys.push_back(std::make_pair(tagsdh, ""));
309         // Study Instance UID
310         gdcm::Tag tagsid(0x0020,0x000d);
311         keys.push_back(std::make_pair(tagsid, i_val));
312
313         return keys;
314 }
315
316 vvQuery vvQPacsConnection::getQueryforSeries(const std::string patient_id, const std::string series_id, bool bdisplay)
317 {
318         vvQuery query;
319         query.theRoot = gdcm::ePatientRootType;
320         query.theLevel = gdcm::eSeries;
321         query.keys = getSeriesKeys(patient_id, series_id, bdisplay);
322         return query;
323 }
324 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getSeriesKeys(const std::string patient_id, const std::string study_id, bool bdisplay)
325 {
326         std::vector< std::pair<gdcm::Tag, std::string> > keys;
327         // Modality
328   keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), ""));
329         // Study date
330         
331         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x103e),""));
332 // Series Instance UID
333         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), ""));
334
335
336
337         if(!bdisplay)
338         {
339                 // Study Instance UID
340                 gdcm::Tag tagsid(0x0020,0x1209);
341                 keys.push_back(std::make_pair(tagsid, study_id));
342         }
343
344         return keys;
345 }
346 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
347 {
348         std::vector< std::pair<gdcm::Tag, std::string> > keys;
349         // Patient Name
350         gdcm::Tag tag(0x0010,0x0010);
351         keys.push_back(std::make_pair(tag, ""));
352
353         //// Patient ID
354         gdcm::Tag tagpid(0x0010,0x0020);
355         keys.push_back(std::make_pair(tagpid, ""));
356
357         // Modality
358         gdcm::Tag tagmod(0x0008,0x0061);
359         keys.push_back(std::make_pair(tagmod, ""));
360
361         // date of birth
362         gdcm::Tag tagdb(0x0010,0x0030);
363         keys.push_back(std::make_pair(tagdb, ""));
364
365         // Study Date
366         gdcm::Tag tagsd(0x0020,0x000D);
367         keys.push_back(std::make_pair(tagsd, ""));
368
369         //// Study Time
370         //gdcm::Tag tagst(8,30);
371         //keys.push_back(std::make_pair(tagst, ""));
372
373         //// Study Description
374         //gdcm::Tag tagsdc(8,1030);
375         //keys.push_back(std::make_pair(tagsdc, ""));
376
377         //// Accession n°
378         //gdcm::Tag tagacc(8,50);
379         //keys.push_back(std::make_pair(tagacc, ""));
380
381         return keys;
382 }
383
384 void vvQPacsConnection::on_check_ModAll_clicked(bool state)
385 {
386         ui.check_MR->setEnabled(!state);
387         ui.check_CR->setEnabled(!state);
388         ui.check_OT->setEnabled(!state);
389         ui.check_RF->setEnabled(!state);
390         ui.check_SC->setEnabled(!state);
391         ui.check_CT->setEnabled(!state);
392         ui.check_US->setEnabled(!state);
393         ui.check_NM->setEnabled(!state);
394         ui.check_DR->setEnabled(!state);
395         ui.check_US->setEnabled(!state);
396         ui.check_NM->setEnabled(!state);
397         ui.check_DR->setEnabled(!state);
398         ui.check_SR->setEnabled(!state);
399         ui.check_XA->setEnabled(!state);
400         ui.check_MG->setEnabled(!state);
401         if(state)
402         {
403                 ui.check_MR->setChecked(state);
404                 ui.check_CR->setChecked(state);
405                 ui.check_OT->setChecked(state);
406                 ui.check_RF->setChecked(state);
407                 ui.check_SC->setChecked(state);
408                 ui.check_CT->setChecked(state);
409                 ui.check_US->setChecked(state);
410                 ui.check_NM->setChecked(state);
411                 ui.check_DR->setChecked(state);
412                 ui.check_US->setChecked(state);
413                 ui.check_NM->setChecked(state);
414                 ui.check_DR->setChecked(state);
415                 ui.check_SR->setChecked(state);
416                 ui.check_XA->setChecked(state);
417                 ui.check_MG->setChecked(state);
418         }
419
420 }
421
422 void vvQPacsConnection::chooseServer(int index)
423 {
424         std::map < std::string, std:: string> values = getDicomServer(ui.networkCombo->currentText());
425         m_port = values["PORT"];
426         m_aetitle = values["AETITLE"];
427         m_adress= values["ADRESS"];
428         m_nickname = values["nickname"];
429         ui.AdressEdit->setText(QString(m_adress.c_str()));
430         ui.AETitleEdit->setText(QString(m_aetitle.c_str()));
431         ui.NameEdit->setText(QString(m_nickname.c_str()));
432         ui.PortEdit->setText(QString(m_port.c_str()));
433 }
434
435 void vvQPacsConnection::on_importButton_clicked()
436         {
437         
438                 bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()),
439                         gdcm::CompositeNetworkFunctions::ConstructQuery(m_query.theRoot, m_query.theLevel ,m_query.keys,true), getDicomClientPort(),
440                          getDicomClientAETitle().c_str(), m_aetitle.c_str(),"D:\\move" );
441                 gdcm::Directory theDir;
442                 theDir.Load("D:\\move");
443            m_files =    theDir.GetFilenames();
444            accept();
445         }
446
447 std::vector <std::string> vvQPacsConnection::getFileNames()
448 {
449         std::vector <std::string> filenames;
450         gdcm::Directory::FilenamesType::iterator it = m_files.begin();
451         for (;it != m_files.end(); it++)
452                 filenames.push_back(it->c_str());
453         return filenames;
454 }
455 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::fillMoveKeys()
456 {
457         std::vector< std::pair<gdcm::Tag, std::string> > keys;
458         switch(m_level)
459         {
460         case gdcm::ePatient:
461                         //keys.push_back(getPatientKeys("",""));
462                         break;
463         }
464
465         return keys;
466 }
467
468 vvQuery vvQPacsConnection::getQueryforStudy(const  std::string patient_id, bool bdisplay)
469 {
470         vvQuery query;
471         query.keys = getQueryKeysforStudy( patient_id, bdisplay);
472         query.theRoot = gdcm::ePatientRootType;
473         query.theLevel = gdcm::eStudy;
474         return query;
475 }
476
477 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getQueryKeysforStudy(const std::string patient_id, bool bdisplay)
478 {
479         std::vector< std::pair<gdcm::Tag, std::string> > keys;
480
481
482         // Study Description
483         gdcm::Tag tagsdc(0x0008,0x1030);
484         keys.push_back(std::make_pair(tagsdc, ""));
485         // Study date
486         gdcm::Tag tagdb(0x0008,0x0020);
487         keys.push_back(std::make_pair(tagdb, ""));
488         // Study Hour
489         gdcm::Tag tagsdh(0x0008,0x0030);
490         keys.push_back(std::make_pair(tagsdh, ""));
491
492         // Study UID
493         gdcm::Tag tagsid(0x020,0x000d);
494         keys.push_back(std::make_pair(tagsid, ""));
495
496         if (!bdisplay)
497         {
498                         // Patient ID
499         gdcm::Tag tagsd(0x0010,0x0020);
500         keys.push_back(std::make_pair(tagsd, patient_id));
501         }
502
503         return keys;
504 }
505
506 vvQuery vvQPacsConnection::getQueryforImages(const std::string patient_id, const std::string study_id, const std::string series_id,bool bdisplay)
507 {
508         vvQuery query;
509         query.keys = getQueryKeysforImages( patient_id, study_id, series_id, bdisplay);
510         query.theRoot = gdcm::ePatientRootType;
511         query.theLevel = gdcm::eImage;
512         return query;
513 }
514
515 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getQueryKeysforImages(const std::string patient_id, const std::string study_id, const std::string series_id,bool bdisplay)
516 {
517
518         std::vector< std::pair<gdcm::Tag, std::string> > keys;
519
520         if (!bdisplay)
521         {
522                 //Patient UID
523                 keys.push_back(std::make_pair(gdcm::Tag (0x0010,0x0020), patient_id));
524
525                 //Study UID
526         //      keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000d), study_id));
527         
528                 //Series UID
529                 keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), series_id));
530         }
531         // Image Description
532
533         keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x0013), ""));
534         //SOP Instance UID
535         keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0018), ""));
536         return keys;
537 }