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