Salome HOME
Added an update of the toolbar after the Rename operation.
[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   app->updateActions(); //SRN: To update a Save button in the toolbar
212 }
213
214 void GEOMToolsGUI::OnCheckGeometry()
215 {
216   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
217   PythonConsole* pyConsole = app->pythonConsole();
218
219   if(pyConsole)
220     pyConsole->exec("from GEOM_usinggeom import *");
221 }
222
223 void GEOMToolsGUI::OnColor()
224 {
225   SALOME_ListIO selected;
226   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
227   if ( app ) {
228     SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
229     if ( aSelMgr ) {
230       aSelMgr->selectedObjects( selected );
231       if ( !selected.IsEmpty() ) {
232         SUIT_ViewWindow* window = app->desktop()->activeWindow();
233         bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
234         bool isVTK = ( window && window->getViewManager()->getType() == VTKViewer_Viewer::Type() );
235         if ( isVTK ) {
236           SVTK_ViewWindow* vtkVW = dynamic_cast<SVTK_ViewWindow*>( window );
237           if ( !vtkVW )
238             return;
239           SVTK_RenderWindowInteractor* rwi = vtkVW->getRWInteractor();
240           QColor initcolor = rwi->GetColor( selected.First()  );
241           QColor c = QColorDialog::getColor( QColor(), app->desktop() );
242           if ( c.isValid() ) {
243             SUIT_OverrideCursor();
244             for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
245               rwi->SetColor( It.Value(), c );
246             }
247           }
248         } // if ( isVTK )
249         else if ( isOCC ) {
250           Handle(AIS_InteractiveObject) io = GEOMBase::GetAIS( selected.First() );
251           if ( !io.IsNull() ) {
252             Quantity_Color aColor;
253             io->Color( aColor );
254             QColor initcolor( (int)( aColor.Red() * 255.0 ), (int)( aColor.Green() * 255.0 ), (int)( aColor.Blue() * 255.0 ) );
255             QColor c =  QColorDialog::getColor( initcolor, app->desktop() );
256             if ( c.isValid() ) {
257               SUIT_OverrideCursor();
258               aColor = Quantity_Color( c.red() / 255., c.green() / 255., c.blue() / 255., Quantity_TOC_RGB );
259               for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
260                 io = GEOMBase::GetAIS( It.Value(), true );
261                 if ( !io.IsNull() ) {
262                   io->SetColor( aColor );
263                   if ( io->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
264                     Handle(GEOM_AISShape)::DownCast( io )->SetShadingColor( aColor );
265
266                   io->Redisplay( Standard_True );
267                 }
268               }
269             } // if c.isValid()
270           } // first IO is not null
271         } // if ( isOCC )
272       } // if ( selection not empty )
273     }
274   }
275   
276   app->updateActions(); //SRN: To update a Save button in the toolbar
277 }
278
279 void GEOMToolsGUI::OnTransparency()
280 {
281   GEOMToolsGUI_TransparencyDlg dlg( SUIT_Session::session()->activeApplication()->desktop() );
282   dlg.exec();
283 }
284
285 void GEOMToolsGUI::OnNbIsos()
286 {
287   SUIT_ViewWindow* window = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
288   bool isOCC = ( window && window->getViewManager()->getType() == OCCViewer_Viewer::Type() );
289
290   if ( !isOCC )
291     return;
292
293   OCCViewer_Viewer* vm = dynamic_cast<OCCViewer_Viewer*>( window->getViewManager()->getViewModel() );
294   Handle (AIS_InteractiveContext) ic = vm->getAISContext();
295
296   ic->InitCurrent();
297   if ( ic->MoreCurrent() ) {
298     Handle(GEOM_AISShape) CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
299     Handle(AIS_Drawer)    CurDrawer = CurObject->Attributes();
300
301     int UIso = CurDrawer->UIsoAspect()->Number();
302     int VIso = CurDrawer->VIsoAspect()->Number();
303
304     GEOMToolsGUI_NbIsosDlg * NbIsosDlg =
305       new GEOMToolsGUI_NbIsosDlg( SUIT_Session::session()->activeApplication()->desktop() );
306
307     NbIsosDlg->setU( UIso );
308     NbIsosDlg->setV( VIso );
309
310     if ( NbIsosDlg->exec() ) {
311       SUIT_OverrideCursor();
312       for(; ic->MoreCurrent(); ic->NextCurrent()) {
313         CurObject = Handle(GEOM_AISShape)::DownCast(ic->Current());
314         Handle(AIS_Drawer) CurDrawer = CurObject->Attributes();
315
316         int nbUIso = NbIsosDlg->getU();
317         int nbVIso = NbIsosDlg->getV();
318
319         CurDrawer->SetUIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbUIso) );
320         CurDrawer->SetVIsoAspect( new Prs3d_IsoAspect(Quantity_NOC_GRAY75, Aspect_TOL_SOLID, 0.5 , nbVIso) );
321
322         ic->SetLocalAttributes(CurObject, CurDrawer);
323         ic->Redisplay(CurObject);
324       }
325     }
326   }
327 }
328
329 void GEOMToolsGUI::OnOpen()
330 {
331 /*
332   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
333   _PTR(Study) aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
334
335   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
336   Handle(SALOME_InteractiveObject) anIObject;
337   for(;It.More();It.Next()) {
338     anIObject = It.Value();
339     _PTR(SObject) obj ( aStudy->FindObjectID(anIObject->getEntry()) );
340     _PTR(AttributePersistentRef) aPersist;
341     _PTR(AttributeIOR) anIOR;
342     if(obj) {
343       // this SObject may be GEOM module root SObject
344       _PTR(ChildIterator) anIter ( aStudy->NewChildIterator() );
345       _PTR(GenericAttribute) anAttr;
346       bool useSubItems = false;
347       while (anIter->More() && !useSubItems) {
348         _PTR(SObject) subobj ( anIter->Value() );
349         if (subobj->FindAttribute(anAttr, "AttributePersistentRef")) {
350           useSubItems = true;
351           obj =  subobj;
352         }
353         else
354           anIter->Next();
355       }
356       obj->FindAttribute(anAttr, "AttributePersistentRef");
357
358       while(useSubItems?anIter->More():!anAttr->_is_nil()) {
359         if(!obj->FindAttribute(anAttr, "AttributeIOR") &&
360            obj->FindAttribute(anAttr, "AttributePersistentRef")) {
361           _PTR(SComponent) FComp ( obj->GetFatherComponent() );
362           if (FComp) {
363             if (FComp->FindAttribute(anAttr, "AttributeName")) {
364               _PTR(AttributeName) aName ( anAttr );
365               QString compName = QAD_Application::getDesktop()->getComponentName(aName->Value().c_str());
366               //                    parent->loadComponentData(parent->getComponentName(aName->Value()));
367               Engines::Component_var comp ;
368               if ( compName.compare("SUPERV") == 0 ) {
369                 comp = QAD_Application::getDesktop()->getEngine( "SuperVisionContainer", compName) ;
370               }
371               else {
372                 comp = QAD_Application::getDesktop()->getEngine( "FactoryServer", compName);
373                 if ( comp->_is_nil() )
374                   comp = QAD_Application::getDesktop()->getEngine( "FactoryServerPy", compName);
375               }
376
377               if (!CORBA::is_nil(comp)) {
378                 SALOMEDS::Driver_var   driver = SALOMEDS::Driver::_narrow(comp);
379                 if (!CORBA::is_nil(driver)) {
380                   SALOMEDS::StudyBuilder_var  B = dynamic_cast<SALOMEDS_Study*>(aStudy.get())->GetStudy()->NewBuilder();
381                   if (!CORBA::is_nil(B)) {
382                     B->LoadWith(FComp,driver);
383                   } else {
384                     return;
385                   }
386                 }
387                 else {
388                   MESSAGE("loadComponentData(): Driver is null");
389                   return;
390                 }
391               }
392               else {
393                 MESSAGE("loadComponentData(): Engine is null");
394                 return;
395               }
396                 //              // load
397                 //              Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer","GEOM");
398                 //              if (!CORBA::is_nil(comp)) {
399                 //                SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(comp);
400                 //                SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
401                 //                SALOMEDS::SComponent_var SC = aStudy->FindComponent("GEOM");
402                 //                if (!CORBA::is_nil(SC))
403                 //                  aStudyBuilder->LoadWith(SC,driver);
404             }
405           }
406           else {
407             MESSAGE("Component is null");
408           }
409         }
410         if(useSubItems) {
411           anIter->Next();
412           obj.reset( anIter->Value() );
413         }
414         else
415           anAttr = NULL;
416       }
417     }
418   }
419 */
420 }