Salome HOME
07b2cdc8a725d106ee13370f7b6d837b80f19e1a
[modules/visu.git] / src / VISUGUI / VisuGUI_FindPane.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_FindPane.cxx
24 //  Author : Oleg Uvarov
25 //  Module : VISU
26 //
27 #include "VisuGUI_FindPane.h"
28 #include "VisuGUI_Tools.h"
29
30 #include "VISU_Actor.h"
31 #include "VISU_Event.h"
32 #include "VISU_GaussPtsAct.h"
33
34 #include "VISU_IDMapper.hxx"
35 #include "VISU_GaussPointsPL.hxx"
36 #include "VISU_ConvertorUtils.hxx"
37
38 #include "SUIT_MessageBox.h"
39 #include "SUIT_ResourceMgr.h"
40
41 #include <QComboBox>
42 #include <QLabel>
43 #include <QLayout>
44 #include <QListWidget>
45 #include <QLineEdit>
46 #include <QToolButton>
47
48 #include <vtkCellData.h>
49 #include <vtkDataArray.h>
50 #include <vtkDataSet.h>
51 #include <vtkMapper.h>
52 #include <vtkPointData.h>
53
54 #define PAGE_SIZE 10
55
56 VisuGUI_FindPane::VisuGUI_FindPane( QWidget* theParent ) :
57   QGroupBox( theParent ),
58   myCurrentPage( 0 ),
59   mySelectionMode( -1 ),
60   myActor( 0 )
61 {
62   setTitle( tr( "FIND_TITLE" ) );
63
64   QGridLayout* aTopLayout = new QGridLayout( this );
65
66   QLabel* aConditionLabel = new QLabel( tr( "CONDITION" ), this );
67
68   myConditionBox = new QComboBox( this );
69   myConditionBox->addItems( QStringList()
70                             << tr( "MINIMUM" )
71                             << tr( "MAXIMUM" )
72                             << "="
73                             << "<="
74                             << ">="
75                             << tr( "BETWEEN" ) );
76
77   connect( myConditionBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( onConditionChanged( int ) ) );
78
79   QDoubleValidator* aDoubleValidator = new QDoubleValidator( this );
80
81   myLeftValue = new QLineEdit( this );
82   myLeftValue->setValidator( aDoubleValidator );
83
84   myDashLabel = new QLabel( "-", this );
85
86   myRightValue = new QLineEdit( this );
87   myRightValue->setValidator( aDoubleValidator );
88
89   QToolButton* anApplyBtn = new QToolButton( this );
90   anApplyBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_APPLY" ) ) );
91   connect( anApplyBtn, SIGNAL( clicked() ), this, SLOT( onApply() ) );
92
93
94   QWidget* anIdsWidget = new QWidget( this );
95   QGridLayout* anIdsLayout = new QGridLayout( anIdsWidget );
96   anIdsLayout->setMargin( 0 );
97
98   myIdsListWidget = new QListWidget( anIdsWidget );
99   myIdsListWidget->setFlow( QListView::LeftToRight );
100   myIdsListWidget->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
101   myIdsListWidget->setFixedHeight( 45 );
102
103   connect( myIdsListWidget, SIGNAL( itemSelectionChanged() ), this, SLOT( onIdChanged() ) );
104
105   myPageLabel = new QLabel( anIdsWidget );
106   myPageLabel->setAlignment( Qt::AlignHCenter );
107
108   myPrevBtn = new QToolButton( anIdsWidget );
109   myPrevBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_SLIDER_PREVIOUS" ) ) );
110   connect( myPrevBtn, SIGNAL( clicked() ), this, SLOT( onPrevPage() ) );
111
112   myNextBtn = new QToolButton( anIdsWidget );
113   myNextBtn->setIcon( VISU::GetResourceMgr()->loadPixmap( "VISU", tr( "ICON_SLIDER_NEXT" ) ) );
114   connect( myNextBtn, SIGNAL( clicked() ), this, SLOT( onNextPage() ) );
115
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 );
120
121
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 );
129
130   onConditionChanged( 0 );
131
132   setSelectionMode( ActorSelection );
133 }
134
135 VisuGUI_FindPane::~VisuGUI_FindPane()
136 {
137 }
138
139 void VisuGUI_FindPane::setSelectionMode( const Selection_Mode theSelectionMode )
140 {
141   if( mySelectionMode != theSelectionMode )
142     clearIds();
143
144   mySelectionMode = theSelectionMode;
145   setEnabled( mySelectionMode != ActorSelection );
146 }
147
148
149 void VisuGUI_FindPane::setActor( VISU_Actor* theActor )
150 {
151   if( myActor != theActor )
152     clearIds();
153
154   myActor = theActor;
155 }
156
157 void VisuGUI_FindPane::onConditionChanged( int theId )
158 {
159   myLeftValue->setEnabled( theId >= 2 );
160   myDashLabel->setEnabled( theId == 5 );
161   myRightValue->setEnabled( theId == 5 );
162 }
163
164 void VisuGUI_FindPane::onApply()
165 {
166   if( !isValid() )
167   {
168     SUIT_MessageBox::warning( this, tr( "WRN_VISU" ), tr( "INCORRECT_VALUES" ) );
169     return;
170   }
171
172   myIdsListWidget->clear();
173
174   if( !myActor )
175     return;
176
177   vtkDataSet* aDataSet = myActor->GetInput();
178
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 )
185   {
186     if( VISU_GaussPtsAct* aGaussPtsAct = dynamic_cast<VISU_GaussPtsAct*>( myActor ) )
187       aScalars = aGaussPtsAct->GetInput()->GetPointData()->GetScalars();
188   }
189
190
191   if( !aScalars )
192     return;
193
194   int aCondition = myConditionBox->currentIndex();
195   double aLeftValue = myLeftValue->text().toDouble();
196   double aRightValue = myRightValue->text().toDouble();
197
198   myIdsList.clear();
199
200   double eps = 1.0 / VTK_LARGE_FLOAT;
201
202   double anExtremum = 0;
203   if( aCondition == 0 )
204     anExtremum = VTK_LARGE_FLOAT;
205   else if( aCondition == 1 )
206     anExtremum = -VTK_LARGE_FLOAT;
207
208   for( int aVTKId = 0, aNbVal = aScalars->GetNumberOfTuples(); aVTKId < aNbVal; aVTKId++ )
209   {
210     double aValue = *aScalars->GetTuple( aVTKId );
211
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 )
218     {
219       if( VISU_GaussPtsAct* aGaussPtsAct = dynamic_cast<VISU_GaussPtsAct*>( myActor ) )
220       {
221         VISU::TGaussPointID aGaussPointID = aGaussPtsAct->GetGaussPointsPL()->GetObjID( aVTKId );
222         anId.first = aGaussPointID.first;
223         anId.second = aGaussPointID.second;
224       }
225     }
226     //printf( "(%d) <%d - %d> %f\n", aVTKId, anId.first, anId.second, aValue );
227
228     if( anId.first < 0 ||
229         anId.second < 0 && mySelectionMode == GaussPointSelection )
230       continue;
231
232     bool ok = false;
233     switch( aCondition )
234     {
235     case 0: // Minimum
236       ok = ( aValue - anExtremum ) < eps;
237       break;
238     case 1: // Maximum
239       ok = ( aValue - anExtremum ) > -eps;
240       break;
241     case 2: // =
242       ok = fabs( aValue - aLeftValue ) < eps;
243       break;
244     case 3: // <=
245       ok = ( aValue - aLeftValue ) < eps;
246       break;
247     case 4: // >=
248       ok = ( aValue - aLeftValue ) > -eps;
249       break;
250     case 5: // Between
251       ok = ( aValue - aLeftValue ) > -eps && ( aValue - aRightValue ) < eps;
252       break;
253     default:
254       ok = true;
255       break;
256     }
257
258     if( ok )
259     {
260       if( aCondition <= 1 && fabs( aValue - anExtremum ) > eps )
261       {
262         anExtremum = aValue;
263         myIdsList.clear();
264       }
265       if( !myIdsList.contains( anId ) )
266         myIdsList.append( anId );
267     }
268   }
269
270   qSort( myIdsList );
271
272   myCurrentPage = 0;
273   displayIds();
274 }
275
276 void VisuGUI_FindPane::onIdChanged()
277 {
278   int aFirstId = -1, aSecondId = -1;
279
280   QList<QListWidgetItem*> aSelectedItems = myIdsListWidget->selectedItems();
281   if( aSelectedItems.size() == 1 )
282   {
283     QString aStr = aSelectedItems.first()->text();
284
285     bool ok = false;
286     aFirstId = aStr.toInt( &ok );
287     if( !ok ) // try to parse the string as a pair of ids - "[aFirstId aSecondId]"
288     {
289       aStr.remove( '[' );
290       aStr.remove( ']' );
291       aFirstId = aStr.section( ' ', 0, 0 ).toInt( &ok );
292       if( !ok )
293         aFirstId = -1;
294       else
295       {
296         ok = false;
297         aSecondId = aStr.section( ' ', 1, 1 ).toInt( &ok );
298         if( !ok )
299           aSecondId = -1;
300       }
301     }
302   }
303
304   emit idChanged( aFirstId, aSecondId );
305 }
306
307 void VisuGUI_FindPane::onPrevPage()
308 {
309   myCurrentPage--;
310   displayIds();
311 }
312
313 void VisuGUI_FindPane::onNextPage()
314 {
315   myCurrentPage++;
316   displayIds();
317 }
318
319 bool VisuGUI_FindPane::isValid() const
320 {
321   bool ok = false;;
322   double aLeftValue = myLeftValue->text().toDouble( &ok );
323   if( myLeftValue->isEnabled() && !ok )
324     return false;
325
326   ok = false;
327   double aRightValue = myRightValue->text().toDouble( &ok );
328   if( myRightValue->isEnabled() && ( !ok || aRightValue < aLeftValue ) )
329     return false;
330
331   return true;
332 }
333
334 void VisuGUI_FindPane::clearIds()
335 {
336   myIdsList.clear();
337   myCurrentPage = 0;
338
339   displayIds();
340 }
341
342 void VisuGUI_FindPane::displayIds()
343 {
344   myIdsListWidget->clear();
345
346   int aSize = myIdsList.size();
347
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 ) );
353
354   int aFirstIndex = myCurrentPage * PAGE_SIZE;
355   int aLastIndex = aFirstIndex + PAGE_SIZE - 1;
356   if( aLastIndex >= aSize )
357     aLastIndex = aSize - 1;
358
359   for( int anIndex = aFirstIndex; anIndex <= aLastIndex; anIndex++ )
360   {
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 );
367   }
368 }