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_FindPane.cxx
24 // Author : Oleg Uvarov
27 #include "VisuGUI_FindPane.h"
28 #include "VisuGUI_Tools.h"
30 #include "VISU_Actor.h"
31 #include "VISU_Event.h"
32 #include "VISU_GaussPtsAct.h"
34 #include "VISU_IDMapper.hxx"
35 #include "VISU_GaussPointsPL.hxx"
36 #include "VISU_ConvertorUtils.hxx"
38 #include "SUIT_MessageBox.h"
39 #include "SUIT_ResourceMgr.h"
44 #include <QListWidget>
46 #include <QToolButton>
48 #include <vtkCellData.h>
49 #include <vtkDataArray.h>
50 #include <vtkDataSet.h>
51 #include <vtkMapper.h>
52 #include <vtkPointData.h>
56 VisuGUI_FindPane::VisuGUI_FindPane( QWidget* theParent ) :
57 QGroupBox( theParent ),
59 mySelectionMode( -1 ),
62 setTitle( tr( "FIND_TITLE" ) );
64 QGridLayout* aTopLayout = new QGridLayout( this );
66 QLabel* aConditionLabel = new QLabel( tr( "CONDITION" ), this );
68 myConditionBox = new QComboBox( this );
69 myConditionBox->addItems( QStringList()
77 connect( myConditionBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onConditionChanged( int ) ) );
79 QDoubleValidator* aDoubleValidator = new QDoubleValidator( this );
81 myLeftValue = new QLineEdit( this );
82 myLeftValue->setValidator( aDoubleValidator );
84 myDashLabel = new QLabel( "-", this );
86 myRightValue = new QLineEdit( this );
87 myRightValue->setValidator( aDoubleValidator );
89 QToolButton* anApplyBtn = new QToolButton( this );
90 anApplyBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_APPLY" ) ) );
91 connect( anApplyBtn, SIGNAL( clicked() ), this, SLOT( onApply() ) );
94 QWidget* anIdsWidget = new QWidget( this );
95 QGridLayout* anIdsLayout = new QGridLayout( anIdsWidget );
96 anIdsLayout->setMargin( 0 );
98 myIdsListWidget = new QListWidget( anIdsWidget );
99 myIdsListWidget->setFlow( QListView::LeftToRight );
100 myIdsListWidget->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
101 myIdsListWidget->setFixedHeight( 45 );
103 connect( myIdsListWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( onIdChanged() ) );
105 myPageLabel = new QLabel( anIdsWidget );
106 myPageLabel->setAlignment( Qt::AlignHCenter );
108 myPrevBtn = new QToolButton( anIdsWidget );
109 myPrevBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_SLIDER_PREVIOUS" ) ) );
110 connect( myPrevBtn, SIGNAL( clicked() ), this, SLOT( onPrevPage() ) );
112 myNextBtn = new QToolButton( anIdsWidget );
113 myNextBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_SLIDER_NEXT" ) ) );
114 connect( myNextBtn, SIGNAL( clicked() ), this, SLOT( onNextPage() ) );
116 anIdsLayout->addWidget( myIdsListWidget, 0, 0, 2, 1 );
117 anIdsLayout->addWidget( myPageLabel, 0, 1, 1, 2 );
118 anIdsLayout->addWidget( myPrevBtn, 1, 1 );
119 anIdsLayout->addWidget( myNextBtn, 1, 2 );
122 aTopLayout->addWidget( aConditionLabel, 0, 0, 1, 5 );
123 aTopLayout->addWidget( myConditionBox, 1, 0 );
124 aTopLayout->addWidget( myLeftValue, 1, 1 );
125 aTopLayout->addWidget( myDashLabel, 1, 2 );
126 aTopLayout->addWidget( myRightValue, 1, 3 );
127 aTopLayout->addWidget( anApplyBtn, 1, 4 );
128 aTopLayout->addWidget( anIdsWidget, 2, 0, 1, 5 );
130 onConditionChanged( 0 );
132 setSelectionMode( ActorSelection );
135 VisuGUI_FindPane::~VisuGUI_FindPane()
139 void VisuGUI_FindPane::setSelectionMode( const Selection_Mode theSelectionMode )
141 if( mySelectionMode != theSelectionMode )
144 mySelectionMode = theSelectionMode;
145 setEnabled( mySelectionMode != ActorSelection );
149 void VisuGUI_FindPane::setActor( VISU_Actor* theActor )
151 if( myActor != theActor )
157 void VisuGUI_FindPane::onConditionChanged( int theId )
159 myLeftValue->setEnabled( theId >= 2 );
160 myDashLabel->setEnabled( theId == 5 );
161 myRightValue->setEnabled( theId == 5 );
164 void VisuGUI_FindPane::onApply()
168 SUIT_MessageBox::warning( this, tr( "WRN_VISU" ), tr( "INCORRECT_VALUES" ) );
172 myIdsListWidget->clear();
177 vtkDataSet* aDataSet = myActor->GetInput();
179 vtkDataArray* aScalars = 0;
180 if( mySelectionMode == NodeSelection )
181 aScalars = aDataSet->GetPointData()->GetScalars();
182 else if( mySelectionMode == CellSelection )
183 aScalars = aDataSet->GetCellData()->GetScalars();
184 else if( mySelectionMode == GaussPointSelection )
186 if( VISU_GaussPtsAct* aGaussPtsAct = dynamic_cast<VISU_GaussPtsAct*>( myActor ) )
187 aScalars = aGaussPtsAct->GetInput()->GetPointData()->GetScalars();
194 int aCondition = myConditionBox->currentIndex();
195 double aLeftValue = myLeftValue->text().toDouble();
196 double aRightValue = myRightValue->text().toDouble();
200 double eps = 1.0 / VTK_LARGE_FLOAT;
202 double anExtremum = 0;
203 if( aCondition == 0 )
204 anExtremum = VTK_LARGE_FLOAT;
205 else if( aCondition == 1 )
206 anExtremum = -VTK_LARGE_FLOAT;
208 for( int aVTKId = 0, aNbVal = aScalars->GetNumberOfTuples(); aVTKId < aNbVal; aVTKId++ )
210 double aValue = *aScalars->GetTuple( aVTKId );
212 TFindId anId( -1, -1 );
213 if( mySelectionMode == NodeSelection )
214 anId.first = VISU::GetNodeObjID( aDataSet, aVTKId );
215 else if( mySelectionMode == CellSelection )
216 anId.first = VISU::GetElemObjID( aDataSet, aVTKId );
217 else if( mySelectionMode == GaussPointSelection )
219 if( VISU_GaussPtsAct* aGaussPtsAct = dynamic_cast<VISU_GaussPtsAct*>( myActor ) )
221 VISU::TGaussPointID aGaussPointID = aGaussPtsAct->GetGaussPointsPL()->GetObjID( aVTKId );
222 anId.first = aGaussPointID.first;
223 anId.second = aGaussPointID.second;
226 //printf( "(%d) <%d - %d> %f\n", aVTKId, anId.first, anId.second, aValue );
228 if( anId.first < 0 ||
229 anId.second < 0 && mySelectionMode == GaussPointSelection )
236 ok = ( aValue - anExtremum ) < eps;
239 ok = ( aValue - anExtremum ) > -eps;
242 ok = fabs( aValue - aLeftValue ) < eps;
245 ok = ( aValue - aLeftValue ) < eps;
248 ok = ( aValue - aLeftValue ) > -eps;
251 ok = ( aValue - aLeftValue ) > -eps && ( aValue - aRightValue ) < eps;
260 if( aCondition <= 1 && fabs( aValue - anExtremum ) > eps )
265 if( !myIdsList.contains( anId ) )
266 myIdsList.append( anId );
276 void VisuGUI_FindPane::onIdChanged()
278 int aFirstId = -1, aSecondId = -1;
280 QList<QListWidgetItem*> aSelectedItems = myIdsListWidget->selectedItems();
281 if( aSelectedItems.size() == 1 )
283 QString aStr = aSelectedItems.first()->text();
286 aFirstId = aStr.toInt( &ok );
287 if( !ok ) // try to parse the string as a pair of ids - "[aFirstId aSecondId]"
291 aFirstId = aStr.section( ' ', 0, 0 ).toInt( &ok );
297 aSecondId = aStr.section( ' ', 1, 1 ).toInt( &ok );
304 emit idChanged( aFirstId, aSecondId );
307 void VisuGUI_FindPane::onPrevPage()
313 void VisuGUI_FindPane::onNextPage()
319 bool VisuGUI_FindPane::isValid() const
322 double aLeftValue = myLeftValue->text().toDouble( &ok );
323 if( myLeftValue->isEnabled() && !ok )
327 double aRightValue = myRightValue->text().toDouble( &ok );
328 if( myRightValue->isEnabled() && ( !ok || aRightValue < aLeftValue ) )
334 void VisuGUI_FindPane::clearIds()
342 void VisuGUI_FindPane::displayIds()
344 myIdsListWidget->clear();
346 int aSize = myIdsList.size();
348 myPrevBtn->setEnabled( myCurrentPage != 0 );
349 myNextBtn->setEnabled( ( myCurrentPage + 1 ) * PAGE_SIZE < aSize );
350 myPageLabel->setText( QString( "Page %1/%2" )
351 .arg( aSize > 0 ? myCurrentPage + 1 : 0 )
352 .arg( aSize > 0 ? ( aSize - 1 ) / PAGE_SIZE + 1 : 0 ) );
354 int aFirstIndex = myCurrentPage * PAGE_SIZE;
355 int aLastIndex = aFirstIndex + PAGE_SIZE - 1;
356 if( aLastIndex >= aSize )
357 aLastIndex = aSize - 1;
359 for( int anIndex = aFirstIndex; anIndex <= aLastIndex; anIndex++ )
361 TFindId anId = myIdsList[ anIndex ];
362 int aFirstId = anId.first, aSecondId = anId.second;
363 QString aStr = aSecondId < 0 ?
364 QString( "%1" ).arg( aFirstId ) :
365 QString( "[%1 %2]" ).arg( aFirstId ).arg( aSecondId );
366 myIdsListWidget->addItem( aStr );