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