Salome HOME
Suppress warnings.
[modules/visu.git] / src / VISUGUI / VisuGUI_EvolutionDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  File   : VisuGUI_EvolutionDlg.cxx
21 //  Author : Oleg UVAROV
22 //  Module : VISU
23 //
24 #include "VisuGUI_EvolutionDlg.h"
25
26 #include "VisuGUI.h"
27 #include "VisuGUI_Tools.h"
28 #include "VisuGUI_ViewTools.h"
29
30 #include <VISU_Evolution.h>
31
32 #include <VISU_Gen_i.hh>
33 #include <VISU_Result_i.hh>
34
35 #include <VISU_Convertor.hxx>
36
37 #include <SALOME_InteractiveObject.hxx>
38
39 #include <SUIT_ResourceMgr.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_Desktop.h>
42
43 #include <LightApp_SelectionMgr.h>
44
45 #include <SPlot2d_ViewModel.h>
46 #include <Plot2d_ViewFrame.h>
47
48 #include <QComboBox>
49 #include <QGroupBox>
50 #include <QIntValidator>
51 #include <QKeyEvent>
52 #include <QLabel>
53 #include <QLayout>
54 #include <QLineEdit>
55
56 #include <vtkDataSetMapper.h>
57
58 VisuGUI_EvolutionDlg::VisuGUI_EvolutionDlg( VisuGUI* theModule, _PTR(Study) theStudy ) :
59   QtxDialog( VISU::GetDesktop( theModule ), false, false, QtxDialog::Standard, Qt::Dialog ),
60   myModule( theModule ),
61   myViewWindow( NULL ),
62   myPreviewActor( NULL )
63 {
64   setWindowTitle( tr( "TITLE" ) );
65   setAttribute( Qt::WA_DeleteOnClose, true );
66
67   myEngine = new VISU_Evolution( theStudy );
68
69   QFrame* aMainFrame = mainFrame();
70
71   // Parameters
72   QGroupBox* aParamGrp = new QGroupBox( tr( "PARAMETERS" ), aMainFrame );
73
74   QLabel* aFieldLabel = new QLabel( tr( "FIELD" ), aParamGrp );
75   myFieldLE = new QLineEdit( aParamGrp );
76   myFieldLE->setReadOnly( true );
77
78   QLabel* aPointLabel = new QLabel( tr( "POINT" ) );
79   myPointLE = new QLineEdit( aParamGrp );
80
81   myPointValidator = new QIntValidator( this );
82   myPointLE->setValidator( myPointValidator );
83
84   QLabel* aComponentLabel = new QLabel( tr( "COMPONENT" ) );
85   myComponentCB = new QComboBox( aParamGrp );
86   myComponentCB->setEditable( false );
87
88   QGridLayout* aParamLayout = new QGridLayout( aParamGrp );
89   aParamLayout->setMargin( 11 );
90   aParamLayout->setSpacing( 6 );
91   aParamLayout->addWidget( aFieldLabel,     0, 0 );
92   aParamLayout->addWidget( myFieldLE,       0, 1 );
93   aParamLayout->addWidget( aPointLabel,     1, 0 );
94   aParamLayout->addWidget( myPointLE,       1, 1 );
95   aParamLayout->addWidget( aComponentLabel, 2, 0 );
96   aParamLayout->addWidget( myComponentCB,   2, 1 );
97
98   // Common
99   QBoxLayout* aMainLayout = new QVBoxLayout( aMainFrame );
100   aMainLayout->setMargin( 0 );
101   aMainLayout->setSpacing( 0 );
102   aMainLayout->addWidget( aParamGrp );
103
104   connect( myPointLE, SIGNAL( textEdited( const QString& ) ),
105            this, SLOT( onPointEdited( const QString& ) ) );
106
107   connect( myModule->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),
108            this,                               SLOT( onSelectionEvent() ) );
109
110   connect( this, SIGNAL( dlgHelp() ), this, SLOT( onHelp() ) );
111
112   myStoredSelectionMode = ActorSelection;
113
114   myViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>( myModule );
115   if( myViewWindow )
116   {
117     connect( myViewWindow, SIGNAL( destroyed() ),                 this, SLOT( onViewWindowClosed() ) );
118     connect( myViewWindow, SIGNAL( closing( SUIT_ViewWindow* ) ), this, SLOT( onViewWindowClosed() ) );
119   }
120 }
121
122 //------------------------------------------------------------------------
123 VisuGUI_EvolutionDlg::~VisuGUI_EvolutionDlg()
124 {
125   if( myEngine != NULL )
126   {
127     delete myEngine;
128     myEngine = NULL;
129   }
130 }
131
132 //------------------------------------------------------------------------
133 bool VisuGUI_EvolutionDlg::setField( _PTR(SObject) theField )
134 {
135   if( myEngine->setField( theField ) )
136   {
137     updateFromEngine();
138     return true;
139   }
140   return false;
141 }
142
143 //------------------------------------------------------------------------
144 void VisuGUI_EvolutionDlg::updateFromEngine()
145 {
146   std::string aFieldName = myEngine->getFieldName();
147   myFieldLE->setText( QString( aFieldName.c_str() ) );
148
149   int aNbPoints = myEngine->getNbPoints();
150   myPointValidator->setRange( 0, aNbPoints-1 );
151
152   QStringList aComponentList;
153   VISU::ComponentDataList aComponentDataList = myEngine->getComponentDataList();
154   int aNbComp = aComponentDataList.size();
155
156   bool isModulus;
157   int aComponentId = 0;
158   VISU::ComponentDataListIterator anIter( aComponentDataList );
159   while( anIter.hasNext() )
160   {
161     VISU::ComponentData aComponentData = anIter.next();
162     QString aComponent = aComponentData.first;
163     QString anUnit = aComponentData.second;
164
165     isModulus = aNbComp > 1 && aComponentId == 0;
166     if( !isModulus )
167     {
168       aComponent = "[" + QString::number( aComponentId ) + "] " + aComponent;
169       if( anUnit.isEmpty() )
170         anUnit = "-";
171       aComponent = aComponent + ", " + anUnit;
172     }
173
174     aComponentList << aComponent;
175     aComponentId++;
176   }
177
178   myComponentCB->clear();
179   myComponentCB->addItems( aComponentList );
180
181   bool isScalarMode = ( aNbComp > 1 );
182   myComponentCB->setCurrentIndex( isScalarMode ? 0 : 1 );
183   myComponentCB->setEnabled( isScalarMode );
184 }
185
186 //------------------------------------------------------------------------
187 void VisuGUI_EvolutionDlg::accept()
188 {
189   _PTR(Study) aStudy = VISU::GetCStudy( VISU::GetAppStudy( myModule ) );
190   if( VISU::IsStudyLocked( aStudy ) )
191   {
192     SUIT_MessageBox::warning( VISU::GetDesktop( myModule ),
193                               tr( "WRN_VISU_WARNING" ),
194                               tr( "WRN_STUDY_LOCKED" ) );
195     return;
196   }
197
198   bool ok = false;
199   int aPointId = myPointLE->text().toInt( &ok );
200   if( !ok )
201   {
202     SUIT_MessageBox::warning( VISU::GetDesktop( myModule ),
203                               tr( "WRN_VISU_WARNING" ),
204                               tr( "ERR_INVALID_SELECTION" ) );
205     return;
206   }
207   myEngine->setPointId( aPointId );
208
209   int aComponentId = myComponentCB->currentIndex();
210   myEngine->setComponentId( aComponentId );
211
212   // create or get a Plot2d view
213   SPlot2d_Viewer* aView = VISU::GetPlot2dViewer( myModule, true );
214   if( aView )
215   {
216     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
217     myEngine->setViewer( aPlot );
218   }
219
220   if( !myEngine->showEvolution() )
221   {
222     SUIT_MessageBox::warning( VISU::GetDesktop( myModule ),
223                               tr( "WRN_VISU_WARNING" ),
224                               tr( "ERR_EXTRACTION_OF_DATA_FAILED" ) );
225     return;
226   }
227
228   std::string anEvolutionEntry = myEngine->getEvolutionEntry();
229   if( anEvolutionEntry != "" )
230   {
231     _PTR(SObject) anEvolutionObject = aStudy->FindObjectID( anEvolutionEntry.c_str() );
232     VISU::UpdateObjBrowser( myModule, true, anEvolutionObject );
233   }
234
235   QtxDialog::accept();
236 }
237
238 //------------------------------------------------------------------------
239 void VisuGUI_EvolutionDlg::setVisible( bool theIsVisible )
240 {
241   QtxDialog::setVisible( theIsVisible );
242
243   if( !myViewWindow )
244     return;
245
246   VISU::Result_i* aResult = myEngine->getResult();
247   if( !aResult )
248     return;
249
250   if( theIsVisible && !myPreviewActor )
251   {
252     // get an id of the pre-selected point
253     // it should be done before changing of selection mode because
254     // that operation forces onSelectionEvent() and clears myPointLE
255     bool hasPreSelection = false;
256     int aPointId = myPointLE->text().toInt( &hasPreSelection );
257
258     // store current selection mode and switch to point selection
259     myStoredSelectionMode = myViewWindow->SelectionMode();
260     myViewWindow->SetSelectionMode( NodeSelection );
261
262     // create preview actor and display it in the view
263     VISU::PNamedIDMapper aMapper = aResult->GetInput()->GetMeshOnEntity( myEngine->getMeshName(), myEngine->getEntity() );
264
265     vtkDataSetMapper* aPreviewMapper = vtkDataSetMapper::New();
266     aPreviewMapper->SetInput( aMapper->GetOutput() );
267   
268     vtkProperty* aProperty = vtkProperty::New();
269     aProperty->SetColor( 1, 1, 1 );
270     aProperty->SetPointSize( SALOME_POINT_SIZE );
271     aProperty->SetRepresentationToPoints();
272
273     myPreviewActor = SALOME_Actor::New();
274     myPreviewActor->PickableOn();
275     myPreviewActor->SetMapper( aPreviewMapper );
276     myPreviewActor->SetProperty( aProperty );
277
278     aProperty->Delete();
279     aPreviewMapper->Delete();
280
281     Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject( myEngine->getFieldEntry().c_str(), "", "" );
282     myPreviewActor->setIO( anIO );
283
284     myViewWindow->AddActor(myPreviewActor);
285
286     if( hasPreSelection )
287     {
288       myPointLE->setText( QString::number( aPointId ) );
289
290       SVTK_Selector* aSelector = myViewWindow->GetSelector();
291       aSelector->ClearIObjects();
292       aSelector->AddOrRemoveIndex( anIO, aPointId, false );
293       aSelector->AddIObject( anIO );
294
295       myPreviewActor->Highlight( true );
296     }
297
298     myViewWindow->onFitAll();
299   }
300   else if( !theIsVisible && myPreviewActor )
301   {
302     SVTK_Selector* aSelector = myViewWindow->GetSelector();
303     aSelector->ClearIObjects();
304
305     // restore selection mode
306     myViewWindow->SetSelectionMode( myStoredSelectionMode );
307
308     // remove preview actor from the view and delete it
309     myViewWindow->RemoveActor( myPreviewActor );
310     myPreviewActor->Delete();
311     myPreviewActor = 0;
312   }
313 }
314
315 //------------------------------------------------------------------------
316 void VisuGUI_EvolutionDlg::restoreFromStudy( _PTR(SObject) theEvolution )
317 {
318   myEngine->restoreFromStudy( theEvolution );
319
320   updateFromEngine();
321
322   myPointLE->setText( QString::number( myEngine->getPointId() ) );
323   myComponentCB->setCurrentIndex( myEngine->getComponentId() );
324 }
325
326 //------------------------------------------------------------------------
327 void VisuGUI_EvolutionDlg::onPointEdited( const QString& theText )
328 {
329   if( !myViewWindow )
330     return;
331
332   if( !myPreviewActor || !myPreviewActor->hasIO() )
333     return;
334
335   bool ok = false;
336   int aPointId = theText.toInt( &ok );
337   if( !ok )
338     return;
339
340   Handle(SALOME_InteractiveObject) anIO =  myPreviewActor->getIO();
341
342   SVTK_Selector* aSelector = myViewWindow->GetSelector();
343   aSelector->ClearIObjects();
344   aSelector->AddOrRemoveIndex( anIO, aPointId, false );
345   aSelector->AddIObject( anIO );
346
347   myPreviewActor->Highlight( true );
348
349   myViewWindow->Repaint();
350 }
351
352 //------------------------------------------------------------------------
353 void VisuGUI_EvolutionDlg::onSelectionEvent()
354 {
355   myPointLE->clear();
356
357   if( !myViewWindow || myViewWindow->SelectionMode() != NodeSelection )
358     return;
359
360   if( !myPreviewActor || !myPreviewActor->hasIO() )
361     return;
362
363   SVTK_Selector* aSelector = myViewWindow->GetSelector();
364   LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr( myModule );
365   SALOME_ListIO aListIO;
366   aSelectionMgr->selectedObjects( aListIO );
367   SALOME_ListIteratorOfListIO anIter( aListIO );
368   while( anIter.More() )
369   {
370     Handle(SALOME_InteractiveObject) anIO = anIter.Value();
371     if( !anIO.IsNull() && strcmp( anIO->getEntry(), myPreviewActor->getIO()->getEntry() ) == 0 )
372     {
373       TColStd_IndexedMapOfInteger aMapIndex;
374       aSelector->GetIndex( anIO, aMapIndex );
375       if( aMapIndex.Extent() == 1 )
376       {
377         int anId = aMapIndex( 1 );
378         myPointLE->setText( QString::number( anId ) );
379       }
380       break;
381     }
382     anIter.Next();
383   }
384 }
385
386 //------------------------------------------------------------------------
387 void VisuGUI_EvolutionDlg::onViewWindowClosed()
388 {
389   if( myViewWindow )
390   {
391     if( myPreviewActor )
392     {
393       myPreviewActor->Delete();
394       myPreviewActor = 0;
395     }
396     myViewWindow = 0;
397   }    
398 }
399
400 //------------------------------------------------------------------------
401 void VisuGUI_EvolutionDlg::onHelp()
402 {
403   QString aHelpFileName = "evolution_page.html";
404   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
405   if( app )
406     app->onHelpContextModule( myModule ? app->moduleName( myModule->moduleName() ) : QString( "" ), aHelpFileName );
407   else
408   {
409     QString platform;
410 #ifdef WIN32
411     platform = "winapplication";
412 #else
413     platform = "application";
414 #endif
415     SUIT_MessageBox::warning( VISU::GetDesktop( myModule ),
416                               tr( "WRN_WARNING" ),
417                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
418                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( aHelpFileName ),
419                               tr( "BUT_OK" ) );
420   }
421 }
422
423 //------------------------------------------------------------------------
424 void VisuGUI_EvolutionDlg::keyPressEvent( QKeyEvent* e )
425 {
426   QDialog::keyPressEvent( e );
427   if ( e->isAccepted() )
428     return;
429
430   if ( e->key() == Qt::Key_F1 )
431   {
432     e->accept();
433     onHelp();
434   }
435 }