Salome HOME
58841200b3e5c866794033c9980351001116dd57
[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 // SALOME Includes
32 #include "Utils_ORB_INIT.hxx"
33 #include "Utils_SINGLETON.hxx"
34
35 #include "QAD_Desktop.h"
36 #include "QAD_Application.h"
37 #include "QAD_RightFrame.h"
38 #include "QAD_Config.h"
39 #include "QAD_Tools.h"
40 #include "QAD_MessageBox.h"
41 #include "QAD_Resource.h"
42 #include "SALOMEGUI_Desktop.h"
43
44 #include "OCCViewer_Viewer3d.h"
45 #include "OCCViewer_ViewFrame.h"
46 #include "OCCViewer_ViewPort3d.h"
47 #include "OCCViewer_Prs.h"
48
49 #include "VTKViewer_ViewFrame.h"
50 #include "VTKViewer_RenderWindowInteractor.h"
51 #include "VTKViewer_InteractorStyleSALOME.h"
52 #include "VTKViewer_Prs.h"
53 #include "SALOME_Actor.h"
54
55 #include "SALOME_Selection.h"
56 #include "SALOME_ListIteratorOfListIO.hxx"
57 #include "GEOM_AISShape.hxx"
58 #include "GEOM_Displayer.h"
59
60 #include "GEOMImpl_Types.hxx"
61
62 // External includes
63 #include <qfileinfo.h>
64 #include <Prs3d_Drawer.hxx>
65 #include <Prs3d_IsoAspect.hxx>
66 #include <OSD_SharedLibrary.hxx>
67
68 #include "utilities.h"
69
70 #include <vtkCamera.h>
71 #include <vtkRenderer.h>
72
73 using namespace std;
74
75 extern "C" {
76   Standard_EXPORT SALOMEGUI* GetComponentGUI() {
77     return GeometryGUI::GetGeomGUI();
78   }
79 }
80
81 //=================================================================================
82 // class   : CustomItem
83 // purpose : Set Font to a text.
84 //=================================================================================
85 class CustomItem : public QCustomMenuItem
86 {
87 public:
88   CustomItem(const QString& s, const QFont& f) : myString(s), myFont(f) {}
89   ~CustomItem() {}
90
91   void paint(QPainter* p, const QColorGroup& cg, bool act, bool /*enabled*/, int x, int y, int w, int h)
92   {
93     p->save();
94     p->fillRect( x, y, w, h, act ? cg.highlight() : cg.mid() );
95     p->setPen( act ? cg.highlightedText() : cg.buttonText() );
96     p->setFont( myFont );
97     p->drawText( x, y, w, h, AlignHCenter | AlignVCenter | ShowPrefix | DontClip | SingleLine, myString );
98     p->restore();
99   }
100
101   QSize sizeHint()
102   {
103     return QFontMetrics( myFont ).size( AlignHCenter | AlignVCenter | ShowPrefix | DontClip | SingleLine, myString );
104   }
105
106   bool fullSpan() const
107   {
108     return true;
109   }
110
111 private:
112   QString myString;
113   QFont   myFont;
114 };
115
116 //=======================================================================
117 // function : GeometryGUI::GetGeomGUI()
118 // purpose  : Gets the only object of GeometryGUI [ static ]
119 //=======================================================================
120 GeometryGUI* GeometryGUI::GetGeomGUI()
121 {
122   static GeometryGUI myContext;
123   return &myContext;
124 }
125
126 //=======================================================================
127 // function : GetORB
128 // purpose  : Returns a reference to ORB [ static ]
129 //=======================================================================
130 CORBA::ORB_var GeometryGUI::GetORB()
131 {
132   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
133   return init.orb();
134 }
135
136 //=======================================================================
137 // function : GeometryGUI::GeometryGUI()
138 // purpose  : Constructor
139 //=======================================================================
140 GeometryGUI::GeometryGUI() :
141   SALOMEGUI()
142 {
143   QAD_Desktop* desktop = QAD_Application::getDesktop();
144   Engines::Component_var comp = desktop->getEngine( "FactoryServer", "GEOM" );
145
146   myComponentGeom   = GEOM::GEOM_Gen::_narrow( comp );
147   myState           = -1;
148   myActiveDialogBox = 0;
149   myFatherior       = "";
150
151   gp_Pnt origin = gp_Pnt(0., 0., 0.);
152   gp_Dir direction = gp_Dir(0., 0., 1.);
153   myWorkingPlane = gp_Ax3(origin, direction);
154 }
155
156 //=======================================================================
157 // function : GeometryGUI::~GeometryGUI()
158 // purpose  : Destructor
159 //=======================================================================
160 GeometryGUI::~GeometryGUI()
161 {
162 }
163
164 //=======================================================================
165 // function : GeometryGUI::getLibrary()
166 // purpose  : get or load GUI library by name [ internal ]
167 //=======================================================================
168 typedef GEOMGUI* (*LibraryGUI)();
169 GEOMGUI* GeometryGUI::getLibrary( const QString& libraryName )
170 {
171   if ( !myGUIMap.contains( libraryName ) ) {
172     // try to load library if it is not loaded yet
173     QCString libs;
174     if( ( libs = getenv( "LD_LIBRARY_PATH" ) ) ) {
175       QStringList dirList = QStringList::split( ":", libs, false ); // skip empty entries
176       for( int i = dirList.count()-1; i >= 0; i-- ) {
177         QString dir = dirList[ i ];
178         QFileInfo fi( QAD_Tools::addSlash( dirList[ i ] ) + libraryName );
179         if( fi.exists() ) {
180           OSD_SharedLibrary aSharedLibrary( (char*)fi.fileName().latin1() );
181           bool res = aSharedLibrary.DlOpen( OSD_RTLD_LAZY );
182           if( !res ) {
183             MESSAGE( "Can't open library : " << aSharedLibrary.DlError() );
184             continue; // continue search further
185           }
186           OSD_Function osdF = aSharedLibrary.DlSymb( "GetLibGUI" );
187           if ( osdF != NULL ) {
188             LibraryGUI func = (GEOMGUI* (*) ())osdF;
189             GEOMGUI* libGUI = (*func)();
190             if ( libGUI ) {
191               myGUIMap[ libraryName ] = libGUI;
192               break; // found and loaded!
193             }
194           }
195         }
196       }
197     }
198   }
199   if ( myGUIMap.contains( libraryName ) )
200     // library is successfully loaded
201     return myGUIMap[ libraryName ];
202   return 0;
203 }
204
205 //=======================================================================
206 // function : GeometryGUI::ActiveWorkingPlane()
207 // purpose  : Activate Working Plane View
208 //=======================================================================
209 void GeometryGUI::ActiveWorkingPlane()
210 {
211   gp_Dir DZ = myWorkingPlane.Direction();
212   gp_Dir DY = myWorkingPlane.YDirection();
213
214   if( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
215     OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewPort();
216     Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)vp)->getView();
217
218     view3d->SetProj(DZ.X(), DZ.Y(), DZ.Z());
219     view3d->SetUp(DY.X(), DY.Y(), DY.Z());
220
221     ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->onViewFitAll();
222   }
223   else if( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
224     vtkRenderer* myRenderer = ((VTKViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRenderer();
225
226     vtkCamera* camera = myRenderer->GetActiveCamera();
227     camera->SetPosition(DZ.X(), DZ.Y(), DZ.Z());
228     camera->SetViewUp(DY.X(), DY.Y(), DY.Z());
229     camera->SetFocalPoint(0,0,0);
230
231     ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->onViewFitAll();
232   }
233
234 }
235
236 //=======================================================================
237 // function : GeometryGUI::SetActiveDialogBox()
238 // purpose  : Set active dialog box
239 //=======================================================================
240 void GeometryGUI::SetActiveDialogBox( QDialog* aDlg )
241 {
242   myActiveDialogBox = (QDialog*)aDlg;
243 }
244
245 //=======================================================================
246 // function : GeometryGUI::EmitSignalDeactivateDialog()
247 // purpose  : Emit a signal to deactivate the active dialog Box
248 //=======================================================================
249 void GeometryGUI::EmitSignalDeactivateDialog()
250 {
251   emit SignalDeactivateActiveDialog();
252 }
253
254 //=======================================================================
255 // function : GeometryGUI::EmitSignalCloseAllDialogs()
256 // purpose  : Emit a signal to close all non modal dialogs box
257 //=======================================================================
258 void GeometryGUI::EmitSignalCloseAllDialogs()
259 {
260   emit SignalCloseAllDialogs();
261 }
262
263 //=======================================================================
264 // function : GeometryGUI::EmitSignalDefaultStepValueChanged()
265 // purpose  : Emit a signal to inform that default real spin box step has
266 //            been changed
267 //=======================================================================
268 void GeometryGUI::EmitSignalDefaultStepValueChanged(double newVal)
269 {
270   emit SignalDefaultStepValueChanged(newVal);
271 }
272
273 //=======================================================================
274 // function : GeometryGUI::OnGUIEvent()
275 // purpose  : manage all events on GUI [static]
276 //=======================================================================
277 bool GeometryGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
278 {
279   GeometryGUI* geomGUI = GeometryGUI::GetGeomGUI();
280
281   // get main menu
282   QMenuBar* Mb = parent->getMainMenuBar();
283   // check if current viewframe is of OCC type
284   bool ViewOCC = parent->getActiveStudy()->getActiveStudyFrame()->getTypeView()  == VIEW_OCC;
285   // if current viewframe is not of OCC and not of VTK type - return immediately
286   if( !ViewOCC && parent->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
287     return false;
288
289   // disable non-OCC viewframe menu commands
290   //Mb->setItemEnabled( 404, ViewOCC ); // SKETCHER
291   Mb->setItemEnabled( 603, ViewOCC ); // SuppressFace
292   Mb->setItemEnabled( 604, ViewOCC ); // SuppressHole
293   Mb->setItemEnabled( 606, ViewOCC ); // CloseContour
294   Mb->setItemEnabled( 607, ViewOCC ); // RemoveInternalWires
295   Mb->setItemEnabled( 608, ViewOCC ); // AddPointOnEdge
296   //Mb->setItemEnabled( 609, ViewOCC ); // Free boundaries
297   Mb->setItemEnabled( 413, ViewOCC ); // Isos Settings
298
299   Mb->setItemEnabled( 800, ViewOCC ); // Create Group
300   Mb->setItemEnabled( 801, ViewOCC ); // Edit Group
301
302   Mb->setItemEnabled(9998, ViewOCC ); // MENU BLOCKS - MULTI-TRANSFORMATION
303
304   GEOMGUI* library = 0;
305   // try to get-or-load corresponding GUI library
306   if( theCommandID == 111  ||  // MENU FILE - IMPORT BREP
307       theCommandID == 112  ||  // MENU FILE - IMPORT IGES
308       theCommandID == 113  ||  // MENU FILE - IMPORT STEP
309       theCommandID == 121  ||  // MENU FILE - EXPORT BREP
310       theCommandID == 122  ||  // MENU FILE - EXPORT IGES
311       theCommandID == 123  ||  // MENU FILE - EXPORT STEP
312       theCommandID == 31   ||  // MENU EDIT - COPY
313       theCommandID == 33   ||  // MENU EDIT - DELETE
314       theCommandID == 411  ||  // MENU SETTINGS - ADD IN STUDY
315       theCommandID == 412  ||  // MENU SETTINGS - SHADING COLOR
316       theCommandID == 413  ||  // MENU SETTINGS - ISOS
317       theCommandID == 414  ||  // MENU SETTINGS - STEP VALUE FOR SPIN BOXES
318       theCommandID == 5103 ||  // MENU TOOLS - CHECK GEOMETRY
319       theCommandID == 5104 ||  // MENU TOOLS - LOAD SCRIPT
320       theCommandID == 8032 ||  // POPUP VIEWER - COLOR
321       theCommandID == 8033 ||  // POPUP VIEWER - TRANSPARENCY
322       theCommandID == 8034 ||  // POPUP VIEWER - ISOS
323       theCommandID == 804  ||  // POPUP VIEWER - ADD IN STUDY
324       theCommandID == 901  ||  // OBJECT BROWSER - RENAME
325       theCommandID == 9024 ) { // OBJECT BROWSER - OPEN
326     library = geomGUI->getLibrary( "libGEOMToolsGUI.so" );
327   }
328   else if( theCommandID == 211  ||  // MENU VIEW - WIREFRAME/SHADING
329            theCommandID == 212  ||  // MENU VIEW - DISPLAY ALL
330            theCommandID == 213  ||  // MENU VIEW - DISPLAY ONLY
331            theCommandID == 214  ||  // MENU VIEW - ERASE ALL
332            theCommandID == 215  ||  // MENU VIEW - ERASE
333            theCommandID == 216  ||  // MENU VIEW - DISPLAY
334            theCommandID == 80311 ||  // POPUP VIEWER - WIREFRAME
335            theCommandID == 80312 ) { // POPUP VIEWER - SHADING
336     library = geomGUI->getLibrary( "libDisplayGUI.so" );
337   }
338   else if( theCommandID == 4011 ||  // MENU BASIC - POINT
339            theCommandID == 4012 ||  // MENU BASIC - LINE
340            theCommandID == 4013 ||  // MENU BASIC - CIRCLE
341            theCommandID == 4014 ||  // MENU BASIC - ELLIPSE
342            theCommandID == 4015 ||  // MENU BASIC - ARC
343            theCommandID == 4016 ||  // MENU BASIC - VECTOR
344            theCommandID == 4017 ||  // MENU BASIC - PLANE
345            theCommandID == 4018 ||  // MENU BASIC - WPLANE
346            theCommandID == 4019 ||  // MENU BASIC - CURVE
347            theCommandID == 4020 ) { // MENU BASIC - REPAIR
348     library = geomGUI->getLibrary( "libBasicGUI.so" );
349   }
350   else if( theCommandID == 4021 ||  // MENU PRIMITIVE - BOX
351            theCommandID == 4022 ||  // MENU PRIMITIVE - CYLINDER
352            theCommandID == 4023 ||  // MENU PRIMITIVE - SPHERE
353            theCommandID == 4024 ||  // MENU PRIMITIVE - TORUS
354            theCommandID == 4025 ) { // MENU PRIMITIVE - CONE
355     library = geomGUI->getLibrary( "libPrimitiveGUI.so" );
356   }
357   else if( theCommandID == 4031 ||  // MENU GENERATION - PRISM
358            theCommandID == 4032 ||  // MENU GENERATION - REVOLUTION
359            theCommandID == 4033 ||  // MENU GENERATION - FILLING
360            theCommandID == 4034 ) { // MENU GENERATION - PIPE
361     library = geomGUI->getLibrary( "libGenerationGUI.so" );
362   }
363   else if( theCommandID == 404 ||   // MENU ENTITY - SKETCHER
364            theCommandID == 407 ) {  // MENU ENTITY - EXPLODE
365     library = geomGUI->getLibrary( "libEntityGUI.so" );
366   }
367   else if( theCommandID == 4081 ||  // MENU BUILD - EDGE
368            theCommandID == 4082 ||  // MENU BUILD - WIRE
369            theCommandID == 4083 ||  // MENU BUILD - FACE
370            theCommandID == 4084 ||  // MENU BUILD - SHELL
371            theCommandID == 4085 ||  // MENU BUILD - SOLID
372            theCommandID == 4086 ) { // MENU BUILD - COMPUND
373     library = geomGUI->getLibrary( "libBuildGUI.so" );
374   }
375   else if( theCommandID == 5011 ||  // MENU BOOLEAN - FUSE
376            theCommandID == 5012 ||  // MENU BOOLEAN - COMMON
377            theCommandID == 5013 ||  // MENU BOOLEAN - CUT
378            theCommandID == 5014 ) { // MENU BOOLEAN - SECTION
379     library = geomGUI->getLibrary( "libBooleanGUI.so" );
380   }
381   else if( theCommandID == 5021 ||  // MENU TRANSFORMATION - TRANSLATION
382            theCommandID == 5022 ||  // MENU TRANSFORMATION - ROTATION
383            theCommandID == 5023 ||  // MENU TRANSFORMATION - LOCATION
384            theCommandID == 5024 ||  // MENU TRANSFORMATION - MIRROR
385            theCommandID == 5025 ||  // MENU TRANSFORMATION - SCALE
386            theCommandID == 5026 ||  // MENU TRANSFORMATION - OFFSET
387            theCommandID == 5027 ||  // MENU TRANSFORMATION - MULTI-TRANSLATION
388            theCommandID == 5028 ) { // MENU TRANSFORMATION - MULTI-ROTATION
389     library = geomGUI->getLibrary( "libTransformationGUI.so" );
390   }
391   else if( theCommandID == 503 ||   // MENU OPERATION - PARTITION
392            theCommandID == 504 ||   // MENU OPERATION - ARCHIMEDE
393            theCommandID == 505 ||   // MENU OPERATION - FILLET
394            theCommandID == 506 ||   // MENU OPERATION - CHAMFER  
395            theCommandID == 507 ) {  // MENU OPERATION - CLIPPING RANGE
396     library = geomGUI->getLibrary( "libOperationGUI.so" );
397   }
398   else if( theCommandID == 601 ||   // MENU REPAIR - SEWING
399            theCommandID == 603 ||   // MENU REPAIR - SUPPRESS FACES
400            theCommandID == 604 ||   // MENU REPAIR - SUPPRESS HOLE
401            theCommandID == 605 ||   // MENU REPAIR - SHAPE PROCESSING
402            theCommandID == 606 ||   // MENU REPAIR - CLOSE CONTOUR
403            theCommandID == 607 ||   // MENU REPAIR - REMOVE INTERNAL WIRES
404            theCommandID == 608 ||   // MENU REPAIR - ADD POINT ON EDGE
405            theCommandID == 609 ||   // MENU REPAIR - FREE BOUNDARIES
406            theCommandID == 610 ||   // MENU REPAIR - FREE FACES
407            theCommandID == 602 ) {  // MENU REPAIR - GLUE FACES
408     library = geomGUI->getLibrary( "libRepairGUI.so" );
409   }
410   else if( theCommandID == 701   ||  // MENU MEASURE - PROPERTIES
411            theCommandID == 702   ||  // MENU MEASURE - CDG
412            theCommandID == 703   ||  // MENU MEASURE - INERTIA
413            theCommandID == 7041  ||  // MENU MEASURE - BOUNDING BOX
414            theCommandID == 7042  ||  // MENU MEASURE - MIN DISTANCE
415            theCommandID == 705   ||  // MENU MEASURE - TOLERANCE
416            theCommandID == 706   ||  // MENU MEASURE - WHATIS
417            theCommandID == 707   ||  // MENU MEASURE - CHECK
418            theCommandID == 7072  ||  // MENU MEASURE - CHECK COMPOUND OF BLOCKS
419            theCommandID == 708 ) {  // MENU MEASURE - POINT COORDINATES
420     library = geomGUI->getLibrary( "libMeasureGUI.so" );
421   }
422   else if( theCommandID == 800  ||  // MENU GROUP - CREATE
423            theCommandID == 801 ) {  // MENU GROUP - EDIT
424     library = geomGUI->getLibrary( "libGroupGUI.so" );
425   }
426   else if( theCommandID == 9999  ||  // MENU BLOCKS - HEXAHEDRAL SOLID
427            theCommandID == 9998  ||  // MENU BLOCKS - MULTI-TRANSFORMATION
428            theCommandID == 9997  ||  // MENU BLOCKS - QUADRANGLE FACE
429            theCommandID == 99991 ||  // MENU BLOCKS - PROPAGATE
430            theCommandID == 9995 ) { // MENU BLOCKS - EXPLODE ON BLOCKS
431     library = geomGUI->getLibrary( "libBlocksGUI.so" );
432   }
433
434   // call method of corresponding GUI library
435   if ( library ) {
436     return library->OnGUIEvent( theCommandID, parent );
437   }
438   else {
439     QAD_MessageBox::error1( parent, tr( "GEOM_ERROR" ), tr( "GEOM_ERR_LIB_NOT_FOUND" ), tr( "GEOM_BUT_OK" ) );
440   }
441   return false;
442 }
443
444
445 //=================================================================================
446 // function : GeometryGUI::OnKeyPress()
447 // purpose  : Called when any key is pressed by user [static]
448 //=================================================================================
449 bool GeometryGUI::OnKeyPress(QKeyEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
450 {
451   GeometryGUI* geomGUI = GeometryGUI::GetGeomGUI();
452   GUIMap::Iterator it;
453   bool bOk = true;
454   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
455     bOk = bOk && it.data()->OnKeyPress( pe, parent, studyFrame );
456   return bOk;
457 }
458
459
460 //=================================================================================
461 // function : GeometryGUI::OnMouseMove()
462 // purpose  : Manages mouse move events [static]
463 //=================================================================================
464 bool GeometryGUI::OnMouseMove(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
465 {
466   GeometryGUI* geomGUI = GeometryGUI::GetGeomGUI();
467   GUIMap::Iterator it;
468   bool bOk = true;
469   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
470     bOk = bOk && it.data()->OnMouseMove( pe, parent, studyFrame );
471   return bOk;
472 }
473
474
475 //=================================================================================
476 // function : GeometryGUI::0nMousePress()
477 // purpose  : Manage mouse press events [static]
478 //=================================================================================
479 bool GeometryGUI::OnMousePress(QMouseEvent* pe, QAD_Desktop* parent, QAD_StudyFrame* studyFrame)
480 {
481   GeometryGUI* geomGUI = GeometryGUI::GetGeomGUI();
482   GUIMap::Iterator it;
483   // OnMousePress() should return false if this event should be processed further
484   // (see OCCViewer_Viewer3d::onMousePress() for explanation)
485   bool processed = false;
486   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
487     processed = processed || it.data()->OnMousePress( pe, parent, studyFrame );
488   return processed;
489 }
490
491 static void UpdateVtkSelection(QAD_Desktop* parent)
492 {
493   if (!parent->getActiveStudy()) return;
494
495   QList<QAD_StudyFrame> aFrameList = parent->getActiveStudy()->getStudyFrames();
496
497   for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
498     if (aStudyFrame->getTypeView() == VIEW_VTK) {
499       QAD_ViewFrame* aViewFrame = aStudyFrame->getRightFrame()->getViewFrame();
500       VTKViewer_ViewFrame* aVtkViewFrame = dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
501       if (!aVtkViewFrame) continue;
502       VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
503       if (anInteractor) {
504         anInteractor->SetSelectionProp();
505         anInteractor->SetSelectionTolerance();
506         VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
507         if (aStyle) {
508           aStyle->setPreselectionProp();
509         }
510       }
511     }
512   }
513 }
514
515 //=================================================================================
516 // function : GeometryGUI::SetSettings()
517 // purpose  : Called when GEOM module is activated [static]
518 //=================================================================================
519 bool GeometryGUI::SetSettings(QAD_Desktop* parent)
520 {
521   GeometryGUI* geomGUI = GetGeomGUI();
522   QMenuBar*    Mb = parent->getMainMenuBar();
523   QAD_Study*   ActiveStudy = parent->getActiveStudy();
524     
525   
526
527   /* Wireframe or Shading */
528   int DisplayMode = 0;
529   bool ViewOCC = false;
530   if ( ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) {
531     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
532     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
533     DisplayMode = ic->DisplayMode();
534     ViewOCC = true;
535   }
536   else if (ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
537     VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
538     DisplayMode = myRenderInter->GetDisplayMode();
539   }
540
541   if( DisplayMode == 1 )
542     Mb->changeItem( 211, tr( "GEOM_MEN_WIREFRAME" ) );
543   else
544     Mb->changeItem( 211, tr( "GEOM_MEN_SHADING" ) );
545
546
547   /* Add in Study  - !!!ALWAYS TRUE!!! */ /////// VSR : TO BE REMOVED
548   QString AddInStudy = QAD_CONFIG->getSetting("Geometry:SettingsAddInStudy");
549   int Settings_AddInStudy;
550   /*if(!AddInStudy.isEmpty())
551     Settings_AddInStudy = AddInStudy.toInt();
552   else
553   */
554   Settings_AddInStudy = 1;
555   Mb->setItemChecked(411, Settings_AddInStudy);
556
557   /* step value */
558   QString S = QAD_CONFIG->getSetting("Geometry:SettingsGeomStep");
559   if(S.isEmpty())
560     QAD_CONFIG->addSetting("Geometry:SettingsGeomStep", "100");
561
562   /* isos */
563   int count = ActiveStudy->getStudyFramesCount();
564   for(int i = 0; i < count; i++) {
565     if(ActiveStudy->getStudyFrame(i)->getTypeView() == VIEW_OCC) {
566       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
567       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
568
569       QString IsoU = QAD_CONFIG->getSetting("Geometry:SettingsIsoU");
570       QString IsoV = QAD_CONFIG->getSetting("Geometry:SettingsIsoV");
571       if(!IsoU.isEmpty())
572         ic->DefaultDrawer()->UIsoAspect()->SetNumber(IsoU.toInt());
573       if(!IsoV.isEmpty())
574         ic->DefaultDrawer()->VIsoAspect()->SetNumber(IsoV.toInt());
575     }
576   }
577
578   //Mb->setItemEnabled(404, ViewOCC); // SKETCHER
579   Mb->setItemEnabled(603, ViewOCC); // SuppressFace
580   Mb->setItemEnabled(604, ViewOCC); // SuppressHole
581   Mb->setItemEnabled(606, ViewOCC); // CloseContour
582   Mb->setItemEnabled(607, ViewOCC); // RemoveInternalWires
583   Mb->setItemEnabled(608, ViewOCC); // AddPointOnEdge
584 //  Mb->setItemEnabled(609, ViewOCC); // Free boundaries
585   Mb->setItemEnabled(413, ViewOCC); // Isos Settings
586
587   Mb->setItemEnabled( 800, ViewOCC ); // Create Group
588   Mb->setItemEnabled( 801, ViewOCC ); // Edit Group
589
590   Mb->setItemEnabled(9998, ViewOCC); // MENU BLOCKS - MULTI-TRANSFORMATION
591
592       
593   // PAL5356: update VTK selection
594   ::UpdateVtkSelection(parent);
595   bool bOk = true;
596   GUIMap::Iterator it;
597   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
598     bOk = bOk && it.data()->SetSettings( parent );
599     
600   // MZN: Enable/disable "Clipping range" menu item(from GEOM_CLIPPING variable)        
601   if (getenv( "GEOM_CLIPPING" ) == NULL)
602     {
603       QMenuItem* mi = Mb->findItem(50);
604       if (mi && mi->popup())
605       mi->popup()->removeItem(507);     
606     } 
607     
608   return bOk;
609 }
610
611 //=======================================================================
612 // function : GeometryGUI::Deactivate()
613 // purpose  : Called when GEOM module is deactivated [ static ]
614 //=======================================================================
615 void GeometryGUI::Deactivate()
616 {
617   GeometryGUI* geomGUI = GetGeomGUI();
618   GetGeomGUI()->EmitSignalCloseAllDialogs();
619   GUIMap::Iterator it;
620   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
621     it.data()->Deactivate();
622 }
623
624 //=================================================================================
625 // function : GeometryGUI::DefinePopup()
626 // purpose  : Called from desktop to define popup menu [static]
627 //=================================================================================
628 void GeometryGUI::DefinePopup(QString& theContext, QString& theParent, QString& theObject)
629 {
630   GeometryGUI* geomGUI   = GetGeomGUI();
631   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
632   SALOME_Selection* Sel  = SALOME_Selection::Selection(ActiveStudy->getSelection());
633
634   theObject  = "";
635   theContext = "";
636
637   if ( theParent == "Viewer" ) {
638     if ( Sel->IObjectCount() == 0 )
639       theContext = "NothingSelected";
640   }
641
642   if ( Sel->IObjectCount() == 1 ) {
643     Handle(SALOME_InteractiveObject) IO = Sel->firstIObject();
644     if( IO->hasEntry() ) {
645       SALOMEDS::SObject_var sobj = ActiveStudy->getStudyDocument()->FindObjectID( IO->getEntry() );
646       if ( !sobj->_is_nil() ) {
647         SALOMEDS::SComponent_var scomp = sobj->GetFatherComponent();
648         if ( !strcmp(scomp->GetID(), IO->getEntry() ) ) {
649           // component is selected
650           theObject = "Component";
651         }
652         else {
653           GEOM::GEOM_Object_var aGObj = GEOM::GEOM_Object::_narrow( sobj->GetObject() );
654           if ( !CORBA::is_nil( aGObj ) ) {
655             switch( aGObj->GetType() ) {
656             case GEOM_GROUP:
657               theObject = "Group";
658               break;
659             default:
660               theObject = "Shape";
661               break;
662             }
663           }
664         }
665       }
666     }
667   }
668 }
669
670 //=================================================================================
671 // function : GeometryGUI::CustomPopup()
672 // purpose  : Called from desktop to create popup menu [static]
673 //=================================================================================
674 bool GeometryGUI::CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString& theContext,
675                               const QString& theParent, const QString& theObject)
676 {
677   GeometryGUI* geomGUI = GetGeomGUI();
678
679   // Deactivate any non modal dialog box to get the neutral point
680   geomGUI->EmitSignalDeactivateDialog();
681   QAD_Study* anActiveStudy    = parent->getActiveStudy();
682   QAD_StudyFrame* aStudyFrame = anActiveStudy->getActiveStudyFrame();
683   QAD_ViewFrame* aViewFrame   = aStudyFrame->getRightFrame()->getViewFrame();
684   SALOME_Selection* Sel       = SALOME_Selection::Selection(anActiveStudy->getSelection());
685   QString parentComponent     = ((SALOMEGUI_Desktop*)parent)->getComponentFromSelection();
686   bool isOCCViewer            = aViewFrame->getTypeView() == VIEW_OCC;
687   bool isVTKViewer            = aViewFrame->getTypeView() == VIEW_VTK;
688   int aDisplayMode            = 0;
689   QString objectName;
690
691   if ( aViewFrame->getTypeView() == VIEW_OCC )
692     aDisplayMode = ((OCCViewer_ViewFrame*)aViewFrame)->getViewer()->getAISContext()->DisplayMode();
693   else if ( aViewFrame->getTypeView() == VIEW_VTK )
694     aDisplayMode = ((VTKViewer_ViewFrame*)aViewFrame)->getRWInteractor()->GetDisplayMode();
695
696   int nbSel = Sel->IObjectCount();
697
698   if( nbSel == 0 ) {
699     ////// NOTHING SELECTED
700     popup->clear();
701   }
702   else if ( nbSel == 1 ) {
703     ////// SINGLE OBJECT SELECTION
704     if ( parentComponent != parent->getActiveComponent() )  {
705       ////// selected object does not belong to GEOM module:
706       // remove all commands except Display/Erase...
707       while ( 1 ) {
708         int id = popup->idAt( 0 );
709         if ( id <= QAD_TopLabel_Popup_ID )
710           popup->removeItemAt( 0 );
711         else
712           break;
713       }
714     }
715     else {
716       ////// selected object belong to the GEOM module
717       // get interactive object
718       Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
719       objectName = IObject->getName();
720       // if object has entry get SObject
721       SALOMEDS::SObject_var SO;
722       if ( IObject->hasEntry() )
723         SO = anActiveStudy->getStudyDocument()->FindObjectID( IObject->getEntry() );
724
725       if ( theObject == "Component" ) {
726         ////// menu for component
727         if ( !isOCCViewer && !isVTKViewer ) {
728           popup->removeItem( QAD_DisplayOnly_Popup_ID );
729           popup->removeItem( QAD_Display_Popup_ID );
730           popup->removeItem( QAD_Erase_Popup_ID );
731         }
732       }
733       else {
734         ////// not component (should be shape)
735         if ( IObject->hasEntry() )  /////// VSR : TO BE REMOVED
736           popup->removeItem( 804 ); // "Add in Study"
737
738         // Here could be analysis of the geom shape's type
739         // ... //
740
741         SALOMEDS::GenericAttribute_var aTmpAttr;
742         if( SO->_is_nil() || SO->GetFatherComponent()->FindAttribute( aTmpAttr, "AttributeIOR") )
743           popup->removeItem( 9024 ); // "Open" /////// VSR : TO BE REMOVED
744
745         if ( !isOCCViewer && theParent == "ObjectBrowser" ) {
746           if ( theObject == "Shape" )
747             popup->removeItem( 800 ); // Create Group
748           else if ( theObject == "Group" )
749             popup->removeItem( 801 ); // Edit Group
750         }
751
752         if ( isOCCViewer || isVTKViewer ) {
753           ////// current viewer is OCC or VTK
754           SALOME_Prs* prs = aViewFrame->CreatePrs( IObject->getEntry() );
755           if ( aViewFrame->isVisible( IObject ) ) {
756             ////// object is already displayed in the viewer
757             popup->removeItem( QAD_Display_Popup_ID );
758             if ( isOCCViewer ) {
759               ////// OCC viewer only
760               OCCViewer_Prs* occPrs = dynamic_cast<OCCViewer_Prs*>( prs );
761               if ( occPrs && !occPrs->IsNull() ) {
762                 AIS_ListOfInteractive ioList;
763                 occPrs->GetObjects( ioList );
764                 QMenuItem* mi = popup->findItem( 803 );
765                 if ( mi && mi->popup() ) {
766                   if ( ioList.First()->DisplayMode() == 0 )
767                     mi->popup()->setItemChecked( 80311, true ); // "Wireframe"
768                   else if ( ioList.First()->DisplayMode() == 1 )
769                     mi->popup()->setItemChecked( 80312, true ); // "Shading"
770                   else if ( ioList.First()->DisplayMode() < 0 )
771                     mi->popup()->setItemChecked( aDisplayMode == 0 ? 80311 : 80312 , true ); // "Wireframe" or "Shading"
772                 }
773               }
774             }
775             else {
776               ////// VTK viewer only
777               popup->removeItem( 8034 ); // "Isos"
778               VTKViewer_Prs* vtkPrs = dynamic_cast<VTKViewer_Prs*>( prs );
779               if ( vtkPrs && !vtkPrs->IsNull() ) {
780                 vtkActorCollection* actorList = vtkPrs->GetObjects();
781                 actorList->InitTraversal();
782                 SALOME_Actor* ac = SALOME_Actor::SafeDownCast( actorList->GetNextActor() );
783                 QMenuItem* mi = popup->findItem( 803 );
784                 if ( ac && mi && mi->popup() ) {
785                   if ( ac->getDisplayMode() == 0 )
786                     mi->popup()->setItemChecked( 80311, true ); // "Wireframe"
787                   else if ( ac->getDisplayMode() == 1 )
788                     mi->popup()->setItemChecked( 80312, true ); // "Shading"
789                   else
790                     mi->popup()->setItemChecked( aDisplayMode == 0 ? 80311 : 80312 , true ); // "Wireframe" or "Shading"
791                 }
792               }
793             }
794           }
795           else {
796             ////// object is not yet displayed in the viewer
797             popup->removeItem( 803 );  // "Display Mode"
798             popup->removeItem( 8032 ); // "Color"
799             popup->removeItem( 8033 ); // "Transparency"
800             popup->removeItem( 8034 ); // "Isos"
801             popup->removeItem( QAD_Erase_Popup_ID );
802           }
803           delete prs;
804         }
805         else {
806           ////// other viewer type (neither OCC nor VTK)
807           popup->removeItem( 803 );  // "Display Mode"
808           popup->removeItem( 8032 ); // "Color"
809           popup->removeItem( 8033 ); // "Transparency"
810           popup->removeItem( 8034 ); // "Isos"
811           popup->removeItem( QAD_Display_Popup_ID );
812           popup->removeItem( QAD_DisplayOnly_Popup_ID );
813           popup->removeItem( QAD_Erase_Popup_ID );
814         }
815       }
816     }
817   }
818   else {
819     ////// MULTIPLE SELECTION
820     if ( parentComponent != parent->getActiveComponent() )  {
821       ////// not GEOM module objects or objects belong to different modules
822       // remove all commands except Display/Erase...
823       while ( 1 ) {
824         int id = popup->idAt( 0 );
825         if ( id <= QAD_TopLabel_Popup_ID )
826           popup->removeItemAt( 0 );
827         else
828           break;
829       }
830       if ( parentComponent.isNull() )  {
831         ////// objects belong to different modules
832         popup->removeItem(QAD_Display_Popup_ID);
833         popup->removeItem(QAD_DisplayOnly_Popup_ID);
834         popup->removeItem(QAD_Erase_Popup_ID);
835       }
836       else {
837         objectName = tr( "GEOM_MEN_POPUP_NAME" ).arg( nbSel );
838       }
839     }
840     else {
841       ////// all selected objects belong to GEOM module
842       popup->removeItem( 901 ); // "Rename"
843
844       SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
845       bool isComponent  = false;
846       bool needOpen     = false;
847       bool needDisplay  = false;
848       bool needErase    = false;
849       int needToPublish = 0;
850
851       for( ;It.More();It.Next() ) {
852         Handle(SALOME_InteractiveObject) anIObject = It.Value();
853
854         if ( aViewFrame->isVisible( anIObject ) )
855           needErase   = true;
856         else
857           needDisplay = true;
858
859         if( anIObject->hasEntry() ) {
860           needToPublish = -1; /////// VSR : TO BE REMOVED
861           SALOMEDS::SObject_var obj = anActiveStudy->getStudyDocument()->FindObjectID( anIObject->getEntry() );
862           SALOMEDS::GenericAttribute_var aTmpAttr;
863           if ( !obj->_is_nil() && !obj->GetFatherComponent()->FindAttribute( aTmpAttr, "AttributeIOR" ) )
864             needOpen = true;  /////// VSR : TO BE REMOVED
865           if ( !obj->_is_nil() && QString( obj->GetID() ) == QString( obj->GetFatherComponent()->GetID() ) )
866             isComponent = true;
867         }
868         else {
869           if ( needToPublish != -1 ) needToPublish = 1;
870         }
871       }
872       if( needOpen || ( !isOCCViewer && !isVTKViewer ) ) {
873         ////// Data is not loaded yet or current viewer is neither OCC nor VTK
874         popup->removeItem( 803 );  // "Display Mode"
875         popup->removeItem( 8032 ); // "Color"
876         popup->removeItem( 8033 ); // "Transparency"
877         popup->removeItem( 8034 ); // "Isos"
878         popup->removeItem( 804 );  // "Add in Study"
879         popup->removeItem( QAD_DisplayOnly_Popup_ID );
880         popup->removeItem( QAD_Display_Popup_ID );
881         popup->removeItem( QAD_Erase_Popup_ID );
882       }
883       else {
884         popup->removeItem( 9024 );   // "Open"
885         if ( needToPublish <= 0 )
886           popup->removeItem( 804 );  // "Add in Study"
887
888         if( isComponent ) {
889           popup->removeItem( 803 );  // "Display Mode"
890           popup->removeItem( 8032 ); // "Color"
891           popup->removeItem( 8033 ); // "Transparency"
892           popup->removeItem( 8034 ); // "Isos"
893           popup->removeItem( QAD_DisplayOnly_Popup_ID );
894         }
895
896         if ( !needDisplay )
897           popup->removeItem( QAD_Display_Popup_ID );
898         if ( !needErase )
899           popup->removeItem( QAD_Erase_Popup_ID );
900         if ( !isOCCViewer )
901           popup->removeItem( 8034 ); // "Isos"
902       }
903     }
904   }
905
906   // check popup for unnecessary separators
907   QAD_Tools::checkPopup( popup );
908   // find popup menu's TopLabel item (with title)
909   int topItem = popup->indexOf( QAD_TopLabel_Popup_ID );
910   if ( topItem >= 0 ) {
911     // remove popup menu's title item
912     popup->removeItem( QAD_TopLabel_Popup_ID );
913     if ( theParent == "Viewer" && !objectName.isEmpty() && popup->count() > 0 ) {
914       // set bold font for popup menu's title
915       QFont f = popup->font(); f.setBold( TRUE );
916       popup->removeItem( QAD_TopLabel_Popup_ID );
917       popup->insertItem( new CustomItem( objectName, f ), QAD_TopLabel_Popup_ID, topItem );
918     }
919   }
920
921   return false;
922 }
923
924 //=================================================================================
925 // function : GeometryGUI::ActiveStudyChanged()
926 // purpose  : static
927 //=================================================================================
928 bool GeometryGUI::ActiveStudyChanged(QAD_Desktop* parent)
929 {
930   GeometryGUI* geomGUI     = GetGeomGUI();
931   QAD_Study*   ActiveStudy = parent->getActiveStudy();
932   QMenuBar* Mb = QAD_Application::getDesktop()->getMainMenuBar();
933   int DisplayMode = 0;
934   bool ViewOCC = false;
935
936   if( ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC ) {
937     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
938     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
939     DisplayMode = ic->DisplayMode();
940     ViewOCC = true;
941   }
942   else if( ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK ) {
943     VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
944     DisplayMode = myRenderInter->GetDisplayMode();
945   }
946
947   if( DisplayMode == 1 )
948     Mb->changeItem( 211, tr( "GEOM_MEN_WIREFRAME" ) );
949   else
950     Mb->changeItem( 211, tr( "GEOM_MEN_SHADING" ) );
951
952   //Mb->setItemEnabled( 404, ViewOCC ); // SKETCHER
953   Mb->setItemEnabled( 603, ViewOCC ); // SuppressFace
954   Mb->setItemEnabled( 604, ViewOCC ); // SuppressHole
955   Mb->setItemEnabled( 606, ViewOCC ); // CloseContour
956   Mb->setItemEnabled( 413, ViewOCC ); // Isos Settings
957   Mb->setItemEnabled( 800, ViewOCC ); // Create Group
958   Mb->setItemEnabled( 801, ViewOCC ); // Edit Group
959   Mb->setItemEnabled(9998, ViewOCC ); // MENU BLOCKS - MULTI-TRANSFORMATION
960
961   geomGUI->EmitSignalCloseAllDialogs();
962
963   // PAL5356: update VTK selection
964   ::UpdateVtkSelection( parent );
965
966   bool bOk = true;
967   GUIMap::Iterator it;
968   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
969     bOk = bOk && it.data()->ActiveStudyChanged( parent );
970   return bOk;
971 }
972
973 //=======================================================================
974 // function : GeometryGUI::BuildPresentation()
975 // purpose  : static
976 //=======================================================================
977 void GeometryGUI::BuildPresentation( const Handle(SALOME_InteractiveObject)& theIO,
978                                      QAD_ViewFrame* theViewFrame )
979 {
980   GEOM_Displayer().Display( theIO, false, theViewFrame );
981 }
982
983 void GeometryGUI::SupportedViewType(int* buffer, int bufferSize)
984 {
985   if(!buffer || !bufferSize) return;
986   buffer[0] = (int)VIEW_OCC;
987   if (--bufferSize) buffer[1] = (int)VIEW_VTK;
988 }