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