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