Salome HOME
Implementation '22873: EDF 8651 GEOM: Annotate shapes into the OCC viewer' issue
[modules/geom.git] / src / MeasureGUI / MeasureGUI.cxx
1 // Copyright (C) 2007-2016  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   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
199   if ( !anActiveStudy )
200     return;
201
202   Handle(SALOME_InteractiveObject) anIObject = getSingleSelectedIO();
203   if ( anIObject.IsNull()
204    || !anIObject->hasEntry() )
205     return;
206
207   SUIT_OverrideCursor wc;
208
209   GEOMGUI_DimensionProperty aDimensions( anActiveStudy, anIObject->getEntry() );
210
211   for ( int anIt = 0; anIt < aDimensions.GetNumber(); ++anIt ) {
212     aDimensions.SetVisible( anIt, theIsVisible );
213   }
214
215   aDimensions.SaveToAttribute( anActiveStudy, anIObject->getEntry() );
216
217   GEOM_Displayer( anActiveStudy ).Redisplay( anIObject, true );
218 }
219
220 //=======================================================================
221 // function : ChangeAnnotationsVisibility
222 // purpose  : 
223 //=======================================================================
224 void MeasureGUI::ChangeAnnotationsVisibility( const bool theIsVisible )
225 {
226   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
227   if ( !anApp )
228     return;
229
230   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
231   if ( !anActiveStudy )
232     return;
233
234   QString anEntry;
235   Handle(SALOME_InteractiveObject) anIObject = getSingleSelectedIO();
236   if ( !anIObject.IsNull() && anIObject->hasEntry() ) {
237     anEntry = anIObject->getEntry();
238   }
239   if ( anEntry.isEmpty() ) {
240     anEntry = getGeometryGUI()->GetTextTreeWdg()->getSingleSelectedObject();
241   }
242
243   if ( !anEntry.isEmpty() ) {
244     _PTR(SObject) aSObj = anActiveStudy->studyDS()->FindObjectID( anEntry.toStdString() );
245
246     const Handle(GEOMGUI_AnnotationAttrs)
247       aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
248
249     if ( aShapeAnnotations.IsNull() ) {
250       return;
251     }
252
253     const int aCount = aShapeAnnotations->GetNbAnnotation();
254
255     if ( aCount > 0 ) {
256
257       SUIT_OverrideCursor wc;
258
259       for ( int anI = 0; anI < aCount; ++anI ) {
260
261         if ( !theIsVisible ) {
262           getGeometryGUI()->GetAnnotationMgr()->Erase( anEntry, anI );
263         }
264         else {
265           getGeometryGUI()->GetAnnotationMgr()->Display( anEntry , anI );
266         }
267       }
268
269       getGeometryGUI()->emitAnnotationsUpdated( anEntry );
270     }
271   }
272   else {
273     if ( theIsVisible ) {
274       getGeometryGUI()->GetAnnotationMgr()->DisplayAllAnnotations();
275     }
276     else {
277       getGeometryGUI()->GetAnnotationMgr()->EraseAllAnnotations();
278     }
279     getGeometryGUI()->GetTextTreeWdg()->updateVisibility();
280   }
281 }
282
283 //=======================================================================
284 // function : DeleteAnnotation
285 // purpose  : 
286 //=======================================================================
287 void MeasureGUI::DeleteAnnotation()
288 {
289   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
290   if ( !anApp )
291     return;
292
293   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
294   if ( !anActiveStudy )
295     return;
296
297   Handle(SALOME_InteractiveObject) anIObject = getSingleSelectedIO();
298   if ( anIObject.IsNull()
299    || !anIObject->hasEntry() )
300     return;
301
302   const QString aEntry = anIObject->getEntry();
303
304   QString aObjEntry;
305
306   int aIndex = 0;
307
308   if ( getGeometryGUI()->GetAnnotationMgr()->getIndexFromEntry( aEntry, aObjEntry, aIndex ) )
309   {
310     _PTR(SObject) aSObj = anActiveStudy->studyDS()->FindObjectID( aObjEntry.toStdString() );
311
312     const Handle(GEOMGUI_AnnotationAttrs)
313       aShapeAnnotations = GEOMGUI_AnnotationAttrs::FindAttributes( aSObj );
314
315     if ( aShapeAnnotations.IsNull() ) {
316       return;
317     }
318
319     aShapeAnnotations->Remove( aIndex );
320
321     getGeometryGUI()->GetAnnotationMgr()->EraseRemovedAnnotation( aObjEntry, aIndex );
322
323     getGeometryGUI()->emitAnnotationsUpdated( aObjEntry );
324   }
325 }
326
327 //=======================================================================
328 // function : getSingleSelectedIO
329 // purpose  : returns selected interactive object for single selection.
330 //=======================================================================
331 Handle(SALOME_InteractiveObject) MeasureGUI::getSingleSelectedIO()
332 {
333   SalomeApp_Application* anApp = getGeometryGUI()->getApp();
334   if ( !anApp ) {
335     return Handle(SALOME_InteractiveObject)();
336   }
337
338   LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr();
339   if ( !aSelMgr ) {
340     return Handle(SALOME_InteractiveObject)();
341   }
342
343   SALOME_ListIO aListIO;
344   aSelMgr->selectedObjects( aListIO );
345   if ( aListIO.Extent() != 1 ) {
346     return Handle(SALOME_InteractiveObject)();
347   }
348
349   return aListIO.First();
350 }
351
352 //=======================================================================
353 // function : onFinished
354 // purpose  : called then "Manage Dimension" dialog is closed.
355 //=======================================================================
356 void MeasureGUI::onFinished(int /*theResult*/) {
357   if(sender() == myManageDimensionDlg) {
358     myManageDimensionDlg = 0;
359   }
360 }
361
362 //=====================================================================================
363 // EXPORTED METHODS
364 //=====================================================================================
365 extern "C"
366 {
367 #ifdef WIN32
368   __declspec( dllexport )
369 #endif
370   GEOMGUI* GetLibGUI( GeometryGUI* parent )
371   {
372     return new MeasureGUI( parent );
373   }
374 }