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