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