]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/IO/DicomSeriesReader.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[cpPlugins.git] / lib / cpPlugins / Plugins / IO / DicomSeriesReader.cxx
1 #include "DicomSeriesReader.h"
2
3 #ifdef cpPlugins_Interface_QT4
4 #include <queue>
5 #include <map>
6 #include <vector>
7
8 #include <QDialogButtonBox>
9 #include <QDir>
10 #include <QFileDialog>
11 #include <QGridLayout>
12 #include <QLabel>
13 #include <QTreeWidget>
14 #include <QVBoxLayout>
15
16 #endif // cpPlugins_Interface_QT4
17
18 #include <itkGDCMSeriesFileNames.h>
19
20 // -------------------------------------------------------------------------
21 bool cpPlugins::IO::DicomSeriesReader::
22 ExecConfigurationDialog( QWidget* parent )
23 {
24   bool r = false;
25
26 #ifdef cpPlugins_Interface_QT4
27
28   // DICOM series analyzer
29   itk::GDCMSeriesFileNames::GlobalWarningDisplayOff( );
30   itk::GDCMSeriesFileNames::Pointer series =
31     itk::GDCMSeriesFileNames::New( );
32
33   typedef std::map< std::string, TStringList > _TSeries;
34   typedef std::map< std::string, _TSeries > _TFilenames;
35   _TSeries found_series;
36   _TFilenames found_filenames;
37
38   // Show dialog and check if it was accepted
39   QFileDialog dialog( parent );
40   dialog.setFileMode( QFileDialog::DirectoryOnly );
41   dialog.setDirectory( QFileDialog::tr( "." ) );
42   if( dialog.exec( ) )
43   {
44     std::string dir_name = dialog.selectedFiles( ).begin( )->toStdString( );
45     std::queue< std::string > q;
46     q.push( dir_name );
47     while( !( q.empty( ) ) )
48     {
49       dir_name = q.front( );
50       q.pop( );
51
52       // Get DICOM information
53       series->SetUseSeriesDetails( true );
54       series->AddSeriesRestriction( "0008|0021" );
55       series->SetDirectory( dir_name );
56       const TStringList& seriesUID = series->GetSeriesUIDs( );
57       if( seriesUID.size( ) > 0 )
58       {
59         TStringList::const_iterator sIt = seriesUID.begin( );
60         for( ; sIt != seriesUID.end( ); ++sIt )
61         {
62           TStringList filenames = series->GetFileNames( *sIt );
63           if( filenames.size( ) > 0 )
64           {
65             found_series[ dir_name ].push_back( *sIt );
66             found_filenames[ dir_name ][ *sIt ] = filenames;
67
68           } // fi
69
70         } // rof
71
72       } // fi
73
74       // Update queue
75       QDir dir( dir_name.c_str( ) );
76       QFileInfoList contents = dir.entryInfoList( );
77       QFileInfoList::const_iterator i = contents.begin( );
78       for( ; i != contents.end( ); ++i )
79       {
80         if( i->isDir( ) )
81         {
82           std::string new_dir_name = i->absoluteFilePath( ).toStdString( );
83           if( new_dir_name.size( ) > dir_name.size( ) )
84             q.push( new_dir_name );
85
86         } // fi
87
88       } // rof
89
90     } // elihw
91
92   } // fi
93
94   // Show second dialog
95   if( found_series.size( ) > 0 )
96   {
97     QDialog* tree_dialog = new QDialog( parent );
98
99     QLabel* title = new QLabel( tree_dialog );
100     title->setText( "Choose a DICOM series" );
101     QGridLayout* mainLayout = new QGridLayout( tree_dialog );
102     QVBoxLayout* toolsLayout = new QVBoxLayout( );
103     toolsLayout->addWidget( title );
104     mainLayout->addLayout( toolsLayout, 0, 0, 1, 1 );
105
106     QTreeWidget* tree = new QTreeWidget( tree_dialog );
107     QList< QTreeWidgetItem* > tree_items;
108
109     _TSeries::const_iterator fsIt = found_series.begin( );
110     for( ; fsIt != found_series.end( ); ++fsIt )
111     {
112       QTreeWidgetItem* new_item =
113         new QTreeWidgetItem(
114           ( QTreeWidgetItem* )( NULL ), QStringList( fsIt->first.c_str( ) )
115           );
116       TStringList::const_iterator sId = fsIt->second.begin( );
117       for( ; sId != fsIt->second.end( ); ++sId )
118       {
119         QTreeWidgetItem* new_leaf =
120           new QTreeWidgetItem( new_item, QStringList( sId->c_str( ) ) );
121         new_item->addChild( new_leaf );
122
123       } // rof
124       tree_items.append( new_item );
125
126     } // rof
127     tree->insertTopLevelItems( 0, tree_items );
128     toolsLayout->addWidget( tree );
129
130     QDialogButtonBox* bb = new QDialogButtonBox(
131       QDialogButtonBox::Ok | QDialogButtonBox::Cancel
132       );
133     tree_dialog->connect( bb, SIGNAL( accepted( ) ), tree_dialog, SLOT( accept( ) ) );
134     tree_dialog->connect( bb, SIGNAL( rejected( ) ), tree_dialog, SLOT( reject( ) ) );
135     toolsLayout->addWidget( bb );
136
137     if( tree_dialog->exec( ) == 1 )
138     {
139       QTreeWidgetItem* item = tree->currentItem( );
140       QTreeWidgetItem* parent = item->parent( );
141       if( parent != NULL )
142       {
143         std::string serie_dir = parent->text( 0 ).toStdString( );
144         std::string serie_id = item->text( 0 ).toStdString( );
145
146         this->m_Parameters->ClearStringList( "FileNames" );
147         const TStringList& filenames = found_filenames[ serie_dir ][ serie_id ];
148         for( unsigned int f = 0; f < filenames.size( ); ++f )
149           this->m_Parameters->AddToStringList( "FileNames", filenames[ f ] );
150
151         r = true;
152
153       } // fi
154
155     } // fi
156
157   } // fi
158
159   itk::GDCMSeriesFileNames::GlobalWarningDisplayOn( );
160
161 #endif // cpPlugins_Interface_QT4
162
163   return( r );
164 }
165
166 // -------------------------------------------------------------------------
167 cpPlugins::IO::DicomSeriesReader::
168 DicomSeriesReader( )
169   : Superclass( )
170 {
171 }
172
173 // -------------------------------------------------------------------------
174 cpPlugins::IO::DicomSeriesReader::
175 ~DicomSeriesReader( )
176 {
177 }
178
179 // eof - $RCSfile$