]> Creatis software - clitk.git/blob - vv/vvQPacsConnection.cxx
Added missing include
[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 #include <algorithm>
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 = mQFactory.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,mQFactory.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                 connect(ui.imagesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImage(QModelIndex)));
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                 m_patient= Patientmodel->data(index.sibling(index.row(),1)).toString().toStdString();           
196         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
197         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
198         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
199         manageSeriesFilter(true);
200         convertDataSet( findQuery( mQFactory.getQueryforStudy(m_patient, false)) , Studymodel, mQFactory.getQueryKeysforStudy("",true));
201 }
202
203
204 void vvQPacsConnection::clearMove()
205 {
206
207 }
208
209 void vvQPacsConnection::selectSeries(const QModelIndex &index)
210 {
211         m_study= Studymodel->data(index.sibling(index.row(),3)).toString().toStdString();
212         Seriesmodel->removeRows(0, Seriesmodel->rowCount());
213         Imagesmodel->removeRows(0, Imagesmodel->rowCount());
214     convertDataSet( findQuery( mQFactory.getQueryforSeries(m_patient,m_study, false)), Seriesmodel, mQFactory.getSeriesKeys("","",true));
215         
216 }
217
218 void vvQPacsConnection::selectImages(const QModelIndex &index)
219 {
220         m_series = Seriesmodel->data(index.sibling(index.row(),2)).toString().toStdString();
221         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
222         convertDataSet( findQuery( mQFactory.getQueryforImages(m_patient,m_study, m_series, false) ),  Imagesmodel, mQFactory.getQueryKeysforImages("","","",true));
223  
224 }
225
226 void vvQPacsConnection::selectImage(const QModelIndex &index)
227 {
228         std::string _image = Imagesmodel->data(index.sibling(index.row(),1)).toString().toStdString();
229         mQFactory.setQueryforImage(m_patient,m_study, m_series, _image);
230         
231 }
232
233
234 std::vector<gdcm::DataSet> vvQPacsConnection::findQuery(vvQuery i_query)
235 {
236            std::vector<gdcm::DataSet> theDataSet;
237            gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
238                 gdcm::CompositeNetworkFunctions::ConstructQuery(i_query.theRoot, i_query.theLevel,i_query.keys), theDataSet,  
239                 getDicomClientAETitle().c_str(), m_nickname.c_str());
240         return theDataSet;
241 }
242
243 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
244 {
245         ui.text_PHYS->setEnabled(i_enable);
246         ui.text_SDESC->setEnabled(i_enable);
247         ui.dateTab->setEnabled(i_enable);
248
249 }
250
251 void vvQPacsConnection::manageSeriesFilter(bool i_enable)
252 {
253         ui.modalityTab->setEnabled(i_enable);
254 }
255
256
257 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
258 {
259         std::vector< std::pair<gdcm::Tag, std::string> > keys;
260         // Study Description
261         gdcm::Tag tagsdc(0x0008,0x1030);
262         keys.push_back(std::make_pair(tagsdc, ""));
263         // Study date
264         gdcm::Tag tagdb(0x0008,0x0020);
265         keys.push_back(std::make_pair(tagdb, ""));
266         // Study Hour
267         gdcm::Tag tagsdh(0x0008,0x0030);
268         keys.push_back(std::make_pair(tagsdh, ""));
269         // Study Instance UID
270         gdcm::Tag tagsid(0x0020,0x000d);
271         keys.push_back(std::make_pair(tagsid, i_val));
272
273         return keys;
274 }
275
276 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
277 {
278         std::vector< std::pair<gdcm::Tag, std::string> > keys;
279         // Patient Name
280         gdcm::Tag tag(0x0010,0x0010);
281         keys.push_back(std::make_pair(tag, ""));
282
283         //// Patient ID
284         gdcm::Tag tagpid(0x0010,0x0020);
285         keys.push_back(std::make_pair(tagpid, ""));
286
287         // Modality
288         gdcm::Tag tagmod(0x0008,0x0061);
289         keys.push_back(std::make_pair(tagmod, ""));
290
291         // date of birth
292         gdcm::Tag tagdb(0x0010,0x0030);
293         keys.push_back(std::make_pair(tagdb, ""));
294
295         // Study Date
296         gdcm::Tag tagsd(0x0020,0x000D);
297         keys.push_back(std::make_pair(tagsd, ""));
298
299         //// Study Time
300         //gdcm::Tag tagst(8,30);
301         //keys.push_back(std::make_pair(tagst, ""));
302
303         //// Study Description
304         //gdcm::Tag tagsdc(8,1030);
305         //keys.push_back(std::make_pair(tagsdc, ""));
306
307         //// Accession n°
308         //gdcm::Tag tagacc(8,50);
309         //keys.push_back(std::make_pair(tagacc, ""));
310
311         return keys;
312 }
313
314 void vvQPacsConnection::on_check_ModAll_clicked(bool state)
315 {
316         ui.check_MR->setEnabled(!state);
317         ui.check_CR->setEnabled(!state);
318         ui.check_OT->setEnabled(!state);
319         ui.check_RF->setEnabled(!state);
320         ui.check_SC->setEnabled(!state);
321         ui.check_CT->setEnabled(!state);
322         ui.check_US->setEnabled(!state);
323         ui.check_NM->setEnabled(!state);
324         ui.check_DR->setEnabled(!state);
325         ui.check_US->setEnabled(!state);
326         ui.check_NM->setEnabled(!state);
327         ui.check_DR->setEnabled(!state);
328         ui.check_SR->setEnabled(!state);
329         ui.check_XA->setEnabled(!state);
330         ui.check_MG->setEnabled(!state);
331         if(state)
332         {
333                 ui.check_MR->setChecked(state);
334                 ui.check_CR->setChecked(state);
335                 ui.check_OT->setChecked(state);
336                 ui.check_RF->setChecked(state);
337                 ui.check_SC->setChecked(state);
338                 ui.check_CT->setChecked(state);
339                 ui.check_US->setChecked(state);
340                 ui.check_NM->setChecked(state);
341                 ui.check_DR->setChecked(state);
342                 ui.check_US->setChecked(state);
343                 ui.check_NM->setChecked(state);
344                 ui.check_DR->setChecked(state);
345                 ui.check_SR->setChecked(state);
346                 ui.check_XA->setChecked(state);
347                 ui.check_MG->setChecked(state);
348         }
349
350 }
351
352 void vvQPacsConnection::chooseServer(int index)
353 {
354         std::map < std::string, std:: string> values = getDicomServer(ui.networkCombo->currentText());
355         m_port = values["PORT"];
356         m_aetitle = values["AETITLE"];
357         m_adress= values["ADRESS"];
358         m_nickname = values["nickname"];
359         ui.AdressEdit->setText(QString(m_adress.c_str()));
360         ui.AETitleEdit->setText(QString(m_aetitle.c_str()));
361         ui.NameEdit->setText(QString(m_nickname.c_str()));
362         ui.PortEdit->setText(QString(m_port.c_str()));
363 }
364
365 void vvQPacsConnection::on_importButton_clicked()
366         {
367                 setCursor(QCursor(Qt::WaitCursor));
368                 QString path = QDir::homePath() +QString::fromStdString("/.move");
369                 QDir dirpath (path);
370                 if (dirpath.exists())
371                 {
372                         QFileInfoList list = dirpath.entryInfoList( QDir::Files);
373                         QFileInfoList::iterator it_file = dirpath.entryInfoList( QDir::Files).begin();
374                         for(int i = 0; i < list.length() ; i++)
375                         {
376                                         QFile::remove(list.at(i).filePath());
377                         }
378                 }
379                 else
380                 {
381                         dirpath.mkdir(path);
382                 }
383                 bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()),
384                         gdcm::CompositeNetworkFunctions::ConstructQuery(mQFactory.getMoveQuery().theRoot, mQFactory.getMoveQuery().theLevel ,mQFactory.getMoveQuery().keys,true),
385                         getDicomClientPort(),  getDicomClientAETitle().c_str(), m_aetitle.c_str(), path.toStdString().c_str() );
386                 gdcm::Directory theDir;
387                 theDir.Load(path.toStdString().c_str());
388            m_files =    theDir.GetFilenames();
389                 std::sort (m_files.begin(), m_files.end()); // make sure names are in lexicographical order
390            
391            accept();
392           setCursor(QCursor(Qt::ArrowCursor));
393         }
394
395
396
397 std::vector <std::string> vvQPacsConnection::getFileNames()
398 {
399         std::vector <std::string> filenames;
400         gdcm::Directory::FilenamesType::iterator it = m_files.begin();
401         for (;it != m_files.end(); it++)
402                 filenames.push_back(it->c_str());
403         return filenames;
404 }
405 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::fillMoveKeys()
406 {
407         std::vector< std::pair<gdcm::Tag, std::string> > keys;
408         switch(m_level)
409         {
410         case gdcm::ePatient:
411                         //keys.push_back(getPatientKeys("",""));
412                         break;
413         }
414
415         return keys;
416 }