Salome HOME
Fix for Bug IPAL9053( 3.0.0: "Check Geometry" and "Load script" functionalities from...
[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.org 
21 //
22 //            
23 //
24 //  File   : GEOMToolsGUI_1.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include <PythonConsole_PyConsole.h>
30
31 #include "GEOMToolsGUI.h"
32 #include "GeometryGUI.h"
33 #include "GEOM_Actor.h"
34 #include "GEOMBase.h"
35 #include "GEOMToolsGUI_TransparencyDlg.h"
36 #include "GEOMToolsGUI_NbIsosDlg.h"        // Method ISOS adjustement
37 #include "GEOMToolsGUI_NameDlg.h"
38
39 #include <SALOME_ListIO.hxx>
40 #include <SALOME_ListIteratorOfListIO.hxx>
41
42 #include <VTKViewer_ViewModel.h>
43 #include <OCCViewer_ViewModel.h>
44 #include <OCCViewer_ViewWindow.h>
45
46 #include <SVTK_ViewWindow.h>
47 #include <SVTK_RenderWindowInteractor.h>
48
49 #include <SUIT_ViewManager.h>
50 #include <SUIT_Application.h>
51 #include <SUIT_Desktop.h>
52 #include <SUIT_ResourceMgr.h>
53 #include <SUIT_Session.h>
54 #include <SUIT_OverrideCursor.h>
55 #include <SUIT_MessageBox.h>
56
57 #include <SalomeApp_Application.h>
58 #include <SalomeApp_SelectionMgr.h>
59 #include <SalomeApp_Study.h>
60 #include <SalomeApp_Module.h>
61
62 #include <AIS_Drawer.hxx>
63 #include <AIS_ListOfInteractive.hxx>
64 #include <AIS_ListIteratorOfListOfInteractive.hxx>
65 #include <Prs3d_IsoAspect.hxx>
66
67 #include <vtkBMPReader.h>
68 #include <vtkTexture.h>
69 #include <vtkTextureMapToPlane.h>
70 #include <vtkTransformTextureCoords.h>
71 #include <vtkDataSetMapper.h>
72 #include <vtkRenderer.h>
73
74 #include <qfileinfo.h>
75 #include <qcolordialog.h>
76 #include <qspinbox.h>
77 #include <qapplication.h>
78
79 #include "utilities.h"
80
81 using namespace std;
82
83 #include "SALOMEDSClient.hxx"
84 #include "SALOMEDS_Study.hxx"
85
86
87
88 void GEOMToolsGUI::OnSettingsColor()
89 {
90   SUIT_Session* sess = SUIT_Session::session();
91   SUIT_ResourceMgr* resMgr = sess->resourceMgr();
92   SUIT_Desktop* desk = sess->activeApplication()->desktop();
93
94   QColor anInitColor = resMgr->colorValue( "Geometry:SettingsShadingColor", QColor( "yellow" ) );
95   
96   QColor aDialogColor = QColorDialog::getColor(anInitColor, desk );
97   if( aDialogColor.isValid() ) 
98   {
99     QString type = desk->activeWindow()->getViewManager()->getType();
100     if( type != OCCViewer_Viewer::Type() && type != VTKViewer_Viewer::Type() )
101       MESSAGE("Settings Color is not supported for current Viewer");
102     
103     resMgr->setValue( "Geometry:SettingsShadingColor", aDialogColor );
104   }
105 }
106
107 void GEOMToolsGUI::OnSettingsIsos()
108 {
109 /*
110   SUIT_Session* sess = SUIT_Session::session();
111   SUIT_ResourceMgr* resMgr = sess->resourceMgr();
112   SUIT_Desktop* desk = sess->activeApplication()->desktop();
113
114   SUIT_ViewManager* vman = desk->activeWindow()->getViewManager();
115   QString type = vman->getType();
116
117   if ( type != OCCViewer_Viewer::Type() )
118     return;
119   
120   OCCViewer_Viewer* vm = (OCCViewer_Viewer*)vman->getViewModel();
121   Handle (AIS_InteractiveContext) ic = vm->getAISContext();
122   
123   int IsoU = resMgr->integerValue( "Geometry:SettingsIsoU", 1 );
124   int IsoV = resMgr->integerValue( "Geometry:SettingsIsoV", 1 );
125   
126   ic->DefaultDrawer()->UIsoAspect()->SetNumber( IsoU );
127   ic->DefaultDrawer()->VIsoAspect()->SetNumber( IsoV );
128   
129   GEOMBase_NbIsosDlg* NbIsosDlg = new GEOMBase_NbIsosDlg(desk, tr("GEOM_MEN_ISOS"), TRUE);      
130   
131   NbIsosDlg->SpinBoxU->setValue(IsoU);
132   NbIsosDlg->SpinBoxV->setValue(IsoV);
133   
134   if(NbIsosDlg->exec()) {
135     IsoU = NbIsosDlg->SpinBoxU->text().toInt();
136     IsoV = NbIsosDlg->SpinBoxV->text().toInt();
137     
138     ic->DefaultDrawer()->UIsoAspect()->SetNumber(UIso);
139     ic->DefaultDrawer()->VIsoAspect()->SetNumber(VIso);
140     resMgr->setValue("Geometry:SettingsIsoU", isoU);
141     resMgr->setValue("Geometry:SettingsIsoV", isoV);
142   }
143 */
144 }
145
146 void GEOMToolsGUI::OnSettingsStep()
147 {
148   SUIT_Session* sess = SUIT_Session::session();
149   SUIT_ResourceMgr* resMgr = sess->resourceMgr();
150
151   double step = resMgr->doubleValue( "Geometry:SettingsGeomStep", 100. );
152   
153   Standard_Boolean res = false;
154   double dd = GEOMBase::Parameter(res, QString("%1").arg(step), tr("GEOM_MEN_STEP_LABEL"), tr("GEOM_STEP_TITLE"), 0.001, 10000.0, 3);
155   if(res) {
156     resMgr->setValue( "Geometry:SettingsGeomStep", dd );
157     
158     /* Emit signal to GeometryGUI_SpinBoxes */
159     getGeometryGUI()->EmitSignalDefaultStepValueChanged( dd );
160   }
161   else
162     sess->activeApplication()->putInfo(tr("GEOM_PRP_ABORT"));
163 }
164
165 void GEOMToolsGUI::OnRename()
166 {
167   SALOME_ListIO selected;
168   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
169   if ( app ) {
170     SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
171     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
172     if ( aSelMgr && appStudy ) {
173       aSelMgr->selectedObjects( selected );
174       if ( !selected.IsEmpty() ) {
175         _PTR(Study) aStudy = appStudy->studyDS();
176
177         bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
178         if ( aLocked ) {
179           SUIT_MessageBox::warn1 ( app->desktop(),
180                                    QObject::tr("WRN_WARNING"), 
181                                    QObject::tr("WRN_STUDY_LOCKED"),
182                                    QObject::tr("BUT_OK") );
183           return;
184         }
185   
186         for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
187           Handle(SALOME_InteractiveObject) IObject = It.Value();
188     
189           _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
190           _PTR(GenericAttribute) anAttr;
191           if ( obj ) {
192             if( obj->FindAttribute(anAttr, "AttributeName") ) {
193               _PTR(AttributeName) aName (anAttr);
194         
195               QString newName = GEOMToolsGUI_NameDlg::getName( app->desktop(), aName->Value().c_str() );
196               if ( !newName.isEmpty() ) {
197                 aName->SetValue( newName.latin1() ); // rename the SObject
198                 IObject->setName( newName.latin1() );// rename the InteractiveObject
199                 (dynamic_cast<SalomeApp_Module*>(app->activeModule()))->updateObjBrowser( false );
200               }
201             } // if ( name attribute )
202           } // if ( obj )
203         } // iterator
204       }
205     }
206   }
207 }
208
209 void GEOMToolsGUI::OnCheckGeometry()
210 {
211   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
212   PythonConsole* pyConsole = app->pythonConsole();
213   
214   if(pyConsole)
215     pyConsole->exec("from GEOM_usinggeom import *");
216 }
217
218 void GEOMToolsGUI::OnColor()
219 {
220   SALOME_ListIO selected;
221   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
222   if ( app ) {
223     SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
224     if ( aSelMgr ) {
225       aSelMgr->selectedObjects( selected );
226       if ( !selected.IsEmpty() ) {
227         SUIT_ViewWindow* window = app->desktop()->activeWindow();
228         bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
229         bool isVTK = ( window && window->getViewManager()->getType() == VTKViewer_Viewer::Type() );
230         if ( isVTK ) {
231           SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
232           if ( !vtkVW )
233             return;
234           SVTK_RenderWindowInteractor* rwi = vtkVW->getRWInteractor();
235           QColor initcolor = rwi->GetColor( selected.First()  );
236           QColor c = QColorDialog::getColor( QColor(), app->desktop() );
237           if ( c.isValid() ) {
238             SUIT_OverrideCursor();
239             for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
240               rwi->SetColor( It.Value(), c );
241             }
242           }
243         } // if ( isVTK )
244         else if ( isOCC ) {
245           Handle(AIS_InteractiveObject) io = GEOMBase::GetAIS( selected.First() );
246           if ( !io.IsNull() ) {
247             Quantity_Color aColor;
248             io->Color( aColor );
249             QColor initcolor( (int)( aColor.Red() * 255.0 ), (int)( aColor.Green() * 255.0 ), (int)( aColor.Blue() * 255.0 ) );
250             QColor c =  QColorDialog::getColor( initcolor, app->desktop() );
251             if ( c.isValid() ) {
252               SUIT_OverrideCursor();
253               aColor = Quantity_Color( c.red() / 255., c.green() / 255., c.blue() / 255., Quantity_TOC_RGB );
254               for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
255                 io = GEOMBase::GetAIS( It.Value(), true );
256                 if ( !io.IsNull() ) {
257                   io->SetColor( aColor );
258                   if ( io->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
259                     Handle(GEOM_AISShape)::DownCast( io )->SetShadingColor( aColor );
260
261                   io->Redisplay( Standard_True );
262                 }
263               }
264             } // if c.isValid()
265           } // first IO is not null
266         } // if ( isOCC )
267       } // if ( selection not empty )
268     }
269   }
270 }
271
272 void GEOMToolsGUI::OnTransparency()
273 {
274   GEOMToolsGUI_TransparencyDlg dlg( SUIT_Session::session()->activeApplication()->desktop() );
275   dlg.exec();
276 }
277
278 void GEOMToolsGUI::OnNbIsos()
279 {
280   SUIT_ViewWindow* window = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
281   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
282
283   if ( !isOCC )
284     return;
285   
286   OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
287   Handle (AIS_InteractiveContext) ic = vm->getAISContext();
288   
289   ic->InitCurrent();
290   if ( ic->MoreCurrent() ) {
291     Handle(GEOM_AISShape) CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
292     Handle(AIS_Drawer)    CurDrawer = CurObject->Attributes();
293     
294     int UIso = CurDrawer->UIsoAspect()->Number();
295     int VIso = CurDrawer->VIsoAspect()->Number();
296     
297     GEOMToolsGUI_NbIsosDlg * NbIsosDlg =
298       new GEOMToolsGUI_NbIsosDlg( SUIT_Session::session()->activeApplication()->desktop() );
299     
300     NbIsosDlg->setU( UIso );
301     NbIsosDlg->setV( VIso );
302     
303     if ( NbIsosDlg->exec() ) {
304       SUIT_OverrideCursor();
305       for(; ic->MoreCurrent(); ic->NextCurrent()) {
306         CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
307         Handle(AIS_Drawer) CurDrawer = CurObject->Attributes();
308         
309         int nbUIso = NbIsosDlg->getU();
310         int nbVIso = NbIsosDlg->getV();
311         
312         CurDrawer->SetUIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbUIso) );
313         CurDrawer->SetVIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbVIso) );
314         
315         ic->SetLocalAttributes(CurObject, CurDrawer);
316         ic->Redisplay(CurObject);
317       }
318     }
319   }
320 }
321
322 void GEOMToolsGUI::OnOpen()
323 {
324 /*
325   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
326   _PTR(Study) aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
327
328   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
329   Handle(SALOME_InteractiveObject) anIObject;
330   for(;It.More();It.Next()) {
331     anIObject = It.Value();
332     _PTR(SObject) obj ( aStudy->FindObjectID(anIObject->getEntry()) );
333     _PTR(AttributePersistentRef) aPersist;
334     _PTR(AttributeIOR) anIOR;
335     if(obj) {
336       // this SObject may be GEOM module root SObject
337       _PTR(ChildIterator) anIter ( aStudy->NewChildIterator() );
338       _PTR(GenericAttribute) anAttr;
339       bool useSubItems = false;
340       while (anIter->More() && !useSubItems) {
341         _PTR(SObject) subobj ( anIter->Value() );
342         if (subobj->FindAttribute(anAttr, "AttributePersistentRef")) {
343           useSubItems = true;
344           obj =  subobj;
345         }
346         else 
347           anIter->Next();
348       }
349       obj->FindAttribute(anAttr, "AttributePersistentRef");
350               
351       while(useSubItems?anIter->More():!anAttr->_is_nil()) { 
352         if(!obj->FindAttribute(anAttr, "AttributeIOR") &&
353            obj->FindAttribute(anAttr, "AttributePersistentRef")) {
354           _PTR(SComponent) FComp ( obj->GetFatherComponent() );
355           if (FComp) {
356             if (FComp->FindAttribute(anAttr, "AttributeName")) {
357               _PTR(AttributeName) aName ( anAttr );
358               QString compName = QAD_Application::getDesktop()->getComponentName(aName->Value().c_str());
359               //                    parent->loadComponentData(parent->getComponentName(aName->Value()));
360               Engines::Component_var comp ;
361               if ( compName.compare("SUPERV") == 0 ) {
362                 comp = QAD_Application::getDesktop()->getEngine( "SuperVisionContainer", compName) ;
363               }
364               else {
365                 comp = QAD_Application::getDesktop()->getEngine( "FactoryServer", compName);
366                 if ( comp->_is_nil() )
367                   comp = QAD_Application::getDesktop()->getEngine( "FactoryServerPy", compName);
368               }
369
370               if (!CORBA::is_nil(comp)) {
371                 SALOMEDS::Driver_var   driver = SALOMEDS::Driver::_narrow(comp);
372                 if (!CORBA::is_nil(driver)) {
373                   SALOMEDS::StudyBuilder_var  B = dynamic_cast<SALOMEDS_Study*>(aStudy.get())->GetStudy()->NewBuilder();
374                   if (!CORBA::is_nil(B)) {
375                     B->LoadWith(FComp,driver);
376                   } else {
377                     return;
378                   }
379                 } 
380                 else {
381                   MESSAGE("loadComponentData(): Driver is null");
382                   return;
383                 }
384               }
385               else {
386                 MESSAGE("loadComponentData(): Engine is null");
387                 return;
388               }
389                 //              // load
390                 //              Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer","GEOM");
391                 //              if (!CORBA::is_nil(comp)) {
392                 //                SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(comp);
393                 //                SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
394                 //                SALOMEDS::SComponent_var SC = aStudy->FindComponent("GEOM");
395                 //                if (!CORBA::is_nil(SC))
396                 //                  aStudyBuilder->LoadWith(SC,driver);
397             } 
398           }
399           else {
400             MESSAGE("Component is null");
401           }
402         }
403         if(useSubItems) {
404           anIter->Next();
405           obj.reset( anIter->Value() );
406         } 
407         else 
408           anAttr = NULL;
409       }
410     }
411   }
412 */
413 }