Salome HOME
d175c1a99965b6b91b11fee4953f7a2d13130caf
[modules/geom.git] / src / MeasureGUI / MeasureGUI.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : MeasureGUI.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26
27 #include "MeasureGUI.h"
28
29 #include <GeometryGUI.h>
30 #include "GeometryGUI_Operations.h"
31 #include <GEOMGUI_TextTreeWdg.h>
32
33 #include <GEOMGUI_DimensionProperty.h>
34 #include <GEOMGUI_AnnotationAttrs.h>
35 #include <GEOMGUI_AnnotationMgr.h>
36
37 #include <LightApp_SelectionMgr.h>
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_Desktop.h>
40 #include <SalomeApp_Application.h>
41 #include <SalomeApp_Study.h>
42
43 #include "MeasureGUI_PropertiesDlg.h"             // Method PROPERTIES
44 #include "MeasureGUI_CenterMassDlg.h"             // Method CENTER MASS
45 #include "MeasureGUI_NormaleDlg.h"                // Method NORMALE
46 #include "MeasureGUI_InertiaDlg.h"                // Method INERTIA
47 #include "MeasureGUI_BndBoxDlg.h"                 // Method BNDBOX
48 #include "MeasureGUI_DistanceDlg.h"               // Method DISTANCE
49 #include "MeasureGUI_AngleDlg.h"                  // Method ANGLE
50 #include "MeasureGUI_AnnotationDlg.h"             // Method ANNOTATION
51 #include "MeasureGUI_MaxToleranceDlg.h"           // Method MAXTOLERANCE
52 #include "MeasureGUI_WhatisDlg.h"                 // Method WHATIS
53 #include "MeasureGUI_CheckShapeDlg.h"             // Method CHECKSHAPE
54 #include "MeasureGUI_CheckCompoundOfBlocksDlg.h"  // Method CHECKCOMPOUND
55 #include "MeasureGUI_GetNonBlocksDlg.h"           // Method GET NON BLOCKS
56 #include "MeasureGUI_CheckSelfIntersectionsDlg.h" // Method CHECK SELF INTERSCTIONS
57 #include "MeasureGUI_FastCheckIntersectionsDlg.h" // Method FAST CHECK INTERSCTIONS
58 #include "MeasureGUI_PointDlg.h"                  // Method POINTCOORDINATES
59 #include "MeasureGUI_ManageDimensionsDlg.h"       // Method MANAGEDIMENSIONS
60 #ifndef DISABLE_PLOT2DVIEWER
61 #include "MeasureGUI_ShapeStatisticsDlg.h"        // Method SHAPE STATISTICS
62 #endif
63
64 #include <QApplication>
65
66 //=======================================================================
67 // function : MeasureGUI()
68 // purpose  : Constructor
69 //=======================================================================
70 MeasureGUI::MeasureGUI( GeometryGUI* parent ) : GEOMGUI( parent )
71 {
72   myManageDimensionDlg = 0;
73 }
74
75 //=======================================================================
76 // function : ~MeasureGUI()
77 // purpose  : Destructor
78 //=======================================================================
79 MeasureGUI::~MeasureGUI()
80 {
81 }
82
83
84 //=======================================================================
85 // function : OnGUIEvent()
86 // purpose  : 
87 //=======================================================================
88 bool MeasureGUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
89 {
90   SalomeApp_Application* app = getGeometryGUI()->getApp();
91   if ( !app ) return false;
92
93   getGeometryGUI()->EmitSignalDeactivateDialog();
94
95   QDialog* dlg = 0;
96   switch ( theCommandID ) {
97   case GEOMOp::OpProperties:
98     dlg = new MeasureGUI_PropertiesDlg( getGeometryGUI(), parent );
99     break; // LENGTH, AREA AND VOLUME
100   case GEOMOp::OpCenterMass:
101     dlg = new MeasureGUI_CenterMassDlg( getGeometryGUI(), parent );
102     break; // CENTER MASS7
103   case GEOMOp::OpInertia:
104     dlg = new MeasureGUI_InertiaDlg( getGeometryGUI(), parent );
105     break; // INERTIA
106   case GEOMOp::OpNormale : 
107     dlg = new MeasureGUI_NormaleDlg( getGeometryGUI(), parent );
108     break; // NORMALE
109   case GEOMOp::OpBoundingBox:
110     dlg = new MeasureGUI_BndBoxDlg( getGeometryGUI(), parent );
111     break; // BOUNDING BOX
112   case GEOMOp::OpMinDistance:
113     dlg = new MeasureGUI_DistanceDlg( getGeometryGUI(), parent );
114     break; // MIN DISTANCE
115   case GEOMOp::OpAngle: 
116     dlg = new MeasureGUI_AngleDlg( getGeometryGUI(), parent );
117     break; // ANGLE
118   case GEOMOp::OpAnnotation:
119     dlg = new MeasureGUI_AnnotationDlg( getGeometryGUI(), true, parent );
120     break; // ANNOTATION
121   case GEOMOp::OpEditAnnotation:
122     dlg = new MeasureGUI_AnnotationDlg( getGeometryGUI(), false, parent );
123     break; // ANNOTATION
124   case GEOMOp::OpTolerance: 
125     dlg = new MeasureGUI_MaxToleranceDlg( getGeometryGUI(), parent );
126     break; // MAXTOLERANCE
127   case GEOMOp::OpWhatIs:
128     dlg = new MeasureGUI_WhatisDlg( getGeometryGUI(), parent );
129     break; // WHATIS
130   case GEOMOp::OpCheckShape:
131     dlg = new MeasureGUI_CheckShapeDlg( getGeometryGUI(), parent );
132     break; // CHECKSHAPE
133   case GEOMOp::OpCheckCompound:
134     dlg = new MeasureGUI_CheckCompoundOfBlocksDlg( getGeometryGUI(), parent );
135     break; // CHECKCOMPOUND
136   case GEOMOp::OpGetNonBlocks:
137     dlg = new MeasureGUI_GetNonBlocksDlg(getGeometryGUI(), parent);
138     break; // GET NON BLOCKS
139   case GEOMOp::OpCheckSelfInters:
140     dlg = new MeasureGUI_CheckSelfIntersectionsDlg( getGeometryGUI(), parent );
141     break; // CHECK SELF INTERSCTIONS
142   case GEOMOp::OpFastCheckInters:
143     dlg = new MeasureGUI_FastCheckIntersectionsDlg( getGeometryGUI(), parent );
144     break; // FAST CHECK INTERSCTIONS
145 #ifndef DISABLE_PLOT2DVIEWER
146   case GEOMOp::OpShapeStatistics:
147     dlg = new MeasureGUI_ShapeStatisticsDlg( parent );
148     break; // SHAPE STATISTICS
149 #endif
150   case GEOMOp::OpPointCoordinates:
151     dlg = new MeasureGUI_PointDlg( getGeometryGUI(), parent );
152     break; // POINT COORDINATES
153   case GEOMOp::OpManageDimensions:
154     if( !myManageDimensionDlg ) {
155       dlg = new MeasureGUI_ManageDimensionsDlg( getGeometryGUI(), parent );
156       myManageDimensionDlg = dlg;
157       connect( dlg, SIGNAL( finished(int) ), this, SLOT( onFinished(int) ) );
158     }
159     myManageDimensionDlg->activateWindow();
160     break; // MANAGE DIMENSIONS
161   case GEOMOp::OpShowAllDimensions:
162     ChangeDimensionsVisibility( true );
163     break; // SHOW ALL DIMENSIONS
164   case GEOMOp::OpHideAllDimensions:
165     ChangeDimensionsVisibility( false );
166     break; // HIDE ALL DIMENSIONS
167   case GEOMOp::OpShowAllAnnotations:
168     ChangeAnnotationsVisibility( true );
169     break; // SHOW ALL ANNOTATIONS
170   case GEOMOp::OpHideAllAnnotations:
171     ChangeAnnotationsVisibility( false );
172     break; // HIDE ALL ANNOTATIONS
173   case GEOMOp::OpDeleteAnnotation:
174     DeleteAnnotation();
175     break; // DELETE ANNOTATIOn
176   default: 
177     app->putInfo( tr( "GEOM_PRP_COMMAND" ).arg( theCommandID ) ); 
178     break;
179   }
180   if ( dlg ) {
181     dlg->updateGeometry();
182     dlg->resize( dlg->minimumSizeHint() );
183     dlg->show();
184   }
185   return true;
186 }
187
188 //=======================================================================
189 // function : ChangeDimensionsVisibility
190 // purpose  : 
191 //=======================================================================
192 void MeasureGUI::ChangeDimensionsVisibility( const bool theIsVisible )
193 {
194   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
195   if ( !anApp )
196     return;
197
198
199   Handle(SALOME_InteractiveObject) anIObject = getSingleSelectedIO();
200   if ( anIObject.IsNull()
201    || !anIObject->hasEntry() )
202     return;
203
204   SUIT_OverrideCursor wc;
205
206   GEOMGUI_DimensionProperty aDimensions( QString(anIObject->getEntry()) );
207
208   for ( int anIt = 0; anIt < aDimensions.GetNumber(); ++anIt ) {
209     aDimensions.SetVisible( anIt, theIsVisible );
210   }
211
212   aDimensions.SaveToAttribute( anIObject->getEntry() );
213
214   GEOM_Displayer().Redisplay( anIObject, true );
215 }
216
217 //=======================================================================
218 // function : ChangeAnnotationsVisibility
219 // purpose  : 
220 //=======================================================================
221 void MeasureGUI::ChangeAnnotationsVisibility( const bool theIsVisible )
222 {
223   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
224   if ( !anApp )
225     return;
226
227   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
228   if ( !anActiveStudy )
229     return;
230
231   QString anEntry;
232   Handle(SALOME_InteractiveObject) anIObject = getSingleSelectedIO();
233   if ( !anIObject.IsNull() && anIObject->hasEntry() ) {
234     anEntry = anIObject->getEntry();
235   }
236   if ( anEntry.isEmpty() ) {
237     anEntry = getGeometryGUI()->GetTextTreeWdg()->getSingleSelectedObject();
238   }
239
240   if ( !anEntry.isEmpty() ) {
241     _PTR(SObject) aSObj = anActiveStudy->studyDS()->FindObjectID( anEntry.toStdString() );
242
243     const Handle(GEOMGUI_AnnotationAttrs)
244       aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
245
246     if ( aShapeAnnotations.IsNull() ) {
247       return;
248     }
249
250     const int aCount = aShapeAnnotations->GetNbAnnotation();
251
252     if ( aCount > 0 ) {
253
254       SUIT_OverrideCursor wc;
255
256       for ( int anI = 0; anI < aCount; ++anI ) {
257
258         if ( !theIsVisible ) {
259           getGeometryGUI()->GetAnnotationMgr()->Erase( anEntry, anI );
260         }
261         else {
262           getGeometryGUI()->GetAnnotationMgr()->Display( anEntry , anI );
263         }
264       }
265
266       getGeometryGUI()->emitAnnotationsUpdated( anEntry );
267     }
268   }
269   else {
270     if ( theIsVisible ) {
271       getGeometryGUI()->GetAnnotationMgr()->DisplayAllAnnotations();
272     }
273     else {
274       getGeometryGUI()->GetAnnotationMgr()->EraseAllAnnotations();
275     }
276     getGeometryGUI()->GetTextTreeWdg()->updateVisibility();
277   }
278 }
279
280 //=======================================================================
281 // function : DeleteAnnotation
282 // purpose  : 
283 //=======================================================================
284 void MeasureGUI::DeleteAnnotation()
285 {
286   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
287   if ( !anApp )
288     return;
289
290   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
291   if ( !anActiveStudy )
292     return;
293
294   Handle(SALOME_InteractiveObject) anIObject = getSingleSelectedIO();
295   if ( anIObject.IsNull()
296    || !anIObject->hasEntry() )
297     return;
298
299   const QString aEntry = anIObject->getEntry();
300
301   QString aObjEntry;
302
303   int aIndex = 0;
304
305   if ( getGeometryGUI()->GetAnnotationMgr()->getIndexFromEntry( aEntry, aObjEntry, aIndex ) )
306   {
307     _PTR(SObject) aSObj = anActiveStudy->studyDS()->FindObjectID( aObjEntry.toStdString() );
308
309     const Handle(GEOMGUI_AnnotationAttrs)
310       aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
311
312     if ( aShapeAnnotations.IsNull() ) {
313       return;
314     }
315
316     aShapeAnnotations->Remove( aIndex );
317
318     getGeometryGUI()->GetAnnotationMgr()->EraseRemovedAnnotation( aObjEntry, aIndex );
319
320     getGeometryGUI()->emitAnnotationsUpdated( aObjEntry );
321   }
322 }
323
324 //=======================================================================
325 // function : getSingleSelectedIO
326 // purpose  : returns selected interactive object for single selection.
327 //=======================================================================
328 Handle(SALOME_InteractiveObject) MeasureGUI::getSingleSelectedIO()
329 {
330   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
331   if ( !anApp ) {
332     return Handle(SALOME_InteractiveObject)();
333   }
334
335   LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
336   if ( !aSelMgr ) {
337     return Handle(SALOME_InteractiveObject)();
338   }
339
340   SALOME_ListIO aListIO;
341   aSelMgr->selectedObjects( aListIO );
342   if ( aListIO.Extent() != 1 ) {
343     return Handle(SALOME_InteractiveObject)();
344   }
345
346   return aListIO.First();
347 }
348
349 //=======================================================================
350 // function : onFinished
351 // purpose  : called then "Manage Dimension" dialog is closed.
352 //=======================================================================
353 void MeasureGUI::onFinished(int /*theResult*/) {
354   if(sender() == myManageDimensionDlg) {
355     myManageDimensionDlg = 0;
356   }
357 }
358
359 //=====================================================================================
360 // EXPORTED METHODS
361 //=====================================================================================
362 extern "C"
363 {
364 #ifdef WIN32
365   __declspec( dllexport )
366 #endif
367   GEOMGUI* GetLibGUI( GeometryGUI* parent )
368   {
369     return new MeasureGUI( parent );
370   }
371 }