]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Selection.cxx
Salome HOME
Roll-back previous integration
[modules/visu.git] / src / VISUGUI / VisuGUI_Selection.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VisuGUI_Selection.cxx
25 //  Author : Laurent CORNABE & Hubert ROLLAND 
26 //  Module : VISU
27 //  $Header$
28
29 using namespace std;
30 #include "utilities.h"
31 #include "SALOME_ListIteratorOfListIO.hxx"
32 #include "SALOME_Selection.h"
33 #include "VISU_Actor.h"
34 #include "VISU_PrsObject_i.hh"
35 #include "VISU_Prs3d_i.hh"
36 #include "VISU_PipeLine.hxx"
37 #include "VisuGUI.h"
38 #include "QAD_MessageBox.h"
39 #include "VTKViewer_ViewFrame.h"
40
41 #include <TColStd_MapOfInteger.hxx>
42 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
43
44 #include <qstring.h>
45 #include <qlabel.h>
46 #include <qspinbox.h>
47 #include <qlistbox.h>
48 #include <qlayout.h>
49 #include <qhbuttongroup.h>
50 #include <qradiobutton.h>
51
52 #include <vtkDataSet.h>
53 #include <vtkCell.h>
54 #include <qvalidator.h>
55
56 #include "VisuGUI_Selection.h"
57
58
59 extern VisuGUI *visuGUI;
60
61
62 VisuGUI_SelectionDlg::VisuGUI_SelectionDlg()    
63   : QDialog( QAD_Application::getDesktop(), 0, false, 
64              WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
65 {
66   setCaption( "Data on elements" );
67   setSizeGripEnabled( TRUE );
68   
69   QGridLayout* TopLayout = new QGridLayout(this);
70   TopLayout->setSpacing(6);
71   TopLayout->setMargin(11);
72   TopLayout->setRowStretch(0, 0);
73   TopLayout->setRowStretch(1, 0);
74   TopLayout->setRowStretch(2, 1);
75   TopLayout->setRowStretch(3, 0);
76
77   QHButtonGroup* aTypeBox = new QHButtonGroup("Selection", this);  
78   
79   QRadioButton* aPointBtn = new QRadioButton("Point", aTypeBox);
80   QRadioButton* aCellBtn = new QRadioButton("Cell", aTypeBox);
81   QRadioButton* aActorBtn = new QRadioButton("Actor", aTypeBox);
82   aPointBtn->setChecked(true);
83
84   connect(aTypeBox, SIGNAL(clicked(int)), this, SLOT(onSelectionChange(int)));
85   
86   TopLayout->addWidget(aTypeBox, 0, 0);
87
88   QWidget* aNamePane = new QWidget(this);
89   QGridLayout* aNameLay = new QGridLayout(aNamePane);
90
91   QLabel* aMeshLbl = new QLabel("Mesh name: ", aNamePane);
92   myMeshName = new QLabel(aNamePane);
93
94   QLabel* aFieldLbl = new QLabel("Field name: ", aNamePane);
95   myFieldName = new QLabel(aNamePane);
96
97   aNameLay->addWidget(aMeshLbl, 0, 0);
98   aNameLay->addWidget(myMeshName, 0, 1);
99   aNameLay->addWidget(aFieldLbl, 1, 0);
100   aNameLay->addWidget(myFieldName, 1, 1);
101
102   TopLayout->addWidget(aNamePane, 1, 0);
103
104   myWidgetStack = new QWidgetStack(this);
105   
106   // Create Points pane
107   myPointsPane = new QVBox(myWidgetStack);
108   myPointsPane->layout()->setSpacing(6);
109   
110   QGroupBox* aDataGrp = new QGroupBox(2, Qt::Horizontal, "Data of Point", myPointsPane);
111   aDataGrp->layout()->setSpacing(6);
112
113   QLabel* aIDLbl = new QLabel("ID:", aDataGrp);
114   myIDValLbl = new QLineEdit("", aDataGrp);
115   QIntValidator* aIntValidator = new QIntValidator(myIDValLbl);
116   aIntValidator->setBottom(0);
117   myIDValLbl->setValidator(aIntValidator);
118   connect(myIDValLbl, SIGNAL(textChanged(const QString&)), this, SLOT(onPointIdEdit(const QString&)));
119
120   QLabel* aValueLbl = new QLabel("Scalar Value:", aDataGrp);
121   myScalarValLbl = new QLabel("", aDataGrp);
122   QLabel* aVectorLbl = new QLabel("Vector Value:", aDataGrp); 
123   myVectorValLbl = new QLabel("", aDataGrp);
124   myVectorValLbl->setMinimumWidth(150);
125
126   QGroupBox* aCoordGrp = new QGroupBox(2, Qt::Horizontal, "Coordinates", myPointsPane);
127   aCoordGrp->layout()->setSpacing(6);
128   QLabel* aXLbl = new QLabel("X:", aCoordGrp);
129   myXValLbl = new QLabel("", aCoordGrp);
130   QLabel* aYLbl = new QLabel("Y:", aCoordGrp);
131   myYValLbl = new QLabel("", aCoordGrp);
132   QLabel* aZLbl = new QLabel("Z:",aCoordGrp );  
133   myZValLbl = new QLabel("", aCoordGrp);
134
135
136   myWidgetStack->addWidget(myPointsPane, 0);
137   
138   // Create Cells pane
139   myCellsPane = new QWidget(myWidgetStack);
140   QGridLayout* aCellLayout = new QGridLayout(myCellsPane);
141   aCellLayout->setSpacing(6);
142   aCellLayout->setRowStretch(0, 0);
143   aCellLayout->setRowStretch(1, 1);
144
145   QGroupBox* aCellGrp = new QGroupBox(2, Qt::Horizontal, "Data of Cell", myCellsPane);
146   
147   QLabel* aCellIDLbl = new QLabel("ID:", aCellGrp);
148   myCellIDValLbl = new QLineEdit("", aCellGrp);
149   myCellIDValLbl->setValidator(aIntValidator);
150   connect(myCellIDValLbl, SIGNAL(textChanged(const QString&)), this, SLOT(onCellIdEdit(const QString&)));
151
152   QLabel* aCellValueLbl = new QLabel("Scalar Value:", aCellGrp);
153   myCellScalarValLbl = new QLabel("", aCellGrp);
154   QLabel* aCellVectorLbl = new QLabel("Vector Value:", aCellGrp);  
155   myCellVectorValLbl = new QLabel("", aCellGrp);
156
157   aCellLayout->addWidget(aCellGrp, 0, 0);
158
159   myListPoints = new QTable(myCellsPane);
160   myListPoints->setReadOnly(true);
161   myListPoints->setNumCols(6);
162   myListPoints->setNumRows(0);
163   myListPoints->setColumnWidth(0, 40);
164   myListPoints->setColumnWidth(1, 40);
165   myListPoints->setColumnWidth(2, 40);
166   myListPoints->setColumnWidth(3, 40);
167   myListPoints->setSelectionMode(QTable::NoSelection);
168   QHeader* aHeader = myListPoints->horizontalHeader();
169   aHeader->setLabel( 0, "ID" );
170   aHeader->setLabel( 1, "X" );
171   aHeader->setLabel( 2, "Y" );
172   aHeader->setLabel( 3, "Z" );
173   aHeader->setLabel( 4, "Scalar" );
174   aHeader->setLabel( 5, "Vector" );
175
176   aCellLayout->addWidget(myListPoints, 1, 0);
177
178   myWidgetStack->addWidget(myCellsPane, 1);
179   
180   // Actor Pane
181   myActorsPane = new QVBox(myWidgetStack);
182   myActorsPane->layout()->setSpacing(6);
183
184   QGroupBox* aPosGrp = new QGroupBox(2, Qt::Horizontal, "Position", myActorsPane);
185   aPosGrp->layout()->setSpacing(6);
186   QLabel* aXPosLbl = new QLabel("X:", aPosGrp);
187   myXPosLbl = new QLabel("0", aPosGrp);
188   QLabel* aYPosLbl = new QLabel("Y:", aPosGrp);
189   myYPosLbl = new QLabel("0", aPosGrp);
190   QLabel* aZPosLbl = new QLabel("Z:", aPosGrp);  
191   myZPosLbl = new QLabel("0", aPosGrp);
192
193   QGroupBox* aSizeGrp = new QGroupBox(2, Qt::Horizontal, "Size", myActorsPane);
194   aSizeGrp->layout()->setSpacing(6);
195   QLabel* aXSizeLbl = new QLabel("dX:", aSizeGrp);
196   myDXLbl = new QLabel("0", aSizeGrp);
197   QLabel* aYSizeLbl = new QLabel("dY:", aSizeGrp);
198   myDYLbl = new QLabel("0", aSizeGrp);
199   QLabel* aZSizeLbl = new QLabel("dZ:",aSizeGrp );  
200   myDZLbl = new QLabel("0", aSizeGrp);
201
202   myWidgetStack->addWidget(myActorsPane, 2);
203
204
205   TopLayout->addWidget(myWidgetStack, 2, 0);
206
207   // Create buttons group
208   QHBox* aBtnBox = new QHBox(this);
209   QHBoxLayout* aBtnLayout = (QHBoxLayout*)aBtnBox->layout(); 
210   aBtnLayout->addStretch();
211
212   QPushButton* aCloseBtn = new QPushButton(tr("BUT_CLOSE"), aBtnBox);
213   connect(aCloseBtn, SIGNAL(clicked()), this, SLOT(close()));
214   
215   TopLayout->addWidget(aBtnBox, 3, 0);
216
217   mySelection = SALOME_Selection::Selection(visuGUI->GetActiveStudy()->getSelection());
218   connect(mySelection, SIGNAL(currentSelectionChanged()), this, SLOT(onSelectionEvent()));
219   
220   onSelectionChange(0);
221 }
222
223
224 void VisuGUI_SelectionDlg::onSelectionChange(int theId) {
225   clearFields();
226   switch (theId) {
227   case 0: // Points
228     myWidgetStack->raiseWidget(myPointsPane);    
229     QAD_Application::getDesktop()->SetSelectionMode(1, true);
230     break;
231   case 1: // Cells
232     myWidgetStack->raiseWidget(myCellsPane);
233     QAD_Application::getDesktop()->SetSelectionMode(3, true);
234     break;
235   case 2: // Actor
236     myWidgetStack->raiseWidget(myActorsPane);
237     QAD_Application::getDesktop()->SetSelectionMode(4, true);  
238   }
239   onSelectionEvent();
240 //   if (VTKViewer_ViewFrame* vf = visuGUI->GetVtkViewFrame())
241 //     vf->Repaint();
242 }
243
244
245 void VisuGUI_SelectionDlg::closeEvent(QCloseEvent* theEvent) {  
246   QAD_Application::getDesktop()->SetSelectionMode(4, true);
247   disconnect(mySelection,0,0,0);
248   QDialog::closeEvent(theEvent);
249 }
250
251 template<class TData> QString getValue(TData* theData, int theId){
252   if (vtkDataArray *aScalar = theData->GetScalars()){
253     float aVal = aScalar->GetTuple1(theId);
254     return QString::number(aVal);
255   } else {
256     return QString("No data");      
257   }
258 }
259
260 template<class TData> QString getVector(TData* theData, int theId){
261   if (vtkDataArray *aVector = theData->GetVectors()) {
262     float *aVal = aVector->GetTuple3(theId);
263     return QString("%1; %2; %3").arg(aVal[0]).arg(aVal[1]).arg(aVal[2]); 
264   } else {
265     return QString("No data");
266   }
267 }
268
269
270 #define ABS(a) (a>=0)?a:-a
271
272 void VisuGUI_SelectionDlg::onSelectionEvent() {
273   Handle(SALOME_InteractiveObject) anIO;
274   int aType = myWidgetStack->id(myWidgetStack->visibleWidget());
275   if(VISU::Prs3d_i* aPrs3d = visuGUI->GetSelectedPrs3d(&anIO)){
276     SALOMEDS::SObject_var aSObject = visuGUI->GetStudyDocument()->FindObjectID(anIO->getEntry());
277     QString aMeshName = VisuGUI::getValue(aSObject, "myMeshName");
278     QString aFieldName = VisuGUI::getValue(aSObject, "myFieldName");
279     myMeshName->setText((aMeshName == "NULL")? QString("No name") : aMeshName);
280     myFieldName->setText((aFieldName == "NULL")? QString("No name") : aFieldName);
281     
282     VISU_Actor* anVISUActor = visuGUI->GetActor(aPrs3d);
283     if (anVISUActor) {
284       float aCoord[6];
285       anVISUActor->GetBounds(aCoord);
286       myXPosLbl->setText(QString::number( aCoord[0] ));
287       myYPosLbl->setText(QString::number( aCoord[2] ));
288       myZPosLbl->setText(QString::number( aCoord[4] ));
289       
290       myDXLbl->setText(QString::number( ABS(aCoord[1]-aCoord[0]) ));
291       myDYLbl->setText(QString::number( ABS(aCoord[3]-aCoord[2]) ));
292       myDZLbl->setText(QString::number( ABS(aCoord[5]-aCoord[4]) ));
293     }
294     TColStd_MapOfInteger aMapIndex;
295     mySelection->GetIndex(anIO, aMapIndex);
296     TColStd_MapIteratorOfMapOfInteger anIter(aMapIndex);
297     if(anIter.More()){
298       VISU_Actor* anActor = visuGUI->GetActor(aPrs3d);  
299       vtkDataSet* aDataSet = anActor->GetMapper()->GetInput();
300       //      vtkDataSet* aDataSet = aPrs3d->GetPL()->GetMapper()->GetInput();
301       int aID = anIter.Key();
302       vtkPointData* aPntData = aDataSet->GetPointData();
303       
304       switch (aType) {
305       case 0:
306         {
307           float aCoord[3];
308           aDataSet->GetPoint(aID, aCoord);
309           myXValLbl->setText( QString::number( aCoord[0] ) );
310           myYValLbl->setText( QString::number( aCoord[1] ) );
311           myZValLbl->setText( QString::number( aCoord[2] ) );
312           myIDValLbl->setText( QString::number(aID) );
313           myScalarValLbl->setText(getValue(aPntData, aID));
314           myVectorValLbl->setText(getVector(aPntData, aID));
315           //      adjustSize();
316           //qApp->processEvents();
317         }
318         break;
319       case 1:
320         {
321           int aMaxId = aDataSet->GetNumberOfCells();
322           //cout<<"### MaxId="<<aMaxId<<endl;
323           //cout<<"### SelectedId="<<aID<<endl;
324           vtkCellData* aData = aDataSet->GetCellData();
325           vtkCell* aCell = aDataSet->GetCell(aID);
326           if (aCell == NULL) return;
327
328           int aNbOfPoints = aCell->GetNumberOfPoints();
329           if (aNbOfPoints <=1 ) { // Cell is point
330             clearFields();
331             return;
332           }
333           
334           myCellIDValLbl->setText( QString::number(aID) );
335           myCellScalarValLbl->setText(getValue(aData, aID));
336           myCellVectorValLbl->setText(getVector(aData, aID));
337           
338           float aCoord[3];
339           vtkIdList *aPointList = aCell->GetPointIds();
340           QListViewItem* anItem = NULL;
341           myListPoints->setNumRows(aNbOfPoints);
342           for(int i = 0; i < aNbOfPoints; i++){
343             int idCurrent = aPointList->GetId(i);
344             aDataSet->GetPoint(idCurrent,aCoord);
345             myListPoints->verticalHeader()->setLabel(i, QString::number( i ));
346             myListPoints->setText(i, 0, QString::number( idCurrent ));
347             myListPoints->setText(i, 1, QString::number( aCoord[0] ));
348             myListPoints->setText(i, 2, QString::number( aCoord[1] ));
349             myListPoints->setText(i, 3, QString::number( aCoord[2] ));
350             myListPoints->setText(i, 4, getValue(aPntData, idCurrent));
351             myListPoints->setText(i, 5, getVector(aPntData, idCurrent));
352           }
353         }
354         break;
355       }
356     }
357   } else {
358     clearFields();
359   }
360 }
361 #undef ABS
362
363
364 void VisuGUI_SelectionDlg::clearFields() {
365   int aType = myWidgetStack->id(myWidgetStack->visibleWidget());
366   switch (aType) {
367   case 0:
368     myXValLbl->setText( "" );
369     myYValLbl->setText( "" );
370     myZValLbl->setText( "" );
371     myIDValLbl->setText( "" );
372     myScalarValLbl->setText("");
373     myVectorValLbl->setText("");
374     break;
375   case 1:
376     myCellIDValLbl->setText( "" );
377     myCellScalarValLbl->setText("");
378     myCellVectorValLbl->setText("");
379     myListPoints->setNumRows(0);
380     break;
381   case 2:
382     myXPosLbl->setText("");
383     myYPosLbl->setText("");
384     myZPosLbl->setText("");      
385     myDXLbl->setText("");
386     myDYLbl->setText("");
387     myDZLbl->setText("");
388   }
389 }
390
391
392 #define SELECTBYNUM(METHODNAME, DATASETMETHOD) \
393 void VisuGUI_SelectionDlg::METHODNAME(const QString& theText) { \
394   int anId = theText.toInt(); \
395   Handle(SALOME_InteractiveObject) anIO; \
396   if(VISU::Prs3d_i* aPrs3d = visuGUI->GetSelectedPrs3d(&anIO)){ \
397     TColStd_MapOfInteger aMapIndex; \
398     mySelection->GetIndex(anIO, aMapIndex); \
399     TColStd_MapIteratorOfMapOfInteger anIter(aMapIndex); \
400     if(anIter.More()){ \
401       vtkDataSet* aDataSet = aPrs3d->GetPL()->GetMapper()->GetInput(); \
402       int aMaxId = aDataSet->DATASETMETHOD(); \
403       if(0 <= anId && anId < aMaxId){ \
404         mySelection->AddIObject(anIO); \
405         mySelection->AddOrRemoveIndex(anIO, anId, false); \
406       } \
407     } \
408   } else { \
409     myMeshName->setText(tr ("WRN_NO_AVAILABLE_DATA")); \
410     myFieldName->setText(""); \
411   } \
412 }
413
414 SELECTBYNUM(onPointIdEdit, GetNumberOfPoints);
415 SELECTBYNUM(onCellIdEdit, GetNumberOfCells);
416
417 #undef SELECTBYNUM
418