Salome HOME
Windows porting
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : GEOMBase_Tools.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOMToolsGUI.h"
30
31 #include "GeometryGUI.h"
32 #include "GEOM_Actor.h"
33 #include "GEOMBase.h"
34 #include "GEOM_Operation.h"
35 #include "GEOM_Displayer.h"
36
37 #include <SUIT_Session.h>
38 #include <SUIT_Application.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Tools.h>
42 #include <SUIT_FileDlg.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_ViewModel.h>
45
46 #include <SalomeApp_Application.h>
47 #include <SalomeApp_Study.h>
48 #include <SalomeApp_SelectionMgr.h>
49
50 #include <SALOME_ListIteratorOfListIO.hxx>
51 #include <SALOME_Prs.h>
52
53 #include "utilities.h"
54
55 // QT Includes
56 #include <qapplication.h>
57 #include <qmap.h>
58
59 // OCCT Includes
60 #include <TCollection_AsciiString.hxx>
61
62 using namespace std;
63
64 typedef QMap<QString, QString> FilterMap;
65
66 //=======================================================================
67 // function : getFileName
68 // purpose  : Selection of a file name for Import/Export. Returns also
69 //            the selected file type code through <filter> argument.
70 //=======================================================================
71 static QString getFileName( QWidget*           parent,
72                             const QString&     initial,
73                             const FilterMap&   filterMap,
74                             const QString&     caption,
75                             bool               open,
76                             QString&           format )
77 {
78   static QString lastUsedFilter;
79   QStringList filters;
80   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
81     filters.push_back( it.key() );
82
83   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
84   if ( !caption.isEmpty() )
85     fd->setCaption( caption );
86
87   if ( !initial.isEmpty() )
88     fd->setSelection( initial );
89
90   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
91     fd->setSelectedFilter( lastUsedFilter );
92
93   fd->setFilters( filters );
94
95   fd->exec();
96   QString filename = fd->selectedFile();
97   format = filterMap[fd->selectedFilter()];
98   lastUsedFilter = fd->selectedFilter();
99   delete fd;
100   qApp->processEvents();
101   return filename;
102 }
103
104 //=======================================================================
105 // function : GEOMToolsGUI()
106 // purpose  : Constructor
107 //=======================================================================
108 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
109 : GEOMGUI( parent )
110 {
111 }
112
113
114 //=======================================================================
115 // function : ~GEOMToolsGUI()
116 // purpose  : Destructor
117 //=======================================================================
118 GEOMToolsGUI::~GEOMToolsGUI()
119 {
120 }
121
122
123 //=======================================================================
124 // function : OnGUIEvent()
125 // purpose  :
126 //=======================================================================
127 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
128 {
129   getGeometryGUI()->EmitSignalDeactivateDialog();
130
131   switch (theCommandID)
132     {
133     case 31: // COPY
134       {
135         OnEditCopy();
136         break;
137       }
138     case 33: // DELETE
139       {
140         OnEditDelete();
141         break;
142       }
143     case 111: // IMPORT BREP
144     case 112: // IMPORT IGES
145     case 113: // IMPORT STEP
146       {
147         Import();
148         break;
149       }
150     case 121: // EXPORT BREP
151     case 122: // EXPORT IGES
152     case 123: // EXPORT STEP
153       {
154         Export();
155         break;
156       }
157     case 411: // SETTINGS - ADD IN STUDY
158       {
159         // SAN -- TO BE REMOVED !!!
160         break;
161       }
162     case 412: // SETTINGS - SHADING COLOR
163       {
164         OnSettingsColor();
165         break;
166       }
167     case 413: // SETTINGS - ISOS
168       {
169         OnSettingsIsos();
170         break;
171       }
172     case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
173       {
174         OnSettingsStep();
175         break;
176       }
177     case 804: // ADD IN STUDY - POPUP VIEWER
178       {
179         // SAN -- TO BE REMOVED !!!!
180         break;
181       }
182     case 901: // RENAME
183       {
184         OnRename();
185         break;
186       }
187     case 5103: // CHECK GEOMETRY
188       {
189         OnCheckGeometry();
190         break;
191       }
192     case 8032: // COLOR - POPUP VIEWER
193       {
194         OnColor();
195         break;
196       }
197     case 8033: // TRANSPARENCY - POPUP VIEWER
198       {
199         OnTransparency();
200         break;
201       }
202     case 8034: // ISOS - POPUP VIEWER
203       {
204         OnNbIsos();
205         break;
206       }
207     case 9024 : // OPEN - OBJBROSER POPUP
208       {
209         OnOpen();
210         break;
211       }
212     default:
213       {
214         SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
215         break;
216       }
217     }
218   return true;
219 }
220
221
222
223 //===============================================================================
224 // function : OnEditDelete()
225 // purpose  :
226 //===============================================================================
227 void GEOMToolsGUI::OnEditDelete()
228 {
229   SALOME_ListIO selected;
230   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
231   if ( app ) {
232     SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
233     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
234     if ( aSelMgr && appStudy ) {
235       aSelMgr->selectedObjects( selected );
236       if ( !selected.IsEmpty() ) {
237         _PTR(Study) aStudy = appStudy->studyDS();
238
239         bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
240         if ( aLocked ) {
241           SUIT_MessageBox::warn1 ( app->desktop(),
242                                    QObject::tr("WRN_WARNING"),
243                                    QObject::tr("WRN_STUDY_LOCKED"),
244                                    QObject::tr("BUT_OK") );
245           return;
246         }
247
248         // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
249         // modifications of ASV 01.06.05
250         QString parentComp = getParentComponent( aStudy, selected );
251         const char* geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
252         QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR ) );
253
254         if ( parentComp != geomComp )  {
255           SUIT_MessageBox::warn1 ( app->desktop(),
256                                   QObject::tr("ERR_ERROR"),
257                                   QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
258                                   QObject::tr("BUT_OK") );
259           return;
260         }
261         // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
262
263         if ( SUIT_MessageBox::warn2( app->desktop(),
264                                      QObject::tr( "GEOM_WRN_WARNING" ),
265                                      QObject::tr( "GEOM_REALLY_DELETE" ),
266                                      QObject::tr( "GEOM_BUT_YES" ),
267                                      QObject::tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
268           return;
269
270         //      QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
271         //      op->start();
272
273         // prepare list of SALOME_Views
274         QPtrList<SALOME_View> views;
275         SALOME_View* view;
276         // fill the list
277         ViewManagerList vmans = app->viewManagers();
278         SUIT_ViewManager* vman;
279         for ( vman = vmans.first(); vman; vman = vmans.next() ) {
280           SUIT_ViewModel* vmod = vman->getViewModel();
281           view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
282           if ( view )
283             views.append( view );
284         }
285
286         _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
287         _PTR(GenericAttribute) anAttr;
288         GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
289
290         // MAIN LOOP OF SELECTED OBJECTS
291         for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
292
293           Handle(SALOME_InteractiveObject) io = It.Value();
294           if ( !io->hasEntry() )
295             continue;
296
297           _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
298
299           // disable removal of "Geometry" component object
300           if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
301             continue;
302
303           // iterate through all children of obj, find IOR attributes on children
304           // and remove shapes that correspond to these IORs
305           for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
306             _PTR(SObject) child (it->Value());
307             if (child->FindAttribute(anAttr, "AttributeIOR")) {
308               _PTR(AttributeIOR) anIOR (anAttr);
309
310               // Delete child( s ) shape in Client :
311               const TCollection_AsciiString ASCior ((char*)anIOR->Value().c_str());
312               getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCior);
313
314               CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(child);
315               GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow(corbaObj);
316               if (!CORBA::is_nil(geomObj)) {
317                 for (view = views.first(); view; view = views.next()) {
318                   disp->Erase(geomObj, true, view);
319                 }
320               }
321             }
322           } // for ( children of obj )
323
324           // Erase main graphical object
325           for ( view = views.first(); view; view = views.next() )
326             disp->Erase( io, true, view );
327
328           // Delete main shape in Client :
329           if ( obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
330             _PTR(AttributeIOR) anIOR( anAttr );
331             const TCollection_AsciiString ASCIor( (char*)anIOR->Value().c_str() );
332             getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer( ASCIor );
333           }
334
335           CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
336           GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
337           if ( !CORBA::is_nil( geomObj ) )
338             GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
339
340           // Remove objects from Study
341           aStudyBuilder->RemoveObject( obj );
342
343           //deleted = true;
344         } // MAIN LOOP of selected
345
346         selected.Clear();
347         aSelMgr->setSelectedObjects( selected );
348         getGeometryGUI()->updateObjBrowser();
349       } // if ( selected not empty )
350     } // if ( selMgr && appStudy )
351
352     app->updateActions(); //SRN: To update a Save button in the toolbar 
353
354   } // if ( app )
355
356
357   //  if ( deleted )
358   //    op->finish();
359   //  else
360   //    op->abort();
361 }
362
363
364 //==============================================================================
365 // function : OnEditCopy()
366 // purpose  :
367 //==============================================================================
368 void GEOMToolsGUI::OnEditCopy()
369 {
370 /*
371  SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
372   GEOM::string_array_var listIOR = new GEOM::string_array;
373
374   const SALOME_ListIO& List = Sel->StoredIObjects();
375
376   myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
377
378   Sel->ClearIObjects();
379
380   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
381   int aStudyID = aStudy->StudyId();
382
383   for (unsigned int ind = 0; ind < listIOR->length();ind++) {
384     GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
385     try {
386       GEOM::GEOM_IInsertOperations_var IOp =  myGeom->GetIInsertOperations(aStudyID);
387       GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
388       myGeomBase->Display(result);
389     }
390     catch  (const SALOME::SALOME_Exception& S_ex) {
391       QtCatchCorbaException(S_ex);
392     }
393   }
394
395   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
396 */
397 }
398
399
400 //=====================================================================================
401 // function : Import
402 // purpose  : BRep, Iges, Step
403 //=====================================================================================
404 bool GEOMToolsGUI::Import()
405 {
406   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() ); 
407   //SUIT_Application* app = getGeometryGUI()->getApp();
408   if (! app) return false;
409
410   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
411   if ( !stud ) {
412     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
413     return false;
414   }
415   _PTR(Study) aStudy = stud->studyDS();
416
417   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
418   if ( aLocked ) {
419     SUIT_MessageBox::warn1 ( app->desktop(),
420                             QObject::tr("WRN_WARNING"),
421                             QObject::tr("WRN_STUDY_LOCKED"),
422                             QObject::tr("BUT_OK") );
423     return false;
424   }
425
426   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
427   if ( CORBA::is_nil( eng ) ) {
428     SUIT_MessageBox::error1( app->desktop(),
429                             QObject::tr("WRN_WARNING"),
430                             QObject::tr( "GEOM Engine is not started" ),
431                             QObject::tr("BUT_OK") );
432       return false;
433     }
434
435   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
436   if ( aInsOp->_is_nil() )
437     return false;
438
439   GEOM::GEOM_Object_var anObj;
440
441   // Obtain a list of available import formats
442   FilterMap aMap;
443   GEOM::string_array_var aFormats, aPatterns;
444   aInsOp->ImportTranslators( aFormats, aPatterns );
445
446   for ( int i = 0, n = aFormats->length(); i < n; i++ )
447     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
448
449   QString fileType;
450
451   QString file = getFileName(app->desktop(), "", aMap, tr("GEOM_MEN_IMPORT"), true, fileType );
452   if( file.isEmpty() || fileType.isEmpty() )
453     return false;
454
455   GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
456   try {
457     SUIT_OverrideCursor wc;
458
459     app->putInfo( tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
460
461     anOp->start();
462
463     anObj = aInsOp->Import( file.latin1(), fileType.latin1() );
464
465     if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
466       anObj->SetName( GEOMBase::GetDefaultName( QObject::tr( "GEOM_IMPORT" ) ).latin1() );
467       QString aPublishObjName = GEOMBase::GetDefaultName( SUIT_Tools::file( file, /*withExten=*/true ));
468
469       SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
470       GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
471                                                 SALOMEDS::SObject::_nil(),
472                                                 anObj,
473                                                 aPublishObjName);
474
475       GEOM_Displayer( stud ).Display( anObj.in() );
476
477       // update data model and object browser
478       getGeometryGUI()->updateObjBrowser( true );
479
480       anOp->commit();
481     }
482     else {
483       anOp->abort();
484       wc.suspend();
485       SUIT_MessageBox::error1( app->desktop(),
486                               QObject::tr( "GEOM_ERROR" ),
487                               QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
488                               QObject::tr("BUT_OK") );
489     }
490   }
491   catch( const SALOME::SALOME_Exception& S_ex ) {
492     //QtCatchCorbaException(S_ex);
493     anOp->abort();
494     return false;
495   }
496
497   app->updateActions(); //SRN: To update a Save button in the toolbar
498
499   return true;
500 }
501
502
503 //=====================================================================================
504 // function : Export
505 // purpose  : BRep, Iges, Step
506 //=====================================================================================
507 bool GEOMToolsGUI::Export()
508 {
509   SalomeApp_Application* app = getGeometryGUI()->getApp();
510   if (!app) return false;
511
512   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
513   if ( !stud ) {
514     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
515     return false;
516   }
517   _PTR(Study) aStudy = stud->studyDS();
518
519   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
520   if ( CORBA::is_nil( eng ) ) {
521     SUIT_MessageBox::error1( app->desktop(),
522                              QObject::tr("WRN_WARNING"),
523                              QObject::tr( "GEOM Engine is not started" ),
524                              QObject::tr("BUT_OK") );
525     return false;
526   }
527
528   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
529   if ( aInsOp->_is_nil() )
530     return false;
531
532   // Obtain a list of available export formats
533   FilterMap aMap;
534   GEOM::string_array_var aFormats, aPatterns;
535   aInsOp->ExportTranslators( aFormats, aPatterns );
536   for ( int i = 0, n = aFormats->length(); i < n; i++ )
537     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
538
539   // Get selected objects
540   SalomeApp_SelectionMgr* sm = app->selectionMgr();
541   if ( !sm )
542     return false;
543
544   SALOME_ListIO selectedObjects;
545   sm->selectedObjects( selectedObjects );
546
547   SALOME_ListIteratorOfListIO It( selectedObjects );
548   for(;It.More();It.Next()) {
549     Handle(SALOME_InteractiveObject) IObject = It.Value();
550     Standard_Boolean found;
551     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
552
553     if ( !found || anObj->_is_nil() )
554       continue;
555
556     QString fileType;
557     QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
558                                tr("GEOM_MEN_EXPORT"), false, fileType);
559
560     // User has pressed "Cancel" --> stop the operation
561     if ( file.isEmpty() || fileType.isEmpty() )
562       return false;
563
564     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
565     try {
566       SUIT_OverrideCursor wc;
567
568       app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
569
570       anOp->start();
571
572
573       aInsOp->Export( anObj, file, fileType.latin1() );
574
575       if ( aInsOp->IsDone() )
576         anOp->commit();
577       else
578         {
579           anOp->abort();
580           wc.suspend();
581           SUIT_MessageBox::error1( app->desktop(),
582                                    QObject::tr( "GEOM_ERROR" ),
583                                    QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
584                                    QObject::tr("BUT_OK") );
585           return false;
586         }
587     }
588     catch (const SALOME::SALOME_Exception& S_ex) {
589       //QtCatchCorbaException(S_ex);
590       anOp->abort();
591       return false;
592     }
593   }
594
595   return true;
596 }
597
598
599 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
600 {
601   QString parentComp;
602
603   for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
604
605     Handle(SALOME_InteractiveObject) io = it.Value();
606     if ( !io->hasEntry() )
607       continue;
608
609     QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
610
611     if ( parentComp.isNull() )
612       parentComp = compName;
613     else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
614       parentComp = QString::null;
615       break;
616     }
617   }
618
619   return parentComp;
620 }
621
622 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
623 {
624   if ( obj ) {
625     _PTR(SComponent) comp = obj->GetFatherComponent();
626     if ( comp ) {
627       _PTR(GenericAttribute) anAttr;
628       if ( comp->FindAttribute( anAttr, "AttributeName") ) {
629         _PTR(AttributeName) aName( anAttr );
630         return QString( aName->Value().c_str() );
631       }
632     }
633   }
634   return QString();
635 }
636
637 //=====================================================================================
638 // EXPORTED METHODS
639 //=====================================================================================
640 extern "C"
641 {
642   GEOMGUI* GetLibGUI( GeometryGUI* parent )
643   {
644     return new GEOMToolsGUI( parent );
645   }
646 }