Salome HOME
Merge with version on tag OCC-V2_1_0d
[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     if ( !act ) {
94       p->fillRect( x, y+1, w, h-2, cg.mid() );
95       p->drawRect( x, y+1, w, h-2 );
96     }
97     else {
98       p->fillRect( x, y+1, w, h-2, cg.midlight() );
99       QPen oldPen = p->pen();
100       p->setPen( cg.mid() );
101       p->drawRect( x, y+1, w, h-2 );
102       p->setPen( oldPen );
103     }
104     p->setFont( myFont );
105     p->drawText( x, y, w, h, AlignHCenter | AlignVCenter | ShowPrefix | DontClip, myString );
106   }
107
108   QSize sizeHint()
109   {
110     return QFontMetrics( myFont ).size( AlignHCenter | AlignVCenter | ShowPrefix | DontClip, myString );
111   }
112
113 private:
114   QString myString;
115   QFont   myFont;
116
117 };
118
119 //=======================================================================
120 // function : GeometryGUI::GetGeomGUI()
121 // purpose  : Gets the only object of GeometryGUI [ static ]
122 //=======================================================================
123 GeometryGUI* GeometryGUI::GetGeomGUI()
124 {
125   static GeometryGUI myContext;
126   return &myContext;
127 }
128
129 //=======================================================================
130 // function : GetORB
131 // purpose  : Returns a reference to ORB [ static ]
132 //=======================================================================
133 CORBA::ORB_var GeometryGUI::GetORB()
134 {
135   ORB_INIT& init = *SINGLETON_<ORB_INIT>::Instance();
136   return init.orb();
137 }
138
139 //=======================================================================
140 // function : GeometryGUI::GeometryGUI()
141 // purpose  : Constructor
142 //=======================================================================
143 GeometryGUI::GeometryGUI() :
144   SALOMEGUI()
145 {
146   QAD_Desktop* desktop = QAD_Application::getDesktop();
147   Engines::Component_var comp = desktop->getEngine( "FactoryServer", "GEOM" );
148
149   myComponentGeom   = GEOM::GEOM_Gen::_narrow( comp );
150   myState           = -1;
151   myActiveDialogBox = 0;
152   myFatherior       = "";
153
154   gp_Pnt origin = gp_Pnt(0., 0., 0.);
155   gp_Dir direction = gp_Dir(0., 0., 1.);
156   myWorkingPlane = gp_Ax3(origin, direction);
157 }
158
159 //=======================================================================
160 // function : GeometryGUI::~GeometryGUI()
161 // purpose  : Destructor
162 //=======================================================================
163 GeometryGUI::~GeometryGUI()
164 {
165 }
166
167 //=======================================================================
168 // function : GeometryGUI::getLibrary()
169 // purpose  : get or load GUI library by name [ internal ]
170 //=======================================================================
171 typedef GEOMGUI* (*LibraryGUI)();
172 GEOMGUI* GeometryGUI::getLibrary( const QString& libraryName )
173 {
174   if ( !myGUIMap.contains( libraryName ) ) {
175     // try to load library if it is not loaded yet
176     QCString libs;
177     if( ( libs = getenv( "LD_LIBRARY_PATH" ) ) ) {
178       QStringList dirList = QStringList::split( ":", libs, false ); // skip empty entries
179       for( int i = dirList.count()-1; i >= 0; i-- ) {
180         QString dir = dirList[ i ];
181         QFileInfo fi( QAD_Tools::addSlash( dirList[ i ] ) + libraryName );
182         if( fi.exists() ) {
183           OSD_SharedLibrary aSharedLibrary( (char*)fi.fileName().latin1() );
184           bool res = aSharedLibrary.DlOpen( OSD_RTLD_LAZY );
185           if( !res ) {
186             MESSAGE( "Can't open library : " << aSharedLibrary.DlError() );
187             continue; // continue search further
188           }
189           OSD_Function osdF = aSharedLibrary.DlSymb( "GetLibGUI" );
190           if ( osdF != NULL ) {
191             LibraryGUI func = (GEOMGUI* (*) ())osdF;
192             GEOMGUI* libGUI = (*func)();
193             if ( libGUI ) {
194               myGUIMap[ libraryName ] = libGUI;
195               break; // found and loaded!
196             }
197           }
198         }
199       }
200     }
201   }
202   if ( myGUIMap.contains( libraryName ) )
203     // library is successfully loaded
204     return myGUIMap[ libraryName ];
205   return 0;
206 }
207
208 //=======================================================================
209 // function : GeometryGUI::ActiveWorkingPlane()
210 // purpose  : Activate Working Plane View
211 //=======================================================================
212 void GeometryGUI::ActiveWorkingPlane()
213 {
214   gp_Dir DZ = myWorkingPlane.Direction();
215   gp_Dir DY = myWorkingPlane.YDirection();
216
217   if( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
218     OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewPort();
219     Handle(V3d_View) view3d = ((OCCViewer_ViewPort3d*)vp)->getView();
220
221     view3d->SetProj(DZ.X(), DZ.Y(), DZ.Z());
222     view3d->SetUp(DY.X(), DY.Y(), DY.Z());
223
224     ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->onViewFitAll();
225   }
226   else if( QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
227     vtkRenderer* myRenderer = ((VTKViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRenderer();
228
229     vtkCamera* camera = myRenderer->GetActiveCamera();
230     camera->SetPosition(DZ.X(), DZ.Y(), DZ.Z());
231     camera->SetViewUp(DY.X(), DY.Y(), DY.Z());
232     camera->SetFocalPoint(0,0,0);
233
234     ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->onViewFitAll();
235   }
236
237 }
238
239 //=======================================================================
240 // function : GeometryGUI::SetActiveDialogBox()
241 // purpose  : Set active dialog box
242 //=======================================================================
243 void GeometryGUI::SetActiveDialogBox( QDialog* aDlg )
244 {
245   myActiveDialogBox = (QDialog*)aDlg;
246 }
247
248 //=======================================================================
249 // function : GeometryGUI::EmitSignalDeactivateDialog()
250 // purpose  : Emit a signal to deactivate the active dialog Box
251 //=======================================================================
252 void GeometryGUI::EmitSignalDeactivateDialog()
253 {
254   emit SignalDeactivateActiveDialog();
255 }
256
257 //=======================================================================
258 // function : GeometryGUI::EmitSignalCloseAllDialogs()
259 // purpose  : Emit a signal to close all non modal dialogs box
260 //=======================================================================
261 void GeometryGUI::EmitSignalCloseAllDialogs()
262 {
263   emit SignalCloseAllDialogs();
264 }
265
266 //=======================================================================
267 // function : GeometryGUI::EmitSignalDefaultStepValueChanged()
268 // purpose  : Emit a signal to inform that default real spin box step has
269 //            been changed
270 //=======================================================================
271 void GeometryGUI::EmitSignalDefaultStepValueChanged(double newVal)
272 {
273   emit SignalDefaultStepValueChanged(newVal);
274 }
275
276 //=======================================================================
277 // function : GeometryGUI::OnGUIEvent()
278 // purpose  : manage all events on GUI [static]
279 //=======================================================================
280 bool GeometryGUI::OnGUIEvent(int theCommandID, QAD_Desktop* parent)
281 {
282   GeometryGUI* geomGUI = GeometryGUI::GetGeomGUI();
283
284   // get main menu
285   QMenuBar* Mb = parent->getMainMenuBar();
286   // check if current viewframe is of OCC type
287   bool ViewOCC = parent->getActiveStudy()->getActiveStudyFrame()->getTypeView()  == VIEW_OCC;
288   // if current viewframe is not of OCC and not of VTK type - return immediately
289   if( !ViewOCC && parent->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_VTK)
290     return false;
291
292   // disable non-OCC viewframe menu commands
293   //Mb->setItemEnabled( 404, ViewOCC ); // SKETCHER
294   Mb->setItemEnabled( 603, ViewOCC ); // SuppressFace
295   Mb->setItemEnabled( 604, ViewOCC ); // SuppressHole
296   Mb->setItemEnabled( 606, ViewOCC ); // CloseContour
297   Mb->setItemEnabled( 607, ViewOCC ); // RemoveInternalWires
298   Mb->setItemEnabled( 608, ViewOCC ); // AddPointOnEdge
299   //Mb->setItemEnabled( 609, ViewOCC ); // Free boundaries
300   Mb->setItemEnabled( 413, ViewOCC ); // Isos Settings
301
302   Mb->setItemEnabled( 800, ViewOCC ); // Create Group
303   Mb->setItemEnabled( 801, ViewOCC ); // Edit Group
304
305   Mb->setItemEnabled(9998, ViewOCC ); // MENU BLOCKS - MULTI-TRANSFORMATION
306
307   GEOMGUI* library = 0;
308   // try to get-or-load corresponding GUI library
309   if( theCommandID == 111  ||  // MENU FILE - IMPORT BREP
310       theCommandID == 112  ||  // MENU FILE - IMPORT IGES
311       theCommandID == 113  ||  // MENU FILE - IMPORT STEP
312       theCommandID == 121  ||  // MENU FILE - EXPORT BREP
313       theCommandID == 122  ||  // MENU FILE - EXPORT IGES
314       theCommandID == 123  ||  // MENU FILE - EXPORT STEP
315       theCommandID == 31   ||  // MENU EDIT - COPY
316       theCommandID == 33   ||  // MENU EDIT - DELETE
317       theCommandID == 411  ||  // MENU SETTINGS - ADD IN STUDY
318       theCommandID == 412  ||  // MENU SETTINGS - SHADING COLOR
319       theCommandID == 413  ||  // MENU SETTINGS - ISOS
320       theCommandID == 414  ||  // MENU SETTINGS - STEP VALUE FOR SPIN BOXES
321       theCommandID == 5103 ||  // MENU TOOLS - CHECK GEOMETRY
322       theCommandID == 5104 ||  // MENU TOOLS - LOAD SCRIPT
323       theCommandID == 8032 ||  // POPUP VIEWER - COLOR
324       theCommandID == 8033 ||  // POPUP VIEWER - TRANSPARENCY
325       theCommandID == 8034 ||  // POPUP VIEWER - ISOS
326       theCommandID == 804  ||  // POPUP VIEWER - ADD IN STUDY
327       theCommandID == 901  ||  // OBJECT BROWSER - RENAME
328       theCommandID == 9024 ) { // OBJECT BROWSER - OPEN
329     library = geomGUI->getLibrary( "libGEOMToolsGUI.so" );
330   }
331   else if( theCommandID == 211  ||  // MENU VIEW - WIREFRAME/SHADING
332            theCommandID == 212  ||  // MENU VIEW - DISPLAY ALL
333            theCommandID == 213  ||  // MENU VIEW - DISPLAY ONLY
334            theCommandID == 214  ||  // MENU VIEW - ERASE ALL
335            theCommandID == 215  ||  // MENU VIEW - ERASE
336            theCommandID == 216  ||  // MENU VIEW - DISPLAY
337            theCommandID == 80311 ||  // POPUP VIEWER - WIREFRAME
338            theCommandID == 80312 ) { // POPUP VIEWER - SHADING
339     library = geomGUI->getLibrary( "libDisplayGUI.so" );
340   }
341   else if( theCommandID == 4011 ||  // MENU BASIC - POINT
342            theCommandID == 4012 ||  // MENU BASIC - LINE
343            theCommandID == 4013 ||  // MENU BASIC - CIRCLE
344            theCommandID == 4014 ||  // MENU BASIC - ELLIPSE
345            theCommandID == 4015 ||  // MENU BASIC - ARC
346            theCommandID == 4016 ||  // MENU BASIC - VECTOR
347            theCommandID == 4017 ||  // MENU BASIC - PLANE
348            theCommandID == 4018 ||  // MENU BASIC - WPLANE
349            theCommandID == 4019 ||  // MENU BASIC - CURVE
350            theCommandID == 4020 ) { // MENU BASIC - REPAIR
351     library = geomGUI->getLibrary( "libBasicGUI.so" );
352   }
353   else if( theCommandID == 4021 ||  // MENU PRIMITIVE - BOX
354            theCommandID == 4022 ||  // MENU PRIMITIVE - CYLINDER
355            theCommandID == 4023 ||  // MENU PRIMITIVE - SPHERE
356            theCommandID == 4024 ||  // MENU PRIMITIVE - TORUS
357            theCommandID == 4025 ) { // MENU PRIMITIVE - CONE
358     library = geomGUI->getLibrary( "libPrimitiveGUI.so" );
359   }
360   else if( theCommandID == 4031 ||  // MENU GENERATION - PRISM
361            theCommandID == 4032 ||  // MENU GENERATION - REVOLUTION
362            theCommandID == 4033 ||  // MENU GENERATION - FILLING
363            theCommandID == 4034 ) { // MENU GENERATION - PIPE
364     library = geomGUI->getLibrary( "libGenerationGUI.so" );
365   }
366   else if( theCommandID == 404 ||   // MENU ENTITY - SKETCHER
367            theCommandID == 407 ) {  // MENU ENTITY - EXPLODE
368     library = geomGUI->getLibrary( "libEntityGUI.so" );
369   }
370   else if( theCommandID == 4081 ||  // MENU BUILD - EDGE
371            theCommandID == 4082 ||  // MENU BUILD - WIRE
372            theCommandID == 4083 ||  // MENU BUILD - FACE
373            theCommandID == 4084 ||  // MENU BUILD - SHELL
374            theCommandID == 4085 ||  // MENU BUILD - SOLID
375            theCommandID == 4086 ) { // MENU BUILD - COMPUND
376     library = geomGUI->getLibrary( "libBuildGUI.so" );
377   }
378   else if( theCommandID == 5011 ||  // MENU BOOLEAN - FUSE
379            theCommandID == 5012 ||  // MENU BOOLEAN - COMMON
380            theCommandID == 5013 ||  // MENU BOOLEAN - CUT
381            theCommandID == 5014 ) { // MENU BOOLEAN - SECTION
382     library = geomGUI->getLibrary( "libBooleanGUI.so" );
383   }
384   else if( theCommandID == 5021 ||  // MENU TRANSFORMATION - TRANSLATION
385            theCommandID == 5022 ||  // MENU TRANSFORMATION - ROTATION
386            theCommandID == 5023 ||  // MENU TRANSFORMATION - LOCATION
387            theCommandID == 5024 ||  // MENU TRANSFORMATION - MIRROR
388            theCommandID == 5025 ||  // MENU TRANSFORMATION - SCALE
389            theCommandID == 5026 ||  // MENU TRANSFORMATION - OFFSET
390            theCommandID == 5027 ||  // MENU TRANSFORMATION - MULTI-TRANSLATION
391            theCommandID == 5028 ) { // MENU TRANSFORMATION - MULTI-ROTATION
392     library = geomGUI->getLibrary( "libTransformationGUI.so" );
393   }
394   else if( theCommandID == 503 ||   // MENU OPERATION - PARTITION
395            theCommandID == 504 ||   // MENU OPERATION - ARCHIMEDE
396            theCommandID == 505 ||   // MENU OPERATION - FILLET
397            theCommandID == 506 ||   // MENU OPERATION - CHAMFER  
398            theCommandID == 507 ) {  // MENU OPERATION - CLIPPING RANGE
399     library = geomGUI->getLibrary( "libOperationGUI.so" );
400   }
401   else if( theCommandID == 601 ||   // MENU REPAIR - SEWING
402            theCommandID == 603 ||   // MENU REPAIR - SUPPRESS FACES
403            theCommandID == 604 ||   // MENU REPAIR - SUPPRESS HOLE
404            theCommandID == 605 ||   // MENU REPAIR - SHAPE PROCESSING
405            theCommandID == 606 ||   // MENU REPAIR - CLOSE CONTOUR
406            theCommandID == 607 ||   // MENU REPAIR - REMOVE INTERNAL WIRES
407            theCommandID == 608 ||   // MENU REPAIR - ADD POINT ON EDGE
408            theCommandID == 609 ||   // MENU REPAIR - FREE BOUNDARIES
409            theCommandID == 602 ) {  // MENU REPAIR - GLUE FACES
410     library = geomGUI->getLibrary( "libRepairGUI.so" );
411   }
412   else if( theCommandID == 701  ||  // MENU MEASURE - PROPERTIES
413            theCommandID == 702  ||  // MENU MEASURE - CDG
414            theCommandID == 703  ||  // MENU MEASURE - INERTIA
415            theCommandID == 7041 ||  // MENU MEASURE - BOUNDING BOX
416            theCommandID == 7042 ||  // MENU MEASURE - MIN DISTANCE
417            theCommandID == 705  ||  // MENU MEASURE - TOLERANCE
418            theCommandID == 706  ||  // MENU MEASURE - WHATIS
419            theCommandID == 707  ||  // MENU MEASURE - CHECK
420            theCommandID == 708 ) {  // MENU MEASURE - POINT COORDINATES
421     library = geomGUI->getLibrary( "libMeasureGUI.so" );
422   }
423   else if( theCommandID == 800  ||  // MENU GROUP - CREATE
424            theCommandID == 801 ) {  // MENU GROUP - EDIT
425     library = geomGUI->getLibrary( "libGroupGUI.so" );
426   }
427   else if( theCommandID == 9999 ||  // MENU BLOCKS - HEXAHEDRAL SOLID
428            theCommandID == 9998 ||  // MENU BLOCKS - MULTI-TRANSFORMATION
429            theCommandID == 9997 ||  // MENU BLOCKS - QUADRANGLE FACE
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         popup->removeItem( QAD_DisplayOnly_Popup_ID );
728         if ( !isOCCViewer && !isVTKViewer ) {
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 = QApplication::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(9998, ViewOCC ); // MENU BLOCKS - MULTI-TRANSFORMATION
958
959   geomGUI->EmitSignalCloseAllDialogs();
960
961   // PAL5356: update VTK selection
962   ::UpdateVtkSelection( parent );
963
964   bool bOk = true;
965   GUIMap::Iterator it;
966   for ( it = geomGUI->myGUIMap.begin(); it != geomGUI->myGUIMap.end(); ++it )
967     bOk = bOk && it.data()->ActiveStudyChanged( parent );
968   return bOk;
969 }
970
971 //=======================================================================
972 // function : GeometryGUI::BuildPresentation()
973 // purpose  : static
974 //=======================================================================
975 void GeometryGUI::BuildPresentation( const Handle(SALOME_InteractiveObject)& theIO,
976                                      QAD_ViewFrame* theViewFrame )
977 {
978   GEOM_Displayer().Display( theIO, false, theViewFrame );
979 }
980
981 void GeometryGUI::SupportedViewType(int* buffer, int bufferSize)
982 {
983   if(!buffer || !bufferSize) return;
984   buffer[0] = (int)VIEW_OCC;
985   if (--bufferSize) buffer[1] = (int)VIEW_VTK;
986 }