]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Plugins.cxx
UML-code sync
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
1 #include <cpPlugins/Interface/Plugins.h>
2
3 #ifdef cpPlugins_Interface_QT4
4
5 /*
6   #include <QApplication>
7   #include <QFileDialog>
8   #include <QMenu>
9   #include <QMessageBox>
10   #include <QWidget>
11 */
12
13 #ifdef _WIN32
14 #  define PLUGIN_PREFIX ""
15 #  define PLUGIN_EXT "dll"
16 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
17 #else // Linux
18 #  define PLUGIN_PREFIX "lib"
19 #  define PLUGIN_EXT "so"
20 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
21 #endif // _WIN32
22
23 #endif // cpPlugins_Interface_QT4
24
25 // -------------------------------------------------------------------------
26 cpPlugins::Interface::Plugins::
27 Plugins( )
28   : m_Widget( NULL ),
29     m_Application( NULL ),
30     m_Interface( NULL ),
31     m_LastLoadedPlugin( "" ),
32     m_ActiveFilter( NULL )
33 {
34   this->m_Interface = new TInterface( );
35 }
36
37 // -------------------------------------------------------------------------
38 cpPlugins::Interface::Plugins::
39 ~Plugins( )
40 {
41   if( this->m_Interface != NULL )
42     delete this->m_Interface;
43 }
44
45 // -------------------------------------------------------------------------
46 QWidget* cpPlugins::Interface::Plugins::
47 GetWidget( )
48 {
49   return( this->m_Widget );
50 }
51
52 // -------------------------------------------------------------------------
53 const QWidget* cpPlugins::Interface::Plugins::
54 GetWidget( ) const
55 {
56   return( this->m_Widget );
57 }
58
59 // -------------------------------------------------------------------------
60 void cpPlugins::Interface::Plugins::
61 SetWidget( QWidget* widget )
62 {
63   this->m_Widget = widget;
64 }
65
66 // -------------------------------------------------------------------------
67 void cpPlugins::Interface::Plugins::
68 BlockWidget( )
69 {
70 #ifdef cpPlugins_Interface_QT4
71   if( this->m_Widget != NULL )
72   {
73     QApplication::setOverrideCursor( Qt::WaitCursor );
74     this->m_Widget->setEnabled( false );
75
76   } // fi
77 #endif // cpPlugins_Interface_QT4
78 }
79
80 // -------------------------------------------------------------------------
81 void cpPlugins::Interface::Plugins::
82 UnblockWidget( )
83 {
84 #ifdef cpPlugins_Interface_QT4
85   if( this->m_Widget != NULL )
86   {
87     QApplication::restoreOverrideCursor( );
88     this->m_Widget->setEnabled( true );
89
90   } // fi
91 #endif // cpPlugins_Interface_QT4
92 }
93
94 // -------------------------------------------------------------------------
95 void cpPlugins::Interface::Plugins::
96 DialogLoadPlugins( )
97 {
98 #ifdef cpPlugins_Interface_QT4
99   if( this->m_Widget != NULL )
100   {
101     QFileDialog dialog( this->m_Widget );
102     dialog.setFileMode( QFileDialog::ExistingFile );
103     dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
104     dialog.setNameFilter( QFileDialog::tr( PLUGIN_REGEX ) );
105     dialog.setDefaultSuffix( QFileDialog::tr( PLUGIN_EXT ) );
106     if( !( dialog.exec( ) ) )
107       return;
108
109     std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
110     if( !( this->LoadPlugins( fname ) ) )
111       QMessageBox::critical(
112         this->m_Widget,
113         QMessageBox::tr( "Ignoring plugin" ),
114         QMessageBox::tr( fname.c_str( ) )
115         );
116
117   } // fi
118 #endif // cpPlugins_Interface_QT4
119 }
120
121 // -------------------------------------------------------------------------
122 BaseApplication* cpPlugins::Interface::Plugins::
123 GetApplication( )
124 {
125   return( this->m_Application );
126 }
127
128 // -------------------------------------------------------------------------
129 const BaseApplication* cpPlugins::Interface::Plugins::
130 GetApplication( ) const
131 {
132   return( this->m_Application );
133 }
134
135 // -------------------------------------------------------------------------
136 void cpPlugins::Interface::Plugins::
137 SetApplication( BaseApplication* a )
138 {
139   this->m_Application = a;
140 }
141
142 // -------------------------------------------------------------------------
143 bool cpPlugins::Interface::Plugins::
144 LoadPlugins( )
145 {
146   // TODO: what to do here?
147 }
148
149 // -------------------------------------------------------------------------
150 bool cpPlugins::Interface::Plugins::
151 LoadPlugins( const std::string& fname )
152 {
153   this->BlockWidget( );
154
155   // Is it already loaded?
156   bool ret = true;
157   if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
158   {
159     // Was it succesfully loaded?
160     ret = this->m_Interface->Load( fname );
161
162     // Update a simple track
163     if( ret )
164     {
165       this->m_LoadedPlugins.insert( fname );
166       this->m_LastLoadedPlugin = fname;
167       this->_UpdateLoadedPluginsInformation( );
168
169     } // fi
170
171   } // fi
172
173   this->UnblockWidget( );
174   return( ret );
175 }
176
177 // -------------------------------------------------------------------------
178 bool cpPlugins::Interface::Plugins::
179 LoadPluginsConfigurationFile( const std::string& fname )
180 {
181   // Load file into a char buffer
182   std::ifstream in(
183     fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
184     );
185   if( !in.is_open( ) )
186     return( false );
187
188   std::streampos size = in.tellg( );
189   char* buffer = new char[ size ];
190   in.seekg( 0, std::ios::beg );
191   in.read( buffer, size );
192   in.close( );
193
194   // Read stream
195   std::stringstream in_stream( buffer );
196   char line[ 4096 ];
197   while( in_stream )
198   {
199     in_stream.getline( line, 4096 );
200     this->LoadPlugins( line );
201
202   } // elihw
203   delete buffer;
204
205   return( true );
206 }
207
208 // -------------------------------------------------------------------------
209 const cpPlugins::Interface::Plugins::
210 TStringContainer& cpPlugins::Interface::Plugins::
211 GetLoadedPlugins( ) const
212 {
213   return( this->m_LoadedPlugins );
214 }
215
216 // -------------------------------------------------------------------------
217 void cpPlugins::Interface::Plugins::
218 GetLoadedFilters( TStringContainer& filters ) const
219 {
220   filters.clear( );
221   auto pIt = this->m_LoadedFilters.begin( );
222   for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
223     for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
224       filters.insert( *fIt );
225 }
226
227 // -------------------------------------------------------------------------
228 const cpPlugins::Interface::Plugins::
229 TStringContainer& cpPlugins::Interface::Plugins::
230 GetLoadedFilters( const std::string& plugin ) const
231 {
232   static const TStringContainer EMPTY;
233   auto pIt = this->m_LoadedFilters.find( plugin );
234   if( pIt != this->m_LoadedFilters.end( ) )
235     return( pIt->second );
236   else
237     return( EMPTY );
238 }
239
240 // -------------------------------------------------------------------------
241 void cpPlugins::Interface::Plugins::
242 AddInteractor( vtkRenderWindowInteractor* interactor )
243 {
244   this->m_Interactors.insert( interactor );
245 }
246
247 // -------------------------------------------------------------------------
248 void cpPlugins::Interface::Plugins::
249 RemoveInteractor( vtkRenderWindowInteractor* interactor )
250 {
251   this->m_Interactors.erase( interactor );
252 }
253
254 // -------------------------------------------------------------------------
255 void cpPlugins::Interface::Plugins::
256 ClearInteractors( )
257 {
258   this->m_Interactors.clear( );
259 }
260
261 // -------------------------------------------------------------------------
262 #define cpPlugins_Plugins_HasMacro( F )                                 \
263   bool cpPlugins::Interface::Plugins::                                  \
264   Has##F( ) const                                                       \
265   {                                                                     \
266     return( this->m_IOFilters.find( #F ) != this->m_IOFilters.end( ) ); \
267   }
268
269 cpPlugins_Plugins_HasMacro( ImageReader );
270 cpPlugins_Plugins_HasMacro( DicomSeriesReader );
271 cpPlugins_Plugins_HasMacro( MeshReader );
272 cpPlugins_Plugins_HasMacro( ImageWriter );
273 cpPlugins_Plugins_HasMacro( MeshWriter );
274
275 // -------------------------------------------------------------------------
276 std::string cpPlugins::Interface::Plugins::
277 ReadImage( const std::string& fname, const std::string& parent )
278 {
279   std::vector< std::string > fnames( 1, fname );
280   return( this->ReadImage( fnames, parent ) );
281 }
282
283 // -------------------------------------------------------------------------
284 std::string cpPlugins::Interface::Plugins::
285 ReadImage(
286   const std::vector< std::string >& fnames, const std::string& parent
287   )
288 {
289   // Activate reader
290   auto fIt = this->m_IOFilters.find( "ImageReader" );
291   if( fIt == this->m_IOFilters.end( ) )
292     return( "" );
293   this->m_ActiveFilter = fIt->second;
294
295   // Configure reader
296   TParameters* params = this->GetActiveFilterParameters( );
297   params->ClearStringList( "FileNames" );
298   for( auto nIt = fnames.begin( ); nIt != fnames.end( ); ++nIt )
299     params->AddToStringList( "FileNames", *nIt );
300
301   // Execute filter
302   std::string err = this->UpdateActiveFilter( );
303   if( err == "" )
304   {
305 #error GET OBJECT NAME
306   }
307   else
308   {
309 #error THROW ERROR
310   } // fi
311 }
312
313 // -------------------------------------------------------------------------
314 std::string cpPlugins::Interface::Plugins::
315 ReadImage( const std::string& parent )
316 {
317 }
318
319 // -------------------------------------------------------------------------
320 std::string cpPlugins::Interface::Plugins::
321 ReadDicomSeries( const std::string& parent )
322 {
323 }
324
325 // -------------------------------------------------------------------------
326 std::string cpPlugins::Interface::Plugins::
327 ReadMesh(
328   const std::string& fname, const std::string& parent
329   )
330 {
331 }
332
333 // -------------------------------------------------------------------------
334 std::string cpPlugins::Interface::Plugins::
335 ReadMesh( const std::string& parent )
336 {
337 }
338
339 // -------------------------------------------------------------------------
340 std::string cpPlugins::Interface::Plugins::
341 WriteDataObject(
342   const std::string& fname, const std::string& name
343   )
344 {
345 }
346
347 // -------------------------------------------------------------------------
348 std::string cpPlugins::Interface::Plugins::
349 WriteDataObject( const std::string& name )
350 {
351 }
352
353 // -------------------------------------------------------------------------
354 void cpPlugins::Interface::Plugins::
355 ClearDataObjects( )
356 {
357 }
358
359 // -------------------------------------------------------------------------
360 void cpPlugins::Interface::Plugins::
361 DeleteDataObject( const std::string& name )
362 {
363 }
364
365 // -------------------------------------------------------------------------
366 void cpPlugins::Interface::Plugins::
367 GetDataObjects( TStringContainer& names )
368 {
369 }
370
371 // -------------------------------------------------------------------------
372 std::string cpPlugins::Interface::Plugins::
373 GetParent( const std::string& name ) const
374 {
375 }
376
377 // -------------------------------------------------------------------------
378 void cpPlugins::Interface::Plugins::
379 GetChildren(
380   TStringContainer& names, const std::string& name
381   ) const
382 {
383 }
384
385 // -------------------------------------------------------------------------
386 void cpPlugins::Interface::Plugins::
387 GetRoots( TStringContainer& names ) const
388 {
389 }
390
391 // -------------------------------------------------------------------------
392 bool cpPlugins::Interface::Plugins::
393 ActivateFilter( const std::string& name )
394 {
395 }
396
397 // -------------------------------------------------------------------------
398 void cpPlugins::Interface::Plugins::
399 DeactivateFilter( )
400 {
401 }
402
403 // -------------------------------------------------------------------------
404 bool cpPlugins::Interface::Plugins::
405 HasActiveFilter( ) const
406 {
407 }
408
409 // -------------------------------------------------------------------------
410 bool cpPlugins::Interface::Plugins::
411 IsActiveFilterInteractive( ) const
412 {
413 }
414
415 // -------------------------------------------------------------------------
416 void cpPlugins::Interface::Plugins::
417 GetActiveFilterInputsNames( TStringContainer& names ) const
418 {
419 }
420
421 // -------------------------------------------------------------------------
422 void cpPlugins::Interface::Plugins::
423 GetActiveFilterOutputsNames( TStringContainer& names ) const
424 {
425 }
426
427 // -------------------------------------------------------------------------
428 bool cpPlugins::Interface::Plugins::
429 ConnectInputInActiveFilter(
430   const std::string& object_name, const std::string& input_name
431   )
432 {
433 }
434
435 // -------------------------------------------------------------------------
436 bool cpPlugins::Interface::Plugins::
437 SetOutputNameInActiveFilter(
438   const std::string& new_object_name, const std::string& output_name
439   )
440 {
441 }
442
443 // -------------------------------------------------------------------------
444 TParameters* cpPlugins::Interface::Plugins::
445 GetActiveFilterParameters( )
446 {
447 }
448
449 // -------------------------------------------------------------------------
450 const TParameters* cpPlugins::Interface::Plugins::
451 GetActiveFilterParameters( ) const
452 {
453 }
454
455 // -------------------------------------------------------------------------
456 TProcessObject::DialogResult cpPlugins::Interface::Plugins::
457 ConfigureActiveFilter( )
458 {
459 }
460
461 // -------------------------------------------------------------------------
462 std::string cpPlugins::Interface::Plugins::
463 UpdateActiveFilter( )
464 {
465 }
466
467 // -------------------------------------------------------------------------
468 void cpPlugins::Interface::Plugins::
469 _UpdateLoadedPluginsInformation( )
470 {
471 }
472
473 // -------------------------------------------------------------------------
474 bool cpPlugins::Interface::Plugins::
475 _InsertNewData( TDataObject* dobj, const std::string& parent )
476 {
477 }
478
479 /*
480   protected:
481   // MVC objects
482   QWidget*         m_Widget;
483   BaseApplication* m_Application;
484   
485   // Plugins interface
486   TInterface       m_Interface;
487   TStringContainer m_LoadedPlugins;
488   std::string      m_LastLoadedPlugin;
489   
490   // Loaded filters
491   std::map< std::string, TProcessObject::Pointer > m_IOFilters;
492   TProcessObject::Pointer                       m_ActiveFilter;
493   std::map< std::string, std::string >   m_ActiveFilterOutputs;
494   std::map< std::string, TStringContainer >    m_LoadedFilters;
495   
496   // Loaded data objects
497   typedef std::pair< std::string, TDataObject::Pointer >  _TTreeNode;
498   std::map< std::string, _TTreeNode > m_DataObjects;
499   
500   // Associated interactors
501   std::set< vtkRenderWindowInteractor* > m_Interactors;
502   };
503   
504   } // ecapseman
505   
506   } // ecapseman
507 */
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586 #include <cpPlugins/Interface/Config.h>
587
588 #include <fstream>
589 #include <sstream>
590
591 #ifdef cpPlugins_Interface_QT4
592
593 #include <QApplication>
594 #include <QFileDialog>
595 #include <QMenu>
596 #include <QMessageBox>
597 #include <QWidget>
598
599 #ifdef _WIN32
600 #  define PLUGIN_PREFIX ""
601 #  define PLUGIN_EXT "dll"
602 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
603 #else // Linux
604 #  define PLUGIN_PREFIX "lib"
605 #  define PLUGIN_EXT "so"
606 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
607 #endif // _WIN32
608
609 #endif // cpPlugins_Interface_QT4
610
611
612
613
614 // -------------------------------------------------------------------------
615 void cpPlugins::Interface::Plugins::
616 AddInteractor( vtkRenderWindowInteractor* interactor )
617 {
618   this->m_Interactors.insert( interactor );
619 }
620
621 // -------------------------------------------------------------------------
622 void cpPlugins::Interface::Plugins::
623 RemoveInteractor( vtkRenderWindowInteractor* interactor )
624 {
625   this->m_Interactors.erase( interactor );
626 }
627
628 // -------------------------------------------------------------------------
629 void cpPlugins::Interface::Plugins::
630 ClearInteractors( )
631 {
632   this->m_Interactors.clear( );
633 }
634
635 // -------------------------------------------------------------------------
636 bool cpPlugins::Interface::Plugins::
637 HasImageReader( ) const
638 {
639   return( this->m_ImageReader.IsNotNull( ) );
640 }
641
642 // -------------------------------------------------------------------------
643 bool cpPlugins::Interface::Plugins::
644 HasDicomSeriesReader( ) const
645 {
646   return( this->m_DicomSeriesReader.IsNotNull( ) );
647 }
648
649 // -------------------------------------------------------------------------
650 bool cpPlugins::Interface::Plugins::
651 HasMeshReader( ) const
652 {
653   return( this->m_MeshReader.IsNotNull( ) );
654 }
655
656 // -------------------------------------------------------------------------
657 bool cpPlugins::Interface::Plugins::
658 HasImageWriter( ) const
659 {
660   return( this->m_ImageWriter.IsNotNull( ) );
661 }
662
663 // -------------------------------------------------------------------------
664 bool cpPlugins::Interface::Plugins::
665 HasMeshWriter( ) const
666 {
667   return( this->m_MeshWriter.IsNotNull( ) );
668 }
669
670 // -------------------------------------------------------------------------
671 std::string cpPlugins::Interface::Plugins::
672 ReadImage( const std::string& fname, const std::string& parent )
673 {
674   std::vector< std::string > fnames;
675   fnames.push_back( fname );
676   return( this->ReadImage( fnames, parent ) );
677 }
678
679 // -------------------------------------------------------------------------
680 std::string cpPlugins::Interface::Plugins::
681 ReadImage(
682   const std::vector< std::string >& fnames, const std::string& parent
683   )
684 {
685   // Check if object exists
686   if( this->m_ImageReader.IsNull( ) )
687     return( "" );
688
689   // Configure object
690   TParameters* params = this->m_ImageReader->GetParameters( );
691   params->ClearStringList( "FileNames" );
692   auto i = fnames.begin( );
693   for( ; i != fnames.end( ); ++i )
694     params->AddToStringList( "FileNames", *i );
695
696   // Execute filter
697   this->BlockWidget( );
698   std::string err = this->m_ImageReader->Update( );
699   this->UnblockWidget( );
700
701   // Get result, if any
702   if( err == "" )
703   {
704     TImage* image = this->m_ImageReader->GetOutput< TImage >( "Output" );
705     this->m_ImageReader->DisconnectOutputs( );
706
707     // Add newly added data
708     if( this->_InsertNewData( image, parent ) )
709       return( image->GetName( ) );
710     else
711       return( "" );
712   }
713   else
714   {
715 #ifdef cpPlugins_Interface_QT4
716     if( this->m_Widget != NULL )
717       QMessageBox::critical(
718         this->m_Widget,
719         QMessageBox::tr( "Error reading image." ),
720         QMessageBox::tr( err.c_str( ) )
721         );
722 #else // cpPlugins_Interface_QT4
723     std::cerr << "Error reading image: " << err << std::endl;
724 #endif // cpPlugins_Interface_QT4
725     return( "" );
726
727   } // fi
728 }
729
730 // -------------------------------------------------------------------------
731 std::string cpPlugins::Interface::Plugins::
732 ReadImage( const std::string& parent )
733 {
734   // Check if object exists
735   if( this->m_ImageReader.IsNull( ) )
736     return( "" );
737
738   // Configure object
739   TProcessObject::DialogResult dret =
740     this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
741   if( dret == TProcessObject::DialogResult_Cancel )
742     return( "" );
743
744   // Execute filter
745   this->BlockWidget( );
746   std::string err = this->m_ImageReader->Update( );
747   this->UnblockWidget( );
748
749   // Get result, if any
750   if( err == "" )
751   {
752     TImage* image = this->m_ImageReader->GetOutput< TImage >( "Output" );
753     this->m_ImageReader->DisconnectOutputs( );
754
755     // Add newly added data
756     if( this->_InsertNewData( image, parent ) )
757       return( image->GetName( ) );
758     else
759       return( "" );
760   }
761   else
762   {
763 #ifdef cpPlugins_Interface_QT4
764     if( this->m_Widget != NULL )
765       QMessageBox::critical(
766         this->m_Widget,
767         QMessageBox::tr( "Error reading image." ),
768         QMessageBox::tr( err.c_str( ) )
769         );
770 #else // cpPlugins_Interface_QT4
771     std::cerr << "Error reading image: " << err << std::endl;
772 #endif // cpPlugins_Interface_QT4
773     return( "" );
774
775   } // fi
776 }
777
778 // -------------------------------------------------------------------------
779 std::string cpPlugins::Interface::Plugins::
780 ReadDicomSeries( const std::string& parent )
781 {
782   // Check if object exists
783   if( this->m_DicomSeriesReader.IsNull( ) )
784     return( "" );
785
786   // Configure object
787   TProcessObject::DialogResult dret =
788     this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget );
789   if( dret == TProcessObject::DialogResult_Cancel )
790     return( "" );
791
792   // Execute filter
793   this->BlockWidget( );
794   std::string err = this->m_DicomSeriesReader->Update( );
795   this->UnblockWidget( );
796
797   // Get result, if any
798   if( err == "" )
799   {
800     TImage* image =
801       this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
802     this->m_DicomSeriesReader->DisconnectOutputs( );
803
804     // Add newly added data
805     if( this->_InsertNewData( image, parent ) )
806       return( image->GetName( ) );
807     else
808       return( "" );
809   }
810   else
811   {
812 #ifdef cpPlugins_Interface_QT4
813     if( this->m_Widget != NULL )
814       QMessageBox::critical(
815         this->m_Widget,
816         QMessageBox::tr( "Error reading image." ),
817         QMessageBox::tr( err.c_str( ) )
818         );
819 #else // cpPlugins_Interface_QT4
820     std::cerr << "Error reading image: " << err << std::endl;
821 #endif // cpPlugins_Interface_QT4
822     return( "" );
823
824   } // fi
825 }
826
827 // -------------------------------------------------------------------------
828 std::string cpPlugins::Interface::Plugins::
829 ReadMesh( const std::string& fname, const std::string& parent )
830 {
831   // Check if object exists
832   if( this->m_MeshReader.IsNull( ) )
833     return( "" );
834
835   // Configure object
836   TParameters* params = this->m_MeshReader->GetParameters( );
837   params->SetString( "FileName", fname );
838
839   // Execute filter
840   this->BlockWidget( );
841   std::string err = this->m_MeshReader->Update( );
842   this->UnblockWidget( );
843
844   // Get result, if any
845   if( err == "" )
846   {
847     TMesh* mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
848     this->m_MeshReader->DisconnectOutputs( );
849
850     // Add newly added data
851     if( this->_InsertNewData( mesh, parent ) )
852       return( mesh->GetName( ) );
853     else
854       return( "" );
855   }
856   else
857   {
858 #ifdef cpPlugins_Interface_QT4
859     if( this->m_Widget != NULL )
860       QMessageBox::critical(
861         this->m_Widget,
862         QMessageBox::tr( "Error reading mesh." ),
863         QMessageBox::tr( err.c_str( ) )
864         );
865 #else // cpPlugins_Interface_QT4
866     std::cerr << "Error reading mesh: " << err << std::endl;
867 #endif // cpPlugins_Interface_QT4
868     return( "" );
869
870   } // fi
871 }
872
873 // -------------------------------------------------------------------------
874 std::string cpPlugins::Interface::Plugins::
875 ReadMesh( const std::string& parent )
876 {
877   // Check if object exists
878   if( this->m_MeshReader.IsNull( ) )
879     return( "" );
880
881   // Configure object
882   TProcessObject::DialogResult dret =
883     this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
884   if( dret == TProcessObject::DialogResult_Cancel )
885     return( "" );
886
887   // Execute filter
888   this->BlockWidget( );
889   std::string err = this->m_MeshReader->Update( );
890   this->UnblockWidget( );
891
892   // Get result, if any
893   if( err == "" )
894   {
895     TMesh* mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
896     this->m_MeshReader->DisconnectOutputs( );
897
898     // Add newly added data
899     if( this->_InsertNewData( mesh, parent ) )
900       return( mesh->GetName( ) );
901     else
902       return( "" );
903   }
904   else
905   {
906 #ifdef cpPlugins_Interface_QT4
907     if( this->m_Widget != NULL )
908       QMessageBox::critical(
909         this->m_Widget,
910         QMessageBox::tr( "Error reading mesh." ),
911         QMessageBox::tr( err.c_str( ) )
912         );
913 #else // cpPlugins_Interface_QT4
914     std::cerr << "Error reading mesh: " << err << std::endl;
915 #endif // cpPlugins_Interface_QT4
916     return( "" );
917
918   } // fi
919 }
920
921 // -------------------------------------------------------------------------
922 bool cpPlugins::Interface::Plugins::
923 WriteImage( const std::string& fname, const std::string& name )
924 {
925   // Check if objects exist
926   if( this->m_ImageWriter.IsNull( ) )
927     return( false );
928   TImage* image = this->GetImage( name );
929   if( image == NULL )
930     return( false );
931
932   // Configure writer
933   this->m_ImageWriter->GetParameters( )->SetString( "FileName", fname );
934   this->m_ImageWriter->SetInput( "Input", image );
935
936   // Execute filter
937   this->BlockWidget( );
938   std::string err = this->m_ImageWriter->Update( );
939   this->UnblockWidget( );
940
941   // Get result, if any
942   if( err != "" )
943   {
944 #ifdef cpPlugins_Interface_QT4
945     if( this->m_Widget != NULL )
946       QMessageBox::critical(
947         this->m_Widget,
948         QMessageBox::tr( "Error reading mesh." ),
949         QMessageBox::tr( err.c_str( ) )
950         );
951 #else // cpPlugins_Interface_QT4
952     std::cerr << "Error reading mesh: " << err << std::endl;
953 #endif // cpPlugins_Interface_QT4
954     return( false );
955   }
956   else
957     return( true );
958 }
959
960 // -------------------------------------------------------------------------
961 bool cpPlugins::Interface::Plugins::
962 WriteImage( const std::string& name )
963 {
964   // Check if objects exist
965   if( this->m_ImageWriter.IsNull( ) )
966     return( false );
967   TImage* image = this->GetImage( name );
968   if( image == NULL )
969     return( false );
970
971   // Configure writer
972   TProcessObject::DialogResult dret = 
973     this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
974   if( dret == TProcessObject::DialogResult_Cancel )
975     return( "" );
976   this->m_ImageWriter->SetInput( "Input", image );
977
978   // Execute filter
979   this->BlockWidget( );
980   std::string err = this->m_ImageWriter->Update( );
981   this->UnblockWidget( );
982
983   // Get result, if any
984   if( err != "" )
985   {
986 #ifdef cpPlugins_Interface_QT4
987     if( this->m_Widget != NULL )
988       QMessageBox::critical(
989         this->m_Widget,
990         QMessageBox::tr( "Error reading mesh." ),
991         QMessageBox::tr( err.c_str( ) )
992         );
993 #else // cpPlugins_Interface_QT4
994     std::cerr << "Error reading mesh: " << err << std::endl;
995 #endif // cpPlugins_Interface_QT4
996     return( false );
997   }
998   else
999     return( true );
1000 }
1001
1002 // -------------------------------------------------------------------------
1003 bool cpPlugins::Interface::Plugins::
1004 WriteMesh( const std::string& fname, const std::string& name )
1005 {
1006   // Check if objects exist
1007   if( this->m_MeshWriter.IsNull( ) )
1008     return( false );
1009   TMesh* mesh = this->GetMesh( name );
1010   if( mesh == NULL )
1011     return( false );
1012
1013   // Configure writer
1014   this->m_MeshWriter->GetParameters( )->SetString( "FileName", fname );
1015   this->m_MeshWriter->SetInput( "Input", mesh );
1016
1017   // Execute filter
1018   this->BlockWidget( );
1019   std::string err = this->m_MeshWriter->Update( );
1020   this->UnblockWidget( );
1021
1022   // Get result, if any
1023   if( err != "" )
1024   {
1025 #ifdef cpPlugins_Interface_QT4
1026     if( this->m_Widget != NULL )
1027       QMessageBox::critical(
1028         this->m_Widget,
1029         QMessageBox::tr( "Error reading mesh." ),
1030         QMessageBox::tr( err.c_str( ) )
1031         );
1032 #else // cpPlugins_Interface_QT4
1033     std::cerr << "Error reading mesh: " << err << std::endl;
1034 #endif // cpPlugins_Interface_QT4
1035     return( false );
1036   }
1037   else
1038     return( true );
1039 }
1040
1041 // -------------------------------------------------------------------------
1042 bool cpPlugins::Interface::Plugins::
1043 WriteMesh( const std::string& name )
1044 {
1045   // Check if objects exist
1046   if( this->m_MeshWriter.IsNull( ) )
1047     return( false );
1048   TMesh* mesh = this->GetMesh( name );
1049   if( mesh == NULL )
1050     return( false );
1051
1052   // Configure writer
1053   TProcessObject::DialogResult dret = 
1054     this->m_MeshWriter->ExecConfigurationDialog( this->m_Widget );
1055   if( dret == TProcessObject::DialogResult_Cancel )
1056     return( "" );
1057   this->m_MeshWriter->SetInput( "Input", mesh );
1058
1059   // Execute filter
1060   this->BlockWidget( );
1061   std::string err = this->m_MeshWriter->Update( );
1062   this->UnblockWidget( );
1063
1064   // Get result, if any
1065   if( err != "" )
1066   {
1067 #ifdef cpPlugins_Interface_QT4
1068     if( this->m_Widget != NULL )
1069       QMessageBox::critical(
1070         this->m_Widget,
1071         QMessageBox::tr( "Error reading mesh." ),
1072         QMessageBox::tr( err.c_str( ) )
1073         );
1074 #else // cpPlugins_Interface_QT4
1075     std::cerr << "Error reading mesh: " << err << std::endl;
1076 #endif // cpPlugins_Interface_QT4
1077     return( false );
1078   }
1079   else
1080     return( true );
1081 }
1082
1083 // -------------------------------------------------------------------------
1084 void cpPlugins::Interface::Plugins::
1085 ClearDataObjects( )
1086 {
1087   this->m_Objects.clear( );
1088 }
1089
1090 // -------------------------------------------------------------------------
1091 void cpPlugins::Interface::Plugins::
1092 DeleteDataObject( const std::string& name )
1093 {
1094   auto i = this->m_Objects.find( name );
1095   if( i != this->m_Objects.end( ) )
1096   {
1097     this->m_Objects.erase( i );
1098
1099     // Get children
1100     std::vector< std::string > children;
1101     for( i = this->m_Objects.begin( ); i != this->m_Objects.end( ); ++i )
1102       if( i->second.first == name )
1103         children.push_back( i->first );
1104
1105     // Erase children
1106     auto c = children.begin( );
1107     for( ; c != children.end( ); ++c )
1108       this->DeleteDataObject( *c );
1109     
1110   } // fi
1111 }
1112
1113 // -------------------------------------------------------------------------
1114 std::string cpPlugins::Interface::Plugins::
1115 GetParent( const std::string& name ) const
1116 {
1117   auto i = this->m_Objects.find( name );
1118   if( i != this->m_Objects.end( ) )
1119     return( i->second.first );
1120   else
1121     return( "" );
1122 }
1123
1124 // -------------------------------------------------------------------------
1125 const cpPlugins::Interface::Plugins::
1126 TTree& cpPlugins::Interface::Plugins::
1127 GetDataObjects( ) const
1128 {
1129   return( this->m_Objects );
1130 }
1131
1132 // -------------------------------------------------------------------------
1133 cpPlugins::Interface::Plugins::
1134 TDataObject* cpPlugins::Interface::Plugins::
1135 GetDataObject( const std::string& name )
1136 {
1137   auto i = this->m_Objects.find( name );
1138   if( i != this->m_Objects.end( ) )
1139     return( dynamic_cast< TDataObject* >( i->second.second.GetPointer( ) ) );
1140   else
1141     return( NULL );
1142 }
1143
1144 // -------------------------------------------------------------------------
1145 const cpPlugins::Interface::Plugins::
1146 TDataObject* cpPlugins::Interface::Plugins::
1147 GetDataObject( const std::string& name ) const
1148 {
1149   auto i = this->m_Objects.find( name );
1150   if( i != this->m_Objects.end( ) )
1151     return(
1152       dynamic_cast< const TDataObject* >( i->second.second.GetPointer( ) )
1153       );
1154   else
1155     return( NULL );
1156 }
1157
1158 // -------------------------------------------------------------------------
1159 cpPlugins::Interface::Plugins::
1160 TImage* cpPlugins::Interface::Plugins::
1161 GetImage( const std::string& name )
1162 {
1163   auto i = this->m_Objects.find( name );
1164   if( i != this->m_Objects.end( ) )
1165     return( dynamic_cast< TImage* >( i->second.second.GetPointer( ) ) );
1166   else
1167     return( NULL );
1168 }
1169
1170 // -------------------------------------------------------------------------
1171 const cpPlugins::Interface::Plugins::
1172 TImage* cpPlugins::Interface::Plugins::
1173 GetImage( const std::string& name ) const
1174 {
1175   auto i = this->m_Objects.find( name );
1176   if( i != this->m_Objects.end( ) )
1177     return( dynamic_cast< const TImage* >( i->second.second.GetPointer( ) ) );
1178   else
1179     return( NULL );
1180 }
1181
1182 // -------------------------------------------------------------------------
1183 cpPlugins::Interface::Plugins::
1184 TMesh* cpPlugins::Interface::Plugins::
1185 GetMesh( const std::string& name )
1186 {
1187   auto i = this->m_Objects.find( name );
1188   if( i != this->m_Objects.end( ) )
1189     return( dynamic_cast< TMesh* >( i->second.second.GetPointer( ) ) );
1190   else
1191     return( NULL );
1192 }
1193
1194 // -------------------------------------------------------------------------
1195 const cpPlugins::Interface::Plugins::
1196 TMesh* cpPlugins::Interface::Plugins::
1197 GetMesh( const std::string& name ) const
1198 {
1199   auto i = this->m_Objects.find( name );
1200   if( i != this->m_Objects.end( ) )
1201     return( dynamic_cast< const TMesh* >( i->second.second.GetPointer( ) ) );
1202   else
1203     return( NULL );
1204 }
1205
1206 // -------------------------------------------------------------------------
1207 bool cpPlugins::Interface::Plugins::
1208 ActivateFilter( const std::string& name )
1209 {
1210   this->m_ActiveFilter = this->m_Interface.CreateProcessObject( name );
1211   if( this->m_ActiveFilter.IsNotNull( ) )
1212   {
1213     this->m_ActiveFilter->SetPlugins( this );
1214     this->m_ActiveFilterOutputs.clear( );
1215     auto i = this->m_Interactors.begin( );
1216     for( ; i != this->m_Interactors.end( ); ++i )
1217       this->m_ActiveFilter->AddInteractor( *i );
1218     return( true );
1219   }
1220   else
1221     return( false );
1222 }
1223
1224 // -------------------------------------------------------------------------
1225 void cpPlugins::Interface::Plugins::
1226 DeactivateFilter( )
1227 {
1228   this->m_ActiveFilter = NULL;
1229 }
1230
1231 // -------------------------------------------------------------------------
1232 bool cpPlugins::Interface::Plugins::
1233 HasActiveFilter( ) const
1234 {
1235   return( this->m_ActiveFilter.IsNotNull( ) );
1236 }
1237
1238 // -------------------------------------------------------------------------
1239 bool cpPlugins::Interface::Plugins::
1240 IsActiveFilterInteractive( ) const
1241 {
1242   if( this->m_ActiveFilter.IsNotNull( ) )
1243     return( this->m_ActiveFilter->IsInteractive( ) );
1244   else
1245     return( false );
1246 }
1247
1248 // -------------------------------------------------------------------------
1249 unsigned int cpPlugins::Interface::Plugins::
1250 GetNumberOfInputsInActiveFilter( ) const
1251 {
1252   if( this->m_ActiveFilter.IsNotNull( ) )
1253     return( this->m_ActiveFilter->GetNumberOfInputs( ) );
1254   else
1255     return( 0 );
1256 }
1257
1258 // -------------------------------------------------------------------------
1259 unsigned int cpPlugins::Interface::Plugins::
1260 GetNumberOfOutputsInActiveFilter( ) const
1261 {
1262   if( this->m_ActiveFilter.IsNotNull( ) )
1263     return( this->m_ActiveFilter->GetNumberOfOutputs( ) );
1264   else
1265     return( 0 );
1266 }
1267
1268 // -------------------------------------------------------------------------
1269 std::vector< std::string > cpPlugins::Interface::Plugins::
1270 GetActiveFilterInputsNames( ) const
1271 {
1272   if( this->m_ActiveFilter.IsNotNull( ) )
1273     return( this->m_ActiveFilter->GetInputsNames( ) );
1274   else
1275     return( std::vector< std::string >( ) );
1276 }
1277
1278 // -------------------------------------------------------------------------
1279 std::vector< std::string > cpPlugins::Interface::Plugins::
1280 GetActiveFilterOutputsNames( ) const
1281 {
1282   if( this->m_ActiveFilter.IsNotNull( ) )
1283     return( this->m_ActiveFilter->GetOutputsNames( ) );
1284   else
1285     return( std::vector< std::string >( ) );
1286 }
1287
1288 // -------------------------------------------------------------------------
1289 void cpPlugins::Interface::Plugins::
1290 ConnectInputInActiveFilter(
1291   const std::string& object_name, const std::string& input
1292   )
1293 {
1294   if( this->m_ActiveFilter.IsNotNull( ) )
1295   {
1296     TDataObject* dobj = this->GetDataObject( object_name );
1297     if( dobj != NULL )
1298       this->m_ActiveFilter->SetInput( input, dobj );
1299
1300   } // fi
1301 }
1302
1303 // -------------------------------------------------------------------------
1304 void cpPlugins::Interface::Plugins::
1305 SetOutputNameInActiveFilter(
1306   const std::string& new_name, const std::string& output
1307   )
1308 {
1309   this->m_ActiveFilterOutputs[ output ] = new_name;
1310 }
1311
1312 // -------------------------------------------------------------------------
1313 cpPlugins::Interface::Plugins::
1314 TParameters* cpPlugins::Interface::Plugins::
1315 GetActiveFilterParameters( )
1316 {
1317   if( this->m_ActiveFilter.IsNotNull( ) )
1318     return( this->m_ActiveFilter->GetParameters( ) );
1319   else
1320     return( NULL );
1321 }
1322
1323 // -------------------------------------------------------------------------
1324 const cpPlugins::Interface::Plugins::
1325 TParameters* cpPlugins::Interface::Plugins::
1326 GetActiveFilterParameters( ) const
1327 {
1328   if( this->m_ActiveFilter.IsNotNull( ) )
1329     return( this->m_ActiveFilter->GetParameters( ) );
1330   else
1331     return( NULL );
1332 }
1333
1334 // -------------------------------------------------------------------------
1335 cpPlugins::Interface::Plugins::
1336 TProcessObject::DialogResult cpPlugins::Interface::Plugins::
1337 ConfigureActiveFilter( )
1338 {
1339   if( this->m_ActiveFilter.IsNotNull( ) )
1340     return( this->m_ActiveFilter->ExecConfigurationDialog( this->m_Widget ) );
1341   else
1342     return( TProcessObject::DialogResult_Cancel );
1343 }
1344
1345 // -------------------------------------------------------------------------
1346 std::string cpPlugins::Interface::Plugins::
1347 UpdateActiveFilter( std::vector< std::string >& outputs )
1348 {
1349   // Execute filter
1350   this->BlockWidget( );
1351   std::string err = this->m_ActiveFilter->Update( );
1352   this->UnblockWidget( );
1353
1354   // Associate outputs
1355   outputs.clear( );
1356   if( err == "" )
1357   {
1358     std::string parent = "";
1359     if( this->GetNumberOfInputsInActiveFilter( ) > 0 )
1360     {
1361       std::string input = this->m_ActiveFilter->GetInputsNames( )[ 0 ];
1362       parent =
1363         this->m_ActiveFilter->GetInput< TDataObject >( input )->GetName( );
1364
1365     } // fi
1366
1367     auto i = this->m_ActiveFilterOutputs.begin( );
1368     for( ; i != this->m_ActiveFilterOutputs.end( ); ++i )
1369     {
1370       TDataObject* out =
1371         this->m_ActiveFilter->GetOutput< TDataObject >( i->first );
1372       out->SetName( i->second );
1373       outputs.push_back( out->GetName( ) );
1374       this->_InsertNewData( out, parent );
1375
1376     } // rof
1377
1378   } // fi
1379   return( err );
1380 }
1381
1382 // -------------------------------------------------------------------------
1383 void cpPlugins::Interface::Plugins::
1384 _UpdateLoadedPluginsInformation( )
1385 {
1386   typedef TInterface::TClasses _C;
1387
1388   this->m_Filters.clear( );
1389   _C& classes = this->m_Interface.GetClasses( );
1390   for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
1391   {
1392     TProcessObject::Pointer o =
1393       this->m_Interface.CreateProcessObject( i->first );
1394     std::string name = o->GetClassName( );
1395     std::string category = o->GetClassCategory( );
1396     if( category == "ImageReader" )
1397       this->m_ImageReader = o;
1398     else if( category == "ImageWriter" )
1399       this->m_ImageWriter = o;
1400     else if( category == "MeshReader" )
1401       this->m_MeshReader = o;
1402     else if( category == "MeshWriter" )
1403       this->m_MeshWriter = o;
1404     else if( category == "DicomSeriesReader" )
1405       this->m_DicomSeriesReader = o;
1406     else
1407       this->m_Filters[ category ].insert( name );
1408
1409   } // rof
1410 }
1411
1412 // -------------------------------------------------------------------------
1413 bool cpPlugins::Interface::Plugins::
1414 _InsertNewData( TDataObject* dobj, const std::string& parent )
1415 {
1416   std::string name = dobj->GetName( );
1417   auto i = this->m_Objects.find( name );
1418   bool ret = true;
1419   if( i == this->m_Objects.end( ) )
1420   {
1421     if( parent != "" )
1422     {
1423       auto j = this->m_Objects.find( parent );
1424       if( j != this->m_Objects.end( ) )
1425         this->m_Objects[ name ] = TTreeNode( parent, dobj );
1426       else
1427         ret = false;
1428     }
1429     else
1430       this->m_Objects[ name ] = TTreeNode( "", dobj );
1431   }
1432   else
1433     i->second.second = dobj;
1434
1435   if( !ret )
1436   {
1437 #ifdef cpPlugins_Interface_QT4
1438     if( this->m_Widget != NULL )
1439       QMessageBox::critical(
1440         this->m_Widget,
1441         QMessageBox::tr( "Error inserting data." ),
1442         QMessageBox::tr( "Given parent does not exists." )
1443         );
1444 #else // cpPlugins_Interface_QT4
1445     std::cerr
1446       << "Error inserting data: Given parent does not exists."
1447       << std::endl;
1448 #endif // cpPlugins_Interface_QT4
1449   } // fi
1450   return( ret );
1451 }
1452
1453 // eof - $RCSfile$