Salome HOME
NRI : Merge from V1_2.
[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
36 #include <TColStd_MapOfInteger.hxx>
37 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
38
39 #include <qstring.h>
40 #include <qlabel.h>
41 #include <qspinbox.h>
42 #include <qlistbox.h>
43
44 #include <vtkDataSet.h>
45 #include <vtkCell.h>
46
47 #include "VisuGUI_CellsResultsDlg.h"
48 #include "VisuGUI_PointsResultsDlg.h"
49 #include "VisuGUI_Selection.h"
50
51 int PickViewer(QAD_Study *theActiveStudy, int theSelectionMode){
52   SALOME_Selection *aSel = SALOME_Selection::Selection(theActiveStudy->getSelection());
53   SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
54   for(;It.More(); It.Next()){
55     Handle(SALOME_InteractiveObject) IObject = It.Value();
56     if (aSel->SelectionMode() == theSelectionMode ){
57       if (aSel->HasIndex(IObject)) {
58         TColStd_MapOfInteger MapIndex ;
59         aSel->GetIndex(IObject, MapIndex) ;
60         TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
61         for(;ite.More(); ite.Next())
62           return ite.Key();
63       }
64     }
65   }
66   return -1;
67 }
68
69 template<class TData, class TDialog> void SetMessage(TData* theData, int theId, TDialog* theDialog){
70   // Scalar result
71   QString Msg_1;
72   if (vtkDataArray *Scalar = theData->GetScalars()){
73     float aVal = Scalar->GetTuple1(theId);
74     Msg_1.sprintf("%6.3g",aVal);
75   }else
76     Msg_1.sprintf("NO DATA");
77   theDialog->ScalValue->setText( Msg_1.latin1() );
78   
79   // Vector result
80   QString Msg_2;
81   if (vtkDataArray *Vector = theData->GetVectors()){
82     float *aVal = Vector->GetTuple3(theId);
83     Msg_2.sprintf("%6.3g; %6.3g; %6.3g",aVal[0],aVal[1],aVal[2]);
84   }else
85     Msg_2.sprintf("NO DATA");
86   theDialog->VectTxt->setText( Msg_2.latin1() );
87 }
88
89 // ============================================================================================ //
90 //                                      PICKING CELL                                            //
91 // ============================================================================================ //
92
93 void VisuGUI_Selection::PickingCell(QAD_Study *theActiveStudy, vtkDataSet *theDataSet){
94   int id = PickViewer(theActiveStudy,SelectionCell);
95   if(id >= 0){
96     VisuGUI_CellsResultsDlg *aResultsDlg = new VisuGUI_CellsResultsDlg(QAD_Application::getDesktop(), 0, TRUE, 0); 
97     vtkCellData *aData = theDataSet->GetCellData();
98     vtkCell *aCell = theDataSet->GetCell(id);
99
100     float aPntCoord[12][3];
101     int aNbOfPoints = aCell->GetNumberOfPoints();
102     vtkIdList *aPointList = aCell->GetPointIds();
103     QListViewItem* anItem = NULL;
104     for(int i = 0; i < aNbOfPoints; i++){
105       float aCoord[3];
106       int idCurrent = aPointList->GetId(i);
107       theDataSet->GetPoint(idCurrent,aCoord);
108       for (int j=0; j<3; j++)
109         aPntCoord[i][j]=aCoord[j];
110       anItem = new QListViewItem(aResultsDlg->ListPoints, 
111                                  anItem, 
112                                  QString::number( idCurrent ),
113                                  QString::number( aPntCoord[i][0] ),
114                                  QString::number( aPntCoord[i][1] ),
115                                  QString::number( aPntCoord[i][2] ));
116     }
117
118     SetMessage(aData,id,aResultsDlg);
119
120     aResultsDlg->IdCell->setText( QString::number( id ) );
121     aResultsDlg->NbPoint->setText( QString::number( aNbOfPoints ) );
122     aResultsDlg->exec(); 
123     delete aResultsDlg;
124   }
125 }
126
127
128 // ============================================================================================ //
129 //                                      PICKING POINT                                           //
130 // ============================================================================================ //
131
132 void VisuGUI_Selection::PickingPoint(QAD_Study *theActiveStudy, vtkDataSet *theDataSet){
133   int id = PickViewer(theActiveStudy,SelectionPoint);
134   if(id >= 0){
135     VisuGUI_PointsResultsDlg *aResultsDlg = new VisuGUI_PointsResultsDlg(QAD_Application::getDesktop(), 0, TRUE, 0); 
136     vtkPointData *aData = theDataSet->GetPointData();
137
138     float Coord[3];
139     theDataSet->GetPoint(id,Coord);
140     aResultsDlg->CoordX->setText( QString::number( Coord[0] ) );
141     aResultsDlg->CoordY->setText( QString::number( Coord[1] ) );
142     aResultsDlg->CoordZ->setText( QString::number( Coord[2] ) );
143     aResultsDlg->IdPoint->setText( QString::number(id) );
144     
145     SetMessage(aData,id,aResultsDlg);
146
147     aResultsDlg->exec(); 
148     delete aResultsDlg;
149   }
150 }
151
152 void VisuGUI_Selection::Highlight(QAD_Study *theActiveStudy, const Handle(SALOME_InteractiveObject)& theIO, int theId){
153   MESSAGE("HighlightCell");
154   SALOME_Selection *aSel = SALOME_Selection::Selection(theActiveStudy->getSelection());
155   aSel->AddIObject( theIO, false );
156   aSel->AddOrRemoveIndex( theIO, theId, false, true );
157 }