]> Creatis software - clitk.git/blobdiff - vv/vvQPacsConnection.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / vv / vvQPacsConnection.cxx
index 8c5ebc007e851c018482befbc1ef29b2f49d8850..4ae0d33b9981bdddc7a31e98e2f2aa0b55c16b7f 100644 (file)
@@ -7,14 +7,16 @@
 #include <QDateTime>
 #include "vvPacsSettingsDialog.h"
 #include "vvUtils.h"
-
+#include <algorithm>
+#include <itkGDCMImageIO.h>
+#include <itkGDCMSeriesFileNames.h>
 
 
 vvQPacsConnection::vvQPacsConnection(QWidget *i_parent)
        :QDialog(i_parent)
 {
        ui.setupUi(this);
-       setWindowTitle(QString::fromUtf8("PACS CONNECTIONHHHH"));
+       setWindowTitle(QString::fromUtf8("PACS CONNECTION"));
        createTreeView();
        ui.tabFilter->setTabText(0,QString(tr("Modality")));
        ui.tabFilter->setTabText(1,QString(tr("Date")));
@@ -22,20 +24,26 @@ vvQPacsConnection::vvQPacsConnection(QWidget *i_parent)
        ui. tabNetwork->setTabText(0,QString(tr("Network")));
        ui. tabNetwork->setTabText(1,QString(tr("Configuration")));
        ui.check_ModAll->setEnabled(true);
-       //on_check_ModAll_clicked(true);
-       
-       
        ui.networkCombo->addItem("");
        ui.networkCombo->addItems(getDicomServers());
        
+       // Connection   
        connect(ui.networkCombo,SIGNAL(currentIndexChanged(int)),this,SLOT(chooseServer(int)));
-       //connect(ui.scanButton,SIGNAL(clicked()),this,SLOT(on_scanButton_clicked()));
-       //connect(ui.importButton, SIGNAL(clicked()), this, SLOT(on_importButton_clicked()));
        connect(ui.removeNetworkButton,SIGNAL(clicked()),this,SLOT(removeServer()));
        connect(ui.NetworkButton,SIGNAL(clicked()),this,SLOT(modifyServer()));
+       connect(ui.exitButton,SIGNAL(clicked()),this,SLOT(close()));
+       
        update();
 }
 
+// Exit window
+bool vvQPacsConnection::close()
+{
+    QApplication::restoreOverrideCursor();
+    return QWidget::close();
+}
+
+// remote a Dicom Server in VV settings
 void vvQPacsConnection::removeServer()
 {
        removeDicomServer(m_nickname);
@@ -44,13 +52,19 @@ void vvQPacsConnection::removeServer()
        refreshNetworks();
 }
 
+// modify a Dicom Server in VV settings
 void vvQPacsConnection::modifyServer()
 {
+       int indexCombo = ui.networkCombo->currentIndex();
+       removeDicomServer(m_nickname);
        AddDicomServer(ui.NameEdit->text().toStdString(),ui.AETitleEdit->text().toStdString(),ui.AdressEdit->text().toStdString(),ui.PortEdit->text().toStdString());
-       removeServer();
+       ui.networkCombo->clear();
+       ui.networkCombo->addItem(QString());
+       ui.networkCombo->addItems(getDicomServers());
+       ui.networkCombo->setCurrentIndex(indexCombo);
 }
 
-
+// refresh the list of Dicom Servers available from VV settings
 void vvQPacsConnection::refreshNetworks()
 {
        ui.networkCombo->clear();
@@ -73,56 +87,29 @@ void vvQPacsConnection::on_clearButton_clicked()
 
 void vvQPacsConnection::on_scanButton_clicked()
 {
-
+       cleanTree();
        manageStudiesFilter(true);
-       bool didItWork = gdcm::CompositeNetworkFunctions::CEcho(m_adress.c_str(), atoi(m_port.c_str()), "CREATIS", m_nickname.c_str() );
+
+       // test first if echo works
+       bool didItWork = gdcm::CompositeNetworkFunctions::CEcho(m_adress.c_str(), atoi(m_port.c_str()), getDicomClientAETitle().c_str(), m_nickname.c_str() );
        if (didItWork)
        {
-               std::vector< std::pair<gdcm::Tag, std::string> > keys = getKeys();
-
                m_level =gdcm::ePatient;
                std::vector<gdcm::DataSet> theDataSet;
-               gdcm::EQueryLevel theLevel = gdcm::ePatient;
-               gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
-               gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,getPatientKeys(""));
-               bool cfindWork = gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet,   "CREATIS", m_nickname.c_str());
+               f_query = mQFactory.getQueryPatient(ui.patientName->toPlainText().toStdString(),        ui.patientID->toPlainText().toStdString());
+
+               bool cfindWork = gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
+                       gdcm::CompositeNetworkFunctions::ConstructQuery(f_query.theRoot, f_query.theLevel ,f_query.keys),
+                       theDataSet, getDicomClientAETitle().c_str()     , m_nickname.c_str());
                if( cfindWork)
                {
-                       std::vector<gdcm::DataSet>::iterator it_ds = theDataSet.begin();
-
-                       for(; it_ds != theDataSet.end(); it_ds++)
-                       {
-                               QList<QStandardItem *> items;
-                               const gdcm::DataSet ds = (*it_ds);
-                               std::vector< std::pair<gdcm::Tag, std::string> >::iterator it_key = keys.begin();
-                               int ind = 0;
-                               for(; it_key != keys.end(); it_key++, ind++)
-                               {
-                                       gdcm::DataElement de = ds.GetDataElement((*it_key).first);
-                                       QStandardItem *item = new QStandardItem;
-                                       const gdcm::ByteValue *bv = (de).GetByteValue();
-                                       if( !de.IsEmpty() )
-                                       {
-                                               std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
-                                               item->setText(tr(buffer.c_str()));
-                                       }
-                                       else
-                                       {
-                                               item->setText(tr(""));
-                                       }
-                                       if(ind ==0)
-                                       {
-                                               item->setCheckable(true);
-                                       }
-                                       items.push_back(item);
-                               }
-                               Patientmodel->appendRow(items);
-                       }
+                       convertDataSet(theDataSet,Patientmodel,mQFactory.getPatientKeys("",""));
                } // end cfindwork
        } // end didItwork
 }
 
 
+/// show Options DialogBox to set a new Dicom Server
 void vvQPacsConnection::on_optionsButton_clicked()
 {
        vvPacsSettingsDialog *dg  = new vvPacsSettingsDialog(this);
@@ -131,7 +118,7 @@ void vvQPacsConnection::on_optionsButton_clicked()
 
 void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandardItemModel *i_model, std::vector< std::pair<gdcm::Tag, std::string> > keys)
 {
-       gdcm::Tag tagdb(0x0008,0x0020);
+
        std::vector<gdcm::DataSet>::iterator it_ds = i_ds.begin();
        for(; it_ds != i_ds.end(); it_ds++)
        {
@@ -147,16 +134,7 @@ void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandar
                        if( !de.IsEmpty() )
                        {
                                std::string buffer = std::string( bv->GetPointer(), bv->GetLength() );
-                               /*if((*it_key).first == tagdb)
-                               {
-                               QDate date;
-                               date.fromString(tr(buffer.c_str()),"yyyy'MM'd");
-                               item->setText(date.toString());
-                               }
-                               else
-                               {*/
                                item->setText(tr(buffer.c_str()));
-                               //}
                        }
                        else
                        {
@@ -170,7 +148,6 @@ void vvQPacsConnection::convertDataSet(std::vector<gdcm::DataSet> i_ds, QStandar
                }
                i_model->appendRow(items);
        }
-
 }
 
 // TreeViews creation
@@ -192,6 +169,7 @@ void vvQPacsConnection::createTreeView()
        Studylist.push_back(tr("DESCRIPTION"));
        Studylist.push_back(tr("DATE"));
        Studylist.push_back(tr("HOUR"));
+       Studylist.push_back(tr("STUDY ID"));
        Studymodel->setHorizontalHeaderLabels(Studylist);
        ui.studyTreeView->setModel(Studymodel);
        connect(ui.studyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectSeries(QModelIndex)));
@@ -206,7 +184,7 @@ void vvQPacsConnection::createTreeView()
        Seriesmodel->setHorizontalHeaderLabels(Serieslist);
        ui.seriesTreeView->setModel(Seriesmodel);
        connect(ui.seriesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImages(QModelIndex)));
-
+               connect(ui.imagesTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(selectImage(QModelIndex)));
        // Images Tree View
        Imagesmodel = new QStandardItemModel(0,1,this); 
        QStringList Imageslist;
@@ -216,104 +194,67 @@ void vvQPacsConnection::createTreeView()
        ui.imagesTreeView->setModel(Imagesmodel);
 }
 
+// clean the different model Trees
+void vvQPacsConnection::cleanTree()
+{
+       Patientmodel->removeRows(0,Patientmodel->rowCount());
+       Studymodel->removeRows(0,Patientmodel->rowCount());
+       Seriesmodel->removeRows(0,Patientmodel->rowCount());
+       Imagesmodel->removeRows(0,Patientmodel->rowCount());
+
+}
+
 void vvQPacsConnection::selectStudies(const QModelIndex &index)
 {
-       m_level =gdcm::ePatient;
+
+               m_patient= Patientmodel->data(index.sibling(index.row(),1)).toString().toStdString();           
        Studymodel->removeRows(0, Studymodel->rowCount(),QModelIndex());
-       QVariant elt= Patientmodel->data(index.sibling(index.row(),1));
-       QVariant elt2= Patientmodel->data(index.sibling(index.row(),1));
-       m_patient=elt.toString().toStdString();
+       Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
+       Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
        manageSeriesFilter(true);
-       
-       std::vector<gdcm::DataSet> theDataSet;
-       if (  gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
-               mquery.composeQueryStudy(m_patient), theDataSet, "CREATIS", m_nickname.c_str()) )
-       {
-               convertDataSet(theDataSet, Studymodel, getStudyKeys(""));
-       }
+       convertDataSet( findQuery( mQFactory.getQueryforStudy(m_patient, false)) , Studymodel, mQFactory.getQueryKeysforStudy("",true));
 }
 
 
-void vvQPacsConnection::selectSeries(const QModelIndex &index)
+void vvQPacsConnection::clearMove()
 {
-       m_level =gdcm::eImage;
-       Seriesmodel->removeRows(0, Seriesmodel->rowCount(),QModelIndex());
-       QVariant elt= Studymodel->data(index.sibling(index.row(),3));
-       QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
-
-
-       std::vector<gdcm::DataSet> theDataSet;
-       std::vector< std::pair<gdcm::Tag, std::string> > keys;
-
-       //Patient Unique key
-       keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0020), m_patient));
-
-       //Study Unique Key
-       keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000d), elt.toString().toStdString()));
 
+}
 
-       // Modality
-       keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
-       // Description
-       keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
-       // Acceptance NUmber????
-       keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
+void vvQPacsConnection::selectSeries(const QModelIndex &index)
+{
+       m_study= Studymodel->data(index.sibling(index.row(),3)).toString().toStdString();
+       Seriesmodel->removeRows(0, Seriesmodel->rowCount());
+       Imagesmodel->removeRows(0, Imagesmodel->rowCount());
+    convertDataSet( findQuery( mQFactory.getQueryforSeries(m_patient,m_study, false)), Seriesmodel, mQFactory.getSeriesKeys("","",true));
        
-       if ( gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), mquery.composeQuerySeries(keys), theDataSet, "CREATIS", m_nickname.c_str()), "C:\\Boost")
-       {
-               keys.clear();
-       // Modality
-       keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
-       // Description
-       keys.push_back(std::make_pair(gdcm::Tag(0x0008,0x0060), elt.toString().toStdString()));
-       // Acceptance NUmber????
-       keys.push_back(std::make_pair(gdcm::Tag(0x0020,0x000e), elt.toString().toStdString()));
-               convertDataSet(theDataSet, Seriesmodel, getSeriesKeys(""));
-       }
 }
 
 void vvQPacsConnection::selectImages(const QModelIndex &index)
 {
-       m_level = gdcm::eImage;
+       m_series = Seriesmodel->data(index.sibling(index.row(),2)).toString().toStdString();
        Imagesmodel->removeRows(0, Imagesmodel->rowCount(),QModelIndex());
-       QVariant elt= Seriesmodel->data(index.sibling(index.row(),2));
-       QVariant elt2= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
-
-       //manageImagesFilter(true);
-       gdcm::EQueryLevel theLevel = gdcm::eImage;
-       gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
-       std::vector<gdcm::DataSet> theDataSet;
-       std::vector< std::pair<gdcm::Tag, std::string> > keys;
-
-       gdcm::Tag tagsd(0x0010,0x0020);
-       keys.push_back(std::make_pair(tagsd, m_patient));
-
-       gdcm::Tag tagss(0x0020,0x000e);
-       keys.push_back(std::make_pair(tagss, elt.toString().toStdString()));
-       //= getStudyKeys(elt.toString().toStdString());
-
-       // Image Description
-       gdcm::Tag tagsdc(0x0020,0x0013);
-       keys.push_back(std::make_pair(tagsdc, ""));
-               gdcm::Tag tagsic(0x0008,0x0018);
-
-
-       keys.push_back(std::make_pair(tagsic, ""));
+       convertDataSet( findQuery( mQFactory.getQueryforImages(m_patient,m_study, m_series, false) ),  Imagesmodel, mQFactory.getQueryKeysforImages("","","",true));
+}
 
-       gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, theLevel ,keys);
-       keys.clear();
-       // SOP Instance UID 
+void vvQPacsConnection::selectImage(const QModelIndex &index)
+{
+       std::string _image = Imagesmodel->data(index.sibling(index.row(),1)).toString().toStdString();
+       mQFactory.setQueryforImage(m_patient,m_study, m_series, _image);
        
+}
 
 
-       keys.push_back(std::make_pair(tagsdc, ""));
-keys.push_back(std::make_pair(tagsic, ""));
-
-       gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), theQuery, theDataSet, "CREATIS", m_nickname.c_str());
-       convertDataSet(theDataSet, Imagesmodel, keys);
+std::vector<gdcm::DataSet> vvQPacsConnection::findQuery(vvQuery i_query)
+{
+          std::vector<gdcm::DataSet> theDataSet;
+          gdcm::CompositeNetworkFunctions::CFind(m_adress.c_str(), atoi(m_port.c_str()), 
+               gdcm::CompositeNetworkFunctions::ConstructQuery(i_query.theRoot, i_query.theLevel,i_query.keys), theDataSet,  
+               getDicomClientAETitle().c_str(), m_nickname.c_str());
+       return theDataSet;
 }
 
-
 void vvQPacsConnection::manageStudiesFilter(bool i_enable)
 {
        ui.text_PHYS->setEnabled(i_enable);
@@ -327,18 +268,6 @@ void vvQPacsConnection::manageSeriesFilter(bool i_enable)
        ui.modalityTab->setEnabled(i_enable);
 }
 
-std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getPatientKeys(const std::string i_val)
-{
-       std::vector< std::pair<gdcm::Tag, std::string> > keys;
-       // Patient Name
-       gdcm::Tag tag(0x0010,0x0010);
-       keys.push_back(std::make_pair(tag, ""));
-
-       //// Patient ID
-       gdcm::Tag tagpid(0x0010,0x0020);
-       keys.push_back(std::make_pair(tagpid, i_val));
-       return keys;
-}
 
 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys(const std::string i_val)
 {
@@ -359,25 +288,6 @@ std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getStudyKeys
        return keys;
 }
 
-
-std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getSeriesKeys(const std::string i_val)
-{
-       std::vector< std::pair<gdcm::Tag, std::string> > keys;
-       // Modality
-       gdcm::Tag tagsm(0x0008,0x0060);
-       keys.push_back(std::make_pair(tagsm, ""));
-       // Study date
-       gdcm::Tag tagdb(0x0008,0x103e);
-       keys.push_back(std::make_pair(tagdb, ""));
-       // Study Hour
-       gdcm::Tag tagsdh(0x0020,0x000e);
-       keys.push_back(std::make_pair(tagsdh, ""));
-       // Study Instance UID
-       gdcm::Tag tagsid(0x0020,0x1209);
-       keys.push_back(std::make_pair(tagsid, i_val));
-
-       return keys;
-}
 std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
 {
        std::vector< std::pair<gdcm::Tag, std::string> > keys;
@@ -409,7 +319,7 @@ std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::getKeys()
        //gdcm::Tag tagsdc(8,1030);
        //keys.push_back(std::make_pair(tagsdc, ""));
 
-       //// Accession n°
+       //// Accession
        //gdcm::Tag tagacc(8,50);
        //keys.push_back(std::make_pair(tagacc, ""));
 
@@ -469,44 +379,59 @@ void vvQPacsConnection::chooseServer(int index)
 
 void vvQPacsConnection::on_importButton_clicked()
        {
-               int sel = 0;
-               //QModelIndexList list = ui.imagesTreeView-selectedIndexes();
-               QModelIndexList indices =  ui.imagesTreeView->selectionModel()->selectedRows(); 
-               QModelIndexList::iterator it = indices.begin();
-               for(; it != indices.end(); it++)
-                       sel = it->row();
-               QModelIndex index;
-               QVariant elt= Patientmodel->data(index.sibling(ui.patientTreeView->selectionModel()->selectedRows().first().row(),1));
-               
-
-               gdcm::ERootType theRoot  = gdcm::ePatientRootType;//ePatientRootType;
-               std::vector<gdcm::DataSet> theDataSet;
-               std::vector< std::pair<gdcm::Tag, std::string> > keys;
-
-
-               // Study Description
-       //      gdcm::Tag tagsdc(0x0010,0x0020);
-               //keys.push_back(std::make_pair(tagsdc, elt.toString().toStdString()));
-
-       
-       // Study Description
-
-
-               gdcm::SmartPointer<gdcm::BaseRootQuery> theQuery =  gdcm::CompositeNetworkFunctions::ConstructQuery(theRoot, m_level ,fillMoveKeys(), true);
-
-               bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()), theQuery, 1234,
-       "VVCREATIS", m_aetitle.c_str(),"D:\\move" );
-                gdcm::Directory theDir;
-               theDir.Load("D:\\move");
-          m_files =    theDir.GetFilenames();
+               setCursor(QCursor(Qt::WaitCursor));
+               QString path = QString::fromStdString(getCMoveDirectory());
+               QDir dirpath (path);
+               if (dirpath.exists())
+               {
+                       QFileInfoList list = dirpath.entryInfoList( QDir::Files);
+                       QFileInfoList::iterator it_file = dirpath.entryInfoList( QDir::Files).begin();
+                       for(int i = 0; i < list.length() ; i++)
+                       {
+                                       QFile::remove(list.at(i).filePath());
+                       }
+               }
+               else
+               {
+                       dirpath.mkdir(path);
+               }
+               bool didItWork =  gdcm::CompositeNetworkFunctions::CMove(m_adress.c_str(),atoi(m_port.c_str()),
+                       gdcm::CompositeNetworkFunctions::ConstructQuery(mQFactory.getMoveQuery().theRoot, mQFactory.getMoveQuery().theLevel ,mQFactory.getMoveQuery().keys, gdcm::eMove),
+                       getDicomClientPort(),  getDicomClientAETitle().c_str(), m_aetitle.c_str(), path.toStdString().c_str() );
+               gdcm::Directory theDir;
+               theDir.Load(path.toStdString().c_str());
+     //m_files =       theDir.GetFilenames();
+
+     typedef itk::GDCMSeriesFileNames NamesGeneratorType;
+     NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
+     nameGenerator->SetUseSeriesDetails(true);
+
+     //ds gerer recursive moi-meme pour progress ...
+     nameGenerator->SetInputDirectory(path.toStdString());
+
+     // insert in table
+     typedef std::vector<std::string> SeriesIdContainer;
+     const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
+       std::map<std::string, std::vector<std::string>* > mListOfSeriesFilenames;
+
+
+          m_fileseries.clear();
+          
+     for (unsigned int i=0; i<seriesUID.size(); i++) {
+               m_fileseries.push_back( nameGenerator->GetFileNames(seriesUID[i]));
+        }
+          
           accept();
+         setCursor(QCursor(Qt::ArrowCursor));
        }
 
-std::vector <std::string> vvQPacsConnection::getFileNames()
+
+
+std::vector <std::string> vvQPacsConnection::getFileNames(int i_series)
 {
        std::vector <std::string> filenames;
-       gdcm::Directory::FilenamesType::iterator it = m_files.begin();
-       for (;it != m_files.end(); it++)
+       gdcm::Directory::FilenamesType::iterator it = m_fileseries[i_series].begin();
+       for (;it != m_fileseries[i_series].end(); it++)
                filenames.push_back(it->c_str());
        return filenames;
 }
@@ -516,10 +441,9 @@ std::vector< std::pair<gdcm::Tag, std::string> > vvQPacsConnection::fillMoveKeys
        switch(m_level)
        {
        case gdcm::ePatient:
-                       keys.push_back(std::make_pair(gdcm::Tag(0x0010,0x0020), m_patient));
-               //Patientmodel->data(index.sibling(index.row(),1)).toString().toStdString();
-               break;
+                       //keys.push_back(getPatientKeys("",""));
+                       break;
        }
 
        return keys;
-}
\ No newline at end of file
+}