]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_EvolutionDlg.cxx
Salome HOME
Fix for the "0051899: curves are not shown in opened study" issue.
[modules/visu.git] / src / VISUGUI / VisuGUI_EvolutionDlg.cxx
1 // Copyright (C) 2007-2013  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->SetInputConnection( aMapper->GetOutputPort() );
267     aPreviewMapper->Update();
268   
269     vtkProperty* aProperty = vtkProperty::New();
270     aProperty->SetColor( 1, 1, 1 );
271     aProperty->SetPointSize( SALOME_POINT_SIZE );
272     aProperty->SetRepresentationToPoints();
273
274     myPreviewActor = SALOME_Actor::New();
275     myPreviewActor->PickableOn();
276     myPreviewActor->SetMapper( aPreviewMapper );
277     myPreviewActor->SetProperty( aProperty );
278
279     aProperty->Delete();
280     aPreviewMapper->Delete();
281
282     Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject( myEngine->getFieldEntry().c_str(), "", "" );
283     myPreviewActor->setIO( anIO );
284
285     myViewWindow->AddActor(myPreviewActor);
286
287     if( hasPreSelection )
288     {
289       myPointLE->setText( QString::number( aPointId ) );
290
291       SVTK_Selector* aSelector = myViewWindow->GetSelector();
292       aSelector->ClearIObjects();
293       aSelector->AddOrRemoveIndex( anIO, aPointId, false );
294       aSelector->AddIObject( anIO );
295
296       myPreviewActor->Highlight( true );
297     }
298
299     myViewWindow->onFitAll();
300   }
301   else if( !theIsVisible && myPreviewActor )
302   {
303     SVTK_Selector* aSelector = myViewWindow->GetSelector();
304     aSelector->ClearIObjects();
305
306     // restore selection mode
307     myViewWindow->SetSelectionMode( myStoredSelectionMode );
308
309     // remove preview actor from the view and delete it
310     myViewWindow->RemoveActor( myPreviewActor );
311     myPreviewActor->Delete();
312     myPreviewActor = 0;
313   }
314 }
315
316 //------------------------------------------------------------------------
317 void VisuGUI_EvolutionDlg::restoreFromStudy( _PTR(SObject) theEvolution )
318 {
319   myEngine->restoreFromStudy( theEvolution );
320
321   updateFromEngine();
322
323   myPointLE->setText( QString::number( myEngine->getPointId() ) );
324   myComponentCB->setCurrentIndex( myEngine->getComponentId() );
325 }
326
327 //------------------------------------------------------------------------
328 void VisuGUI_EvolutionDlg::onPointEdited( const QString& theText )
329 {
330   if( !myViewWindow )
331     return;
332
333   if( !myPreviewActor || !myPreviewActor->hasIO() )
334     return;
335
336   bool ok = false;
337   int aPointId = theText.toInt( &ok );
338   if( !ok )
339     return;
340
341   Handle(SALOME_InteractiveObject) anIO =  myPreviewActor->getIO();
342
343   SVTK_Selector* aSelector = myViewWindow->GetSelector();
344   aSelector->ClearIObjects();
345   aSelector->AddOrRemoveIndex( anIO, aPointId, false );
346   aSelector->AddIObject( anIO );
347
348   myPreviewActor->Highlight( true );
349
350   myViewWindow->Repaint();
351 }
352
353 //------------------------------------------------------------------------
354 void VisuGUI_EvolutionDlg::onSelectionEvent()
355 {
356   myPointLE->clear();
357
358   if( !myViewWindow || myViewWindow->SelectionMode() != NodeSelection )
359     return;
360
361   if( !myPreviewActor || !myPreviewActor->hasIO() )
362     return;
363
364   SVTK_Selector* aSelector = myViewWindow->GetSelector();
365   LightApp_SelectionMgr* aSelectionMgr = VISU::GetSelectionMgr( myModule );
366   SALOME_ListIO aListIO;
367   aSelectionMgr->selectedObjects( aListIO );
368   SALOME_ListIteratorOfListIO anIter( aListIO );
369   while( anIter.More() )
370   {
371     Handle(SALOME_InteractiveObject) anIO = anIter.Value();
372     if( !anIO.IsNull() && strcmp( anIO->getEntry(), myPreviewActor->getIO()->getEntry() ) == 0 )
373     {
374       TColStd_IndexedMapOfInteger aMapIndex;
375       aSelector->GetIndex( anIO, aMapIndex );
376       if( aMapIndex.Extent() == 1 )
377       {
378         int anId = aMapIndex( 1 );
379         myPointLE->setText( QString::number( anId ) );
380       }
381       break;
382     }
383     anIter.Next();
384   }
385 }
386
387 //------------------------------------------------------------------------
388 void VisuGUI_EvolutionDlg::onViewWindowClosed()
389 {
390   if( myViewWindow )
391   {
392     if( myPreviewActor )
393     {
394       myPreviewActor->Delete();
395       myPreviewActor = 0;
396     }
397     myViewWindow = 0;
398   }    
399 }
400
401 //------------------------------------------------------------------------
402 void VisuGUI_EvolutionDlg::onHelp()
403 {
404   QString aHelpFileName = "evolution_page.html";
405   LightApp_Application* app = (LightApp_Application*)( SUIT_Session::session()->activeApplication() );
406   if( app )
407     app->onHelpContextModule( myModule ? app->moduleName( myModule->moduleName() ) : QString( "" ), aHelpFileName );
408   else
409   {
410     QString platform;
411 #ifdef WIN32
412     platform = "winapplication";
413 #else
414     platform = "application";
415 #endif
416     SUIT_MessageBox::warning( VISU::GetDesktop( myModule ),
417                               tr( "WRN_WARNING" ),
418                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
419                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( aHelpFileName ),
420                               tr( "BUT_OK" ) );
421   }
422 }
423
424 //------------------------------------------------------------------------
425 void VisuGUI_EvolutionDlg::keyPressEvent( QKeyEvent* e )
426 {
427   QDialog::keyPressEvent( e );
428   if ( e->isAccepted() )
429     return;
430
431   if ( e->key() == Qt::Key_F1 )
432   {
433     e->accept();
434     onHelp();
435   }
436 }