]> Creatis software - cpPlugins.git/blob - appli/PipelineEditor/PipelineEditor.cxx
edac828cd0a5c0e0860eedf82e9e6e3b40a85025
[cpPlugins.git] / appli / PipelineEditor / PipelineEditor.cxx
1 #include <PipelineEditor.h>
2 #include <ui_PipelineEditor.h>
3
4 #include <cpPlugins/Utility.h>
5 #include <cpExtensions/QT/SimpleMPRWidget.h>
6 #include <cpExtensions/QT/ImageWidget.h>
7 #include <cpExtensions/QT/ConfigurationChooser.h>
8 #include <cpExtensions/QT/ActorsWidgetInterface.h>
9 #include <vtkImageData.h>
10 #include <vtkPolyData.h>
11
12 // -------------------------------------------------------------------------
13 PipelineEditor::
14 PipelineEditor( int argc, char* argv[], QWidget* parent )
15   : Superclass( argc, argv, parent ),
16     m_UI( new Ui::PipelineEditor ),
17     m_ActiveWS( "ws" )
18 {
19   this->m_UI->setupUi( this );
20   this->m_UI->Navigator->Update( );
21
22   cpBaseQtApplication_ConnectAction(
23     actionLoadDirectory, _loadPluginsFromPath
24     );
25   cpBaseQtApplication_ConnectAction(
26     actionLoadLibrary, _loadPlugins
27     );
28
29   this->connect(
30     this->m_UI->actionOpenWorkspace, SIGNAL( triggered( ) ),
31     this, SLOT( _slotOpenWorkspace( ) )
32     );
33   this->connect(
34     this->m_UI->actionSaveWorkspace, SIGNAL( triggered( ) ),
35     this, SLOT( _slotSaveWorkspace( ) )
36     );
37   this->connect(
38     this->m_UI->actionSaveWorkspaceAs, SIGNAL( triggered( ) ),
39     this, SLOT( _slotSaveWorkspaceAs( ) )
40     );
41   this->connect(
42     this->m_UI->actionActorsProperties, SIGNAL( triggered( ) ),
43     this, SLOT( _slotActorsProperties( ) )
44     );
45   this->m_UI->Canvas->connectOutputPortSlot(
46     this, SLOT( _slotView( const std::string&, bool ) )
47     );
48
49   // Load command-line given workspace
50   if( argc > 1 )
51   {
52     this->_loadWorkspace( argv[ 1 ] );
53     this->m_ActiveWS = argv[ 1 ];
54   }
55   else
56     this->_addWorkspace( this->m_ActiveWS );
57   this->m_UI->Canvas->setWorkspace( this->workspace( this->m_ActiveWS ) );
58   this->setWindowTitle(
59     (
60       std::string( "PipelineEditor - " ) +
61       this->m_ActiveWS
62       ).c_str( )
63     );
64
65   this->setCanvas( this->m_UI->Canvas );
66   this->setNavigator( this->m_UI->Navigator );
67 }
68
69 // -------------------------------------------------------------------------
70 PipelineEditor::
71 ~PipelineEditor( )
72 {
73   delete this->m_UI;
74 }
75
76 // -------------------------------------------------------------------------
77 template< class _TWidget >
78 std::pair< _TWidget*, bool > PipelineEditor::
79 _configureViewer( )
80 {
81   auto new_viewer = dynamic_cast< _TWidget* >( this->m_UI->Viewer );
82   bool ok = false;
83   if( new_viewer == NULL )
84   {
85     new_viewer = new _TWidget( );
86     delete this->m_UI->Viewer;
87     this->m_UI->Viewer = new_viewer;
88     ok = true;
89
90     auto interactors = new_viewer->GetInteractors( );
91     for( auto w : this->m_Workspaces )
92       for( auto i : interactors )
93         w.second->AddInteractor( i );
94
95   } // fi
96   this->m_UI->MainSplitter->insertWidget( 0, this->m_UI->Viewer );
97   return( std::pair< _TWidget*, bool >( new_viewer, ok ) );
98 }
99
100 // -------------------------------------------------------------------------
101 void PipelineEditor::
102 _addWorkspace( const std::string& name )
103 {
104   typedef cpExtensions::QT::ActorsWidgetInterface _TInterface;
105
106   this->Superclass::_addWorkspace( name );
107   auto ws = this->m_Workspaces.find( name );
108   if( ws != this->m_Workspaces.end( ) )
109   {
110     ws->second->PrintExecutionOn( );
111     auto wdg = dynamic_cast< _TInterface* >( this->m_UI->Viewer );
112     if( wdg == NULL )
113       return;
114     auto interactors = wdg->GetInteractors( );
115     for( auto i : interactors )
116       ws->second->AddInteractor( i );
117
118   } // fi
119 }
120
121 // -------------------------------------------------------------------------
122 void PipelineEditor::
123 _slotOpenWorkspace( )
124 {
125   this->m_LastSaveFileName = "";
126   this->_loadWorkspace( );
127   if( this->m_Workspaces.size( ) == 2 )
128   {
129     auto wIt = this->m_Workspaces.find( this->m_ActiveWS );
130     this->m_Workspaces.erase( wIt );
131     this->m_ActiveWS = this->m_Workspaces.begin( )->first;
132   }
133   else if( this->m_Workspaces.size( ) == 1 )
134   {
135     this->m_ActiveWS = this->m_Workspaces.begin( )->first;
136   }
137   else
138   {
139     this->m_ActiveWS = "ws";
140     this->_addWorkspace( this->m_ActiveWS );
141
142   } // fi
143   this->m_UI->Canvas->setWorkspace( this->workspace( this->m_ActiveWS ) );
144   this->setWindowTitle(
145     (
146       std::string( "PipelineEditor - " ) +
147       this->m_ActiveWS
148       ).c_str( )
149     );
150 }
151
152 // -------------------------------------------------------------------------
153 void PipelineEditor::
154 _slotSaveWorkspace( )
155 {
156   this->_saveWorkspace( this->m_ActiveWS, false );
157   this->setWindowTitle(
158     (
159       std::string( "PipelineEditor - " ) +
160       this->m_LastSaveFileName
161       ).c_str( )
162     );
163 }
164
165 // -------------------------------------------------------------------------
166 void PipelineEditor::
167 _slotSaveWorkspaceAs( )
168 {
169   this->_saveWorkspace( this->m_ActiveWS, true );
170   this->setWindowTitle(
171     (
172       std::string( "PipelineEditor - " ) +
173       this->m_LastSaveFileName
174       ).c_str( )
175     );
176 }
177
178 // -------------------------------------------------------------------------
179 void PipelineEditor::
180 _slotView( const std::string& name, bool show )
181 {
182   std::vector< std::string > tokens;
183   cpPlugins::Tokenize( tokens, name, "@" );
184   if( tokens.size( ) != 2 )
185     return;
186   try
187   {
188     auto ws = this->workspace( this->m_ActiveWS );
189     auto filter = ws->GetFilter( tokens[ 1 ] );
190     if( filter != NULL )
191     {
192       cpBaseQtApplication_Execute( filter->Update( ) );
193       auto image = filter->GetOutputData< vtkImageData >( tokens[ 0 ] );
194       auto mesh = filter->GetOutputData< vtkPolyData >( tokens[ 0 ] );
195       if( image != NULL )
196       {
197         int dim = image->GetDataDimension( );
198         if( dim == 2 )
199         {
200           auto viewer =
201             this->_configureViewer< cpExtensions::QT::ImageWidget >( );
202           this->m_Blocker.block( );
203           viewer.first->SetImage( image, 2, name );
204           viewer.first->ResetCamera( );
205           viewer.first->Render( );
206           this->m_Blocker.unblock( );
207         }
208         else if( dim == 3 )
209         {
210           auto viewer =
211             this->_configureViewer< cpExtensions::QT::SimpleMPRWidget >( );
212           this->m_Blocker.block( );
213           viewer.first->SetImage( image, name );
214           viewer.first->ResetCameras( );
215           viewer.first->Render( );
216           this->m_Blocker.unblock( );
217
218         } // fi
219       }
220       else if( mesh != NULL )
221       {
222         auto viewer =
223           dynamic_cast< cpExtensions::QT::SimpleMPRWidget* >(
224             this->m_UI->Viewer
225             );
226         if( viewer != NULL )
227         {
228           this->m_Blocker.block( );
229           viewer->Add( mesh, name );
230           viewer->Render( );
231           this->m_Blocker.unblock( );
232
233         } // fi
234
235       } // fi
236
237     } // fi
238   }
239   catch( std::exception& err )
240   {
241     QMessageBox::critical(
242       NULL,
243       QMessageBox::tr( "Error showing data" ),
244       QMessageBox::tr( err.what( ) )
245       );
246
247   } // yrt
248 }
249
250 // -------------------------------------------------------------------------
251 void PipelineEditor::
252 _slotActorsProperties( )
253 {
254   auto data =
255     dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
256       this->m_UI->Viewer
257       );
258   if( data != NULL )
259   {
260     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
261     dlg->setData( data );
262     dlg->exec( );
263
264   } // fi
265 }
266
267 // -------------------------------------------------------------------------
268 #include <cpBaseQtApplication/MainHelper.h>
269 cpBaseQtApplication_Main( PipelineEditor );
270 cpBaseQtApplication_MainComplement;
271
272 // eof - $RCSfile$