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