]> Creatis software - clitk.git/blob - vv/vvQPacsConnection.cxx
89ac8412bc37a8a160c788473f2e9af3760b38dd
[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 #include <itkGDCMImageIO.h>
12 #include <itkGDCMSeriesFileNames.h>
13
14
15 vvQPacsConnection::vvQPacsConnection(QWidget *i_parent)
16         :QDialog(i_parent)
17 {
18         ui.setupUi(this);
19         setWindowTitle(QString::fromUtf8("PACS CONNECTIONHHHH"));
20         createTreeView();
21         ui.tabFilter->setTabText(0,QString(tr("Modality")));
22         ui.tabFilter->setTabText(1,QString(tr("Date")));
23
24         ui. tabNetwork->setTabText(0,QString(tr("Network")));
25         ui. tabNetwork->setTabText(1,QString(tr("Configuration")));
26         ui.check_ModAll->setEnabled(true);
27         ui.networkCombo->addItem("");
28         ui.networkCombo->addItems(getDicomServers());
29         
30         // Connection   
31         connect(ui.networkCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(chooseServer(int)));
32         connect(ui.removeNetworkButton,SIGNAL(clicked()),this,SLOT(removeServer()));
33         connect(ui.NetworkButton,SIGNAL(clicked()),this,SLOT(modifyServer()));
34         
35         update();
36 }
37
38 // remote a Dicom Server in VV settings
39 void vvQPacsConnection::removeServer()
40 {
41         removeDicomServer(m_nickname);
42         ui.networkCombo->removeItem(ui.networkCombo->findText(QString(m_nickname.c_str())));
43         m_nickname="";
44         refreshNetworks();
45 }
46
47 // modify a Dicom Server in VV settings
48 void vvQPacsConnection::modifyServer()
49 {
50         AddDicomServer(ui.NameEdit->text().toStdString(),ui.AETitleEdit->text().toStdString(),ui.AdressEdit->text().toStdString(),ui.PortEdit->text().toStdString());
51         removeServer();
52 }
53
54 // refresh the list of Dicom Servers available from VV settings
55 void vvQPacsConnection::refreshNetworks()
56 {
57         ui.networkCombo->clear();
58         ui.networkCombo->addItem(QString());
59         ui.networkCombo->addItems(getDicomServers());
60         ui.NameEdit->setText(QString());
61         ui.AETitleEdit->setText(QString());
62         ui.AdressEdit->setText(QString());
63         ui.PortEdit->setText(QString());
64         ui.tabNetwork->setCurrentIndex(0);
65 }
66
67 void vvQPacsConnection::on_clearButton_clicked()
68 {
69         Patientmodel->removeRows(0, Patientmodel->rowCount(),QModelIndex());
70         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
71         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
72         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
73 }
74
75 void vvQPacsConnection::on_scanButton_clicked()
76 {
77         cleanTree();
78         manageStudiesFilter(true);
79
80         // test first if echo works
81         bool didItWork = gdcm::CompositeNetworkFunctions::CEcho(m_adress.c_str(), atoi(m_port.c_str()), getDicomClientAETitle().c_str(), m_nickname.c_str() );
82         if (didItWork)
83         {
84                 m_level =gdcm::ePatient;
85                 std::vector<gdcm::DataSet> theDataSet;
86                 f_query = mQFactory.getQueryPatient(ui.patientName->toPlainText().toStdString(),        ui.patientID->toPlainText().toStdString());
87
88                 bool cfindWork = gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
89                         gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys),
90                         theDataSet, getDicomClientAETitle().c_str()     , m_nickname.c_str());
91                 if( cfindWork)
92                 {
93                         convertDataSet(theDataSet,Patientmodel,mQFactory.getPatientKeys("",""));
94                 } // end cfindwork
95         } // end didItwork
96 }
97
98
99 /// show Options DialogBox to set a new Dicom Server
100 void vvQPacsConnection::on_optionsButton_clicked()
101 {
102         vvPacsSettingsDialog *dg  = new vvPacsSettingsDialog(this);
103         dg->show();
104 }
105
106 void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandardItemModel *i_model, std::vector< std::pair<gdcm::Tag, std::string> > keys)
107 {
108
109         std::vector<gdcm::DataSet>::iterator it_ds = i_ds.begin();
110         for(; it_ds != i_ds.end(); it_ds++)
111         {
112                 QList<QStandardItem *> items;
113                 const gdcm::DataSet ds = (*it_ds);
114                 std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
115                 int ind = 0;
116                 for(; it_key != keys.end(); it_key++, ind++)
117                 {
118                         gdcm::DataElement de = ds.GetDataElement((*it_key).first);
119                         QStandardItem *item = new QStandardItem;
120                         const gdcm::ByteValue *bv = (de).GetByteValue();
121                         if( !de.IsEmpty() )
122                         {
123                                 std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
124                                 item->setText(tr(buffer.c_str()));
125                         }
126                         else
127                         {
128                                 item->setText(tr(""));
129                         }
130                         if(ind ==0)
131                         {
132                                 item->setCheckable(true);
133                         }
134                         items.push_back(item);
135                 }
136                 i_model->appendRow(items);
137         }
138 }
139
140 // TreeViews creation
141 void vvQPacsConnection::createTreeView()
142 {
143         // Patient Tree View
144         Patientmodel = new QStandardItemModel(0,2,this); 
145         QStringList Patientlist;
146         Patientlist.push_back(tr("PATIENT NAME"));
147         Patientlist.push_back(tr("PATIENT ID"));
148         Patientmodel->setHorizontalHeaderLabels(Patientlist);
149         ui.patientTreeView->setModel(Patientmodel);
150         ui.patientTreeView->setEnabled(true);
151         connect(ui.patientTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectStudies(QModelIndex)));
152
153         // Study Tree View
154         Studymodel = new QStandardItemModel(0,3,this); 
155         QStringList Studylist;
156         Studylist.push_back(tr("DESCRIPTION"));
157         Studylist.push_back(tr("DATE"));
158         Studylist.push_back(tr("HOUR"));
159         Studylist.push_back(tr("STUDY ID"));
160         Studymodel->setHorizontalHeaderLabels(Studylist);
161         ui.studyTreeView->setModel(Studymodel);
162         connect(ui.studyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectSeries(QModelIndex)));
163
164
165         // Series Tree View
166         Seriesmodel = new QStandardItemModel(0,2,this); 
167         QStringList Serieslist;
168         Serieslist.push_back(tr("MODALITY"));
169         Serieslist.push_back(tr("DESCRIPTION"));
170         Serieslist.push_back(tr("no. accept."));
171         Seriesmodel->setHorizontalHeaderLabels(Serieslist);
172         ui.seriesTreeView->setModel(Seriesmodel);
173         connect(ui.seriesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImages(QModelIndex)));
174                 connect(ui.imagesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImage(QModelIndex)));
175         // Images Tree View
176         Imagesmodel = new QStandardItemModel(0,1,this); 
177         QStringList Imageslist;
178         Imageslist.push_back(tr("instance number"));
179         Imageslist.push_back(tr("sopuid"));
180         Imagesmodel->setHorizontalHeaderLabels(Imageslist);
181         ui.imagesTreeView->setModel(Imagesmodel);
182 }
183
184 // clean the different model Trees
185 void vvQPacsConnection::cleanTree()
186 {
187         Patientmodel->removeRows(0,Patientmodel->rowCount());
188         Studymodel->removeRows(0,Patientmodel->rowCount());
189         Seriesmodel->removeRows(0,Patientmodel->rowCount());
190         Imagesmodel->removeRows(0,Patientmodel->rowCount());
191
192 }
193
194 void vvQPacsConnection::selectStudies(const QModelIndex &index)
195 {
196
197                 m_patient= Patientmodel->data(index.sibling(index.row(),1)).toString().toStdString();           
198         Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
199         Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
200         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
201         manageSeriesFilter(true);
202         convertDataSet( findQuery( mQFactory.getQueryforStudy(m_patient, false)) , Studymodel, mQFactory.getQueryKeysforStudy("",true));
203 }
204
205
206 void vvQPacsConnection::clearMove()
207 {
208
209 }
210
211 void vvQPacsConnection::selectSeries(const QModelIndex &index)
212 {
213         m_study= Studymodel->data(index.sibling(index.row(),3)).toString().toStdString();
214         Seriesmodel->removeRows(0, Seriesmodel->rowCount());
215         Imagesmodel->removeRows(0, Imagesmodel->rowCount());
216     convertDataSet( findQuery( mQFactory.getQueryforSeries(m_patient,m_study, false)), Seriesmodel, mQFactory.getSeriesKeys("","",true));
217         
218 }
219
220 void vvQPacsConnection::selectImages(const QModelIndex &index)
221 {
222         m_series = Seriesmodel->data(index.sibling(index.row(),2)).toString().toStdString();
223         Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
224         convertDataSet( findQuery( mQFactory.getQueryforImages(m_patient,m_study, m_series, false) ),  Imagesmodel, mQFactory.getQueryKeysforImages("","","",true));
225  
226 }
227
228 void vvQPacsConnection::selectImage(const QModelIndex &index)
229 {
230         std::string _image = Imagesmodel->data(index.sibling(index.row(),1)).toString().toStdString();
231         mQFactory.setQueryforImage(m_patient,m_study, m_series, _image);
232         
233 }
234
235
236 std::vector<gdcm::DataSet> vvQPacsConnection::findQuery(vvQuery i_query)
237 {
238            std::vector<gdcm::DataSet> theDataSet;
239            gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
240                 gdcm::CompositeNetworkFunctions::ConstructQuery(i_query.theRoot, i_query.theLevel,i_query.keys), theDataSet,  
241                 getDicomClientAETitle().c_str(), m_nickname.c_str());
242         return theDataSet;
243 }
244
245 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
246 {
247         ui.text_PHYS->setEnabled(i_enable);
248         ui.text_SDESC->setEnabled(i_enable);
249         ui.dateTab->setEnabled(i_enable);
250
251 }
252
253 void vvQPacsConnection::manageSeriesFilter(bool i_enable)
254 {
255         ui.modalityTab->setEnabled(i_enable);
256 }
257
258
259 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
260 {
261         std::vector< std::pair<gdcm::Tag, std::string> > keys;
262         // Study Description
263         gdcm::Tag tagsdc(0x0008,0x1030);
264         keys.push_back(std::make_pair(tagsdc, ""));
265         // Study date
266         gdcm::Tag tagdb(0x0008,0x0020);
267         keys.push_back(std::make_pair(tagdb, ""));
268         // Study Hour
269         gdcm::Tag tagsdh(0x0008,0x0030);
270         keys.push_back(std::make_pair(tagsdh, ""));
271         // Study Instance UID
272         gdcm::Tag tagsid(0x0020,0x000d);
273         keys.push_back(std::make_pair(tagsid, i_val));
274
275         return keys;
276 }
277
278 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
279 {
280         std::vector< std::pair<gdcm::Tag, std::string> > keys;
281         // Patient Name
282         gdcm::Tag tag(0x0010,0x0010);
283         keys.push_back(std::make_pair(tag, ""));
284
285         //// Patient ID
286         gdcm::Tag tagpid(0x0010,0x0020);
287         keys.push_back(std::make_pair(tagpid, ""));
288
289         // Modality
290         gdcm::Tag tagmod(0x0008,0x0061);
291         keys.push_back(std::make_pair(tagmod, ""));
292
293         // date of birth
294         gdcm::Tag tagdb(0x0010,0x0030);
295         keys.push_back(std::make_pair(tagdb, ""));
296
297         // Study Date
298         gdcm::Tag tagsd(0x0020,0x000D);
299         keys.push_back(std::make_pair(tagsd, ""));
300
301         //// Study Time
302         //gdcm::Tag tagst(8,30);
303         //keys.push_back(std::make_pair(tagst, ""));
304
305         //// Study Description
306         //gdcm::Tag tagsdc(8,1030);
307         //keys.push_back(std::make_pair(tagsdc, ""));
308
309         //// Accession
310         //gdcm::Tag tagacc(8,50);
311         //keys.push_back(std::make_pair(tagacc, ""));
312
313         return keys;
314 }
315
316 void vvQPacsConnection::on_check_ModAll_clicked(bool state)
317 {
318         ui.check_MR->setEnabled(!state);
319         ui.check_CR->setEnabled(!state);
320         ui.check_OT->setEnabled(!state);
321         ui.check_RF->setEnabled(!state);
322         ui.check_SC->setEnabled(!state);
323         ui.check_CT->setEnabled(!state);
324         ui.check_US->setEnabled(!state);
325         ui.check_NM->setEnabled(!state);
326         ui.check_DR->setEnabled(!state);
327         ui.check_US->setEnabled(!state);
328         ui.check_NM->setEnabled(!state);
329         ui.check_DR->setEnabled(!state);
330         ui.check_SR->setEnabled(!state);
331         ui.check_XA->setEnabled(!state);
332         ui.check_MG->setEnabled(!state);
333         if(state)
334         {
335                 ui.check_MR->setChecked(state);
336                 ui.check_CR->setChecked(state);
337                 ui.check_OT->setChecked(state);
338                 ui.check_RF->setChecked(state);
339                 ui.check_SC->setChecked(state);
340                 ui.check_CT->setChecked(state);
341                 ui.check_US->setChecked(state);
342                 ui.check_NM->setChecked(state);
343                 ui.check_DR->setChecked(state);
344                 ui.check_US->setChecked(state);
345                 ui.check_NM->setChecked(state);
346                 ui.check_DR->setChecked(state);
347                 ui.check_SR->setChecked(state);
348                 ui.check_XA->setChecked(state);
349                 ui.check_MG->setChecked(state);
350         }
351
352 }
353
354 void vvQPacsConnection::chooseServer(int index)
355 {
356         std::map < std::string, std:: string> values = getDicomServer(ui.networkCombo->currentText());
357         m_port = values["PORT"];
358         m_aetitle = values["AETITLE"];
359         m_adress= values["ADRESS"];
360         m_nickname = values["nickname"];
361         ui.AdressEdit->setText(QString(m_adress.c_str()));
362         ui.AETitleEdit->setText(QString(m_aetitle.c_str()));
363         ui.NameEdit->setText(QString(m_nickname.c_str()));
364         ui.PortEdit->setText(QString(m_port.c_str()));
365 }
366
367 void vvQPacsConnection::on_importButton_clicked()
368         {
369                 setCursor(QCursor(Qt::WaitCursor));
370                 QString path = QString::fromStdString(getCMoveDirectory());
371                 QDir dirpath (path);
372                 if (dirpath.exists())
373                 {
374                         QFileInfoList list = dirpath.entryInfoList( QDir::Files);
375                         QFileInfoList::iterator it_file = dirpath.entryInfoList( QDir::Files).begin();
376                         for(int i = 0; i < list.length() ; i++)
377                         {
378                                         QFile::remove(list.at(i).filePath());
379                         }
380                 }
381                 else
382                 {
383                         dirpath.mkdir(path);
384                 }
385                 bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()),
386                         gdcm::CompositeNetworkFunctions::ConstructQuery(mQFactory.getMoveQuery().theRoot, mQFactory.getMoveQuery().theLevel ,mQFactory.getMoveQuery().keys, gdcm::eMove),
387                         getDicomClientPort(),  getDicomClientAETitle().c_str(), m_aetitle.c_str(), path.toStdString().c_str() );
388                 gdcm::Directory theDir;
389                 theDir.Load(path.toStdString().c_str());
390      //m_files =        theDir.GetFilenames();
391
392      typedef itk::GDCMSeriesFileNames NamesGeneratorType;
393      NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
394      nameGenerator->SetUseSeriesDetails(true);
395
396      //ds gerer recursive moi-meme pour progress ...
397      nameGenerator->SetInputDirectory(path.toStdString());
398
399      // insert in table
400      typedef std::vector<std::string> SeriesIdContainer;
401      const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
402        std::map<std::string, std::vector<std::string>* > mListOfSeriesFilenames;
403
404
405            m_fileseries.clear();
406            
407      for (unsigned int i=0; i<seriesUID.size(); i++) {
408                 m_fileseries.push_back( nameGenerator->GetFileNames(seriesUID[i]));
409         }
410            
411            accept();
412           setCursor(QCursor(Qt::ArrowCursor));
413         }
414
415
416
417 std::vector <std::string> vvQPacsConnection::getFileNames(int i_series)
418 {
419         std::vector <std::string> filenames;
420         gdcm::Directory::FilenamesType::iterator it = m_fileseries[i_series].begin();
421         for (;it != m_fileseries[i_series].end(); it++)
422                 filenames.push_back(it->c_str());
423         return filenames;
424 }
425 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::fillMoveKeys()
426 {
427         std::vector< std::pair<gdcm::Tag, std::string> > keys;
428         switch(m_level)
429         {
430         case gdcm::ePatient:
431                         //keys.push_back(getPatientKeys("",""));
432                         break;
433         }
434
435         return keys;
436 }