1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // VISU VISUGUI : GUI of VISU component
21 // File : VisuGUI_GaussPointsSelectionPane.cxx
22 // Author : Oleg Uvarov
25 #include "VisuGUI_GaussPointsSelectionPane.h"
26 #include "VisuGUI_ViewTools.h"
28 #include "VISU_GaussPtsAct.h"
29 #include "VISU_GaussPtsSettings.h"
30 #include "VISU_GaussPointsPL.hxx"
31 #include "VISU_PickingSettings.h"
33 #include "SUIT_MessageBox.h"
34 #include "SUIT_ResourceMgr.h"
35 #include "SUIT_Session.h"
37 #include "CAM_Module.h"
39 #include "SVTK_Selector.h"
40 #include "SVTK_ViewWindow.h"
41 //#include "SVTK_MainWindow.h"
42 #include "SVTK_RenderWindowInteractor.h"
44 #include "VTKViewer_Algorithm.h"
45 #include "SVTK_Functor.h"
47 #include <vtkActorCollection.h>
48 #include <vtkCallbackCommand.h>
49 #include <vtkObjectFactory.h>
50 #include <vtkRenderer.h>
51 #include <vtkGenericRenderWindowInteractor.h>
52 #include <vtkSmartPointer.h>
54 #include "utilities.h"
61 #include <QPushButton>
62 #include <QVBoxLayout>
64 #include <QIntValidator>
65 #include <QToolButton>
67 #include "LightApp_Application.h"
74 SelectorHelper( const SalomeApp_Module* theModule ):
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();
95 mySelector = anInteractor->GetSelector();
99 const SALOME_ListIO& aListIO = mySelector->StoredIObjects();
100 if ( aListIO.Extent() != 1 )
103 myIO = aListIO.First();
104 if ( mySelector->HasIndex( myIO ) )
105 mySelector->GetIndex(myIO, myMapIndex);
107 VTK::ActorCollectionCopy aCopy(anInteractor->getRenderer()->GetActors());
108 myActor = SVTK::Find<VISU_GaussPtsAct>(aCopy.GetActors(),
109 SVTK::TIsSameIObject<VISU_GaussPtsAct>( myIO ));
113 myPipeLine = myActor->GetGaussPointsPL();
118 const SalomeApp_Module* myModule;
119 TColStd_IndexedMapOfInteger myMapIndex;
120 Handle(SALOME_InteractiveObject) myIO;
121 SVTK_Selector* mySelector;
123 VISU_GaussPointsPL* myPipeLine;
124 VISU_GaussPtsAct* myActor;
129 class GaussPtsIDValidator: public QIntValidator
132 GaussPtsIDValidator( const SalomeApp_Module* theModule,
133 QObject * theParent ):
134 QIntValidator( 0, VTK_LARGE_ID, theParent ),
140 validate ( QString & theInput, int & thePos ) const
142 if ( QIntValidator::validate( theInput, thePos ) == QValidator::Invalid)
143 return QValidator::Invalid;
145 if ( !myHelper.get() )
146 return QValidator::Invalid;
148 return QValidator::Acceptable;
152 mutable SelectorHelper myHelper;
156 class GaussCellIDValidator: public GaussPtsIDValidator
159 GaussCellIDValidator( QLineEdit* theLocalPointLabel,
160 const SalomeApp_Module* theModule,
161 QObject * theParent ):
162 GaussPtsIDValidator( theModule, theParent ),
163 myLocalPointLineEdit( theLocalPointLabel )
168 validate ( QString & theInput, int & thePos ) const
170 if( theInput.isEmpty() )
171 return QValidator::Acceptable;
173 if ( GaussPtsIDValidator::validate( theInput, thePos ) == QValidator::Invalid)
174 return QValidator::Invalid;
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;
182 return QValidator::Acceptable;
186 QLineEdit* myLocalPointLineEdit;
190 class GaussLocalPointIDValidator: public GaussPtsIDValidator
193 GaussLocalPointIDValidator( QLineEdit* theParentElementLineEdit,
194 const SalomeApp_Module* theModule,
195 QObject * theParent ):
196 GaussPtsIDValidator( theModule, theParent ),
197 myParentElementLineEdit( theParentElementLineEdit )
202 validate ( QString & theInput, int & thePos ) const
204 if( theInput.isEmpty() )
205 return QValidator::Acceptable;
207 if ( GaussPtsIDValidator::validate( theInput, thePos ) == QValidator::Invalid)
208 return QValidator::Invalid;
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;
216 return QValidator::Acceptable;
220 QLineEdit* myParentElementLineEdit;
225 VisuGUI_ValidatedLineEdit::VisuGUI_ValidatedLineEdit( QWidget* parent ):
228 connect( this, SIGNAL( textChanged( const QString& ) ), this, SLOT( MarkValidated( const QString& ) ) );
231 void VisuGUI_ValidatedLineEdit::MarkValidated( const QString& theText )
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 ));
245 case QValidator::Acceptable:
246 pal.setColor( foregroundRole(), QColor( 0, 0, 0 ));
252 //---------------------------------------------------------------------------------
254 VisuGUI_GaussPointsSelectionPane::VisuGUI_GaussPointsSelectionPane( const SalomeApp_Module* theModule,
255 QWidget* theParent ) :
256 QWidget( theParent ),
257 myModule( theModule ),
258 myEventCallbackCommand( vtkCallbackCommand::New() )
261 myEventCallbackCommand->Delete();
262 myEventCallbackCommand->SetClientData(this);
263 myEventCallbackCommand->SetCallback(VisuGUI_GaussPointsSelectionPane::ProcessEvents);
265 QVBoxLayout* TopLayout = new QVBoxLayout( this );
266 //TopLayout->setSpacing(6);
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);
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() ) );
280 QToolButton* ParentElementBtn = new QToolButton( PositionGroup );
281 ParentElementBtn->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr( "ICON_OK" ) ) );
282 connect(ParentElementBtn, SIGNAL(clicked()), this, SLOT(onSelectionApply()));
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() ) );
290 QToolButton* LocalPointBtn = new QToolButton( PositionGroup );
291 LocalPointBtn->setIcon( VISU::GetResourceMgr()->loadPixmap("VISU", tr( "ICON_OK" ) ) );
292 connect(LocalPointBtn, SIGNAL(clicked()), this, SLOT(onSelectionApply()));
294 myCellIDValidator = new GaussCellIDValidator( myLocalPointLineEdit, myModule, myParentElementLineEdit );
295 myParentElementLineEdit->setValidator( myCellIDValidator );
297 myLocalPointIDValidator = new GaussLocalPointIDValidator( myParentElementLineEdit, myModule, myLocalPointLineEdit );
298 myLocalPointLineEdit->setValidator( myLocalPointIDValidator );
300 myDisplayParentMeshCheckBox = new QCheckBox( tr( "DISPLAY_PARENT_MESH" ), PositionGroup );
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 );
310 connect( myDisplayParentMeshCheckBox, SIGNAL( toggled( bool ) ), this, SLOT( onApplyDisplayParentMesh( bool ) ) );
312 TopLayout->addWidget( PositionGroup );
313 TopLayout->addStretch();
316 VisuGUI_GaussPointsSelectionPane::~VisuGUI_GaussPointsSelectionPane()
320 void VisuGUI_GaussPointsSelectionPane::update()
322 VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
323 myDisplayParentMeshCheckBox->setChecked( aPickingSettings->GetDisplayParentMesh() );
326 void VisuGUI_GaussPointsSelectionPane::setIds( const int theParentId, const int theLocalId )
328 myParentElementLineEdit->setText( theParentId < 0 ? "" : QString::number( theParentId ) );
329 myLocalPointLineEdit->setText( theLocalId < 0 ? "" : QString::number( theLocalId ) );
333 void VisuGUI_GaussPointsSelectionPane::setInteractor( SVTK_RenderWindowInteractor* theInteractor )
335 //printf( "VisuGUI_GaussPointsSelectionPane::setInteractor( %p )\n", theInteractor );
336 vtkGenericRenderWindowInteractor* aDevice = theInteractor->GetDevice();
337 if( aDevice->HasObserver(vtkCommand::KeyPressEvent) )
338 aDevice->RemoveObservers(vtkCommand::KeyPressEvent);
340 //printf( "AddObserver(vtkCommand::KeyPressEvent)\n" );
341 aDevice->AddObserver(vtkCommand::KeyPressEvent,
342 myEventCallbackCommand.GetPointer(),
345 if( aDevice->HasObserver(vtkCommand::EndPickEvent) )
346 aDevice->RemoveObservers(vtkCommand::EndPickEvent);
348 //printf( "AddObserver(vtkCommand::EndPickEvent)\n" );
349 aDevice->AddObserver(vtkCommand::EndPickEvent,
350 myEventCallbackCommand.GetPointer(),
354 SVTK_RenderWindowInteractor* VisuGUI_GaussPointsSelectionPane::getInteractor()
356 if( SVTK_ViewWindow* aViewWindow = VISU::GetActiveViewWindow<SVTK_ViewWindow>( myModule ) )
358 //if( SVTK_MainWindow* aMainWindow = aViewWindow->getMainWindow() )
360 SVTK_RenderWindowInteractor* anInteractor = aViewWindow->GetInteractor();
367 void VisuGUI_GaussPointsSelectionPane::ProcessEvents(vtkObject* vtkNotUsed(theObject),
368 unsigned long theEvent,
370 void* vtkNotUsed(theCallData))
372 VisuGUI_GaussPointsSelectionPane* self = reinterpret_cast<VisuGUI_GaussPointsSelectionPane*>(theClientData);
375 case vtkCommand::KeyPressEvent:
378 case vtkCommand::EndPickEvent:
379 self->SelectionEvent();
384 void VisuGUI_GaussPointsSelectionPane::KeyPressed()
386 //printf( "VisuGUI_GaussPointsSelectionPane::KeyPressed()\n" );
387 if( getInteractor()->GetDevice()->GetKeyCode() == 'P' )
389 VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
391 bool aDisplayParentMesh = !aPickingSettings->GetDisplayParentMesh();
392 myDisplayParentMeshCheckBox->setChecked( aDisplayParentMesh );
394 aPickingSettings->SetDisplayParentMesh( aDisplayParentMesh );
395 aPickingSettings->InvokeEvent(VISU::UpdatePickingSettingsEvent,NULL);
399 void VisuGUI_GaussPointsSelectionPane::SelectionEvent()
401 //printf( "VisuGUI_GaussPointsSelectionPane::SelectionEvent()\n" );
404 SelectorHelper aHelper( myModule );
407 const TColStd_IndexedMapOfInteger& aMapIndex = aHelper.myMapIndex;
408 if( aMapIndex.Extent() == 1 )
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;
415 myParentElementLineEdit->setText( QString::number( aCellID ) );
416 myLocalPointLineEdit->setText( QString::number( aLocalPntID ) );
421 void VisuGUI_GaussPointsSelectionPane::onSelectionValidate()
423 myParentElementLineEdit->MarkValidated( myParentElementLineEdit->text() );
424 myLocalPointLineEdit->MarkValidated( myLocalPointLineEdit->text() );
427 void VisuGUI_GaussPointsSelectionPane::onSelectionApply()
429 //printf( "VisuGUI_GaussPointsSelectionPane::onSelectionApply()\n" );
430 SelectorHelper aHelper( myModule );
434 vtkIdType anObjVtkId = -1;
435 VISU::TCellID aCellId;
436 VISU::TLocalPntID aLocalPntId;
439 aCellId = myParentElementLineEdit->text().toInt( &ok );
443 aLocalPntId = myLocalPointLineEdit->text().toInt( &ok );
446 VISU::PGaussPtsIDMapper anIDMapper = aHelper.myPipeLine->GetGaussPtsIDMapper();
447 anObjVtkId = anIDMapper->GetVTKID( VISU::TGaussPointID( aCellId, aLocalPntId ) );
452 aHelper.mySelector->ClearIndex();
455 aHelper.myMapIndex.Clear();
456 aHelper.myMapIndex.Add( anObjVtkId );
457 aHelper.mySelector->AddOrRemoveIndex( aHelper.myIO, aHelper.myMapIndex, false );
459 aHelper.myActor->Highlight( aHelper.myIO );
460 getInteractor()->GetDevice()->CreateTimer( VTKI_TIMER_FIRST );
463 void VisuGUI_GaussPointsSelectionPane::clearIDControls()
465 myParentElementLineEdit->setText( "" );
466 myLocalPointLineEdit->setText( "" );
469 void VisuGUI_GaussPointsSelectionPane::apply()
474 void VisuGUI_GaussPointsSelectionPane::onApplyDisplayParentMesh( bool theValue )
476 VISU_PickingSettings* aPickingSettings = VISU_PickingSettings::Get();
477 aPickingSettings->SetDisplayParentMesh( theValue );
478 aPickingSettings->InvokeEvent( VISU::UpdatePickingSettingsEvent,NULL );
482 void VisuGUI_GaussPointsSelectionPane::Help()
484 QString aHelpFileName = "picking.htm";
485 LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
487 app->onHelpContextModule(app->activeModule() ? app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
491 platform = "winapplication";
493 platform = "application";
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"));
503 void VisuGUI_GaussPointsSelectionPane::keyPressEvent( QKeyEvent* e )
505 QDialog::keyPressEvent( e );
506 if ( e->isAccepted() )
509 if ( e->key() == Qt::Key_F1 )