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