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