1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // VISU VISUGUI : GUI of VISU component
23 // File : VisuGUI_GaussPointsSelectionPane.cxx
24 // Author : Oleg Uvarov
27 #include "VisuGUI_GaussPointsSelectionPane.h"
28 #include "VisuGUI_ViewTools.h"
30 #include "VISU_GaussPtsAct.h"
31 #include "VISU_GaussPtsSettings.h"
32 #include "VISU_GaussPointsPL.hxx"
33 #include "VISU_PickingSettings.h"
35 #include "SUIT_MessageBox.h"
36 #include "SUIT_ResourceMgr.h"
37 #include "SUIT_Session.h"
39 #include "CAM_Module.h"
41 #include "SVTK_Selector.h"
42 #include "SVTK_ViewWindow.h"
43 //#include "SVTK_MainWindow.h"
44 #include "SVTK_RenderWindowInteractor.h"
46 #include "VTKViewer_Algorithm.h"
47 #include "SVTK_Functor.h"
49 #include <vtkActorCollection.h>
50 #include <vtkCallbackCommand.h>
51 #include <vtkObjectFactory.h>
52 #include <vtkRenderer.h>
53 #include <vtkGenericRenderWindowInteractor.h>
54 #include <vtkSmartPointer.h>
56 #include "utilities.h"
63 #include <QPushButton>
64 #include <QVBoxLayout>
66 #include <QIntValidator>
67 #include <QToolButton>
69 #include "LightApp_Application.h"
76 SelectorHelper( const SalomeApp_Module* theModule ):
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();
97 mySelector = anInteractor->GetSelector();
101 const SALOME_ListIO& aListIO = mySelector->StoredIObjects();
102 if ( aListIO.Extent() != 1 )
105 myIO = aListIO.First();
106 if ( mySelector->HasIndex( myIO ) )
107 mySelector->GetIndex(myIO, myMapIndex);
109 VTK::ActorCollectionCopy aCopy(anInteractor->getRenderer()->GetActors());
110 myActor = SVTK::Find<VISU_GaussPtsAct>(aCopy.GetActors(),
111 SVTK::TIsSameIObject<VISU_GaussPtsAct>( myIO ));
115 myPipeLine = myActor->GetGaussPointsPL();
120 const SalomeApp_Module* myModule;
121 TColStd_IndexedMapOfInteger myMapIndex;
122 Handle(SALOME_InteractiveObject) myIO;
123 SVTK_Selector* mySelector;
125 VISU_GaussPointsPL* myPipeLine;
126 VISU_GaussPtsAct* myActor;
131 class GaussPtsIDValidator: public QIntValidator
134 GaussPtsIDValidator( const SalomeApp_Module* theModule,
135 QObject * theParent ):
136 QIntValidator( 0, VTK_LARGE_ID, theParent ),
142 validate ( QString & theInput, int & thePos ) const
144 if ( QIntValidator::validate( theInput, thePos ) == QValidator::Invalid)
145 return QValidator::Invalid;
147 if ( !myHelper.get() )
148 return QValidator::Invalid;
150 return QValidator::Acceptable;
154 mutable SelectorHelper myHelper;
158 class GaussCellIDValidator: public GaussPtsIDValidator
161 GaussCellIDValidator( QLineEdit* theLocalPointLabel,
162 const SalomeApp_Module* theModule,
163 QObject * theParent ):
164 GaussPtsIDValidator( theModule, theParent ),
165 myLocalPointLineEdit( theLocalPointLabel )
170 validate ( QString & theInput, int & thePos ) const
172 if( theInput.isEmpty() )
173 return QValidator::Acceptable;
175 if ( GaussPtsIDValidator::validate( theInput, thePos ) == QValidator::Invalid)
176 return QValidator::Invalid;
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;
184 return QValidator::Acceptable;
188 QLineEdit* myLocalPointLineEdit;
192 class GaussLocalPointIDValidator: public GaussPtsIDValidator
195 GaussLocalPointIDValidator( QLineEdit* theParentElementLineEdit,
196 const SalomeApp_Module* theModule,
197 QObject * theParent ):
198 GaussPtsIDValidator( theModule, theParent ),
199 myParentElementLineEdit( theParentElementLineEdit )
204 validate ( QString & theInput, int & thePos ) const
206 if( theInput.isEmpty() )
207 return QValidator::Acceptable;
209 if ( GaussPtsIDValidator::validate( theInput, thePos ) == QValidator::Invalid)
210 return QValidator::Invalid;
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;
218 return QValidator::Acceptable;
222 QLineEdit* myParentElementLineEdit;
227 VisuGUI_ValidatedLineEdit::VisuGUI_ValidatedLineEdit( QWidget* parent ):
230 connect( this, SIGNAL( textChanged( const QString& ) ), this, SLOT( MarkValidated( const QString& ) ) );
233 void VisuGUI_ValidatedLineEdit::MarkValidated( const QString& theText )
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 ));
247 case QValidator::Acceptable:
248 pal.setColor( foregroundRole(), QColor( 0, 0, 0 ));
254 //---------------------------------------------------------------------------------
256 VisuGUI_GaussPointsSelectionPane::VisuGUI_GaussPointsSelectionPane( const SalomeApp_Module* theModule,
257 QWidget* theParent ) :
258 QWidget( theParent ),
259 myModule( theModule ),
260 myEventCallbackCommand( vtkCallbackCommand::New() )
263 myEventCallbackCommand->Delete();
264 myEventCallbackCommand->SetClientData(this);
265 myEventCallbackCommand->SetCallback(VisuGUI_GaussPointsSelectionPane::ProcessEvents);
267 QVBoxLayout* TopLayout = new QVBoxLayout( this );
268 //TopLayout->setSpacing(6);
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);
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() ) );
282 QToolButton* ParentElementBtn = new QToolButton( PositionGroup );
283 ParentElementBtn->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr( "ICON_OK" ) ) );
284 connect(ParentElementBtn, SIGNAL(clicked()), this, SLOT(onSelectionApply()));
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() ) );
292 QToolButton* LocalPointBtn = new QToolButton( PositionGroup );
293 LocalPointBtn->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr( "ICON_OK" ) ) );
294 connect(LocalPointBtn, SIGNAL(clicked()), this, SLOT(onSelectionApply()));
296 myCellIDValidator = new GaussCellIDValidator( myLocalPointLineEdit, myModule, myParentElementLineEdit );
297 myParentElementLineEdit->setValidator( myCellIDValidator );
299 myLocalPointIDValidator = new GaussLocalPointIDValidator( myParentElementLineEdit, myModule, myLocalPointLineEdit );
300 myLocalPointLineEdit->setValidator( myLocalPointIDValidator );
302 myDisplayParentMeshCheckBox = new QCheckBox( tr( "DISPLAY_PARENT_MESH" ), PositionGroup );
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 );
312 connect( myDisplayParentMeshCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( onApplyDisplayParentMesh( bool ) ) );
314 TopLayout->addWidget( PositionGroup );
315 TopLayout->addStretch();
318 VisuGUI_GaussPointsSelectionPane::~VisuGUI_GaussPointsSelectionPane()
322 void VisuGUI_GaussPointsSelectionPane::update()
324 VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
325 myDisplayParentMeshCheckBox->setChecked( aPickingSettings->GetDisplayParentMesh() );
328 void VisuGUI_GaussPointsSelectionPane::setIds( const int theParentId, const int theLocalId )
330 myParentElementLineEdit->setText( theParentId < 0 ? "" : QString::number( theParentId ) );
331 myLocalPointLineEdit->setText( theLocalId < 0 ? "" : QString::number( theLocalId ) );
335 void VisuGUI_GaussPointsSelectionPane::setInteractor( SVTK_RenderWindowInteractor* theInteractor )
337 //printf( "VisuGUI_GaussPointsSelectionPane::setInteractor( %p )\n", theInteractor );
338 vtkGenericRenderWindowInteractor* aDevice = theInteractor->GetDevice();
339 if( aDevice->HasObserver(vtkCommand::KeyPressEvent) )
340 aDevice->RemoveObservers(vtkCommand::KeyPressEvent);
342 //printf( "AddObserver(vtkCommand::KeyPressEvent)\n" );
343 aDevice->AddObserver(vtkCommand::KeyPressEvent,
344 myEventCallbackCommand.GetPointer(),
347 if( aDevice->HasObserver(vtkCommand::EndPickEvent) )
348 aDevice->RemoveObservers(vtkCommand::EndPickEvent);
350 //printf( "AddObserver(vtkCommand::EndPickEvent)\n" );
351 aDevice->AddObserver(vtkCommand::EndPickEvent,
352 myEventCallbackCommand.GetPointer(),
356 SVTK_RenderWindowInteractor* VisuGUI_GaussPointsSelectionPane::getInteractor()
358 if( SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>( myModule ) )
360 //if( SVTK_MainWindow* aMainWindow = aViewWindow->getMainWindow() )
362 SVTK_RenderWindowInteractor* anInteractor = aViewWindow->GetInteractor();
369 void VisuGUI_GaussPointsSelectionPane::ProcessEvents(vtkObject* vtkNotUsed(theObject),
370 unsigned long theEvent,
372 void* vtkNotUsed(theCallData))
374 VisuGUI_GaussPointsSelectionPane* self = reinterpret_cast<VisuGUI_GaussPointsSelectionPane*>(theClientData);
377 case vtkCommand::KeyPressEvent:
380 case vtkCommand::EndPickEvent:
381 self->SelectionEvent();
386 void VisuGUI_GaussPointsSelectionPane::KeyPressed()
388 //printf( "VisuGUI_GaussPointsSelectionPane::KeyPressed()\n" );
389 if( getInteractor()->GetDevice()->GetKeyCode() == 'P' )
391 VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
393 bool aDisplayParentMesh = !aPickingSettings->GetDisplayParentMesh();
394 myDisplayParentMeshCheckBox->setChecked( aDisplayParentMesh );
396 aPickingSettings->SetDisplayParentMesh( aDisplayParentMesh );
397 aPickingSettings->InvokeEvent(VISU::UpdatePickingSettingsEvent,NULL);
401 void VisuGUI_GaussPointsSelectionPane::SelectionEvent()
403 //printf( "VisuGUI_GaussPointsSelectionPane::SelectionEvent()\n" );
406 SelectorHelper aHelper( myModule );
409 const TColStd_IndexedMapOfInteger& aMapIndex = aHelper.myMapIndex;
410 if( aMapIndex.Extent() == 1 )
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;
417 myParentElementLineEdit->setText( QString::number( aCellID ) );
418 myLocalPointLineEdit->setText( QString::number( aLocalPntID ) );
423 void VisuGUI_GaussPointsSelectionPane::onSelectionValidate()
425 myParentElementLineEdit->MarkValidated( myParentElementLineEdit->text() );
426 myLocalPointLineEdit->MarkValidated( myLocalPointLineEdit->text() );
429 void VisuGUI_GaussPointsSelectionPane::onSelectionApply()
431 //printf( "VisuGUI_GaussPointsSelectionPane::onSelectionApply()\n" );
432 SelectorHelper aHelper( myModule );
436 vtkIdType anObjVtkId = -1;
437 VISU::TCellID aCellId;
438 VISU::TLocalPntID aLocalPntId;
441 aCellId = myParentElementLineEdit->text().toInt( &ok );
445 aLocalPntId = myLocalPointLineEdit->text().toInt( &ok );
448 VISU::PGaussPtsIDMapper anIDMapper = aHelper.myPipeLine->GetGaussPtsIDMapper();
449 anObjVtkId = anIDMapper->GetVTKID( VISU::TGaussPointID( aCellId, aLocalPntId ) );
454 aHelper.mySelector->ClearIndex();
457 aHelper.myMapIndex.Clear();
458 aHelper.myMapIndex.Add( anObjVtkId );
459 aHelper.mySelector->AddOrRemoveIndex( aHelper.myIO, aHelper.myMapIndex, false );
461 aHelper.myActor->Highlight( aHelper.myIO );
462 getInteractor()->GetDevice()->CreateTimer( VTKI_TIMER_FIRST );
465 void VisuGUI_GaussPointsSelectionPane::clearIDControls()
467 myParentElementLineEdit->setText( "" );
468 myLocalPointLineEdit->setText( "" );
471 void VisuGUI_GaussPointsSelectionPane::apply()
476 void VisuGUI_GaussPointsSelectionPane::onApplyDisplayParentMesh( bool theValue )
478 VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
479 aPickingSettings->SetDisplayParentMesh( theValue );
480 aPickingSettings->InvokeEvent( VISU::UpdatePickingSettingsEvent,NULL );
484 void VisuGUI_GaussPointsSelectionPane::Help()
486 QString aHelpFileName = "picking.htm";
487 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
489 app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
493 platform = "winapplication";
495 platform = "application";
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"));
505 void VisuGUI_GaussPointsSelectionPane::keyPressEvent( QKeyEvent* e )
507 QDialog::keyPressEvent( e );
508 if ( e->isAccepted() )
511 if ( e->key() == Qt::Key_F1 )