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