Salome HOME
IPAL22292 (Gauss Points doesn't appear in Clipping Planes after reload file): GaussPo...
[modules/visu.git] / src / VISUGUI / VisuGUI_GaussPointsSelectionPane.cxx
1 //  Copyright (C) 2007-2010  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 //  VISU VISUGUI : GUI of VISU component
21 //  File   : VisuGUI_GaussPointsSelectionPane.cxx
22 //  Author : Oleg Uvarov
23 //  Module : VISU
24 //
25 #include "VisuGUI_GaussPointsSelectionPane.h"
26 #include "VisuGUI_ViewTools.h"
27
28 #include "VISU_GaussPtsAct.h"
29 #include "VISU_GaussPtsSettings.h"
30 #include "VISU_GaussPointsPL.hxx"
31 #include "VISU_PickingSettings.h"
32
33 #include "SUIT_MessageBox.h"
34 #include "SUIT_ResourceMgr.h"
35 #include "SUIT_Session.h"
36
37 #include "CAM_Module.h"
38
39 #include "SVTK_Selector.h"
40 #include "SVTK_ViewWindow.h"
41 //#include "SVTK_MainWindow.h"
42 #include "SVTK_RenderWindowInteractor.h"
43
44 #include "VTKViewer_Algorithm.h"
45 #include "SVTK_Functor.h"
46
47 #include <vtkActorCollection.h>
48 #include <vtkCallbackCommand.h>
49 #include <vtkObjectFactory.h>
50 #include <vtkRenderer.h>
51 #include <vtkGenericRenderWindowInteractor.h>
52 #include <vtkSmartPointer.h>
53
54 #include "utilities.h"
55
56 #include <QCheckBox>
57 #include <QComboBox>
58 #include <QGroupBox>
59 #include <QLabel>
60 #include <QLayout>
61 #include <QPushButton>
62 #include <QVBoxLayout>
63 #include <QKeyEvent>
64 #include <QIntValidator>
65 #include <QToolButton>
66
67 #include "LightApp_Application.h"
68
69 namespace
70 {
71   struct SelectorHelper
72   {
73   public:
74     SelectorHelper( const SalomeApp_Module* theModule ):
75       myModule( theModule )
76     {}
77
78     bool
79     get()
80     {
81       bool aResult = false;
82       myMapIndex.Clear();
83       mySelector = NULL;
84       myPipeLine = NULL;
85       myActor = NULL;
86
87       SVTK_RenderWindowInteractor* anInteractor = NULL;
88       if( SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>( myModule ) )
89         //if( SVTK_MainWindow* aMainWindow = aViewWindow->getMainWindow() )
90           anInteractor = aViewWindow->GetInteractor();
91
92       if ( !anInteractor )
93         return aResult;
94       
95       mySelector = anInteractor->GetSelector();
96       if ( !mySelector )
97         return aResult;
98       
99       const SALOME_ListIO& aListIO = mySelector->StoredIObjects();
100       if ( aListIO.Extent() != 1 ) 
101         return aResult;
102       
103       myIO = aListIO.First();
104       if ( mySelector->HasIndex( myIO ) )
105         mySelector->GetIndex(myIO, myMapIndex);
106         
107       VTK::ActorCollectionCopy aCopy(anInteractor->getRenderer()->GetActors());
108       myActor = SVTK::Find<VISU_GaussPtsAct>(aCopy.GetActors(),
109                                              SVTK::TIsSameIObject<VISU_GaussPtsAct>( myIO ));
110       if ( !myActor )
111         return aResult;
112       
113       myPipeLine = myActor->GetGaussPointsPL();
114
115       return true;
116     }
117
118     const SalomeApp_Module* myModule;
119     TColStd_IndexedMapOfInteger myMapIndex;
120     Handle(SALOME_InteractiveObject) myIO;
121     SVTK_Selector* mySelector;
122
123     VISU_GaussPointsPL* myPipeLine;
124     VISU_GaussPtsAct* myActor;
125   };
126
127
128   
129   class GaussPtsIDValidator: public QIntValidator
130   {
131   public:
132     GaussPtsIDValidator( const SalomeApp_Module* theModule,
133                          QObject * theParent ):
134       QIntValidator( 0, VTK_LARGE_ID, theParent ),
135       myHelper(theModule)
136     {}
137
138     virtual 
139     State
140     validate ( QString & theInput, int & thePos ) const
141     {
142       if ( QIntValidator::validate( theInput, thePos ) == QValidator::Invalid)
143         return QValidator::Invalid;
144       
145       if ( !myHelper.get() )
146         return QValidator::Invalid;
147
148       return QValidator::Acceptable;
149     }
150
151   protected:
152     mutable SelectorHelper myHelper;
153   };
154
155
156   class GaussCellIDValidator: public GaussPtsIDValidator
157   {
158   public:
159     GaussCellIDValidator( QLineEdit* theLocalPointLabel,
160                           const SalomeApp_Module* theModule,
161                           QObject * theParent ):
162       GaussPtsIDValidator( theModule, theParent ),
163       myLocalPointLineEdit( theLocalPointLabel )
164     {}
165
166     virtual 
167     State
168     validate ( QString & theInput, int & thePos ) const
169     {
170       if( theInput.isEmpty() )
171         return QValidator::Acceptable;
172
173       if ( GaussPtsIDValidator::validate( theInput, thePos ) == QValidator::Invalid)
174         return QValidator::Invalid;
175
176       VISU::TCellID aCellID = theInput.toInt();
177       VISU::TLocalPntID aLocalPntID = myLocalPointLineEdit->text().toInt();
178       VISU::PGaussPtsIDMapper anIDMapper = myHelper.myPipeLine->GetGaussPtsIDMapper();
179       if ( anIDMapper->GetVTKID( VISU::TGaussPointID( aCellID, aLocalPntID ) ) < 0 )
180         return QValidator::Intermediate;
181
182       return QValidator::Acceptable;
183     }
184
185   private:
186     QLineEdit* myLocalPointLineEdit;
187   };
188
189
190   class GaussLocalPointIDValidator: public GaussPtsIDValidator
191   {
192   public:
193     GaussLocalPointIDValidator( QLineEdit* theParentElementLineEdit,
194                                 const SalomeApp_Module* theModule,
195                                 QObject * theParent ):
196       GaussPtsIDValidator( theModule, theParent ),
197       myParentElementLineEdit( theParentElementLineEdit )
198     {}
199
200     virtual 
201     State
202     validate ( QString & theInput, int & thePos ) const
203     {
204       if( theInput.isEmpty() )
205         return QValidator::Acceptable;
206
207       if ( GaussPtsIDValidator::validate( theInput, thePos ) == QValidator::Invalid)
208         return QValidator::Invalid;
209
210       VISU::TLocalPntID aLocalPntID = theInput.toInt();
211       VISU::TCellID aCellID = myParentElementLineEdit->text().toInt();
212       VISU::PGaussPtsIDMapper anIDMapper = myHelper.myPipeLine->GetGaussPtsIDMapper();
213       if ( anIDMapper->GetVTKID( VISU::TGaussPointID( aCellID, aLocalPntID ) ) < 0 )
214         return QValidator::Intermediate;
215
216       return QValidator::Acceptable;
217     }
218
219   private:
220     QLineEdit* myParentElementLineEdit;
221   };
222 }
223
224
225 VisuGUI_ValidatedLineEdit::VisuGUI_ValidatedLineEdit( QWidget* parent ):
226   QLineEdit( parent )
227 {
228   connect( this, SIGNAL( textChanged( const QString& ) ), this, SLOT( MarkValidated( const QString& ) ) );
229 }
230
231 void VisuGUI_ValidatedLineEdit::MarkValidated( const QString& theText )
232 {
233   if ( !validator() )
234     return;
235   
236   int aPos;
237   QString aText( theText );
238   QPalette pal = palette();
239   switch ( validator()->validate( aText, aPos ) ) {
240   case QValidator::Invalid:
241   case QValidator::Intermediate:
242     pal.setColor( foregroundRole(), QColor( 255, 0, 0 ));
243     setPalette( pal );
244     break;
245   case QValidator::Acceptable:
246     pal.setColor( foregroundRole(), QColor( 0, 0, 0 ));
247     setPalette( pal );
248     break;
249   }
250 }
251
252 //---------------------------------------------------------------------------------
253
254 VisuGUI_GaussPointsSelectionPane::VisuGUI_GaussPointsSelectionPane( const SalomeApp_Module* theModule,
255                                                                     QWidget* theParent ) :
256   QWidget( theParent ),
257   myModule( theModule ),
258   myEventCallbackCommand( vtkCallbackCommand::New() )
259 {
260   myPriority = 0.0;
261   myEventCallbackCommand->Delete();
262   myEventCallbackCommand->SetClientData(this); 
263   myEventCallbackCommand->SetCallback(VisuGUI_GaussPointsSelectionPane::ProcessEvents);
264
265   QVBoxLayout* TopLayout = new QVBoxLayout( this );
266   //TopLayout->setSpacing(6);
267
268   // Display parent mesh element
269   QGroupBox* PositionGroup = new QGroupBox( tr( "DATA_POSITION" ), this );
270   QGridLayout* PositionGroupLayout = new QGridLayout (PositionGroup);
271   PositionGroupLayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
272   //PositionGroupLayout->setSpacing(6);
273
274   QLabel* ParentElementLabel = new QLabel( tr( "PARENT_ELEMENT" ), PositionGroup );
275   ParentElementLabel->setToolTip( tr( "PARENT_ELEMENT_TIP" ) );
276   myParentElementLineEdit = new VisuGUI_ValidatedLineEdit( PositionGroup );
277   connect( myParentElementLineEdit, SIGNAL( textChanged( const QString& ) ), this, SLOT( onSelectionValidate() ) );
278   connect( myParentElementLineEdit, SIGNAL( returnPressed() ), this, SLOT( onSelectionApply() ) );
279
280   QToolButton* ParentElementBtn = new QToolButton( PositionGroup );
281   ParentElementBtn->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr( "ICON_OK" ) ) );
282   connect(ParentElementBtn, SIGNAL(clicked()), this, SLOT(onSelectionApply()));
283
284   QLabel* LocalPointLabel = new QLabel( tr( "LOCAL_POINT" ), PositionGroup );
285   LocalPointLabel->setToolTip( tr( "LOCAL_POINT_TIP" ) );
286   myLocalPointLineEdit = new VisuGUI_ValidatedLineEdit( PositionGroup );
287   connect( myLocalPointLineEdit,    SIGNAL( textChanged( const QString& ) ), this, SLOT( onSelectionValidate() ) );
288   connect( myLocalPointLineEdit,    SIGNAL( returnPressed() ), this, SLOT( onSelectionApply() ) );
289
290   QToolButton* LocalPointBtn = new QToolButton( PositionGroup );
291   LocalPointBtn->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr( "ICON_OK" ) ) );
292   connect(LocalPointBtn, SIGNAL(clicked()), this, SLOT(onSelectionApply()));
293
294   myCellIDValidator = new GaussCellIDValidator( myLocalPointLineEdit, myModule, myParentElementLineEdit );
295   myParentElementLineEdit->setValidator( myCellIDValidator );
296
297   myLocalPointIDValidator = new GaussLocalPointIDValidator( myParentElementLineEdit, myModule, myLocalPointLineEdit );
298   myLocalPointLineEdit->setValidator( myLocalPointIDValidator );
299
300   myDisplayParentMeshCheckBox = new QCheckBox( tr( "DISPLAY_PARENT_MESH" ), PositionGroup );
301
302   PositionGroupLayout->addWidget( ParentElementLabel,          0, 0, 1, 3 );
303   PositionGroupLayout->addWidget( myParentElementLineEdit,     0, 3 );
304   PositionGroupLayout->addWidget( ParentElementBtn,            0, 4 );
305   PositionGroupLayout->addWidget( LocalPointLabel,             1, 0, 1, 3 );
306   PositionGroupLayout->addWidget( myLocalPointLineEdit,        1, 3 );
307   PositionGroupLayout->addWidget( LocalPointBtn,               1, 4 );
308   PositionGroupLayout->addWidget( myDisplayParentMeshCheckBox, 2, 0, 1, 5 );
309
310   connect( myDisplayParentMeshCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( onApplyDisplayParentMesh( bool ) ) );
311
312   TopLayout->addWidget( PositionGroup );
313   TopLayout->addStretch();
314 }
315
316 VisuGUI_GaussPointsSelectionPane::~VisuGUI_GaussPointsSelectionPane()
317 {
318 }
319
320 void VisuGUI_GaussPointsSelectionPane::update()
321 {
322   VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
323   myDisplayParentMeshCheckBox->setChecked( aPickingSettings->GetDisplayParentMesh() );
324 }
325
326 void VisuGUI_GaussPointsSelectionPane::setIds( const int theParentId, const int theLocalId )
327 {
328   myParentElementLineEdit->setText( theParentId < 0 ? "" : QString::number( theParentId ) );
329   myLocalPointLineEdit->setText( theLocalId < 0 ? "" : QString::number( theLocalId ) );
330   onSelectionApply();
331 }
332
333 void VisuGUI_GaussPointsSelectionPane::setInteractor( SVTK_RenderWindowInteractor* theInteractor )
334 {
335   //printf( "VisuGUI_GaussPointsSelectionPane::setInteractor( %p )\n", theInteractor );
336   vtkGenericRenderWindowInteractor* aDevice = theInteractor->GetDevice();
337   if( aDevice->HasObserver(vtkCommand::KeyPressEvent) )
338     aDevice->RemoveObservers(vtkCommand::KeyPressEvent);
339
340   //printf( "AddObserver(vtkCommand::KeyPressEvent)\n" );
341   aDevice->AddObserver(vtkCommand::KeyPressEvent, 
342                        myEventCallbackCommand.GetPointer(), 
343                        myPriority);
344
345   if( aDevice->HasObserver(vtkCommand::EndPickEvent) )
346     aDevice->RemoveObservers(vtkCommand::EndPickEvent);
347
348   //printf( "AddObserver(vtkCommand::EndPickEvent)\n" );
349   aDevice->AddObserver(vtkCommand::EndPickEvent, 
350                        myEventCallbackCommand.GetPointer(), 
351                        myPriority);
352 }
353
354 SVTK_RenderWindowInteractor* VisuGUI_GaussPointsSelectionPane::getInteractor()
355 {
356   if( SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>( myModule ) )
357   {
358     //if( SVTK_MainWindow* aMainWindow = aViewWindow->getMainWindow() )
359     //{
360       SVTK_RenderWindowInteractor* anInteractor = aViewWindow->GetInteractor();
361       return anInteractor;
362       //}
363   }
364   return 0;
365 }
366
367 void VisuGUI_GaussPointsSelectionPane::ProcessEvents(vtkObject* vtkNotUsed(theObject),
368                                                      unsigned long theEvent,
369                                                      void* theClientData,
370                                                      void* vtkNotUsed(theCallData))
371 {
372   VisuGUI_GaussPointsSelectionPane* self = reinterpret_cast<VisuGUI_GaussPointsSelectionPane*>(theClientData);
373
374   switch(theEvent){
375   case vtkCommand::KeyPressEvent:
376     self->KeyPressed();
377     break;
378   case vtkCommand::EndPickEvent:
379     self->SelectionEvent();
380     break;
381   }
382 }
383
384 void VisuGUI_GaussPointsSelectionPane::KeyPressed()
385 {
386   //printf( "VisuGUI_GaussPointsSelectionPane::KeyPressed()\n" );
387   if( getInteractor()->GetDevice()->GetKeyCode() == 'P' )
388   {
389     VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
390
391     bool aDisplayParentMesh = !aPickingSettings->GetDisplayParentMesh();
392     myDisplayParentMeshCheckBox->setChecked( aDisplayParentMesh );
393
394     aPickingSettings->SetDisplayParentMesh( aDisplayParentMesh );
395     aPickingSettings->InvokeEvent(VISU::UpdatePickingSettingsEvent,NULL);
396   }
397 }
398
399 void VisuGUI_GaussPointsSelectionPane::SelectionEvent()
400 {
401   //printf( "VisuGUI_GaussPointsSelectionPane::SelectionEvent()\n" );
402   clearIDControls();
403
404   SelectorHelper aHelper( myModule );
405   if( aHelper.get() )
406   {
407     const TColStd_IndexedMapOfInteger& aMapIndex = aHelper.myMapIndex;
408     if( aMapIndex.Extent() == 1 )
409     {
410       int anObjId = aHelper.myMapIndex(1);
411       VISU::TGaussPointID aGaussPointID = aHelper.myPipeLine->GetObjID( anObjId );
412       VISU::TCellID aCellID = aGaussPointID.first;
413       VISU::TLocalPntID aLocalPntID = aGaussPointID.second;
414
415       myParentElementLineEdit->setText( QString::number( aCellID ) );
416       myLocalPointLineEdit->setText( QString::number( aLocalPntID ) );
417     }
418   }
419 }
420
421 void VisuGUI_GaussPointsSelectionPane::onSelectionValidate() 
422 {
423   myParentElementLineEdit->MarkValidated( myParentElementLineEdit->text() );
424   myLocalPointLineEdit->MarkValidated( myLocalPointLineEdit->text() );
425 }
426
427 void VisuGUI_GaussPointsSelectionPane::onSelectionApply() 
428 {
429   //printf( "VisuGUI_GaussPointsSelectionPane::onSelectionApply()\n" );
430   SelectorHelper aHelper( myModule );
431   if( !aHelper.get() )
432     return;
433
434   vtkIdType anObjVtkId = -1;
435   VISU::TCellID aCellId;
436   VISU::TLocalPntID aLocalPntId;
437
438   bool ok = false;
439   aCellId = myParentElementLineEdit->text().toInt( &ok );
440   if( ok )
441   {
442     ok = false;
443     aLocalPntId = myLocalPointLineEdit->text().toInt( &ok );
444     if( ok )
445     {
446       VISU::PGaussPtsIDMapper anIDMapper = aHelper.myPipeLine->GetGaussPtsIDMapper();
447       anObjVtkId = anIDMapper->GetVTKID( VISU::TGaussPointID( aCellId, aLocalPntId ) );
448     }
449   }
450
451   if( anObjVtkId < 0 )
452     aHelper.mySelector->ClearIndex();
453   else
454   {
455     aHelper.myMapIndex.Clear();
456     aHelper.myMapIndex.Add( anObjVtkId );
457     aHelper.mySelector->AddOrRemoveIndex( aHelper.myIO, aHelper.myMapIndex, false );
458   }
459   aHelper.myActor->Highlight( aHelper.myIO );
460   getInteractor()->GetDevice()->CreateTimer( VTKI_TIMER_FIRST );
461 }
462
463 void VisuGUI_GaussPointsSelectionPane::clearIDControls()
464 {
465   myParentElementLineEdit->setText( "" );
466   myLocalPointLineEdit->setText( "" );
467 }
468
469 void VisuGUI_GaussPointsSelectionPane::apply()
470 {
471   onSelectionApply();
472 }
473
474 void VisuGUI_GaussPointsSelectionPane::onApplyDisplayParentMesh( bool theValue )
475 {
476   VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
477   aPickingSettings->SetDisplayParentMesh( theValue );
478   aPickingSettings->InvokeEvent( VISU::UpdatePickingSettingsEvent,NULL );
479 }
480
481 /*
482 void VisuGUI_GaussPointsSelectionPane::Help()
483 {
484   QString aHelpFileName = "picking.htm";
485   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
486   if (app)
487     app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
488   else {
489     QString platform;
490 #ifdef WIN32
491     platform = "winapplication";
492 #else
493     platform = "application";
494 #endif
495     SUIT_MessageBox::warning( 0,
496                               QObject::tr("WRN_WARNING"),
497                               QObject::tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
498                                 arg(app->resourceMgr()->stringValue("ExternalBrowser", platform)).arg(aHelpFileName),
499                               QObject::tr("BUT_OK"));
500   }
501 }
502
503 void VisuGUI_GaussPointsSelectionPane::keyPressEvent( QKeyEvent* e )
504 {
505   QDialog::keyPressEvent( e );
506   if ( e->isAccepted() )
507     return;
508
509   if ( e->key() == Qt::Key_F1 )
510     {
511       e->accept();
512       Help();
513     }
514 }
515 */