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