Salome HOME
fd961ec3d91328879476452a65a49ab74078edf0
[modules/geom.git] / src / GEOMGUI / GeometryGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry 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   : GeometryGUI.cxx
25 //  Author : Lucien PIGNOLONI
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GeometryGUI.h"
30
31 // Open CASCADE Includes
32 #include <Prs3d_Drawer.hxx>
33 #include <Prs3d_IsoAspect.hxx>
34
35 // SALOME Includes
36 #include "QAD_RightFrame.h"
37 #include "QAD_Config.h"
38
39 #include "OCCViewer_Viewer3d.h"
40 #include "OCCViewer_ViewPort3d.h"
41 #include "VTKViewer_ViewFrame.h"
42 #include "VTKViewer_InteractorStyleSALOME.h"
43
44 #include "SALOME_Selection.h"
45
46 using namespace std;
47
48 /* The object itself created in the static method 'GetOrCreateGEOMBase()' */
49 static GEOMContext* GeomGUI = 0;
50
51 typedef bool OGE(int, QAD_Desktop*);
52 typedef bool OMP(QMouseEvent*, QAD_Desktop*, QAD_StudyFrame*);
53 typedef void BP(const Handle(SALOME_InteractiveObject)&);
54 typedef bool CP(QAD_Desktop*, QPopupMenu*, const QString&,
55                 const QString&, const QString&);
56
57 //=======================================================================
58 // function : GeometryGUI()
59 // purpose  : Constructor
60 //=======================================================================
61 GeometryGUI::GeometryGUI( const QString& theName, QObject* theParent ) :
62   SALOMEGUI( theName, theParent )
63 {}
64
65
66 //=======================================================================
67 // function : ~GeometryGUI()
68 // purpose  : Destructor
69 //=======================================================================
70 GeometryGUI::~GeometryGUI()
71 {
72 }
73
74
75 //=======================================================================
76 // function : GetOrCreateGEOMBase()
77 // purpose  : Gets or create an object 'GEOMBase' with initialisations
78 //          : Returns 'GeomGUI' as a pointer
79 //=======================================================================
80 GEOMContext* GeometryGUI::GetOrCreateGeometryGUI(QAD_Desktop* desktop)
81 {
82   GeomGUI = GeomGUI->GetOrCreateGeomGUI(desktop);
83   return GeomGUI;
84 }
85
86
87 //=======================================================================
88 // function : OnGUIEvent() [static]
89 // purpose  : manage all events on GUI
90 //=======================================================================
91 bool GeometryGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
92 {
93   GeometryGUI::GetOrCreateGeometryGUI(parent);
94
95   QMenuBar* Mb = QAD_Application::getDesktop()->getMainMenuBar();
96   bool ViewOCC;
97
98   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC)
99     ViewOCC = true;    
100   else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK)
101     ViewOCC = false;
102   else
103     return false;
104
105   Mb->setItemEnabled(404, ViewOCC);//SKETCHER
106
107   Mb->setItemEnabled(603, ViewOCC);//SuppressFace
108   Mb->setItemEnabled(604, ViewOCC);//SuppressHole
109   
110   Mb->setItemEnabled(413, ViewOCC);// ShadingColor Settings
111   Mb->setItemEnabled(414, ViewOCC);// Isos Settings
112
113   if(theCommandID == 111 ||  // MENU FILE - IMPORT BREP
114      theCommandID == 112 ||  // MENU FILE - IMPORT IGES
115      theCommandID == 113 ||  // MENU FILE - IMPORT STEP
116      theCommandID == 121 ||  // MENU FILE - EXPORT BREP
117      theCommandID == 122 ||  // MENU FILE - EXPORT IGES
118      theCommandID == 123 ||  // MENU FILE - EXPORT STEP
119      theCommandID == 31 ||   // MENU EDIT - COPY
120      theCommandID == 33 ||   // MENU EDIT - DELETE
121      theCommandID == 411 ||  // MENU SETTINGS - ADD IN STUDY
122      theCommandID == 412 ||  // MENU SETTINGS - SHADING COLOR
123      theCommandID == 413 ||  // MENU SETTINGS - ISOS
124      theCommandID == 414 ||  // MENU SETTINGS - STEP VALUE FOR SPIN BOXES
125      theCommandID == 5103 || // MENU TOOLS - CHECK GEOMETRY
126      theCommandID == 5104 || // MENU TOOLS - LOAD SCRIPT
127      theCommandID == 8032 || // POPUP VIEWER - COLOR
128      theCommandID == 8033 || // POPUP VIEWER - TRANSPARENCY
129      theCommandID == 8034 || // POPUP VIEWER - ISOS
130      theCommandID == 804 ||  // POPUP VIEWER - ADD IN STUDY
131      theCommandID == 901 ||  // OBJECT BROWSER - RENAME
132      theCommandID == 9024) { // OBJECT BROWSER - OPEN
133     if(!GeomGUI->LoadLibrary("libGEOMToolsGUI.so")) 
134       return false;
135   }
136   else if(theCommandID == 211 ||  // MENU VIEW - WIREFRAME/SHADING
137           theCommandID == 212 ||  // MENU VIEW - DISPLAY ALL
138           theCommandID == 213 ||  // MENU VIEW - DISPLAY ONLY
139           theCommandID == 214 ||  // MENU VIEW - ERASE ALL
140           theCommandID == 215 ||  // MENU VIEW - ERASE ONLY
141           theCommandID == 8031) { // POPUP VIEWER - WIREFRAME/SHADING
142     if(!GeomGUI->LoadLibrary("libDisplayGUI.so")) 
143       return false;
144   }
145   else if(theCommandID == 4011 || // MENU BASIC - POINT
146           theCommandID == 4012 || // MENU BASIC - LINE
147           theCommandID == 4013 || // MENU BASIC - CIRCLE
148           theCommandID == 4014 || // MENU BASIC - ELLIPSE
149           theCommandID == 4015 || // MENU BASIC - ARC
150           theCommandID == 4016 || // MENU BASIC - VECTOR
151           theCommandID == 4017 || // MENU BASIC - PLANE
152           theCommandID == 4018) { // MENU BASIC - WPLANE
153     if(!GeomGUI->LoadLibrary("libBasicGUI.so")) 
154       return false;
155   }
156   else if(theCommandID == 4021 || // MENU PRIMITIVE - BOX
157           theCommandID == 4022 || // MENU PRIMITIVE - CYLINDER
158           theCommandID == 4023 || // MENU PRIMITIVE - SPHERE
159           theCommandID == 4024 || // MENU PRIMITIVE - TORUS
160           theCommandID == 4025) { // MENU PRIMITIVE - CONE
161     if(!GeomGUI->LoadLibrary("libPrimitiveGUI.so"))
162       return false;
163   }
164   else if(theCommandID == 4031 || // MENU GENERATION - PRISM
165           theCommandID == 4032 || // MENU GENERATION - REVOLUTION
166           theCommandID == 4033 || // MENU GENERATION - FILLING
167           theCommandID == 4034) { // MENU GENERATION - PIPE
168     if(!GeomGUI->LoadLibrary("libGenerationGUI.so")) 
169       return false;
170   }
171   else if(theCommandID == 404 ||  // MENU ENTITY - SKETCHER
172           theCommandID == 407) {  // MENU ENTITY - EXPLODE
173     if(!GeomGUI->LoadLibrary("libEntityGUI.so")) 
174       return false;
175   }
176   else if(theCommandID == 4081 || // MENU BUILD - EDGE
177           theCommandID == 4082 || // MENU BUILD - WIRE
178           theCommandID == 4083 || // MENU BUILD - FACE
179           theCommandID == 4084 || // MENU BUILD - SHELL
180           theCommandID == 4085 || // MENU BUILD - SOLID
181           theCommandID == 4086) { // MENU BUILD - COMPUND
182     if(!GeomGUI->LoadLibrary("libBuildGUI.so")) 
183       return false;
184   }
185   else if(theCommandID == 5011 || // MENU BOOLEAN - FUSE
186           theCommandID == 5012 || // MENU BOOLEAN - COMMON
187           theCommandID == 5013 || // MENU BOOLEAN - CUT
188           theCommandID == 5014) { // MENU BOOLEAN - SECTION
189     if(!GeomGUI->LoadLibrary("libBooleanGUI.so")) 
190       return false;
191   }
192   else if(theCommandID == 5021 || // MENU TRANSFORMATION - TRANSLATION
193           theCommandID == 5022 || // MENU TRANSFORMATION - ROTATION
194           theCommandID == 5023 || // MENU TRANSFORMATION - MIRROR
195           theCommandID == 5024 || // MENU TRANSFORMATION - SCALE
196           theCommandID == 5025 || // MENU TRANSFORMATION - MULTI-TRANSLATION
197           theCommandID == 5026) { // MENU TRANSFORMATION - MULTI-ROTATION
198     if(!GeomGUI->LoadLibrary("libTransformationGUI.so")) 
199       return false;
200   }
201   else if(theCommandID == 503 ||  // MENU OPERATION - PARTITION
202           theCommandID == 504 ||  // MENU OPERATION - ARCHIMEDE
203           theCommandID == 505 ||  // MENU OPERATION - FILLET
204           theCommandID == 506) {  // MENU OPERATION - CHAMFER
205     if(!GeomGUI->LoadLibrary("libOperationGUI.so")) 
206       return false;
207   }
208   else if(theCommandID == 601 ||  // MENU REPAIR - SEWING
209           theCommandID == 602 ||  // MENU REPAIR - ORIENTATION
210           theCommandID == 603 ||  // MENU REPAIR - SUPPRESS FACES
211           theCommandID == 604) {  // MENU REPAIR - SUPPRESS HOLE
212     if(!GeomGUI->LoadLibrary("libRepairGUI.so")) 
213       return false;
214   }
215   else if(theCommandID == 701 ||  // MENU MEASURE - PROPERTIES
216           theCommandID == 702 ||  // MENU MEASURE - CDG
217           theCommandID == 703 ||  // MENU MEASURE - INERTIA
218           theCommandID == 7041 || // MENU MEASURE - BOUNDING BOX
219           theCommandID == 7042 || // MENU MEASURE - MIN DISTANCE
220           theCommandID == 705 ||  // MENU MEASURE - TOLERANCE
221           theCommandID == 706 ||  // MENU MEASURE - WHATIS
222           theCommandID == 707) {  // MENU MEASURE - CHECK
223     if(!GeomGUI->LoadLibrary("libMeasureGUI.so")) 
224       return false;
225   }
226   else
227     return false;
228
229   //Load Function OnGUIEvent
230   OSD_Function osdF = GeomGUI->myGUILibrary.DlSymb("OnGUIEvent");
231   OGE (*f1) = NULL;
232   if(osdF != NULL) {
233     f1 = (bool (*) (int, QAD_Desktop*)) osdF;
234     (*f1)(theCommandID, parent);
235   }
236   else
237     return false;
238
239   return true;
240 }
241
242
243 //=================================================================================
244 // function : OnKeyPress()
245 // purpose  : [static]
246 //=================================================================================
247 bool GeometryGUI::OnKeyPress(QKeyEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
248 {
249   GeometryGUI::GetOrCreateGeometryGUI(parent);
250
251   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
252     return false;
253   else
254   return true;
255 }
256
257
258 //=================================================================================
259 // function : OnMouseMove()
260 // purpose  : [static] manage mouse events
261 //=================================================================================
262 bool GeometryGUI::OnMouseMove(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
263 {
264   GeometryGUI::GetOrCreateGeometryGUI(parent);
265
266   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
267     return false;
268   else
269   return true;
270 }
271
272
273 //=================================================================================
274 // function : 0nMousePress()
275 // purpose  : [static] manage mouse events
276 //=================================================================================
277 bool GeometryGUI::OnMousePress(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
278 {
279   GeometryGUI::GetOrCreateGeometryGUI(parent);
280
281   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
282     return false;
283
284   if(GeomGUI->myState == 0) {
285     if(!GeomGUI->LoadLibrary("libBasicGUI.so")) 
286       return false;
287
288     //Load Function OnMousePress
289     OSD_Function osdF = GeomGUI->myGUILibrary.DlSymb("OnMousePress");
290     OMP (*f1) = NULL;
291     if(osdF != NULL) {
292       f1 = (bool (*) (QMouseEvent*, QAD_Desktop*, QAD_StudyFrame*)) osdF;
293       (*f1)(pe, parent, studyFrame);
294     }
295   }
296
297   return false;
298 }
299
300 static void UpdateVtkSelection(QAD_Desktop* parent)
301 {
302   if (!parent->getActiveStudy()) return;
303
304   QList<QAD_StudyFrame> aFrameList = parent->getActiveStudy()->getStudyFrames();
305
306   for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
307     if (aStudyFrame->getTypeView() == VIEW_VTK) {
308       QAD_ViewFrame* aViewFrame = aStudyFrame->getRightFrame()->getViewFrame();
309       VTKViewer_ViewFrame* aVtkViewFrame = dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
310       if (!aVtkViewFrame) continue;
311       VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
312       if (anInteractor) {
313         anInteractor->SetSelectionProp();
314         anInteractor->SetSelectionTolerance();
315         VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
316         if (aStyle) {
317           aStyle->setPreselectionProp();
318         }
319       }
320     }
321   }
322 }
323
324
325 //=================================================================================
326 // function : SetSettings()
327 // purpose  : [static]
328 //=================================================================================
329 bool GeometryGUI::SetSettings(QAD_Desktop* parent)
330 {
331   GeometryGUI::GetOrCreateGeometryGUI(parent);
332   QMenuBar* Mb = QAD_Application::getDesktop()->getMainMenuBar();
333   QAD_Study* ActiveStudy = parent->getActiveStudy();
334
335   /* Wireframe or Shading */
336   int DisplayMode = 0;
337   bool ViewOCC = false;
338   if(ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
339     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
340     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
341     DisplayMode = ic->DisplayMode();
342     ViewOCC = true;
343   }
344   else if(ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
345     VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
346     DisplayMode = myRenderInter->GetDisplayMode();
347   }
348
349   if(DisplayMode == 1)
350     Mb->changeItem(211, tr("GEOM_MEN_WIREFRAME"));
351   else
352     Mb->changeItem(211, tr("GEOM_MEN_SHADING"));
353
354
355   /* Add in Study */
356   QString AddInStudy = QAD_CONFIG->getSetting("Geometry:SettingsAddInStudy");
357   int Settings_AddInStudy;
358   if(!AddInStudy.isEmpty())
359     Settings_AddInStudy = AddInStudy.toInt();
360   else
361     Settings_AddInStudy = 1;
362   Mb->setItemChecked(411, Settings_AddInStudy);
363
364
365   /* step value */
366   QString S = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
367   if(S.isEmpty())
368     QAD_CONFIG->addSetting("Geometry:SettingsGeomStep", "100");
369
370
371   /* isos */
372   int count = ActiveStudy->getStudyFramesCount();
373   for(int i = 0; i < count; i++) {
374     if(ActiveStudy->getStudyFrame(i)->getTypeView() == VIEW_OCC) {
375       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
376       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
377       
378       QString IsoU = QAD_CONFIG->getSetting("Geometry:SettingsIsoU");
379       QString IsoV = QAD_CONFIG->getSetting("Geometry:SettingsIsoV");
380       if(!IsoU.isEmpty())
381         ic->DefaultDrawer()->UIsoAspect()->SetNumber(IsoU.toInt());
382       if(!IsoV.isEmpty())
383         ic->DefaultDrawer()->VIsoAspect()->SetNumber(IsoV.toInt());
384     }
385   }
386
387   Mb->setItemEnabled(404, ViewOCC);//SKETCHER
388
389   Mb->setItemEnabled(603, ViewOCC);//SuppressFace
390   Mb->setItemEnabled(604, ViewOCC);//SuppressHole
391   
392   Mb->setItemEnabled(413, ViewOCC);// ShadingColor Settings
393   Mb->setItemEnabled(414, ViewOCC);// Isos Settings
394
395   // PAL5356: update VTK selection
396   ::UpdateVtkSelection(parent);
397
398   return true;
399 }
400
401
402 //=================================================================================
403 // function : DefinePopup()
404 // purpose  : [static]
405 //=================================================================================
406 void GeometryGUI::DefinePopup(QString & theContext, QString & theParent, QString & theObject)
407 {
408   GeometryGUI::GetOrCreateGeometryGUI(QAD_Application::getDesktop());
409
410   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
411   SALOME_Selection* Sel = SALOME_Selection::Selection(ActiveStudy->getSelection());
412   theObject = "";
413   theContext = "";
414
415   if((theParent.compare("Viewer") == 0)) {
416     if(Sel->IObjectCount() == 0)
417       theContext = "NothingSelected";
418   }
419
420   if(Sel->IObjectCount() == 1) {
421     Handle(SALOME_InteractiveObject) IO = Sel->firstIObject();
422     if(IO->hasEntry()) {
423       SALOMEDS::SObject_var sobj = ActiveStudy->getStudyDocument()->FindObjectID(IO->getEntry());
424       if(!sobj->_is_nil()) {
425         SALOMEDS::SComponent_var scomp = sobj->GetFatherComponent();
426         if(strcmp(scomp->GetID(), IO->getEntry()) == 0) {
427           // component is selected
428           theObject = "Component";
429         }
430       }
431     }
432   }
433
434   return;
435 }
436
437
438 //=================================================================================
439 // function : CustomPopup()
440 // purpose  : [static]
441 //=================================================================================
442 bool GeometryGUI::CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString& theContext,
443                               const QString& theParent, const QString& theObject)
444 {
445   GeometryGUI::GetOrCreateGeometryGUI(parent);
446
447   if(!GeomGUI->LoadLibrary("libGEOMBase.so")) 
448     return false;
449
450   //Load Function BuildPresentation
451   OSD_Function osdF = GeomGUI->myGUILibrary.DlSymb("CustomPopup");
452   CP (*f1) = NULL;
453   if(osdF != NULL) {
454     f1 = (bool (*) (QAD_Desktop*, QPopupMenu*, const QString&,
455                     const QString&, const QString&)) osdF;
456     (*f1)(parent, popup, theContext, theParent, theObject);
457   }
458   else
459     return false;
460
461   return true;
462 }
463
464
465 //=================================================================================
466 // function : activeStudyChanged()
467 // purpose  : static
468 //=================================================================================
469 bool GeometryGUI::ActiveStudyChanged(QAD_Desktop* parent)
470 {
471   GeometryGUI::GetOrCreateGeometryGUI(parent); 
472
473   if(GeomGUI != 0) {
474     QMenuBar* Mb = QAD_Application::getDesktop()->getMainMenuBar();
475     bool ViewOCC = false;
476     if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) 
477       ViewOCC = true;
478
479     Mb->setItemEnabled(404, ViewOCC);//SKETCHER
480
481     Mb->setItemEnabled(603, ViewOCC);//SuppressFace
482     Mb->setItemEnabled(604, ViewOCC);//SuppressHole
483   
484     Mb->setItemEnabled(413, ViewOCC);// ShadingColor Settings
485     Mb->setItemEnabled(414, ViewOCC);// Isos Settings
486
487     GeomGUI->EmitSignalCloseAllDialogs();
488     GeomGUI = 0;
489   }
490
491   // PAL5356: update VTK selection
492   ::UpdateVtkSelection(parent);
493
494   return true;
495 }
496
497
498 //=======================================================================
499 // function : BuildPresentation()
500 // purpose  : static
501 //=======================================================================
502 void GeometryGUI::BuildPresentation(const Handle(SALOME_InteractiveObject)& theIO)
503 {
504   GeometryGUI::GetOrCreateGeometryGUI(QAD_Application::getDesktop());
505
506   if(!GeomGUI->LoadLibrary("libDisplayGUI.so")) 
507     return;
508
509   //Load Function BuildPresentation
510   OSD_Function osdF = GeomGUI->myGUILibrary.DlSymb("BuildPresentation");
511   BP (*f1) = NULL;
512   if(osdF != NULL) {
513     f1 = (void (*) (const Handle(SALOME_InteractiveObject)&)) osdF;
514     (*f1)(theIO);
515   }
516   return;
517 }
518
519
520 void GeometryGUI::SupportedViewType(int* buffer, int bufferSize)
521 {
522   if(!buffer || !bufferSize) return;
523   buffer[0] = (int)VIEW_OCC;
524   if (--bufferSize) buffer[1] = (int)VIEW_VTK;
525 }
526
527 void GeometryGUI::Deactivate()
528 {
529   if ( GeomGUI )
530     GeomGUI->EmitSignalCloseAllDialogs();
531 }
532
533 static GeometryGUI aGUI("");
534 extern "C"
535 {
536   Standard_EXPORT SALOMEGUI* GetComponentGUI() {
537     return &aGUI;
538   }
539 }