]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Plugins.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[cpPlugins.git] / lib / cpPlugins / Interface / Plugins.cxx
1 #include <cpPlugins/Interface/Plugins.h>
2 #include <cpPlugins/Interface/Config.h>
3
4 #include <fstream>
5 #include <sstream>
6
7 #ifdef cpPlugins_Interface_QT4
8
9 #include <QApplication>
10 #include <QFileDialog>
11 #include <QMenu>
12 #include <QMessageBox>
13 #include <QWidget>
14
15 #ifdef _WIN32
16 #  define PLUGIN_PREFIX ""
17 #  define PLUGIN_EXT "dll"
18 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
19 #else // Linux
20 #  define PLUGIN_PREFIX "lib"
21 #  define PLUGIN_EXT "so"
22 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
23 #endif // _WIN32
24
25 #endif // cpPlugins_Interface_QT4
26
27 // -------------------------------------------------------------------------
28 cpPlugins::Interface::Plugins::
29 Plugins( QWidget* widget )
30   : m_Widget( widget ),
31     m_Application( NULL ),
32     m_LastLoadedPlugin( "." )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 cpPlugins::Interface::Plugins::
38 ~Plugins( )
39 {
40   // TODO: this causes a segfault? this->m_Interface.UnloadAll( );
41 }
42
43 // -------------------------------------------------------------------------
44 QWidget* cpPlugins::Interface::Plugins::
45 GetWidget( )
46 {
47   return( this->m_Widget );
48 }
49
50 // -------------------------------------------------------------------------
51 const QWidget* cpPlugins::Interface::Plugins::
52 GetWidget( ) const
53 {
54   return( this->m_Widget );
55 }
56
57 // -------------------------------------------------------------------------
58 void cpPlugins::Interface::Plugins::
59 SetWidget( QWidget* widget )
60 {
61   this->m_Widget = widget;
62 }
63
64 // -------------------------------------------------------------------------
65 void cpPlugins::Interface::Plugins::
66 BlockWidget( )
67 {
68 #ifdef cpPlugins_Interface_QT4
69   if( this->m_Widget != NULL )
70   {
71     QApplication::setOverrideCursor( Qt::WaitCursor );
72     this->m_Widget->setEnabled( false );
73
74   } // fi
75 #endif // cpPlugins_Interface_QT4
76 }
77
78 // -------------------------------------------------------------------------
79 void cpPlugins::Interface::Plugins::
80 UnblockWidget( )
81 {
82 #ifdef cpPlugins_Interface_QT4
83   if( this->m_Widget != NULL )
84   {
85     QApplication::restoreOverrideCursor( );
86     this->m_Widget->setEnabled( true );
87
88   } // fi
89 #endif // cpPlugins_Interface_QT4
90 }
91
92 // -------------------------------------------------------------------------
93 void cpPlugins::Interface::Plugins::
94 DialogLoadPlugins( )
95 {
96 #ifdef cpPlugins_Interface_QT4
97   if( this->m_Widget != NULL )
98   {
99     QFileDialog dialog( this->m_Widget );
100     dialog.setFileMode( QFileDialog::ExistingFile );
101     dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
102     dialog.setNameFilter( QFileDialog::tr( PLUGIN_REGEX ) );
103     dialog.setDefaultSuffix( QFileDialog::tr( PLUGIN_EXT ) );
104     if( !( dialog.exec( ) ) )
105       return;
106
107     std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
108     if( !( this->LoadPlugins( fname ) ) )
109       QMessageBox::critical(
110         this->m_Widget,
111         QMessageBox::tr( "Ignoring plugin" ),
112         QMessageBox::tr( fname.c_str( ) )
113         );
114
115   } // fi
116 #endif // cpPlugins_Interface_QT4
117 }
118
119 // -------------------------------------------------------------------------
120 void cpPlugins::Interface::Plugins::
121 AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
122 {
123 #ifdef cpPlugins_Interface_QT4
124   std::map< std::string, std::set< std::string > >::const_iterator i;
125   std::set< std::string >::const_iterator j;
126
127   menu->clear( );
128   for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
129   {
130     QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
131     for( j = i->second.begin( ); j != i->second.end( ); ++j )
132     {
133       QAction* a = newMenu->addAction( j->c_str( ) );
134       QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
135
136     } // rof
137
138   } // rof
139 #endif // cpPlugins_Interface_QT4
140 }
141
142 // -------------------------------------------------------------------------
143 cpPlugins::Interface::
144 BasePluginsApplication* cpPlugins::Interface::Plugins::
145 GetApplication( )
146 {
147   return( this->m_Application );
148 }
149
150 // -------------------------------------------------------------------------
151 const cpPlugins::Interface::
152 BasePluginsApplication* cpPlugins::Interface::Plugins::
153 GetApplication( ) const
154 {
155   return( this->m_Application );
156 }
157
158 // -------------------------------------------------------------------------
159 void cpPlugins::Interface::Plugins::
160 SetApplication( cpPlugins::Interface::BasePluginsApplication* a )
161 {
162   this->m_Application = a;
163 }
164
165 // -------------------------------------------------------------------------
166 bool cpPlugins::Interface::Plugins::
167 LoadPlugins( const std::string& fname )
168 {
169   this->BlockWidget( );
170
171   // Is it already loaded?
172   bool ret = true;
173   if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
174   {
175     // Was it succesfully loaded?
176     ret = this->m_Interface.Load( fname );
177
178     // Update a simple track
179     if( ret )
180     {
181       this->m_LoadedPlugins.insert( fname );
182       this->m_LastLoadedPlugin = fname;
183       this->_UpdateLoadedPluginsInformation( );
184
185     } // fi
186
187   } // fi
188
189   this->UnblockWidget( );
190   return( ret );
191 }
192
193 // -------------------------------------------------------------------------
194 bool cpPlugins::Interface::Plugins::
195 LoadPluginsConfigurationFile( const std::string& fname )
196 {
197   // Load file into a char buffer
198   std::ifstream in(
199     fname.c_str( ), std::ios::in | std::ios::binary | std::ios::ate
200     );
201   if( !in.is_open( ) )
202     return( false );
203
204   std::streampos size = in.tellg( );
205   char* buffer = new char[ size ];
206   in.seekg( 0, std::ios::beg );
207   in.read( buffer, size );
208   in.close( );
209
210   // Read stream
211   std::stringstream in_stream( buffer );
212   char line[ 4096 ];
213   while( in_stream )
214   {
215     in_stream.getline( line, 4096 );
216     this->LoadPlugins( line );
217
218   } // elihw
219   delete buffer;
220
221   return( true );
222 }
223
224 // -------------------------------------------------------------------------
225 void cpPlugins::Interface::Plugins::
226 AddInteractor( vtkRenderWindowInteractor* interactor )
227 {
228   this->m_Interactors.insert( interactor );
229 }
230
231 // -------------------------------------------------------------------------
232 void cpPlugins::Interface::Plugins::
233 RemoveInteractor( vtkRenderWindowInteractor* interactor )
234 {
235   this->m_Interactors.erase( interactor );
236 }
237
238 // -------------------------------------------------------------------------
239 void cpPlugins::Interface::Plugins::
240 ClearInteractors( )
241 {
242   this->m_Interactors.clear( );
243 }
244
245 // -------------------------------------------------------------------------
246 bool cpPlugins::Interface::Plugins::
247 HasImageReader( ) const
248 {
249   return( this->m_ImageReader.IsNotNull( ) );
250 }
251
252 // -------------------------------------------------------------------------
253 bool cpPlugins::Interface::Plugins::
254 HasDicomSeriesReader( ) const
255 {
256   return( this->m_DicomSeriesReader.IsNotNull( ) );
257 }
258
259 // -------------------------------------------------------------------------
260 bool cpPlugins::Interface::Plugins::
261 HasMeshReader( ) const
262 {
263   return( this->m_MeshReader.IsNotNull( ) );
264 }
265
266 // -------------------------------------------------------------------------
267 bool cpPlugins::Interface::Plugins::
268 HasImageWriter( ) const
269 {
270   return( this->m_ImageWriter.IsNotNull( ) );
271 }
272
273 // -------------------------------------------------------------------------
274 bool cpPlugins::Interface::Plugins::
275 HasMeshWriter( ) const
276 {
277   return( this->m_MeshWriter.IsNotNull( ) );
278 }
279
280 // -------------------------------------------------------------------------
281 std::string cpPlugins::Interface::Plugins::
282 ReadImage( const std::string& fname, const std::string& parent )
283 {
284   std::vector< std::string > fnames;
285   fnames.push_back( fname );
286   return( this->ReadImage( fnames, parent ) );
287 }
288
289 // -------------------------------------------------------------------------
290 std::string cpPlugins::Interface::Plugins::
291 ReadImage(
292   const std::vector< std::string >& fnames, const std::string& parent
293   )
294 {
295   // Check if object exists
296   if( this->m_ImageReader.IsNull( ) )
297     return( "" );
298
299   // Configure object
300   TParameters* params = this->m_ImageReader->GetParameters( );
301   params->ClearStringList( "FileNames" );
302   auto i = fnames.begin( );
303   for( ; i != fnames.end( ); ++i )
304     params->AddToStringList( "FileNames", *i );
305
306   // Execute filter
307   this->BlockWidget( );
308   std::string err = this->m_ImageReader->Update( );
309   this->UnblockWidget( );
310
311   // Get result, if any
312   if( err == "" )
313   {
314     TImage* image = this->m_ImageReader->GetOutput< TImage >( "Output" );
315     this->m_ImageReader->DisconnectOutputs( );
316
317     // Add newly added data
318     if( this->_InsertNewData( image, parent ) )
319       return( image->GetName( ) );
320     else
321       return( "" );
322   }
323   else
324   {
325 #ifdef cpPlugins_Interface_QT4
326     if( this->m_Widget != NULL )
327       QMessageBox::critical(
328         this->m_Widget,
329         QMessageBox::tr( "Error reading image." ),
330         QMessageBox::tr( err.c_str( ) )
331         );
332 #else // cpPlugins_Interface_QT4
333     std::cerr << "Error reading image: " << err << std::endl;
334 #endif // cpPlugins_Interface_QT4
335     return( "" );
336
337   } // fi
338 }
339
340 // -------------------------------------------------------------------------
341 std::string cpPlugins::Interface::Plugins::
342 ReadImage( const std::string& parent )
343 {
344   // Check if object exists
345   if( this->m_ImageReader.IsNull( ) )
346     return( "" );
347
348   // Configure object
349   TProcessObject::DialogResult dret =
350     this->m_ImageReader->ExecConfigurationDialog( this->m_Widget );
351   if( dret == TProcessObject::DialogResult_Cancel )
352     return( "" );
353
354   // Execute filter
355   this->BlockWidget( );
356   std::string err = this->m_ImageReader->Update( );
357   this->UnblockWidget( );
358
359   // Get result, if any
360   if( err == "" )
361   {
362     TImage* image = this->m_ImageReader->GetOutput< TImage >( "Output" );
363     this->m_ImageReader->DisconnectOutputs( );
364
365     // Add newly added data
366     if( this->_InsertNewData( image, parent ) )
367       return( image->GetName( ) );
368     else
369       return( "" );
370   }
371   else
372   {
373 #ifdef cpPlugins_Interface_QT4
374     if( this->m_Widget != NULL )
375       QMessageBox::critical(
376         this->m_Widget,
377         QMessageBox::tr( "Error reading image." ),
378         QMessageBox::tr( err.c_str( ) )
379         );
380 #else // cpPlugins_Interface_QT4
381     std::cerr << "Error reading image: " << err << std::endl;
382 #endif // cpPlugins_Interface_QT4
383     return( "" );
384
385   } // fi
386 }
387
388 // -------------------------------------------------------------------------
389 std::string cpPlugins::Interface::Plugins::
390 ReadDicomSeries( const std::string& parent )
391 {
392   // Check if object exists
393   if( this->m_DicomSeriesReader.IsNull( ) )
394     return( "" );
395
396   // Configure object
397   TProcessObject::DialogResult dret =
398     this->m_DicomSeriesReader->ExecConfigurationDialog( this->m_Widget );
399   if( dret == TProcessObject::DialogResult_Cancel )
400     return( "" );
401
402   // Execute filter
403   this->BlockWidget( );
404   std::string err = this->m_DicomSeriesReader->Update( );
405   this->UnblockWidget( );
406
407   // Get result, if any
408   if( err == "" )
409   {
410     TImage* image =
411       this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
412     this->m_DicomSeriesReader->DisconnectOutputs( );
413
414     // Add newly added data
415     if( this->_InsertNewData( image, parent ) )
416       return( image->GetName( ) );
417     else
418       return( "" );
419   }
420   else
421   {
422 #ifdef cpPlugins_Interface_QT4
423     if( this->m_Widget != NULL )
424       QMessageBox::critical(
425         this->m_Widget,
426         QMessageBox::tr( "Error reading image." ),
427         QMessageBox::tr( err.c_str( ) )
428         );
429 #else // cpPlugins_Interface_QT4
430     std::cerr << "Error reading image: " << err << std::endl;
431 #endif // cpPlugins_Interface_QT4
432     return( "" );
433
434   } // fi
435 }
436
437 // -------------------------------------------------------------------------
438 std::string cpPlugins::Interface::Plugins::
439 ReadMesh( const std::string& fname, const std::string& parent )
440 {
441   // Check if object exists
442   if( this->m_MeshReader.IsNull( ) )
443     return( "" );
444
445   // Configure object
446   TParameters* params = this->m_MeshReader->GetParameters( );
447   params->SetString( "FileName", fname );
448
449   // Execute filter
450   this->BlockWidget( );
451   std::string err = this->m_MeshReader->Update( );
452   this->UnblockWidget( );
453
454   // Get result, if any
455   if( err == "" )
456   {
457     TMesh* mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
458     this->m_MeshReader->DisconnectOutputs( );
459
460     // Add newly added data
461     if( this->_InsertNewData( mesh, parent ) )
462       return( mesh->GetName( ) );
463     else
464       return( "" );
465   }
466   else
467   {
468 #ifdef cpPlugins_Interface_QT4
469     if( this->m_Widget != NULL )
470       QMessageBox::critical(
471         this->m_Widget,
472         QMessageBox::tr( "Error reading mesh." ),
473         QMessageBox::tr( err.c_str( ) )
474         );
475 #else // cpPlugins_Interface_QT4
476     std::cerr << "Error reading mesh: " << err << std::endl;
477 #endif // cpPlugins_Interface_QT4
478     return( "" );
479
480   } // fi
481 }
482
483 // -------------------------------------------------------------------------
484 std::string cpPlugins::Interface::Plugins::
485 ReadMesh( const std::string& parent )
486 {
487   // Check if object exists
488   if( this->m_MeshReader.IsNull( ) )
489     return( "" );
490
491   // Configure object
492   TProcessObject::DialogResult dret =
493     this->m_MeshReader->ExecConfigurationDialog( this->m_Widget );
494   if( dret == TProcessObject::DialogResult_Cancel )
495     return( "" );
496
497   // Execute filter
498   this->BlockWidget( );
499   std::string err = this->m_MeshReader->Update( );
500   this->UnblockWidget( );
501
502   // Get result, if any
503   if( err == "" )
504   {
505     TMesh* mesh = this->m_MeshReader->GetOutput< TMesh >( "Output" );
506     this->m_MeshReader->DisconnectOutputs( );
507
508     // Add newly added data
509     if( this->_InsertNewData( mesh, parent ) )
510       return( mesh->GetName( ) );
511     else
512       return( "" );
513   }
514   else
515   {
516 #ifdef cpPlugins_Interface_QT4
517     if( this->m_Widget != NULL )
518       QMessageBox::critical(
519         this->m_Widget,
520         QMessageBox::tr( "Error reading mesh." ),
521         QMessageBox::tr( err.c_str( ) )
522         );
523 #else // cpPlugins_Interface_QT4
524     std::cerr << "Error reading mesh: " << err << std::endl;
525 #endif // cpPlugins_Interface_QT4
526     return( "" );
527
528   } // fi
529 }
530
531 // -------------------------------------------------------------------------
532 bool cpPlugins::Interface::Plugins::
533 WriteImage( const std::string& fname, const std::string& name )
534 {
535   // Check if objects exist
536   if( this->m_ImageWriter.IsNull( ) )
537     return( false );
538   TImage* image = this->GetImage( name );
539   if( image == NULL )
540     return( false );
541
542   // Configure writer
543   this->m_ImageWriter->GetParameters( )->SetString( "FileName", fname );
544   this->m_ImageWriter->SetInput( "Input", image );
545
546   // Execute filter
547   this->BlockWidget( );
548   std::string err = this->m_ImageWriter->Update( );
549   this->UnblockWidget( );
550
551   // Get result, if any
552   if( err != "" )
553   {
554 #ifdef cpPlugins_Interface_QT4
555     if( this->m_Widget != NULL )
556       QMessageBox::critical(
557         this->m_Widget,
558         QMessageBox::tr( "Error reading mesh." ),
559         QMessageBox::tr( err.c_str( ) )
560         );
561 #else // cpPlugins_Interface_QT4
562     std::cerr << "Error reading mesh: " << err << std::endl;
563 #endif // cpPlugins_Interface_QT4
564     return( false );
565   }
566   else
567     return( true );
568 }
569
570 // -------------------------------------------------------------------------
571 bool cpPlugins::Interface::Plugins::
572 WriteImage( const std::string& name )
573 {
574   // Check if objects exist
575   if( this->m_ImageWriter.IsNull( ) )
576     return( false );
577   TImage* image = this->GetImage( name );
578   if( image == NULL )
579     return( false );
580
581   // Configure writer
582   TProcessObject::DialogResult dret = 
583     this->m_ImageWriter->ExecConfigurationDialog( this->m_Widget );
584   if( dret == TProcessObject::DialogResult_Cancel )
585     return( "" );
586   this->m_ImageWriter->SetInput( "Input", image );
587
588   // Execute filter
589   this->BlockWidget( );
590   std::string err = this->m_ImageWriter->Update( );
591   this->UnblockWidget( );
592
593   // Get result, if any
594   if( err != "" )
595   {
596 #ifdef cpPlugins_Interface_QT4
597     if( this->m_Widget != NULL )
598       QMessageBox::critical(
599         this->m_Widget,
600         QMessageBox::tr( "Error reading mesh." ),
601         QMessageBox::tr( err.c_str( ) )
602         );
603 #else // cpPlugins_Interface_QT4
604     std::cerr << "Error reading mesh: " << err << std::endl;
605 #endif // cpPlugins_Interface_QT4
606     return( false );
607   }
608   else
609     return( true );
610 }
611
612 // -------------------------------------------------------------------------
613 bool cpPlugins::Interface::Plugins::
614 WriteMesh( const std::string& fname, const std::string& name )
615 {
616   // Check if objects exist
617   if( this->m_MeshWriter.IsNull( ) )
618     return( false );
619   TMesh* mesh = this->GetMesh( name );
620   if( mesh == NULL )
621     return( false );
622
623   // Configure writer
624   this->m_MeshWriter->GetParameters( )->SetString( "FileName", fname );
625   this->m_MeshWriter->SetInput( "Input", mesh );
626
627   // Execute filter
628   this->BlockWidget( );
629   std::string err = this->m_MeshWriter->Update( );
630   this->UnblockWidget( );
631
632   // Get result, if any
633   if( err != "" )
634   {
635 #ifdef cpPlugins_Interface_QT4
636     if( this->m_Widget != NULL )
637       QMessageBox::critical(
638         this->m_Widget,
639         QMessageBox::tr( "Error reading mesh." ),
640         QMessageBox::tr( err.c_str( ) )
641         );
642 #else // cpPlugins_Interface_QT4
643     std::cerr << "Error reading mesh: " << err << std::endl;
644 #endif // cpPlugins_Interface_QT4
645     return( false );
646   }
647   else
648     return( true );
649 }
650
651 // -------------------------------------------------------------------------
652 bool cpPlugins::Interface::Plugins::
653 WriteMesh( const std::string& name )
654 {
655   // Check if objects exist
656   if( this->m_MeshWriter.IsNull( ) )
657     return( false );
658   TMesh* mesh = this->GetMesh( name );
659   if( mesh == NULL )
660     return( false );
661
662   // Configure writer
663   TProcessObject::DialogResult dret = 
664     this->m_MeshWriter->ExecConfigurationDialog( this->m_Widget );
665   if( dret == TProcessObject::DialogResult_Cancel )
666     return( "" );
667   this->m_MeshWriter->SetInput( "Input", mesh );
668
669   // Execute filter
670   this->BlockWidget( );
671   std::string err = this->m_MeshWriter->Update( );
672   this->UnblockWidget( );
673
674   // Get result, if any
675   if( err != "" )
676   {
677 #ifdef cpPlugins_Interface_QT4
678     if( this->m_Widget != NULL )
679       QMessageBox::critical(
680         this->m_Widget,
681         QMessageBox::tr( "Error reading mesh." ),
682         QMessageBox::tr( err.c_str( ) )
683         );
684 #else // cpPlugins_Interface_QT4
685     std::cerr << "Error reading mesh: " << err << std::endl;
686 #endif // cpPlugins_Interface_QT4
687     return( false );
688   }
689   else
690     return( true );
691 }
692
693 // -------------------------------------------------------------------------
694 void cpPlugins::Interface::Plugins::
695 ClearDataObjects( )
696 {
697   this->m_Objects.clear( );
698 }
699
700 // -------------------------------------------------------------------------
701 void cpPlugins::Interface::Plugins::
702 DeleteDataObject( const std::string& name )
703 {
704   auto i = this->m_Objects.find( name );
705   if( i != this->m_Objects.end( ) )
706   {
707     this->m_Objects.erase( i );
708
709     // Get children
710     std::vector< std::string > children;
711     for( i = this->m_Objects.begin( ); i != this->m_Objects.end( ); ++i )
712       if( i->second.first == name )
713         children.push_back( i->first );
714
715     // Erase children
716     auto c = children.begin( );
717     for( ; c != children.end( ); ++c )
718       this->DeleteDataObject( *c );
719     
720   } // fi
721 }
722
723 // -------------------------------------------------------------------------
724 std::string cpPlugins::Interface::Plugins::
725 GetParent( const std::string& name ) const
726 {
727   auto i = this->m_Objects.find( name );
728   if( i != this->m_Objects.end( ) )
729     return( i->second.first );
730   else
731     return( "" );
732 }
733
734 // -------------------------------------------------------------------------
735 const cpPlugins::Interface::Plugins::
736 TTree& cpPlugins::Interface::Plugins::
737 GetDataObjects( ) const
738 {
739   return( this->m_Objects );
740 }
741
742 // -------------------------------------------------------------------------
743 cpPlugins::Interface::Plugins::
744 TDataObject* cpPlugins::Interface::Plugins::
745 GetDataObject( const std::string& name )
746 {
747   auto i = this->m_Objects.find( name );
748   if( i != this->m_Objects.end( ) )
749     return( dynamic_cast< TDataObject* >( i->second.second.GetPointer( ) ) );
750   else
751     return( NULL );
752 }
753
754 // -------------------------------------------------------------------------
755 const cpPlugins::Interface::Plugins::
756 TDataObject* cpPlugins::Interface::Plugins::
757 GetDataObject( const std::string& name ) const
758 {
759   auto i = this->m_Objects.find( name );
760   if( i != this->m_Objects.end( ) )
761     return(
762       dynamic_cast< const TDataObject* >( i->second.second.GetPointer( ) )
763       );
764   else
765     return( NULL );
766 }
767
768 // -------------------------------------------------------------------------
769 cpPlugins::Interface::Plugins::
770 TImage* cpPlugins::Interface::Plugins::
771 GetImage( const std::string& name )
772 {
773   auto i = this->m_Objects.find( name );
774   if( i != this->m_Objects.end( ) )
775     return( dynamic_cast< TImage* >( i->second.second.GetPointer( ) ) );
776   else
777     return( NULL );
778 }
779
780 // -------------------------------------------------------------------------
781 const cpPlugins::Interface::Plugins::
782 TImage* cpPlugins::Interface::Plugins::
783 GetImage( const std::string& name ) const
784 {
785   auto i = this->m_Objects.find( name );
786   if( i != this->m_Objects.end( ) )
787     return( dynamic_cast< const TImage* >( i->second.second.GetPointer( ) ) );
788   else
789     return( NULL );
790 }
791
792 // -------------------------------------------------------------------------
793 cpPlugins::Interface::Plugins::
794 TMesh* cpPlugins::Interface::Plugins::
795 GetMesh( const std::string& name )
796 {
797   auto i = this->m_Objects.find( name );
798   if( i != this->m_Objects.end( ) )
799     return( dynamic_cast< TMesh* >( i->second.second.GetPointer( ) ) );
800   else
801     return( NULL );
802 }
803
804 // -------------------------------------------------------------------------
805 const cpPlugins::Interface::Plugins::
806 TMesh* cpPlugins::Interface::Plugins::
807 GetMesh( const std::string& name ) const
808 {
809   auto i = this->m_Objects.find( name );
810   if( i != this->m_Objects.end( ) )
811     return( dynamic_cast< const TMesh* >( i->second.second.GetPointer( ) ) );
812   else
813     return( NULL );
814 }
815
816 // -------------------------------------------------------------------------
817 bool cpPlugins::Interface::Plugins::
818 ActivateFilter( const std::string& name )
819 {
820   this->m_ActiveFilter = this->m_Interface.CreateProcessObject( name );
821   if( this->m_ActiveFilter.IsNotNull( ) )
822   {
823     this->m_ActiveFilter->SetPlugins( this );
824     this->m_ActiveFilterOutputs.clear( );
825     auto i = this->m_Interactors.begin( );
826     for( ; i != this->m_Interactors.end( ); ++i )
827       this->m_ActiveFilter->AddInteractor( *i );
828     return( true );
829   }
830   else
831     return( false );
832 }
833
834 // -------------------------------------------------------------------------
835 void cpPlugins::Interface::Plugins::
836 DeactivateFilter( )
837 {
838   this->m_ActiveFilter = NULL;
839 }
840
841 // -------------------------------------------------------------------------
842 bool cpPlugins::Interface::Plugins::
843 HasActiveFilter( ) const
844 {
845   return( this->m_ActiveFilter.IsNotNull( ) );
846 }
847
848 // -------------------------------------------------------------------------
849 bool cpPlugins::Interface::Plugins::
850 IsActiveFilterInteractive( ) const
851 {
852   if( this->m_ActiveFilter.IsNotNull( ) )
853     return( this->m_ActiveFilter->IsInteractive( ) );
854   else
855     return( false );
856 }
857
858 // -------------------------------------------------------------------------
859 unsigned int cpPlugins::Interface::Plugins::
860 GetNumberOfInputsInActiveFilter( ) const
861 {
862   if( this->m_ActiveFilter.IsNotNull( ) )
863     return( this->m_ActiveFilter->GetNumberOfInputs( ) );
864   else
865     return( 0 );
866 }
867
868 // -------------------------------------------------------------------------
869 unsigned int cpPlugins::Interface::Plugins::
870 GetNumberOfOutputsInActiveFilter( ) const
871 {
872   if( this->m_ActiveFilter.IsNotNull( ) )
873     return( this->m_ActiveFilter->GetNumberOfOutputs( ) );
874   else
875     return( 0 );
876 }
877
878 // -------------------------------------------------------------------------
879 std::vector< std::string > cpPlugins::Interface::Plugins::
880 GetActiveFilterInputsNames( ) const
881 {
882   if( this->m_ActiveFilter.IsNotNull( ) )
883     return( this->m_ActiveFilter->GetInputsNames( ) );
884   else
885     return( std::vector< std::string >( ) );
886 }
887
888 // -------------------------------------------------------------------------
889 std::vector< std::string > cpPlugins::Interface::Plugins::
890 GetActiveFilterOutputsNames( ) const
891 {
892   if( this->m_ActiveFilter.IsNotNull( ) )
893     return( this->m_ActiveFilter->GetOutputsNames( ) );
894   else
895     return( std::vector< std::string >( ) );
896 }
897
898 // -------------------------------------------------------------------------
899 void cpPlugins::Interface::Plugins::
900 ConnectInputInActiveFilter(
901   const std::string& object_name, const std::string& input
902   )
903 {
904   if( this->m_ActiveFilter.IsNotNull( ) )
905   {
906     TDataObject* dobj = this->GetDataObject( object_name );
907     if( dobj != NULL )
908       this->m_ActiveFilter->SetInput( input, dobj );
909
910   } // fi
911 }
912
913 // -------------------------------------------------------------------------
914 void cpPlugins::Interface::Plugins::
915 SetOutputNameInActiveFilter(
916   const std::string& new_name, const std::string& output
917   )
918 {
919   this->m_ActiveFilterOutputs[ output ] = new_name;
920 }
921
922 // -------------------------------------------------------------------------
923 cpPlugins::Interface::Plugins::
924 TParameters* cpPlugins::Interface::Plugins::
925 GetActiveFilterParameters( )
926 {
927   if( this->m_ActiveFilter.IsNotNull( ) )
928     return( this->m_ActiveFilter->GetParameters( ) );
929   else
930     return( NULL );
931 }
932
933 // -------------------------------------------------------------------------
934 const cpPlugins::Interface::Plugins::
935 TParameters* cpPlugins::Interface::Plugins::
936 GetActiveFilterParameters( ) const
937 {
938   if( this->m_ActiveFilter.IsNotNull( ) )
939     return( this->m_ActiveFilter->GetParameters( ) );
940   else
941     return( NULL );
942 }
943
944 // -------------------------------------------------------------------------
945 cpPlugins::Interface::Plugins::
946 TProcessObject::DialogResult cpPlugins::Interface::Plugins::
947 ConfigureActiveFilter( )
948 {
949   if( this->m_ActiveFilter.IsNotNull( ) )
950     return( this->m_ActiveFilter->ExecConfigurationDialog( this->m_Widget ) );
951   else
952     return( TProcessObject::DialogResult_Cancel );
953 }
954
955 // -------------------------------------------------------------------------
956 std::string cpPlugins::Interface::Plugins::
957 UpdateActiveFilter( std::vector< std::string >& outputs )
958 {
959   // Execute filter
960   this->BlockWidget( );
961   std::string err = this->m_ActiveFilter->Update( );
962   this->UnblockWidget( );
963
964   // Associate outputs
965   outputs.clear( );
966   if( err == "" )
967   {
968     std::string parent = "";
969     if( this->GetNumberOfInputsInActiveFilter( ) > 0 )
970     {
971       std::string input = this->m_ActiveFilter->GetInputsNames( )[ 0 ];
972       parent =
973         this->m_ActiveFilter->GetInput< TDataObject >( input )->GetName( );
974
975     } // fi
976
977     auto i = this->m_ActiveFilterOutputs.begin( );
978     for( ; i != this->m_ActiveFilterOutputs.end( ); ++i )
979     {
980       TDataObject* out =
981         this->m_ActiveFilter->GetOutput< TDataObject >( i->first );
982       out->SetName( i->second );
983       outputs.push_back( out->GetName( ) );
984       this->_InsertNewData( out, parent );
985
986     } // rof
987
988   } // fi
989   return( err );
990 }
991
992 // -------------------------------------------------------------------------
993 void cpPlugins::Interface::Plugins::
994 _UpdateLoadedPluginsInformation( )
995 {
996   typedef TInterface::TClasses _C;
997
998   this->m_Filters.clear( );
999   _C& classes = this->m_Interface.GetClasses( );
1000   for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
1001   {
1002     TProcessObject::Pointer o =
1003       this->m_Interface.CreateProcessObject( i->first );
1004     std::string name = o->GetClassName( );
1005     std::string category = o->GetClassCategory( );
1006     if( category == "ImageReader" )
1007       this->m_ImageReader = o;
1008     else if( category == "ImageWriter" )
1009       this->m_ImageWriter = o;
1010     else if( category == "MeshReader" )
1011       this->m_MeshReader = o;
1012     else if( category == "MeshWriter" )
1013       this->m_MeshWriter = o;
1014     else if( category == "DicomSeriesReader" )
1015       this->m_DicomSeriesReader = o;
1016     else
1017       this->m_Filters[ category ].insert( name );
1018
1019   } // rof
1020 }
1021
1022 // -------------------------------------------------------------------------
1023 bool cpPlugins::Interface::Plugins::
1024 _InsertNewData( TDataObject* dobj, const std::string& parent )
1025 {
1026   std::string name = dobj->GetName( );
1027   auto i = this->m_Objects.find( name );
1028   bool ret = true;
1029   if( i == this->m_Objects.end( ) )
1030   {
1031     if( parent != "" )
1032     {
1033       auto j = this->m_Objects.find( parent );
1034       if( j != this->m_Objects.end( ) )
1035         this->m_Objects[ name ] = TTreeNode( parent, dobj );
1036       else
1037         ret = false;
1038     }
1039     else
1040       this->m_Objects[ name ] = TTreeNode( "", dobj );
1041   }
1042   else
1043     i->second.second = dobj;
1044
1045   if( !ret )
1046   {
1047 #ifdef cpPlugins_Interface_QT4
1048     if( this->m_Widget != NULL )
1049       QMessageBox::critical(
1050         this->m_Widget,
1051         QMessageBox::tr( "Error inserting data." ),
1052         QMessageBox::tr( "Given parent does not exists." )
1053         );
1054 #else // cpPlugins_Interface_QT4
1055     std::cerr
1056       << "Error inserting data: Given parent does not exists."
1057       << std::endl;
1058 #endif // cpPlugins_Interface_QT4
1059   } // fi
1060   return( ret );
1061 }
1062
1063 // eof - $RCSfile$