1 // Copyright (C) 2007-2011 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_FindPane.cxx
22 // Author : Oleg Uvarov
25 #include "VisuGUI_FindPane.h"
26 #include "VisuGUI_Tools.h"
28 #include "VISU_Actor.h"
29 #include "VISU_Event.h"
30 #include "VISU_GaussPtsAct.h"
32 #include "VISU_IDMapper.hxx"
33 #include "VISU_GaussPointsPL.hxx"
34 #include "VISU_ConvertorUtils.hxx"
36 #include "SUIT_MessageBox.h"
37 #include "SUIT_ResourceMgr.h"
42 #include <QListWidget>
44 #include <QToolButton>
46 #include <vtkCellData.h>
47 #include <vtkDataArray.h>
48 #include <vtkDataSet.h>
49 #include <vtkMapper.h>
50 #include <vtkPointData.h>
54 VisuGUI_FindPane::VisuGUI_FindPane( QWidget* theParent ) :
55 QGroupBox( theParent ),
57 mySelectionMode( -1 ),
60 setTitle( tr( "FIND_TITLE" ) );
62 QGridLayout* aTopLayout = new QGridLayout( this );
64 QLabel* aConditionLabel = new QLabel( tr( "CONDITION" ), this );
66 myConditionBox = new QComboBox( this );
67 myConditionBox->addItems( QStringList()
75 connect( myConditionBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onConditionChanged( int ) ) );
77 QDoubleValidator* aDoubleValidator = new QDoubleValidator( this );
79 myLeftValue = new QLineEdit( this );
80 myLeftValue->setValidator( aDoubleValidator );
82 myDashLabel = new QLabel( "-", this );
84 myRightValue = new QLineEdit( this );
85 myRightValue->setValidator( aDoubleValidator );
87 QToolButton* anApplyBtn = new QToolButton( this );
88 anApplyBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_APPLY" ) ) );
89 connect( anApplyBtn, SIGNAL( clicked() ), this, SLOT( onApply() ) );
92 QWidget* anIdsWidget = new QWidget( this );
93 QGridLayout* anIdsLayout = new QGridLayout( anIdsWidget );
94 anIdsLayout->setMargin( 0 );
96 myIdsListWidget = new QListWidget( anIdsWidget );
97 myIdsListWidget->setFlow( QListView::LeftToRight );
98 myIdsListWidget->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
99 myIdsListWidget->setFixedHeight( 45 );
101 connect( myIdsListWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( onIdChanged() ) );
103 myPageLabel = new QLabel( anIdsWidget );
104 myPageLabel->setAlignment( Qt::AlignHCenter );
106 myPrevBtn = new QToolButton( anIdsWidget );
107 myPrevBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_SLIDER_PREVIOUS" ) ) );
108 connect( myPrevBtn, SIGNAL( clicked() ), this, SLOT( onPrevPage() ) );
110 myNextBtn = new QToolButton( anIdsWidget );
111 myNextBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_SLIDER_NEXT" ) ) );
112 connect( myNextBtn, SIGNAL( clicked() ), this, SLOT( onNextPage() ) );
114 anIdsLayout->addWidget( myIdsListWidget, 0, 0, 2, 1 );
115 anIdsLayout->addWidget( myPageLabel, 0, 1, 1, 2 );
116 anIdsLayout->addWidget( myPrevBtn, 1, 1 );
117 anIdsLayout->addWidget( myNextBtn, 1, 2 );
120 aTopLayout->addWidget( aConditionLabel, 0, 0, 1, 5 );
121 aTopLayout->addWidget( myConditionBox, 1, 0 );
122 aTopLayout->addWidget( myLeftValue, 1, 1 );
123 aTopLayout->addWidget( myDashLabel, 1, 2 );
124 aTopLayout->addWidget( myRightValue, 1, 3 );
125 aTopLayout->addWidget( anApplyBtn, 1, 4 );
126 aTopLayout->addWidget( anIdsWidget, 2, 0, 1, 5 );
128 onConditionChanged( 0 );
130 setSelectionMode( ActorSelection );
133 VisuGUI_FindPane::~VisuGUI_FindPane()
137 void VisuGUI_FindPane::setSelectionMode( const Selection_Mode theSelectionMode )
139 if( mySelectionMode != theSelectionMode )
142 mySelectionMode = theSelectionMode;
143 setEnabled( mySelectionMode != ActorSelection );
147 void VisuGUI_FindPane::setActor( VISU_Actor* theActor )
149 if( myActor != theActor )
155 void VisuGUI_FindPane::onConditionChanged( int theId )
157 myLeftValue->setEnabled( theId >= 2 );
158 myDashLabel->setEnabled( theId == 5 );
159 myRightValue->setEnabled( theId == 5 );
162 void VisuGUI_FindPane::onApply()
166 SUIT_MessageBox::warning( this, tr( "WRN_VISU" ), tr( "INCORRECT_VALUES" ) );
170 myIdsListWidget->clear();
175 vtkDataSet* aDataSet = myActor->GetInput();
177 vtkDataArray* aScalars = 0;
178 if( mySelectionMode == NodeSelection )
179 aScalars = aDataSet->GetPointData()->GetScalars();
180 else if( mySelectionMode == CellSelection )
181 aScalars = aDataSet->GetCellData()->GetScalars();
182 else if( mySelectionMode == GaussPointSelection )
184 if( VISU_GaussPtsAct* aGaussPtsAct = dynamic_cast<VISU_GaussPtsAct*>( myActor ) )
185 aScalars = aGaussPtsAct->GetInput()->GetPointData()->GetScalars();
192 int aCondition = myConditionBox->currentIndex();
193 double aLeftValue = myLeftValue->text().toDouble();
194 double aRightValue = myRightValue->text().toDouble();
198 double eps = 1.0 / VTK_LARGE_FLOAT;
200 double anExtremum = 0;
201 if( aCondition == 0 )
202 anExtremum = VTK_LARGE_FLOAT;
203 else if( aCondition == 1 )
204 anExtremum = -VTK_LARGE_FLOAT;
206 for( int aVTKId = 0, aNbVal = aScalars->GetNumberOfTuples(); aVTKId < aNbVal; aVTKId++ )
208 double aValue = *aScalars->GetTuple( aVTKId );
210 TFindId anId( -1, -1 );
211 if( mySelectionMode == NodeSelection )
212 anId.first = VISU::GetNodeObjID( aDataSet, aVTKId );
213 else if( mySelectionMode == CellSelection )
214 anId.first = VISU::GetElemObjID( aDataSet, aVTKId );
215 else if( mySelectionMode == GaussPointSelection )
217 if( VISU_GaussPtsAct* aGaussPtsAct = dynamic_cast<VISU_GaussPtsAct*>( myActor ) )
219 VISU::TGaussPointID aGaussPointID = aGaussPtsAct->GetGaussPointsPL()->GetObjID( aVTKId );
220 anId.first = aGaussPointID.first;
221 anId.second = aGaussPointID.second;
224 //printf( "(%d) <%d - %d> %f\n", aVTKId, anId.first, anId.second, aValue );
226 if( anId.first < 0 ||
227 anId.second < 0 && mySelectionMode == GaussPointSelection )
234 ok = ( aValue - anExtremum ) < eps;
237 ok = ( aValue - anExtremum ) > -eps;
240 ok = fabs( aValue - aLeftValue ) < eps;
243 ok = ( aValue - aLeftValue ) < eps;
246 ok = ( aValue - aLeftValue ) > -eps;
249 ok = ( aValue - aLeftValue ) > -eps && ( aValue - aRightValue ) < eps;
258 if( aCondition <= 1 && fabs( aValue - anExtremum ) > eps )
263 if( !myIdsList.contains( anId ) )
264 myIdsList.append( anId );
274 void VisuGUI_FindPane::onIdChanged()
276 int aFirstId = -1, aSecondId = -1;
278 QList<QListWidgetItem*> aSelectedItems = myIdsListWidget->selectedItems();
279 if( aSelectedItems.size() == 1 )
281 QString aStr = aSelectedItems.first()->text();
284 aFirstId = aStr.toInt( &ok );
285 if( !ok ) // try to parse the string as a pair of ids - "[aFirstId aSecondId]"
289 aFirstId = aStr.section( ' ', 0, 0 ).toInt( &ok );
295 aSecondId = aStr.section( ' ', 1, 1 ).toInt( &ok );
302 emit idChanged( aFirstId, aSecondId );
305 void VisuGUI_FindPane::onPrevPage()
311 void VisuGUI_FindPane::onNextPage()
317 bool VisuGUI_FindPane::isValid() const
320 double aLeftValue = myLeftValue->text().toDouble( &ok );
321 if( myLeftValue->isEnabled() && !ok )
325 double aRightValue = myRightValue->text().toDouble( &ok );
326 if( myRightValue->isEnabled() && ( !ok || aRightValue < aLeftValue ) )
332 void VisuGUI_FindPane::clearIds()
340 void VisuGUI_FindPane::displayIds()
342 myIdsListWidget->clear();
344 int aSize = myIdsList.size();
346 myPrevBtn->setEnabled( myCurrentPage != 0 );
347 myNextBtn->setEnabled( ( myCurrentPage + 1 ) * PAGE_SIZE < aSize );
348 myPageLabel->setText( QString( "Page %1/%2" )
349 .arg( aSize > 0 ? myCurrentPage + 1 : 0 )
350 .arg( aSize > 0 ? ( aSize - 1 ) / PAGE_SIZE + 1 : 0 ) );
352 int aFirstIndex = myCurrentPage * PAGE_SIZE;
353 int aLastIndex = aFirstIndex + PAGE_SIZE - 1;
354 if( aLastIndex >= aSize )
355 aLastIndex = aSize - 1;
357 for( int anIndex = aFirstIndex; anIndex <= aLastIndex; anIndex++ )
359 TFindId anId = myIdsList[ anIndex ];
360 int aFirstId = anId.first, aSecondId = anId.second;
361 QString aStr = aSecondId < 0 ?
362 QString( "%1" ).arg( aFirstId ) :
363 QString( "[%1 %2]" ).arg( aFirstId ).arg( aSecondId );
364 myIdsListWidget->addItem( aStr );