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