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