Salome HOME
*** empty log message ***
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI_1.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2004  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : GEOMToolsGUI_1.cxx
23 // Author : Sergey ANIKIN, Open CASCADE S.A.S. (sergey.anikin@opencascade.com)
24 //
25
26 #include <PyConsole_Console.h>
27
28 #include "GEOMToolsGUI.h"
29
30 #include <GeometryGUI.h>
31 #include <GEOM_Displayer.h>
32 #include "GEOMToolsGUI_TransparencyDlg.h"
33 #include "GEOMToolsGUI_NbIsosDlg.h"        // Method ISOS adjustement
34
35 #include <GEOMBase.h>
36
37 #include <SALOME_ListIO.hxx>
38 #include <SALOME_ListIteratorOfListIO.hxx>
39
40 #include <SVTK_ViewModel.h>
41 #include <SVTK_ViewWindow.h>
42 #include <SVTK_View.h>
43
44 #include <OCCViewer_ViewModel.h>
45
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_Desktop.h>
48 #include <SUIT_ResourceMgr.h>
49 #include <SUIT_Session.h>
50 #include <SUIT_OverrideCursor.h>
51 #include <SUIT_MessageBox.h>
52
53 #include <SalomeApp_Application.h>
54 #include <SalomeApp_Study.h>
55 #include <SalomeApp_Module.h>
56
57 #include <LightApp_SelectionMgr.h>
58 #include <LightApp_NameDlg.h>
59
60 #include "utilities.h"
61
62 // OCCT Includes
63 #include <AIS_Drawer.hxx>
64 #include <Prs3d_IsoAspect.hxx>
65 #include <Prs3d_PointAspect.hxx>
66 #include <Graphic3d_AspectMarker3d.hxx>
67
68 // QT Includes
69 #include <QColorDialog>
70
71 using namespace std;
72
73
74 void GEOMToolsGUI::OnSettingsColor()
75 {
76   SUIT_Session* sess = SUIT_Session::session();
77   SUIT_ResourceMgr* resMgr = sess->resourceMgr();
78   SUIT_Desktop* desk = sess->activeApplication()->desktop();
79
80   QColor anInitColor = resMgr->colorValue( "Geometry", "SettingsShadingColor", QColor( "yellow" ) );
81
82   QColor aDialogColor = QColorDialog::getColor(anInitColor, desk );
83   if( aDialogColor.isValid() )
84   {
85     QString type = desk->activeWindow()->getViewManager()->getType();
86     if( type != OCCViewer_Viewer::Type() && type != SVTK_Viewer::Type() )
87       MESSAGE("Settings Color is not supported for current Viewer");
88
89     resMgr->setValue( "Geometry", "SettingsShadingColor", aDialogColor );
90   }
91 }
92
93 void GEOMToolsGUI::OnRename()
94 {
95   SALOME_ListIO selected;
96   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
97   if ( app ) {
98     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
99     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
100     if ( aSelMgr && appStudy ) {
101       aSelMgr->selectedObjects( selected );
102       if ( !selected.IsEmpty() ) {
103         _PTR(Study) aStudy = appStudy->studyDS();
104
105         bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
106         if ( aLocked ) {
107           SUIT_MessageBox::warning ( app->desktop(),
108                                      QObject::tr("WRN_WARNING"),
109                                      QObject::tr("WRN_STUDY_LOCKED"),
110                                      QObject::tr("BUT_OK") );
111           return;
112         }
113
114         for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
115           Handle(SALOME_InteractiveObject) IObject = It.Value();
116
117           _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
118           _PTR(GenericAttribute) anAttr;
119           if ( obj ) {
120             if( obj->FindAttribute(anAttr, "AttributeName") ) {
121               _PTR(AttributeName) aName (anAttr);
122
123               QString newName = LightApp_NameDlg::getName( app->desktop(), aName->Value().c_str() );
124               if ( !newName.isEmpty() ) {
125                 aName->SetValue( newName.toLatin1().constData() ); // rename the SObject
126                 IObject->setName( newName.toLatin1() );// rename the InteractiveObject
127                 // Rename the corresponding GEOM_Object
128                 GEOM::GEOM_Object_var anObj =  GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(obj));
129                 if (!CORBA::is_nil( anObj ))
130                   anObj->SetName( newName.toLatin1() );
131                 (dynamic_cast<SalomeApp_Module*>(app->activeModule()))->updateObjBrowser( false );
132               }
133             } // if ( name attribute )
134           } // if ( obj )
135         } // iterator
136       }
137     }
138   }
139   
140   app->updateActions(); //SRN: To update a Save button in the toolbar
141 }
142
143 void GEOMToolsGUI::OnCheckGeometry()
144 {
145   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
146   PyConsole_Console* pyConsole = app->pythonConsole();
147
148   if(pyConsole)
149     pyConsole->exec("from GEOM_usinggeom import *");
150 }
151
152 void GEOMToolsGUI::OnColor()
153 {
154   SALOME_ListIO selected;
155   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
156   if ( app ) {
157     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
158     if ( aSelMgr ) {
159       aSelMgr->selectedObjects( selected );
160       if ( !selected.IsEmpty() ) {
161         SUIT_ViewWindow* window = app->desktop()->activeWindow();
162         bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
163         bool isVTK = ( window && window->getViewManager()->getType() == SVTK_Viewer::Type() );
164         if ( isVTK ) {
165           SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
166           if ( !vtkVW )
167             return;
168           SVTK_View* aView = vtkVW->getView();
169           QColor initcolor = aView->GetColor( selected.First()  );
170           QColor c = QColorDialog::getColor( QColor(), app->desktop() );
171           if ( c.isValid() ) {
172             SUIT_OverrideCursor();
173             for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
174               aView->SetColor( It.Value(), c );
175             }
176           }
177         } // if ( isVTK )
178         else if ( isOCC ) {
179           Handle(AIS_InteractiveObject) io = GEOMBase::GetAIS( selected.First() );
180           if ( !io.IsNull() ) {
181             Quantity_Color aColor;
182             io->Color( aColor );
183             QColor initcolor( (int)( aColor.Red() * 255.0 ), (int)( aColor.Green() * 255.0 ), (int)( aColor.Blue() * 255.0 ) );
184             QColor c =  QColorDialog::getColor( initcolor, app->desktop() );
185             if ( c.isValid() ) {
186               SUIT_OverrideCursor();
187               aColor = Quantity_Color( c.red() / 255., c.green() / 255., c.blue() / 255., Quantity_TOC_RGB );
188               for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
189                 io = GEOMBase::GetAIS( It.Value(), true );
190                 if ( !io.IsNull() ) {
191                   // Set color for a point
192                   OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
193                   Handle (AIS_InteractiveContext) ic = vm->getAISContext();
194                   Handle(AIS_Drawer) aCurDrawer = io->Attributes();
195                   Handle(Prs3d_PointAspect) aCurPointAspect =  aCurDrawer->PointAspect();
196                   Quantity_Color aCurColor;
197                   Standard_Real aCurScale;
198                   Aspect_TypeOfMarker aCurTypeOfMarker;
199                   aCurPointAspect->Aspect()->Values( aCurColor, aCurTypeOfMarker, aCurScale );
200                   aCurDrawer->SetPointAspect( new Prs3d_PointAspect( aCurTypeOfMarker, aColor, aCurScale) );
201                   ic->SetLocalAttributes(io, aCurDrawer);
202                   
203                   io->SetColor( aColor );
204                   if ( io->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
205                     Handle(GEOM_AISShape)::DownCast( io )->SetShadingColor( aColor );
206
207                   io->Redisplay( Standard_True );
208                 }
209               }
210             } // if c.isValid()
211           } // first IO is not null
212         } // if ( isOCC )
213       } // if ( selection not empty )
214     }
215   }
216   
217   app->updateActions(); //SRN: To update a Save button in the toolbar
218 }
219
220 void GEOMToolsGUI::OnTransparency()
221 {
222   GEOMToolsGUI_TransparencyDlg dlg( SUIT_Session::session()->activeApplication()->desktop() );
223   dlg.exec();
224 }
225
226 void GEOMToolsGUI::OnNbIsos()
227 {
228   SUIT_ViewWindow* window = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
229   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
230
231   if ( !isOCC )
232     return;
233
234   OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
235   Handle (AIS_InteractiveContext) ic = vm->getAISContext();
236
237   ic->InitCurrent();
238   if ( ic->MoreCurrent() ) {
239     Handle(GEOM_AISShape) CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
240     Handle(AIS_Drawer)    CurDrawer = CurObject->Attributes();
241
242     int UIso = CurDrawer->UIsoAspect()->Number();
243     int VIso = CurDrawer->VIsoAspect()->Number();
244
245     GEOMToolsGUI_NbIsosDlg * NbIsosDlg =
246       new GEOMToolsGUI_NbIsosDlg( SUIT_Session::session()->activeApplication()->desktop() );
247
248     NbIsosDlg->setU( UIso );
249     NbIsosDlg->setV( VIso );
250
251     if ( NbIsosDlg->exec() ) {
252       SUIT_OverrideCursor();
253       for(; ic->MoreCurrent(); ic->NextCurrent()) {
254         CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
255         Handle(AIS_Drawer) CurDrawer = CurObject->Attributes();
256
257         int nbUIso = NbIsosDlg->getU();
258         int nbVIso = NbIsosDlg->getV();
259
260         CurDrawer->SetUIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbUIso) );
261         CurDrawer->SetVIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbVIso) );
262
263         ic->SetLocalAttributes(CurObject, CurDrawer);
264         ic->Redisplay(CurObject);
265       }
266     }
267   }
268 }
269
270 void GEOMToolsGUI::OnOpen()
271 {
272 /*
273   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
274   _PTR(Study) aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
275
276   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
277   Handle(SALOME_InteractiveObject) anIObject;
278   for(;It.More();It.Next()) {
279     anIObject = It.Value();
280     _PTR(SObject) obj ( aStudy->FindObjectID(anIObject->getEntry()) );
281     _PTR(AttributePersistentRef) aPersist;
282     _PTR(AttributeIOR) anIOR;
283     if(obj) {
284       // this SObject may be GEOM module root SObject
285       _PTR(ChildIterator) anIter ( aStudy->NewChildIterator() );
286       _PTR(GenericAttribute) anAttr;
287       bool useSubItems = false;
288       while (anIter->More() && !useSubItems) {
289         _PTR(SObject) subobj ( anIter->Value() );
290         if (subobj->FindAttribute(anAttr, "AttributePersistentRef")) {
291           useSubItems = true;
292           obj =  subobj;
293         }
294         else
295           anIter->Next();
296       }
297       obj->FindAttribute(anAttr, "AttributePersistentRef");
298
299       while(useSubItems?anIter->More():!anAttr->_is_nil()) {
300         if(!obj->FindAttribute(anAttr, "AttributeIOR") &&
301            obj->FindAttribute(anAttr, "AttributePersistentRef")) {
302           _PTR(SComponent) FComp ( obj->GetFatherComponent() );
303           if (FComp) {
304             if (FComp->FindAttribute(anAttr, "AttributeName")) {
305               _PTR(AttributeName) aName ( anAttr );
306               QString compName = QAD_Application::getDesktop()->getComponentName(aName->Value().c_str());
307               //                    parent->loadComponentData(parent->getComponentName(aName->Value()));
308               Engines::Component_var comp ;
309               if ( compName.compare("SUPERV") == 0 ) {
310                 comp = QAD_Application::getDesktop()->getEngine( "SuperVisionContainer", compName) ;
311               }
312               else {
313                 comp = QAD_Application::getDesktop()->getEngine( "FactoryServer", compName);
314                 if ( comp->_is_nil() )
315                   comp = QAD_Application::getDesktop()->getEngine( "FactoryServerPy", compName);
316               }
317
318               if (!CORBA::is_nil(comp)) {
319                 SALOMEDS::Driver_var   driver = SALOMEDS::Driver::_narrow(comp);
320                 if (!CORBA::is_nil(driver)) {
321                   SALOMEDS::StudyBuilder_var  B = dynamic_cast<SALOMEDS_Study*>(aStudy.get())->GetStudy()->NewBuilder();
322                   if (!CORBA::is_nil(B)) {
323                     B->LoadWith(FComp,driver);
324                   } else {
325                     return;
326                   }
327                 }
328                 else {
329                   MESSAGE("loadComponentData(): Driver is null");
330                   return;
331                 }
332               }
333               else {
334                 MESSAGE("loadComponentData(): Engine is null");
335                 return;
336               }
337                 //              // load
338                 //              Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer","GEOM");
339                 //              if (!CORBA::is_nil(comp)) {
340                 //                SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(comp);
341                 //                SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
342                 //                SALOMEDS::SComponent_var SC = aStudy->FindComponent("GEOM");
343                 //                if (!CORBA::is_nil(SC))
344                 //                  aStudyBuilder->LoadWith(SC,driver);
345             }
346           }
347           else {
348             MESSAGE("Component is null");
349           }
350         }
351         if(useSubItems) {
352           anIter->Next();
353           obj.reset( anIter->Value() );
354         }
355         else
356           anAttr = NULL;
357       }
358     }
359   }
360 */
361 }
362
363 void GEOMToolsGUI::OnSelectOnly(int mode)
364 {
365   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
366   if ( app ) {
367     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
368     GEOM_Displayer aDisp (appStudy);
369     aDisp.GlobalSelection(mode);
370     getGeometryGUI()->setLocalSelectionMode(mode);
371   }
372 }