Salome HOME
NPAL 18378
[modules/geom.git] / src / GEOMBase / GEOMBase_Helper.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   : GEOMBase_Helper.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : GEOM
27 //  $Header$
28
29 #include <SUIT_ViewModel.h>
30
31 #include "GEOMBase_Helper.h"
32 #include "GEOMBase.h"
33 #include "GEOM_Operation.h"
34 #include "GeometryGUI.h"
35 #include "GEOM_Displayer.h"
36 #include "GEOMImpl_Types.hxx"
37
38 #include <SUIT_Session.h>
39 #include <SUIT_ViewWindow.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_OverrideCursor.h>
42
43 #include <SalomeApp_Module.h>
44 #include <SalomeApp_Application.h>
45 #include <SalomeApp_Study.h>
46 #include <LightApp_SelectionMgr.h>
47 #include <LightApp_DataOwner.h>
48 #include <SalomeApp_Tools.h>
49 #include <SalomeApp_DataModel.h>
50
51 #include <OCCViewer_ViewModel.h>
52 #include <SVTK_ViewModel.h>
53
54 #include <OB_Browser.h>
55
56 #include <TColStd_MapOfInteger.hxx>
57 #include <TCollection_AsciiString.hxx>
58
59 using namespace std;
60
61 #include <SALOMEDSClient.hxx>
62
63
64 //================================================================
65 // Function : getActiveView
66 // Purpose  : Get active view window, returns 0 if no open study frame
67 //================================================================
68 static SUIT_ViewWindow* getActiveView()
69 {
70   SUIT_Study* activeStudy = SUIT_Session::session()->activeApplication()->activeStudy();
71   if ( activeStudy )
72     return SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
73
74   return 0;
75 }
76
77
78 //================================================================
79 // Function : getGeomEngine
80 // Purpose  : Static method
81 //================================================================
82 GEOM::GEOM_Gen_ptr GEOMBase_Helper::getGeomEngine()
83 {
84   return GeometryGUI::GetGeomGen();
85 }
86
87 //================================================================
88 // Function : GEOMBase_Helper
89 // Purpose  :
90 //================================================================
91 GEOMBase_Helper::GEOMBase_Helper( SUIT_Desktop* desktop )
92   : myDesktop( desktop ), myViewWindow( 0 ), myDisplayer( 0 ), myCommand( 0 ), isPreview( false )
93 {
94 }
95
96 //================================================================
97 // Function : ~GEOMBase_Helper
98 // Purpose  :
99 //================================================================
100 GEOMBase_Helper::~GEOMBase_Helper()
101 {
102   if ( !SUIT_Session::session()->activeApplication()->desktop() )
103     return;
104
105   if ( myPreview.size() )
106     erasePreview();
107   if ( hasCommand() )
108     abortCommand();
109   SalomeApp_Application* app = (SalomeApp_Application*)(SUIT_Session::session()->activeApplication());
110   if (app) {
111     GeometryGUI* aGeomGUI = dynamic_cast<GeometryGUI*>( app->module( "Geometry" ) );
112     if(aGeomGUI)
113       globalSelection(aGeomGUI->getLocalSelectionMode() , true );
114   }
115   
116   if (myDisplayer)
117     delete myDisplayer;
118 }
119
120 //================================================================
121 // Function : display
122 // Purpose  :
123 //================================================================
124 void GEOMBase_Helper::display( const ObjectList& objList, const bool updateView )
125 {
126   ObjectList::const_iterator it;
127   for ( it = objList.begin(); it != objList.end(); it++ ) {
128     display( *it, false );
129   }
130   if ( !objList.empty() && updateView )
131     getDisplayer()->UpdateViewer();
132 }
133
134 //================================================================
135 // Function  : display
136 // Purpose   : Display object.
137 // Important : Object must be already in study
138 //================================================================
139 void GEOMBase_Helper::display( GEOM::GEOM_Object_ptr object, const bool updateView )
140 {
141   // Unset color of shape ( this color may be set during preview displaying )
142   // Default color will be used
143   getDisplayer()->UnsetColor();
144   getDisplayer()->UnsetWidth();
145
146   // Enable activisation of selection
147   getDisplayer()->SetToActivate( true );
148
149   // Display object
150   getDisplayer()->Display( object, updateView );
151 }
152
153 //================================================================
154 // Function : erase
155 // Purpose  :
156 //================================================================
157 void GEOMBase_Helper::erase( const ObjectList& objList, const bool updateView )
158 {
159   ObjectList::const_iterator it = objList.begin();
160   for ( ; it != objList.end(); it++ ) {
161     erase( *it, false );
162   }
163   if ( !objList.empty() && updateView )
164     getDisplayer()->UpdateViewer();
165 }
166
167 //================================================================
168 // Function : erase
169 // Purpose  :
170 //================================================================
171 void GEOMBase_Helper::erase( GEOM::GEOM_Object_ptr object, const bool updateView )
172 {
173   if ( !object->_is_nil() ) {
174     string entry = getEntry( object );
175     getDisplayer()->Erase( new SALOME_InteractiveObject(
176       entry.c_str(), "GEOM", strdup( GEOMBase::GetName( object ) ) ), true, updateView );
177   }
178 }
179
180 //================================================================
181 // Function : redisplay
182 // Purpose  :
183 //================================================================
184 void GEOMBase_Helper::redisplay( const ObjectList& objList,
185                                  const bool withChildren,
186                                  const bool updateView )
187 {
188   ObjectList::const_iterator it = objList.begin();
189   for ( ; it != objList.end(); it++ ) {
190     redisplay( *it, withChildren, false );
191   }
192   if ( !objList.empty() && updateView )
193     getDisplayer()->UpdateViewer();
194 }
195
196 //================================================================
197 // Function : redisplay
198 // Purpose  :
199 //================================================================
200 void GEOMBase_Helper::redisplay( GEOM::GEOM_Object_ptr object,
201                                  const bool withChildren,
202                                  const bool updateView )
203 {
204   if ( !object->_is_nil() ) {
205     // Unset color of shape ( this color may be set during preview displaying )
206     // Default color will be used
207     getDisplayer()->UnsetColor();
208     getDisplayer()->UnsetWidth();
209
210     // Enable activisation of selection
211     getDisplayer()->SetToActivate( true );
212
213     string entry = getEntry( object );
214     getDisplayer()->Redisplay(new SALOME_InteractiveObject
215                               (entry.c_str(), "GEOM", strdup(GEOMBase::GetName(object))), false);
216   }
217
218   if ( withChildren ) {
219     SalomeApp_Study* aDoc = getStudy();
220     if ( aDoc && aDoc->studyDS() ) {
221       _PTR(Study) aStudy = aDoc->studyDS();
222       _PTR(SObject) aSObj (aStudy->FindObjectIOR(SalomeApp_Application::orb()->object_to_string(object)));
223       if ( aSObj  ) {
224         _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
225         for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
226           GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
227             (GeometryGUI::ClientSObjectToObject(anIt->Value()));
228           if ( !CORBA::is_nil( aChild ) ) {
229             if ( !aChild->_is_nil() ) {
230               string entry = getEntry( aChild );
231               getDisplayer()->Redisplay( new SALOME_InteractiveObject(
232                 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( aChild ) ) ), false );
233             }
234           }
235         }
236       }
237     }
238   }
239
240   if ( updateView )
241     getDisplayer()->UpdateViewer();
242 }
243
244 //================================================================
245 // Function : displayPreview
246 // Purpose  : Method for displaying preview based on execute() results
247 //================================================================
248 void GEOMBase_Helper::displayPreview( const bool   activate,
249                                       const bool   update,
250                                       const bool   toRemoveFromEngine,
251                                       const double lineWidth, 
252                                       const int    displayMode, 
253                                       const int    color )
254 {
255   isPreview = true;
256   QString msg;
257   if ( !isValid( msg ) )
258   {
259     erasePreview( update );
260     isPreview = false;
261     return;
262   }
263
264   erasePreview( false );
265
266   try {
267     SUIT_OverrideCursor wc;
268     ObjectList objects;
269     if ( !execute( objects ) || !getOperation()->IsDone() ) {
270       wc.suspend();
271     }
272     else {
273       for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
274       {
275         displayPreview( *it, true, activate, false, lineWidth, displayMode, color );
276         if ( toRemoveFromEngine )
277           getGeomEngine()->RemoveObject( *it );
278       }
279     }
280   }
281   catch( const SALOME::SALOME_Exception& e ) {
282     SalomeApp_Tools::QtCatchCorbaException( e );
283   }
284
285   isPreview = false;
286
287   if ( update )
288     updateViewer();
289 }
290
291 //================================================================
292 // Function : displayPreview
293 // Purpose  : Method for displaying preview of resulting shape
294 //================================================================
295 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
296                                       const bool            append,
297                                       const bool            activate,
298                                       const bool            update,
299                                       const double          lineWidth, 
300                                       const int             displayMode, 
301                                       const int             color )
302 {
303   // Set color for preview shape
304   getDisplayer()->SetColor( color == -1 ? Quantity_NOC_VIOLET : color );
305
306   // set width of displayed shape
307   getDisplayer()->SetWidth( lineWidth );
308   
309   // set display mode of displayed shape
310   int aPrevDispMode = getDisplayer()->SetDisplayMode( displayMode );
311
312   // Disable activation of selection
313   getDisplayer()->SetToActivate( activate );
314
315   // Make a reference to GEOM_Object
316   getDisplayer()->SetName( SalomeApp_Application::orb()->object_to_string( object ) );
317
318   // Build prs
319   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
320   if ( aPrs == 0 || aPrs->IsNull() )
321     return;
322
323   // Display prs
324   displayPreview( aPrs, append, update );
325
326   getDisplayer()->UnsetName();
327   getDisplayer()->UnsetColor();
328   getDisplayer()->SetDisplayMode( aPrevDispMode );
329
330   // Enable activation of displayed objects
331   getDisplayer()->SetToActivate( true );
332 }
333
334 //================================================================
335 // Function : displayPreview
336 // Purpose  : Method for displaying arbitrary preview objects (not limited to shapes)
337 //================================================================
338 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
339                                       const bool        append,
340                                       const bool        update )
341 {
342   if ( !append )
343     erasePreview( false );
344
345   // remember current view frame to make correct erase preview later
346   myViewWindow = getActiveView();
347
348   if ( myViewWindow == 0 )
349     return;
350
351   // Display prs
352   SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
353   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
354        aViewManager->getType() == SVTK_Viewer::Type() )
355     {
356       SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
357       SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
358       if (aView)
359         aView->Display( prs );
360     }
361
362   // Add prs to the preview list
363   myPreview.push_back( (SALOME_Prs*)prs );
364
365   // Update viewer
366   if ( update )
367     getDisplayer()->UpdateViewer();
368 }
369
370 //================================================================
371 // Function : erasePreview
372 // Purpose  :
373 //================================================================
374 void GEOMBase_Helper::erasePreview( const bool update )
375 {
376   // check view frame where the preview was displayed
377   bool vfOK = checkViewWindow() && myViewWindow;
378   // Iterate through presentations and delete them
379   for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
380     if ( vfOK )
381       {
382          SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
383          if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
384               aViewManager->getType() == SVTK_Viewer::Type() )
385            {
386              SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
387              SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
388              if (aView)
389                aView->Erase( *anIter, true );
390            }
391       }
392     delete *anIter;
393   }
394   myPreview.clear();
395
396   // Update viewer
397   if ( update )
398     updateViewer();
399 }
400
401 //================================================================
402 // Function  : localSelection
403 // Purpose   : Activate selection of objects of a given type
404 // IMPORTANT : Works after localSelection( ... ) method call only
405 //================================================================
406 void GEOMBase_Helper::activate( const int theType )
407 {
408   if (!getStudy()) return;
409   _PTR(Study) aStudy = getStudy()->studyDS();
410   _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
411   if ( !aGeom )
412     return;
413
414   SALOME_ListIO aList;
415   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
416   for ( ; anIter->More(); anIter->Next() )
417   {
418     _PTR(SObject) aSO ( anIter->Value() );
419     if ( aSO )
420     {
421       _PTR(SObject) aRefSO;
422       if ( !aSO->ReferencedObject( aRefSO ) )
423       {
424         GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
425           (GeometryGUI::ClientSObjectToObject(aSO));
426         if ( !anObj->_is_nil() && anObj->GetType() == theType )
427           aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
428       }
429     }
430   }
431
432   getDisplayer()->LocalSelection( aList, 0 );
433 }
434
435 //================================================================
436 // Function : localSelection
437 // Purpose  : Activate selection of subshapes in accordance with mode
438 //            theMode is from TopAbs_ShapeEnum
439 //================================================================
440 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
441 {
442   SALOME_ListIO aListOfIO;
443
444   ObjectList::const_iterator anIter = theObjs.begin();
445   for ( ; anIter != theObjs.end(); ++anIter )
446   {
447     GEOM::GEOM_Object_ptr anObj = *anIter;
448     if ( anObj->_is_nil() )
449       continue;
450     string aEntry = getEntry( anObj );
451     if ( aEntry != "" )
452       aListOfIO.Append( new SALOME_InteractiveObject(
453         aEntry.c_str(), "GEOM", strdup( GEOMBase::GetName( anObj ) ) ) );
454   }
455
456   getDisplayer()->LocalSelection( aListOfIO, theMode );
457 }
458
459 //================================================================
460 // Function : localSelection
461 // Purpose  : Activate selection of subshapes in accordance with mode
462 //            theMode is from TopAbs_ShapeEnum
463 //================================================================
464 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
465 {
466   // If object is null local selection for all objects is activated
467   if ( obj->_is_nil() ) {
468     getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
469     return;
470   }
471
472   ObjectList objList;
473   objList.push_back( obj );
474   localSelection( objList, mode );
475 }
476
477
478 //================================================================
479 // Function : globalSelection
480 // Purpose  : Activate selection of subshapes. Set selection filters
481 //            in accordance with mode. theMode is from GEOMImpl_Types
482 //================================================================
483 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
484 {
485   getDisplayer()->GlobalSelection( theMode, update );
486 }
487
488 //================================================================
489 // Function : globalSelection
490 // Purpose  : Activate selection of subshapes. Set selection filters
491 //            in accordance with mode. theMode is from GEOMImpl_Types
492 //================================================================
493 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
494                                        const bool update )
495 {
496   getDisplayer()->GlobalSelection( theModes, update );
497 }
498
499 //================================================================
500 // Function : addInStudy
501 // Purpose  : Add object in study
502 //================================================================
503 void GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
504 {
505   if ( !hasCommand() )
506     return;
507
508   _PTR(Study) aStudy = getStudy()->studyDS();
509   if ( !aStudy || theObj->_is_nil() )
510     return;
511
512   GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
513
514   getGeomEngine()->AddInStudy(GeometryGUI::ClientStudyToStudy(aStudy),
515                               theObj, theName, aFatherObj);
516 }
517
518 //================================================================
519 // Function : updateObjBrowser
520 // Purpose  : Update object browser
521 //================================================================
522 void GEOMBase_Helper::updateObjBrowser() const
523 {
524   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
525   if (app) {
526     CAM_Module* module = app->module( "Geometry" );
527     SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
528     if ( appMod ) {
529       appMod->updateObjBrowser( true );
530     }
531   }
532 }
533
534 //================================================================
535 // Function : updateViewer
536 // Purpose  : Update active 3D view
537 //================================================================
538 void GEOMBase_Helper::updateViewer()
539 {
540   getDisplayer()->UpdateViewer();
541 }
542
543 //================================================================
544 // Function : getStudyId
545 // Purpose  : Get study Id
546 //================================================================
547 int GEOMBase_Helper::getStudyId() const
548 {
549   int anId = -1;
550   if ( getStudy() )
551     anId = getStudy()->id();
552   return anId;
553 }
554
555 //================================================================
556 // Function : getStudy
557 // Purpose  : Returns the active study. It is recommended to use
558 //            this method instead of direct desktop->getActiveStudy() calls
559 //================================================================
560 SalomeApp_Study* GEOMBase_Helper::getStudy() const
561 {
562   SUIT_Desktop* aDesktop = getDesktop();
563   if (!aDesktop)
564     return 0;
565
566   QPtrList<SUIT_Application> anAppList = SUIT_Session::session()->applications();
567
568   SUIT_Application* anApp = 0;
569   for ( QPtrListIterator<SUIT_Application> it( anAppList ); it.current() ; ++it )
570     {
571       anApp = it.current();
572       if ( anApp->desktop() == aDesktop )
573         break;
574     }
575
576   return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
577 }
578
579 //================================================================
580 // Function : getEntry
581 // Purpose  :
582 //================================================================
583 char* GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
584 {
585   SalomeApp_Study* study = getStudy();
586   if ( study )  {
587     string IOR = GEOMBase::GetIORFromObject( object);
588     if ( IOR != "" ) {
589       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
590       if ( SO ) {
591         return TCollection_AsciiString((char*)SO->GetID().c_str()).ToCString();
592       }
593     }
594   }
595   return "";
596 }
597
598 //================================================================
599 // Function : getDisplayer
600 // Purpose  :
601 //================================================================
602 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
603 {
604   if ( !myDisplayer )
605     myDisplayer = new GEOM_Displayer( getStudy() );
606   return myDisplayer;
607 }
608
609 //================================================================
610 // Function : clearShapeBuffer
611 // Purpose  :
612 //================================================================
613 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
614 {
615   if ( CORBA::is_nil( theObj ) )
616     return;
617
618   string IOR = SalomeApp_Application::orb()->object_to_string( theObj );
619   TCollection_AsciiString asciiIOR( strdup( IOR.c_str() ) );
620   GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
621
622   if ( !getStudy() || !getStudy()->studyDS() )
623     return;
624
625   _PTR(Study) aStudy = getStudy()->studyDS();
626   _PTR(SObject) aSObj ( aStudy->FindObjectIOR( IOR ) );
627   if ( !aSObj )
628     return;
629
630   _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
631   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
632     _PTR(GenericAttribute) anAttr;
633     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
634       _PTR(AttributeIOR) anIOR ( anAttr );
635       TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
636       GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
637     }
638   }
639 }
640
641 //================================================================
642 // Function : openCommand
643 // Purpose  :
644 //================================================================
645 bool GEOMBase_Helper::openCommand()
646 {
647   bool res = false;
648   if ( !getStudy() || hasCommand() )
649     return res;
650
651   GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
652   if ( !anOp->_is_nil() ) {
653     myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
654     myCommand->start();
655     res = true;
656   }
657
658   return res;
659 }
660
661 //================================================================
662 // Function : abortCommand
663 // Purpose  :
664 //================================================================
665 bool GEOMBase_Helper::abortCommand()
666 {
667   if ( !hasCommand() )
668     return false;
669
670   myCommand->abort();
671   myCommand = 0;
672
673   return true;
674 }
675
676 //================================================================
677 // Function : commitCommand
678 // Purpose  :
679 //================================================================
680 bool GEOMBase_Helper::commitCommand( const char* )
681 {
682   if ( !hasCommand() )
683     return false;
684
685   myCommand->commit();
686   myCommand = 0;
687
688   return true;
689 }
690
691 //================================================================
692 // Function : hasCommand
693 // Purpose  :
694 //================================================================
695 bool GEOMBase_Helper::hasCommand() const
696 {
697   return (bool)myCommand;
698 }
699
700 //================================================================
701 // Function : getOperation
702 // Purpose  :
703 //================================================================
704 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
705 {
706   if ( myOperation->_is_nil() )
707     myOperation = createOperation();
708
709   return myOperation;
710 }
711
712
713
714 //================================================================
715 // Function : checkViewWindow
716 // Purpose  :
717 //================================================================
718 bool GEOMBase_Helper::checkViewWindow()
719 {
720   if ( myViewWindow ){
721     QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
722     for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it )
723       {
724         if ( myViewWindow == it.current() )
725           return true;
726       }
727   }
728   myViewWindow = 0;
729   return false;
730 }
731
732 //================================================================
733 // Function : onAccept
734 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
735 //            It perfroms user input validation, then it
736 //            performs a proper operation and manages transactions, etc.
737 //================================================================
738 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
739 {
740   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
741   if ( !appStudy ) return false;
742   _PTR(Study) aStudy = appStudy->studyDS();
743
744   bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
745   if ( aLocked ) {
746     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
747     SUIT_MessageBox::warn1 ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
748                            QObject::tr("WRN_WARNING"),
749                            QObject::tr("WRN_STUDY_LOCKED"),
750                            QObject::tr("BUT_OK") );
751     return false;
752   }
753
754   QString msg;
755   if ( !isValid( msg ) ) {
756     showError( msg );
757     return false;
758   }
759
760   erasePreview( false );
761
762   try {
763     if ( ( !publish && !useTransaction ) || openCommand() ) {
764       SUIT_OverrideCursor wc;
765       SUIT_Session::session()->activeApplication()->putInfo( "" );
766       ObjectList objects;
767       if ( !execute( objects ) || !getOperation()->IsDone() ) {
768         wc.suspend();
769         abortCommand();
770         showError();
771       }
772       else {
773         addSubshapesToStudy(); // add Subshapes if local selection
774         const int nbObjs = objects.size();
775         int aNumber = 1;
776         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
777           if ( publish ) {
778             QString aName = getNewObjectName();
779             if ( nbObjs > 1 ) {
780               if (aName.isEmpty())
781                 aName = getPrefix(*it);
782               if (nbObjs <= 30) {
783                 // Try to find a unique name
784                 aName = GEOMBase::GetDefaultName(aName);
785               } else {
786                 // Don't check name uniqueness in case of numerous objects
787                 aName = aName + "_" + QString::number(aNumber++);
788               }
789             } else {
790               // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
791               if ( aName.isEmpty() )
792                 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
793             }
794             addInStudy( *it, aName.latin1() );
795             // updateView=false
796             display( *it, false );
797           }
798           else {
799             // asv : fix of PAL6454. If publish==false, then the original shape
800             // was modified, and need to be re-cached in GEOM_Client before redisplay
801             clearShapeBuffer( *it );
802             // withChildren=true, updateView=false
803             redisplay( *it, true, false );
804           }
805         }
806
807         if ( nbObjs ) {
808           commitCommand();
809           updateObjBrowser();
810           SUIT_Session::session()->activeApplication()->putInfo( QObject::tr("GEOM_PRP_DONE") );
811         }
812         else
813           abortCommand();
814       }
815     }
816   }
817   catch( const SALOME::SALOME_Exception& e ) {
818     SalomeApp_Tools::QtCatchCorbaException( e );
819     abortCommand();
820   }
821
822   updateViewer();
823
824   return true;
825 }
826
827
828 //================================================================
829 // Function : showError
830 // Purpose  : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
831 //================================================================
832 void GEOMBase_Helper::showError()
833 {
834   QString msg;
835   if ( !getOperation()->_is_nil() )
836     msg = QObject::tr( getOperation()->GetErrorCode() );
837
838   if ( msg.isEmpty() )
839     msg = QObject::tr( "GEOM_PRP_ABORT" );
840
841   SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(),
842                            QObject::tr( "GEOM_ERROR_STATUS" ),
843                            msg,
844                            QObject::tr( "BUT_OK" ) );
845 }
846
847 //================================================================
848 // Function : showError
849 // Purpose  : Shows a error message followed by <msg>
850 //================================================================
851 void GEOMBase_Helper::showError( const QString& msg )
852 {
853   QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
854   if ( !msg.isEmpty() )
855     str += "\n" + msg;
856   SUIT_MessageBox::error1(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
857 }
858
859 //////////////////////////////////////////////////////////////////
860 // Virtual methods to be redefined in dialogs
861 //////////////////////////////////////////////////////////////////
862
863 //================================================================
864 // Function : createOperation
865 // Purpose  : Redefine this method to return proper IOperation reference
866 //================================================================
867 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
868 {
869   GEOM::GEOM_IOperations_var aNilOp;
870   return aNilOp._retn();
871 }
872
873 //================================================================
874 // Function : isValid
875 // Purpose  : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
876 //================================================================
877 bool GEOMBase_Helper::isValid( QString& )
878 {
879   return true;
880 }
881
882 //================================================================
883 // Function : execute
884 // Purpose  : This method is called by onAccept().
885 //            It should perform the required operation and put all new or modified objects into
886 //            <objects> argument.Should return <false> if some error occurs during its execution.
887 //================================================================
888 bool GEOMBase_Helper::execute( ObjectList& objects )
889 {
890   return false;
891 }
892
893 //================================================================
894 // Function : getFather
895 // Purpose  : This method is called by addInStudy(). It should return a father object
896 //            for <theObj> or a nil reference if <theObj> should be published
897 //            as a top-level object.
898 //================================================================
899 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
900 {
901   return GEOM::GEOM_Object::_nil();
902 }
903
904 //================================================================
905 // Function : getNewObjectName
906 // Purpose  : Redefine this method to return proper name for a new object
907 //================================================================
908 const char* GEOMBase_Helper::getNewObjectName() const
909 {
910   return "";
911 }
912
913 //================================================================
914 // Function : getPrefix
915 // Purpose  : Get prefix for name of created object
916 //================================================================
917 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
918 {
919   if ( !myPrefix.isEmpty() || theObj->_is_nil() )
920     return myPrefix;
921
922   //TopoDS_Shape aShape;
923   //if ( !GEOMBase::GetShape( theObj, aShape ) )
924   //  return "";
925   //
926   //long aType = aShape.ShapeType();
927   GEOM::shape_type aType = theObj->GetShapeType();
928
929   switch ( aType )
930   {
931     case GEOM::VERTEX   : return QObject::tr( "GEOM_VERTEX" );
932     case GEOM::EDGE     : return QObject::tr( "GEOM_EDGE" );
933     case GEOM::WIRE     : return QObject::tr( "GEOM_WIRE" );
934     case GEOM::FACE     : return QObject::tr( "GEOM_FACE" );
935     case GEOM::SHELL    : return QObject::tr( "GEOM_SHELL" );
936     case GEOM::SOLID    : return QObject::tr( "GEOM_SOLID" );
937     case GEOM::COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
938     case GEOM::COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
939     default : return "";
940   }
941 }
942
943 //================================================================
944 // Function : selectedIO
945 // Purpose  : Return the list of selected SALOME_InteractiveObject's
946 //================================================================
947 const SALOME_ListIO& GEOMBase_Helper::selectedIO()
948 {
949   mySelected.Clear();
950
951   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
952   if ( app ) {
953     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
954     if ( aSelMgr )
955       aSelMgr->selectedObjects( mySelected );
956   }
957
958   return mySelected;
959 }
960
961 //================================================================
962 // Function : IObjectCount
963 // Purpose  : Return the number of selected objects
964 //================================================================
965 int GEOMBase_Helper::IObjectCount()
966 {
967   return selectedIO().Extent();
968 }
969
970 //================================================================
971 // Function : firstIObject
972 // Purpose  :  Return the first selected object in the selected object list
973 //================================================================
974 Handle(SALOME_InteractiveObject) GEOMBase_Helper::firstIObject()
975 {
976   const SALOME_ListIO& aList = selectedIO();
977   return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
978 }
979
980 //================================================================
981 // Function : lastIObject
982 // Purpose  : Return the last selected object in the selected object list
983 //================================================================
984 Handle(SALOME_InteractiveObject) GEOMBase_Helper::lastIObject()
985 {
986   const SALOME_ListIO& aList = selectedIO();
987   return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
988 }
989
990 //================================================================
991 // Function : getDesktop
992 // Purpose  : Returns myDesktop field.  Initialized in constructor, usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
993 //================================================================
994 SUIT_Desktop* GEOMBase_Helper::getDesktop() const
995 {
996   return myDesktop;
997 }
998
999 //================================================================
1000 // Function : selectObjects
1001 // Purpose  : Selects list of objects 
1002 //================================================================
1003 bool GEOMBase_Helper::selectObjects( ObjectList& objects )
1004 {
1005   SUIT_DataOwnerPtrList aList;
1006   ObjectList::iterator anIter;
1007   for ( anIter = objects.begin(); anIter != objects.end(); ++anIter )
1008   {
1009     string entry = getEntry( *anIter );
1010     QString aEntry( entry.c_str() );
1011     LightApp_DataOwner* anOwher = new LightApp_DataOwner( aEntry );
1012     aList.append( anOwher );
1013   }
1014   
1015   SUIT_Session* session = SUIT_Session::session();
1016   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1017   if ( !app )
1018     return false;
1019
1020   LightApp_SelectionMgr* aMgr = app->selectionMgr();
1021   if ( !aMgr )
1022     return false;
1023   
1024   aMgr->setSelected( aList, false );
1025   
1026   return true;
1027 }
1028   
1029 //================================================================
1030 // Function : findObjectInFather
1031 // Purpose  : It should return an object if its founded in study or
1032 //            return Null object if the object is not founded
1033 //================================================================
1034 GEOM::GEOM_Object_ptr GEOMBase_Helper::findObjectInFather( GEOM::GEOM_Object_ptr theFather, const char* theName)
1035 {
1036   SalomeApp_Application* app =
1037     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
1038   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1039   _PTR(Study) aDStudy = appStudy->studyDS();
1040   string IOR = GEOMBase::GetIORFromObject( theFather );
1041   _PTR(SObject) SObj ( aDStudy->FindObjectIOR( IOR ) );
1042
1043   bool inStudy = false;
1044   GEOM::GEOM_Object_var aReturnObject;
1045   for (_PTR(ChildIterator) iit (aDStudy->NewChildIterator( SObj )); iit->More(); iit->Next()) {
1046     _PTR(SObject) child (iit->Value());
1047     QString aChildName = child->GetName();
1048     if (aChildName == theName) {
1049       inStudy = true;
1050       CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(iit->Value());
1051       aReturnObject = GEOM::GEOM_Object::_narrow( corbaObj );
1052     }
1053   }
1054   if (!inStudy)
1055     return GEOM::GEOM_Object::_nil();
1056   else
1057     return aReturnObject._retn();
1058 }
1059   
1060 //================================================================
1061 // Function : addSubshapesToStudy
1062 // Purpose  : Virtual method to add subshapes if needs
1063 //================================================================  
1064 void GEOMBase_Helper::addSubshapesToStudy()
1065 {
1066   //Impemented in Dialogs, called from Accept method
1067 }
1068
1069 //================================================================
1070 // Function : addSubshapesToFather
1071 // Purpose  : Method to add Father Subshapes to Study if it`s not exist
1072 //================================================================  
1073 void GEOMBase_Helper::addSubshapesToFather( QMap<QString, GEOM::GEOM_Object_var>& theMap )
1074 {
1075   //GetStudyDS
1076   SalomeApp_Application* app =
1077     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
1078   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1079   _PTR(Study) aDStudy = appStudy->studyDS();
1080
1081   GEOM::GEOM_IGroupOperations_var anOp = getGeomEngine()->GetIGroupOperations( getStudyId() );
1082  
1083   for( QMap<QString, GEOM::GEOM_Object_var>::Iterator it = theMap.begin(); it != theMap.end(); it++ )
1084     {
1085       if ( !anOp->_is_nil() ) {
1086         GEOM::GEOM_Object_var aFatherObj = anOp->GetMainShape( it.data() );
1087         GEOM::GEOM_Object_var aFindedObject = findObjectInFather(aFatherObj, it.key() );
1088       
1089         //Add Object to study if its not exist
1090         if ( aFindedObject == GEOM::GEOM_Object::_nil() )
1091           GeometryGUI::GetGeomGen()->AddInStudy(GeometryGUI::ClientStudyToStudy(aDStudy),
1092                                               it.data(), it.key(), aFatherObj );
1093       }
1094       else {
1095         //cout << " anOperations is NULL! " << endl;
1096       }
1097     }
1098 }  
1099