Salome HOME
PAL17233: Projection 2D doesn't work (bis)
[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       CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string(object);
223       _PTR(SObject) aSObj (aStudy->FindObjectIOR(string(objStr.in())));
224       if ( aSObj  ) {
225         _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
226         for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
227           GEOM::GEOM_Object_var aChild = GEOM::GEOM_Object::_narrow
228             (GeometryGUI::ClientSObjectToObject(anIt->Value()));
229           if ( !CORBA::is_nil( aChild ) ) {
230             if ( !aChild->_is_nil() ) {
231               string entry = getEntry( aChild );
232               getDisplayer()->Redisplay( new SALOME_InteractiveObject(
233                 entry.c_str(), "GEOM", strdup( GEOMBase::GetName( aChild ) ) ), false );
234             }
235           }
236         }
237       }
238     }
239   }
240
241   if ( updateView )
242     getDisplayer()->UpdateViewer();
243 }
244
245 //================================================================
246 // Function : displayPreview
247 // Purpose  : Method for displaying preview based on execute() results
248 //================================================================
249 void GEOMBase_Helper::displayPreview( const bool   activate,
250                                       const bool   update,
251                                       const bool   toRemoveFromEngine,
252                                       const double lineWidth, 
253                                       const int    displayMode, 
254                                       const int    color )
255 {
256   isPreview = true;
257   QString msg;
258   if ( !isValid( msg ) )
259   {
260     erasePreview( update );
261     isPreview = false;
262     return;
263   }
264
265   erasePreview( false );
266
267   try {
268     SUIT_OverrideCursor wc;
269     ObjectList objects;
270     if ( !execute( objects ) || !getOperation()->IsDone() ) {
271       wc.suspend();
272     }
273     else {
274       for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it )
275       {
276         displayPreview( *it, true, activate, false, lineWidth, displayMode, color );
277         if ( toRemoveFromEngine )
278           getGeomEngine()->RemoveObject( *it );
279       }
280     }
281   }
282   catch( const SALOME::SALOME_Exception& e ) {
283     SalomeApp_Tools::QtCatchCorbaException( e );
284   }
285
286   isPreview = false;
287
288   if ( update )
289     updateViewer();
290 }
291
292 //================================================================
293 // Function : displayPreview
294 // Purpose  : Method for displaying preview of resulting shape
295 //================================================================
296 void GEOMBase_Helper::displayPreview( GEOM::GEOM_Object_ptr object,
297                                       const bool            append,
298                                       const bool            activate,
299                                       const bool            update,
300                                       const double          lineWidth, 
301                                       const int             displayMode, 
302                                       const int             color )
303 {
304   // Set color for preview shape
305   getDisplayer()->SetColor( color == -1 ? Quantity_NOC_VIOLET : color );
306
307   // set width of displayed shape
308   getDisplayer()->SetWidth( lineWidth );
309   
310   // set display mode of displayed shape
311   int aPrevDispMode = getDisplayer()->SetDisplayMode( displayMode );
312
313   // Disable activation of selection
314   getDisplayer()->SetToActivate( activate );
315
316   // Make a reference to GEOM_Object
317   CORBA::String_var objStr = SalomeApp_Application::orb()->object_to_string( object );
318   getDisplayer()->SetName( objStr.in() );
319
320   // Build prs
321   SALOME_Prs* aPrs = getDisplayer()->BuildPrs( object );
322   if ( aPrs == 0 || aPrs->IsNull() )
323     return;
324
325   // Display prs
326   displayPreview( aPrs, append, update );
327
328   getDisplayer()->UnsetName();
329   getDisplayer()->UnsetColor();
330   getDisplayer()->SetDisplayMode( aPrevDispMode );
331
332   // Enable activation of displayed objects
333   getDisplayer()->SetToActivate( true );
334 }
335
336 //================================================================
337 // Function : displayPreview
338 // Purpose  : Method for displaying arbitrary preview objects (not limited to shapes)
339 //================================================================
340 void GEOMBase_Helper::displayPreview( const SALOME_Prs* prs,
341                                       const bool        append,
342                                       const bool        update )
343 {
344   if ( !append )
345     erasePreview( false );
346
347   // remember current view frame to make correct erase preview later
348   myViewWindow = getActiveView();
349
350   if ( myViewWindow == 0 )
351     return;
352
353   // Display prs
354   SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
355   if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
356        aViewManager->getType() == SVTK_Viewer::Type() )
357     {
358       SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
359       SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
360       if (aView)
361         aView->Display( prs );
362     }
363
364   // Add prs to the preview list
365   myPreview.push_back( (SALOME_Prs*)prs );
366
367   // Update viewer
368   if ( update )
369     getDisplayer()->UpdateViewer();
370 }
371
372 //================================================================
373 // Function : erasePreview
374 // Purpose  :
375 //================================================================
376 void GEOMBase_Helper::erasePreview( const bool update )
377 {
378   // check view frame where the preview was displayed
379   bool vfOK = checkViewWindow() && myViewWindow;
380   // Iterate through presentations and delete them
381   for ( PrsList::iterator anIter = myPreview.begin(); anIter != myPreview.end(); ++anIter ) {
382     if ( vfOK )
383       {
384          SUIT_ViewManager* aViewManager = myViewWindow->getViewManager();
385          if ( aViewManager->getType() == OCCViewer_Viewer::Type() ||
386               aViewManager->getType() == SVTK_Viewer::Type() )
387            {
388              SUIT_ViewModel* aViewModel = aViewManager->getViewModel();
389              SALOME_View* aView = dynamic_cast<SALOME_View*>(aViewModel);
390              if (aView)
391                aView->Erase( *anIter, true );
392            }
393       }
394     delete *anIter;
395   }
396   myPreview.clear();
397
398   // Update viewer
399   if ( update )
400     updateViewer();
401 }
402
403 //================================================================
404 // Function  : localSelection
405 // Purpose   : Activate selection of objects of a given type
406 // IMPORTANT : Works after localSelection( ... ) method call only
407 //================================================================
408 void GEOMBase_Helper::activate( const int theType )
409 {
410   if (!getStudy()) return;
411   _PTR(Study) aStudy = getStudy()->studyDS();
412   _PTR(SComponent) aGeom ( aStudy->FindComponent( "GEOM" ) );
413   if ( !aGeom )
414     return;
415
416   SALOME_ListIO aList;
417   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( aGeom ) );
418   for ( ; anIter->More(); anIter->Next() )
419   {
420     _PTR(SObject) aSO ( anIter->Value() );
421     if ( aSO )
422     {
423       _PTR(SObject) aRefSO;
424       if ( !aSO->ReferencedObject( aRefSO ) )
425       {
426         GEOM::GEOM_Object_var anObj = GEOM::GEOM_Object::_narrow
427           (GeometryGUI::ClientSObjectToObject(aSO));
428         if ( !anObj->_is_nil() && anObj->GetType() == theType )
429           aList.Append( new SALOME_InteractiveObject( aSO->GetID().c_str(), "GEOM", aSO->GetName().c_str()) );
430       }
431     }
432   }
433
434   getDisplayer()->LocalSelection( aList, 0 );
435 }
436
437 //================================================================
438 // Function : localSelection
439 // Purpose  : Activate selection of subshapes in accordance with mode
440 //            theMode is from TopAbs_ShapeEnum
441 //================================================================
442 void GEOMBase_Helper::localSelection( const ObjectList& theObjs, const int theMode )
443 {
444   SALOME_ListIO aListOfIO;
445
446   ObjectList::const_iterator anIter = theObjs.begin();
447   for ( ; anIter != theObjs.end(); ++anIter )
448   {
449     GEOM::GEOM_Object_ptr anObj = *anIter;
450     if ( anObj->_is_nil() )
451       continue;
452     string aEntry = getEntry( anObj );
453     if ( aEntry != "" )
454       aListOfIO.Append( new SALOME_InteractiveObject(
455         aEntry.c_str(), "GEOM", strdup( GEOMBase::GetName( anObj ) ) ) );
456   }
457
458   getDisplayer()->LocalSelection( aListOfIO, theMode );
459 }
460
461 //================================================================
462 // Function : localSelection
463 // Purpose  : Activate selection of subshapes in accordance with mode
464 //            theMode is from TopAbs_ShapeEnum
465 //================================================================
466 void GEOMBase_Helper::localSelection( GEOM::GEOM_Object_ptr obj, const int mode )
467 {
468   // If object is null local selection for all objects is activated
469   if ( obj->_is_nil() ) {
470     getDisplayer()->LocalSelection( Handle(SALOME_InteractiveObject)(), mode );
471     return;
472   }
473
474   ObjectList objList;
475   objList.push_back( obj );
476   localSelection( objList, mode );
477 }
478
479
480 //================================================================
481 // Function : globalSelection
482 // Purpose  : Activate selection of subshapes. Set selection filters
483 //            in accordance with mode. theMode is from GEOMImpl_Types
484 //================================================================
485 void GEOMBase_Helper::globalSelection( const int theMode, const bool update )
486 {
487   getDisplayer()->GlobalSelection( theMode, update );
488 }
489
490 //================================================================
491 // Function : globalSelection
492 // Purpose  : Activate selection of subshapes. Set selection filters
493 //            in accordance with mode. theMode is from GEOMImpl_Types
494 //================================================================
495 void GEOMBase_Helper::globalSelection( const TColStd_MapOfInteger& theModes,
496                                        const bool update )
497 {
498   getDisplayer()->GlobalSelection( theModes, update );
499 }
500
501 //================================================================
502 // Function : addInStudy
503 // Purpose  : Add object in study
504 //================================================================
505 void GEOMBase_Helper::addInStudy( GEOM::GEOM_Object_ptr theObj, const char* theName )
506 {
507   if ( !hasCommand() )
508     return;
509
510   _PTR(Study) aStudy = getStudy()->studyDS();
511   if ( !aStudy || theObj->_is_nil() )
512     return;
513
514   GEOM::GEOM_Object_ptr aFatherObj = getFather( theObj );
515
516   getGeomEngine()->AddInStudy(GeometryGUI::ClientStudyToStudy(aStudy),
517                               theObj, theName, aFatherObj);
518 }
519
520 //================================================================
521 // Function : updateObjBrowser
522 // Purpose  : Update object browser
523 //================================================================
524 void GEOMBase_Helper::updateObjBrowser() const
525 {
526   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>(SUIT_Session::session()->activeApplication());
527   if (app) {
528     CAM_Module* module = app->module( "Geometry" );
529     SalomeApp_Module* appMod = dynamic_cast<SalomeApp_Module*>( module );
530     if ( appMod ) {
531       appMod->updateObjBrowser( true );
532     }
533   }
534 }
535
536 //================================================================
537 // Function : updateViewer
538 // Purpose  : Update active 3D view
539 //================================================================
540 void GEOMBase_Helper::updateViewer()
541 {
542   getDisplayer()->UpdateViewer();
543 }
544
545 //================================================================
546 // Function : getStudyId
547 // Purpose  : Get study Id
548 //================================================================
549 int GEOMBase_Helper::getStudyId() const
550 {
551   int anId = -1;
552   if ( getStudy() )
553     anId = getStudy()->id();
554   return anId;
555 }
556
557 //================================================================
558 // Function : getStudy
559 // Purpose  : Returns the active study. It is recommended to use
560 //            this method instead of direct desktop->getActiveStudy() calls
561 //================================================================
562 SalomeApp_Study* GEOMBase_Helper::getStudy() const
563 {
564   SUIT_Desktop* aDesktop = getDesktop();
565   if (!aDesktop)
566     return 0;
567
568   QPtrList<SUIT_Application> anAppList = SUIT_Session::session()->applications();
569
570   SUIT_Application* anApp = 0;
571   for ( QPtrListIterator<SUIT_Application> it( anAppList ); it.current() ; ++it )
572     {
573       anApp = it.current();
574       if ( anApp->desktop() == aDesktop )
575         break;
576     }
577
578   return dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
579 }
580
581 //================================================================
582 // Function : getEntry
583 // Purpose  :
584 //================================================================
585 char* GEOMBase_Helper::getEntry( GEOM::GEOM_Object_ptr object ) const
586 {
587   SalomeApp_Study* study = getStudy();
588   if ( study )  {
589     char * objIOR = GEOMBase::GetIORFromObject( object );
590     string IOR( objIOR );
591     free( objIOR );
592     if ( IOR != "" ) {
593       _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( IOR ) );
594       if ( SO ) {
595               return TCollection_AsciiString((char*)SO->GetID().c_str()).ToCString();
596       }
597     }
598   }
599   return "";
600 }
601
602 //================================================================
603 // Function : getDisplayer
604 // Purpose  :
605 //================================================================
606 GEOM_Displayer* GEOMBase_Helper::getDisplayer()
607 {
608   if ( !myDisplayer )
609     myDisplayer = new GEOM_Displayer( getStudy() );
610   return myDisplayer;
611 }
612
613 //================================================================
614 // Function : clearShapeBuffer
615 // Purpose  :
616 //================================================================
617 void GEOMBase_Helper::clearShapeBuffer( GEOM::GEOM_Object_ptr theObj )
618 {
619   if ( CORBA::is_nil( theObj ) )
620     return;
621
622   CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( theObj );
623   TCollection_AsciiString asciiIOR( (char *)IOR.in() );
624   GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
625
626   if ( !getStudy() || !getStudy()->studyDS() )
627     return;
628
629   _PTR(Study) aStudy = getStudy()->studyDS();
630   _PTR(SObject) aSObj ( aStudy->FindObjectIOR( string( IOR ) ) );
631   if ( !aSObj )
632     return;
633
634   _PTR(ChildIterator) anIt ( aStudy->NewChildIterator( aSObj ) );
635   for ( anIt->InitEx( true ); anIt->More(); anIt->Next() ) {
636     _PTR(GenericAttribute) anAttr;
637     if ( anIt->Value()->FindAttribute(anAttr, "AttributeIOR") ) {
638       _PTR(AttributeIOR) anIOR ( anAttr );
639       TCollection_AsciiString asciiIOR( (char*)anIOR->Value().c_str() );
640       GEOM_Client().RemoveShapeFromBuffer( asciiIOR );
641     }
642   }
643 }
644
645 //================================================================
646 // Function : openCommand
647 // Purpose  :
648 //================================================================
649 bool GEOMBase_Helper::openCommand()
650 {
651   bool res = false;
652   if ( !getStudy() || hasCommand() )
653     return res;
654
655   GEOM::GEOM_IOperations_var anOp = GEOM::GEOM_IOperations::_narrow( getOperation() );
656   if ( !anOp->_is_nil() ) {
657     myCommand = new GEOM_Operation( SUIT_Session::session()->activeApplication(), anOp.in() );
658     myCommand->start();
659     res = true;
660   }
661
662   return res;
663 }
664
665 //================================================================
666 // Function : abortCommand
667 // Purpose  :
668 //================================================================
669 bool GEOMBase_Helper::abortCommand()
670 {
671   if ( !hasCommand() )
672     return false;
673
674   myCommand->abort();
675   myCommand = 0;
676
677   return true;
678 }
679
680 //================================================================
681 // Function : commitCommand
682 // Purpose  :
683 //================================================================
684 bool GEOMBase_Helper::commitCommand( const char* )
685 {
686   if ( !hasCommand() )
687     return false;
688
689   myCommand->commit();
690   myCommand = 0;
691
692   return true;
693 }
694
695 //================================================================
696 // Function : hasCommand
697 // Purpose  :
698 //================================================================
699 bool GEOMBase_Helper::hasCommand() const
700 {
701   return (bool)myCommand;
702 }
703
704 //================================================================
705 // Function : getOperation
706 // Purpose  :
707 //================================================================
708 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::getOperation()
709 {
710   if ( myOperation->_is_nil() )
711     myOperation = createOperation();
712
713   return myOperation;
714 }
715
716
717
718 //================================================================
719 // Function : checkViewWindow
720 // Purpose  :
721 //================================================================
722 bool GEOMBase_Helper::checkViewWindow()
723 {
724   if ( myViewWindow ){
725     QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
726     for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it )
727       {
728         if ( myViewWindow == it.current() )
729           return true;
730       }
731   }
732   myViewWindow = 0;
733   return false;
734 }
735
736 //================================================================
737 // Function : onAccept
738 // Purpose  : This method should be called from dialog's slots onOk() and onApply()
739 //            It perfroms user input validation, then it
740 //            performs a proper operation and manages transactions, etc.
741 //================================================================
742 bool GEOMBase_Helper::onAccept( const bool publish, const bool useTransaction )
743 {
744   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
745   if ( !appStudy ) return false;
746   _PTR(Study) aStudy = appStudy->studyDS();
747
748   bool aLocked = (_PTR(AttributeStudyProperties) (aStudy->GetProperties()))->IsLocked();
749   if ( aLocked ) {
750     MESSAGE("GEOMBase_Helper::onAccept - ActiveStudy is locked");
751     SUIT_MessageBox::warn1 ( (QWidget*)SUIT_Session::session()->activeApplication()->desktop(),
752                            QObject::tr("WRN_WARNING"),
753                            QObject::tr("WRN_STUDY_LOCKED"),
754                            QObject::tr("BUT_OK") );
755     return false;
756   }
757
758   QString msg;
759   if ( !isValid( msg ) ) {
760     showError( msg );
761     return false;
762   }
763
764   erasePreview( false );
765
766   try {
767     if ( ( !publish && !useTransaction ) || openCommand() ) {
768       SUIT_OverrideCursor wc;
769       SUIT_Session::session()->activeApplication()->putInfo( "" );
770       ObjectList objects;
771       if ( !execute( objects ) || !getOperation()->IsDone() ) {
772         wc.suspend();
773         abortCommand();
774         showError();
775       }
776       else {
777         const int nbObjs = objects.size();
778         int aNumber = 1;
779         for ( ObjectList::iterator it = objects.begin(); it != objects.end(); ++it ) {
780           if ( publish ) {
781             QString aName = getNewObjectName();
782             if ( nbObjs > 1 ) {
783               if (aName.isEmpty())
784                 aName = getPrefix(*it);
785               if (nbObjs <= 30) {
786                 // Try to find a unique name
787                 aName = GEOMBase::GetDefaultName(aName);
788               } else {
789                 // Don't check name uniqueness in case of numerous objects
790                 aName = aName + "_" + QString::number(aNumber++);
791               }
792             } else {
793               // PAL6521: use a prefix, if some dialog box doesn't reimplement getNewObjectName()
794               if ( aName.isEmpty() )
795                 aName = GEOMBase::GetDefaultName( getPrefix( *it ) );
796             }
797             addInStudy( *it, aName.latin1() );
798             // updateView=false
799             display( *it, false );
800           }
801           else {
802             // asv : fix of PAL6454. If publish==false, then the original shape
803             // was modified, and need to be re-cached in GEOM_Client before redisplay
804             clearShapeBuffer( *it );
805             // withChildren=true, updateView=false
806             redisplay( *it, true, false );
807           }
808         }
809
810         if ( nbObjs ) {
811           commitCommand();
812           updateObjBrowser();
813           SUIT_Session::session()->activeApplication()->putInfo( QObject::tr("GEOM_PRP_DONE") );
814         }
815         else
816           abortCommand();
817       }
818     }
819   }
820   catch( const SALOME::SALOME_Exception& e ) {
821     SalomeApp_Tools::QtCatchCorbaException( e );
822     abortCommand();
823   }
824
825   updateViewer();
826
827   return true;
828 }
829
830
831 //================================================================
832 // Function : showError
833 // Purpose  : Shows a message box with infromation about an error taken from getOperation()->GetErrorCode()
834 //================================================================
835 void GEOMBase_Helper::showError()
836 {
837   QString msg;
838   if ( !getOperation()->_is_nil() )
839     msg = QObject::tr( getOperation()->GetErrorCode() );
840
841   if ( msg.isEmpty() )
842     msg = QObject::tr( "GEOM_PRP_ABORT" );
843
844   SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(),
845                            QObject::tr( "GEOM_ERROR_STATUS" ),
846                            msg,
847                            QObject::tr( "BUT_OK" ) );
848 }
849
850 //================================================================
851 // Function : showError
852 // Purpose  : Shows a error message followed by <msg>
853 //================================================================
854 void GEOMBase_Helper::showError( const QString& msg )
855 {
856   QString str( QObject::tr( "GEOM_INCORRECT_INPUT" ) );
857   if ( !msg.isEmpty() )
858     str += "\n" + msg;
859   SUIT_MessageBox::error1(SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ), str, QObject::tr( "BUT_OK" ) );
860 }
861
862 //////////////////////////////////////////////////////////////////
863 // Virtual methods to be redefined in dialogs
864 //////////////////////////////////////////////////////////////////
865
866 //================================================================
867 // Function : createOperation
868 // Purpose  : Redefine this method to return proper IOperation reference
869 //================================================================
870 GEOM::GEOM_IOperations_ptr GEOMBase_Helper::createOperation()
871 {
872   GEOM::GEOM_IOperations_var aNilOp;
873   return aNilOp._retn();
874 }
875
876 //================================================================
877 // Function : isValid
878 // Purpose  : Called by onAccept(). Redefine this method to check validity of user input in dialog boxes.
879 //================================================================
880 bool GEOMBase_Helper::isValid( QString& )
881 {
882   return true;
883 }
884
885 //================================================================
886 // Function : execute
887 // Purpose  : This method is called by onAccept().
888 //            It should perform the required operation and put all new or modified objects into
889 //            <objects> argument.Should return <false> if some error occurs during its execution.
890 //================================================================
891 bool GEOMBase_Helper::execute( ObjectList& objects )
892 {
893   return false;
894 }
895
896 //================================================================
897 // Function : getFather
898 // Purpose  : This method is called by addInStudy(). It should return a father object
899 //            for <theObj> or a nil reference if <theObj> should be published
900 //            as a top-level object.
901 //================================================================
902 GEOM::GEOM_Object_ptr GEOMBase_Helper::getFather( GEOM::GEOM_Object_ptr theObj )
903 {
904   return GEOM::GEOM_Object::_nil();
905 }
906
907 //================================================================
908 // Function : getNewObjectName
909 // Purpose  : Redefine this method to return proper name for a new object
910 //================================================================
911 const char* GEOMBase_Helper::getNewObjectName() const
912 {
913   return "";
914 }
915
916 //================================================================
917 // Function : getPrefix
918 // Purpose  : Get prefix for name of created object
919 //================================================================
920 QString GEOMBase_Helper::getPrefix( GEOM::GEOM_Object_ptr theObj ) const
921 {
922   if ( !myPrefix.isEmpty() || theObj->_is_nil() )
923     return myPrefix;
924
925   //TopoDS_Shape aShape;
926   //if ( !GEOMBase::GetShape( theObj, aShape ) )
927   //  return "";
928   //
929   //long aType = aShape.ShapeType();
930   GEOM::shape_type aType = theObj->GetShapeType();
931
932   switch ( aType )
933   {
934     case GEOM::VERTEX   : return QObject::tr( "GEOM_VERTEX" );
935     case GEOM::EDGE     : return QObject::tr( "GEOM_EDGE" );
936     case GEOM::WIRE     : return QObject::tr( "GEOM_WIRE" );
937     case GEOM::FACE     : return QObject::tr( "GEOM_FACE" );
938     case GEOM::SHELL    : return QObject::tr( "GEOM_SHELL" );
939     case GEOM::SOLID    : return QObject::tr( "GEOM_SOLID" );
940     case GEOM::COMPSOLID: return QObject::tr( "GEOM_COMPOUNDSOLID" );
941     case GEOM::COMPOUND : return QObject::tr( "GEOM_COMPOUND" );
942     default : return "";
943   }
944 }
945
946 //================================================================
947 // Function : selectedIO
948 // Purpose  : Return the list of selected SALOME_InteractiveObject's
949 //================================================================
950 const SALOME_ListIO& GEOMBase_Helper::selectedIO()
951 {
952   mySelected.Clear();
953
954   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
955   if ( app ) {
956     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
957     if ( aSelMgr )
958       aSelMgr->selectedObjects( mySelected );
959   }
960
961   return mySelected;
962 }
963
964 //================================================================
965 // Function : IObjectCount
966 // Purpose  : Return the number of selected objects
967 //================================================================
968 int GEOMBase_Helper::IObjectCount()
969 {
970   return selectedIO().Extent();
971 }
972
973 //================================================================
974 // Function : firstIObject
975 // Purpose  :  Return the first selected object in the selected object list
976 //================================================================
977 Handle(SALOME_InteractiveObject) GEOMBase_Helper::firstIObject()
978 {
979   const SALOME_ListIO& aList = selectedIO();
980   return aList.Extent() > 0 ? aList.First() : Handle(SALOME_InteractiveObject)();
981 }
982
983 //================================================================
984 // Function : lastIObject
985 // Purpose  : Return the last selected object in the selected object list
986 //================================================================
987 Handle(SALOME_InteractiveObject) GEOMBase_Helper::lastIObject()
988 {
989   const SALOME_ListIO& aList = selectedIO();
990   return aList.Extent() > 0 ? aList.Last() : Handle(SALOME_InteractiveObject)();
991 }
992
993 //================================================================
994 // Function : getDesktop
995 // Purpose  : Returns myDesktop field.  Initialized in constructor, usually as dynamic_cast<SUIT_Desktop*>(parentWidget())
996 //================================================================
997 SUIT_Desktop* GEOMBase_Helper::getDesktop() const
998 {
999   return myDesktop;
1000 }
1001
1002 //================================================================
1003 // Function : selectObjects
1004 // Purpose  : Selects list of objects 
1005 //================================================================
1006 bool GEOMBase_Helper::selectObjects( ObjectList& objects )
1007 {
1008   SUIT_DataOwnerPtrList aList;
1009   ObjectList::iterator anIter;
1010   for ( anIter = objects.begin(); anIter != objects.end(); ++anIter )
1011   {
1012     string entry = getEntry( *anIter );
1013     QString aEntry( entry.c_str() );
1014     LightApp_DataOwner* anOwher = new LightApp_DataOwner( aEntry );
1015     aList.append( anOwher );
1016   }
1017   
1018   SUIT_Session* session = SUIT_Session::session();
1019   SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1020   if ( !app )
1021     return false;
1022
1023   LightApp_SelectionMgr* aMgr = app->selectionMgr();
1024   if ( !aMgr )
1025     return false;
1026   
1027   aMgr->setSelected( aList, false );
1028   
1029   return true;
1030 }
1031   
1032
1033   
1034   
1035   
1036