Salome HOME
IPAL9285,9292: Incorrect popups
[modules/visu.git] / src / VISUGUI / VisuGUI_Tools.cxx
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : VisuGUI_Tools.cxx
25 //  Author : Sergey Anikin
26 //  Module : VISU
27
28
29 #include "VisuGUI_Tools.h"
30
31 #include "VISU_Gen_i.hh"
32 #include "VISU_Prs3d_i.hh"
33 #include "VISU_Result_i.hh"
34 #include "VISU_Table_i.hh"
35 #include "VISU_Mesh_i.hh"
36 #include "VISU_ViewManager_i.hh"
37
38 #include "VISU_Actor.h"
39
40 #include "SalomeApp_Module.h"
41 #include "SalomeApp_Study.h"
42 #include "SalomeApp_Application.h"
43 #include "SalomeApp_SelectionMgr.h"
44
45 #include "SALOME_ListIO.hxx"
46 #include "SALOME_ListIteratorOfListIO.hxx"
47
48 #include "SVTK_ViewWindow.h"
49 #include "SVTK_ViewModel.h"
50 #include "SVTK_Functor.h"
51
52 #include "VTKViewer_Algorithm.h"
53
54 #include "SPlot2d_ViewModel.h"
55 #include "Plot2d_ViewFrame.h"
56
57 #include "SUIT_Session.h"
58 #include "SUIT_MessageBox.h"
59
60 #include <vtkRenderer.h>
61 #include <vtkActorCollection.h>
62
63
64 //=============================================================================
65 namespace VISU
66 {
67   SUIT_Desktop*
68   GetDesktop(const CAM_Module* theModule)
69   {
70     return theModule->application()->desktop();
71   }
72
73   SalomeApp_SelectionMgr*
74   GetSelectionMgr(const SalomeApp_Module* theModule)
75   {
76     return theModule->getApp()->selectionMgr();
77   }
78
79   SalomeApp_Study*
80   GetAppStudy(const CAM_Module* theModule)
81   {
82     return
83       dynamic_cast<SalomeApp_Study*>(theModule->application()->activeStudy());
84   }
85
86   _PTR(Study)
87   GetCStudy(const SalomeApp_Study* theStudy)
88   {
89     return theStudy->studyDS();
90   }
91
92   bool
93   IsStudyLocked( _PTR(Study) theStudy )
94   {
95     if( theStudy )
96       return theStudy->GetProperties()->IsLocked();
97     return true;
98   }
99
100   bool
101   CheckLock( _PTR(Study) theStudy )
102   {
103     if(IsStudyLocked(theStudy))
104       throw std::runtime_error(QObject::tr("WRN_STUDY_LOCKED").latin1());
105     return false;
106   }
107
108   VISU_Gen_i*
109   GetVisuGen(const CAM_Module* theModule)
110   {
111     static VISU_Gen_i* aGen = NULL;
112     if(!aGen){
113       SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
114       Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","VISU");
115       VISU_Gen_var aVISU = VISU_Gen::_narrow(aComponent);
116       if(!CORBA::is_nil(aVISU)){
117         aGen = VISU_Gen_i::GetVisuGenImpl();
118         aGen->SetCurrentStudy(GetDSStudy(GetCStudy(GetAppStudy(theModule))));
119       }
120     }
121     if(!aGen)
122       throw std::runtime_error(QObject::tr("ERR_CANT_FIND_VISU_COMPONENT").latin1());
123     return aGen;
124   }
125
126   SALOME_MED::MED_Gen_var
127   GetMEDEngine()
128   {
129     static SALOME_MED::MED_Gen_var aGen;
130     if(CORBA::is_nil(aGen)){
131       SALOME_LifeCycleCORBA aLCC(SalomeApp_Application::namingService());
132       Engines::Component_var aComponent = aLCC.FindOrLoad_Component("FactoryServer","MED");
133       aGen = SALOME_MED::MED_Gen::_narrow(aComponent);
134     }
135     if(CORBA::is_nil(aGen))
136       throw std::runtime_error(QObject::tr("ERR_CANT_FIND_MED_COMPONENT").latin1());
137     return aGen;
138   }
139
140
141   VISU::Storable::TRestoringMap getMapOfValue (_PTR(SObject) theSObject)
142   {
143     VISU::Storable::TRestoringMap aMap;
144     if (theSObject) {
145       _PTR(GenericAttribute) anAttr;
146       if (theSObject->FindAttribute(anAttr, "AttributeComment")) {
147         _PTR(AttributeComment) aComment (anAttr);
148         std::string aValue = aComment->Value();
149         QString aString (aValue.c_str());
150         VISU::Storable::StrToMap(aString, aMap);
151       }
152     }
153     return aMap;
154   }
155
156   QString getValue (_PTR(SObject) theSObject, QString theKey)
157   {
158     QString aStr("");
159     VISU::Storable::TRestoringMap aMap = getMapOfValue(theSObject);
160     if (!aMap.empty())
161       aStr = VISU::Storable::FindValue(aMap, theKey.latin1());
162     return aStr;
163   }
164
165   //************************************************************
166   // Selection
167   CORBA::Object_var
168   GetSelectedObj(const SalomeApp_Study* theStudy,
169                  const QString& theEntry,
170                  VISU::Storable::TRestoringMap* theMap)
171   {
172     if (!theStudy || theEntry.isEmpty())
173       return CORBA::Object::_nil();
174
175     _PTR(Study) aStudy = GetCStudy(theStudy);
176     _PTR(SObject) aSObject = aStudy->FindObjectID(theEntry.latin1());
177     if (aSObject) {
178       _PTR(GenericAttribute) anAttr;
179       if (theMap && aSObject->FindAttribute(anAttr,"AttributeComment")) {
180         _PTR(AttributeComment) aComment (anAttr);
181         std::string aValue = aComment->Value();
182         QString aString (aValue.c_str());
183         VISU::Storable::StrToMap(aString, *theMap);
184       }
185       return VISU::ClientSObjectToObject(aSObject);
186     }
187     return CORBA::Object::_nil();
188   }
189
190   CORBA::Object_var
191   GetSelectedObj(const SalomeApp_Module* theModule,
192                  Handle(SALOME_InteractiveObject)* theIO,
193                  VISU::Storable::TRestoringMap* theMap)
194   {
195     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
196     SALOME_ListIO aListIO;
197     aSelectionMgr->selectedObjects(aListIO);
198     SALOME_ListIteratorOfListIO anIter(aListIO);
199     if(anIter.More()){
200       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
201       if(theIO)
202         *theIO = anIO;
203       if(anIO->hasEntry())
204         return GetSelectedObj( GetAppStudy(theModule), anIO->getEntry(), theMap );
205     }
206     return CORBA::Object::_nil();
207   }
208
209   void
210   Add(SalomeApp_SelectionMgr* theSelectionMgr,
211       const Handle(SALOME_InteractiveObject)& theIO)
212   {
213     SALOME_ListIO aListIO;
214     theSelectionMgr->selectedObjects(aListIO);
215     aListIO.Append(theIO);
216     theSelectionMgr->setSelectedObjects(aListIO);
217   }
218
219   void
220   Remove(SalomeApp_SelectionMgr* theSelectionMgr,
221          const Handle(SALOME_InteractiveObject)& theIO)
222   {
223     if (theIO.IsNull()) return;
224     SALOME_ListIO aListIO, aNewListIO;
225     theSelectionMgr->selectedObjects(aListIO);
226     SALOME_ListIteratorOfListIO anIter (aListIO);
227     for (; anIter.More(); anIter.Next()) {
228       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
229       if (!anIO->isSame(theIO)) {
230         aNewListIO.Append(theIO);
231       }
232     }
233     theSelectionMgr->setSelectedObjects(aNewListIO);
234   }
235
236   /*!
237    * \brief Check, if "Delete" popup-menu can be put on current selection
238    *
239    * \param theModule - is used to access SelectionManager, Study and VISU_Gen
240    * \retval bool - returns TRUE if all currently selected objects are removable
241    */
242   bool
243   IsRemovableSelected (const SalomeApp_Module* theModule)
244   {
245     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
246     SALOME_ListIO aListIO;
247     aSelectionMgr->selectedObjects(aListIO);
248
249     if (aListIO.Extent() < 1)
250       return false;
251
252     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
253     if (!aStudy) return false;
254
255     // In some cases single selection can have its own popup-menu item for deletion
256     /*if (aListIO.Extent() == 1) {
257       Handle(SALOME_InteractiveObject) anIO = aListIO.First();
258       _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
259       if (aSObject) {
260         VISU::VISUType aType = (VISU::VISUType)getValue(aSObject, "myType").toInt();
261         if (aType == VISU::TVIEW3D) {
262           return false; // special case
263         }
264       }
265     }*/
266
267     SALOME_ListIteratorOfListIO anIter (aListIO);
268     for (; anIter.More(); anIter.Next()) {
269       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
270       if (anIO->hasEntry()) {
271         _PTR(SObject) aSObject = aStudy->FindObjectID(anIO->getEntry());
272         VISU::Storable::TRestoringMap pMap;
273         if (aSObject) {
274           CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(aSObject);
275           if (!CORBA::is_nil(aCORBAObject)) {
276             VISU::RemovableObject_var aRemovableObj = VISU::RemovableObject::_narrow(aCORBAObject);
277             if (CORBA::is_nil(aRemovableObj)) {
278               // Not removable CORBA object
279               return false;
280             }
281           } else {
282             // Can be removed, if lays directly under VISU
283             // (first sub-level) or is a child of such an object
284             string aNAME, aVisuNAME = GetVisuGen(theModule)->ComponentDataType();
285             _PTR(GenericAttribute) anAttr;
286             _PTR(AttributeComment) aComment;
287
288             bool isUnderVISU = false;
289             _PTR(SObject) aFatherSObject = aSObject->GetFather();
290             if (aFatherSObject->FindAttribute(anAttr, "AttributeComment")) {
291               _PTR(AttributeComment) aComment (anAttr);
292               aNAME = aComment->Value();
293               if (aNAME == aVisuNAME) {
294                 isUnderVISU = true;
295               }
296             }
297             if (!isUnderVISU) {
298               // Not directly under VISU component, check father
299               aCORBAObject = VISU::ClientSObjectToObject(aFatherSObject);
300               if (!CORBA::is_nil(aCORBAObject)) {
301                 // Father has IOR
302                 return false;
303               }
304
305               isUnderVISU = false;
306               aFatherSObject = aFatherSObject->GetFather();
307               if (aFatherSObject->FindAttribute(anAttr, "AttributeComment")) {
308                 _PTR(AttributeComment) aComment (anAttr);
309                 aNAME = aComment->Value();
310                 if (aNAME == aVisuNAME) {
311                   isUnderVISU = true;
312                 }
313               }
314               if (!isUnderVISU) {
315                 // Father is not directly under VISU component
316                 return false;
317               }
318             }
319           }
320         }
321       }
322     }
323     return true;
324   }
325
326   //************************************************************
327   // Display/Erase
328
329   void
330   ErasePrs (const SalomeApp_Module* theModule,
331             CORBA::Object_ptr theObject, bool theUpdate)
332   {
333     if (!CORBA::is_nil(theObject)) {
334       VISU::Base_var aBase = VISU::Base::_narrow(theObject);
335       if (CORBA::is_nil(aBase)) return;
336       VISU::VISUType aType = aBase->GetType();
337       switch (aType) {
338       case VISU::TCURVE:
339         {
340           if (VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aBase).in()))
341             PlotCurve(theModule, aCurve, VISU::eErase );
342           break;
343         }
344       case VISU::TCONTAINER:
345         {
346           if (VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aBase).in()))
347             PlotContainer(theModule, aContainer, VISU::eErase );
348           break;
349         }
350       case VISU::TTABLE:
351         {
352           if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aBase).in()))
353             PlotTable(theModule, aTable, VISU::eErase );
354           break;
355         }
356       default:
357         {
358           if (VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aBase).in())) {
359             ErasePrs3d(theModule, aPrsObject);
360             if (theUpdate) {
361               if (SVTK_ViewWindow* vw = GetViewWindow(theModule))
362                 vw->Repaint();
363             }
364           }
365         }
366       } // switch (aType)
367     }
368   }
369
370   void
371   DeleteSObject (SalomeApp_Module* theModule,
372                  _PTR(Study)       theStudy,
373                  _PTR(SObject)     theSObject)
374   {
375     _PTR(ChildIterator) aChildIter = theStudy->NewChildIterator(theSObject);
376     for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
377       _PTR(SObject) aChildSObject = aChildIter->Value();
378       CORBA::Object_var aChildObj = VISU::ClientSObjectToObject(aChildSObject);
379       ErasePrs(theModule, aChildObj);
380     }
381
382     CORBA::Object_var anObj = VISU::ClientSObjectToObject(theSObject);
383     if (!CORBA::is_nil(anObj)) {
384       VISU::RemovableObject_var aRemovableObject = VISU::RemovableObject::_narrow(anObj);
385       if (!CORBA::is_nil(aRemovableObject)) {
386         aRemovableObject->RemoveFromStudy();
387       }
388     } else {
389       // Remove aSObject together with all its sub-objects
390       VISU::RemoveFromStudy(theSObject,
391                             false,  // remove not only IOR attribute, but Object With Children
392                             false); // not Destroy() sub-objects
393     }
394   }
395
396   void
397   DeletePrs3d(SalomeApp_Module* theModule,
398               VISU::Prs3d_i* thePrs,
399               const Handle(SALOME_InteractiveObject)& theIO)
400   {
401     if (!thePrs)
402       return;
403     if (CheckLock(GetCStudy(GetAppStudy(theModule))))
404       return;
405     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
406     CORBA::String_var anEntry = aSObject->GetID();
407     SalomeApp_SelectionMgr* aSelectionMgr = GetSelectionMgr(theModule);
408     Remove(aSelectionMgr,theIO);
409     TViewWindows aViewWindows = GetViews(theModule);
410     for(int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++){
411       SVTK_ViewWindow* aView = aViewWindows[i];
412       if(VISU_Actor* anActor = FindActor(aView,anEntry.in())){
413         aView->RemoveActor(anActor);
414         anActor->Delete();
415       }
416     }
417     thePrs->RemoveFromStudy();
418     theModule->updateObjBrowser(); //update Object browser
419   }
420
421   void
422   ErasePrs3d(const SalomeApp_Module* theModule,
423              VISU::Prs3d_i* thePrs)
424   {
425     if ( SVTK_ViewWindow* vw = GetViewWindow( theModule ) ){
426       VISU_Actor* anVISUActor = FindActor( vw, thePrs );
427       if (anVISUActor) {
428         anVISUActor->VisibilityOff();
429       }
430     }
431   }
432
433   //************************************************************
434   // Presentation management
435
436   void
437   ChangeRepresentation (const SalomeApp_Module* theModule,
438                         VISU::PresentationType  theType)
439   {
440     SUIT_ViewWindow* aView = GetActiveView(theModule, VTKViewer_Viewer::Type());
441     if (!aView) return;
442     SVTK_ViewWindow* vw  = (SVTK_ViewWindow*) aView;
443
444     Handle(SALOME_InteractiveObject) anIO;
445     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
446     if (CORBA::is_nil(anObject)) return;
447     PortableServer::ServantBase_var aServant = VISU::GetServant(anObject);
448     if (!aServant.in()) return;
449
450     VISU::Prs3d_i* aPrs3d = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
451     if (aPrs3d) {
452       if (VISU_Actor* anActor = GetActor(aPrs3d, vw)) {
453         switch (theType) {
454         case VISU::SHRINK:
455           if (anActor->IsShrunk())
456             anActor->UnShrink();
457           else
458             anActor->SetShrink();
459           break;
460         default:
461           if (VISU::Mesh_i* aMesh = dynamic_cast<VISU::Mesh_i*>(aPrs3d)) {
462             aMesh->SetPresentationType(theType);
463             RecreateActor(theModule, aMesh);
464           } else {
465             anActor->SetRepresentation(theType);
466           }
467         }
468         vw->Repaint();
469       }
470     }
471   }
472
473   //************************************************************
474   // SObject type
475
476   bool
477   CheckTimeStamp(const SalomeApp_Module* theModule,
478                  _PTR(SObject)&          theSObject,
479                  Handle(SALOME_InteractiveObject)* theIO)
480   {
481     Handle(SALOME_InteractiveObject) anIO;
482     CORBA::Object_var anObject = GetSelectedObj(theModule, &anIO);
483     if (theIO)
484       *theIO = anIO;
485     if (!anIO.IsNull() && anIO->hasEntry()){
486       _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
487       theSObject = aStudy->FindObjectID(anIO->getEntry());
488       QString aValue = getValue(theSObject,"myType");
489       if (aValue.toInt() == int(VISU::TTIMESTAMP))
490         return true;
491     }
492     SUIT_MessageBox::warn1(GetDesktop(theModule),
493                            QObject::tr("WRN_VISU"),
494                            QObject::tr("WRN_NO_AVAILABLE_DATA"),
495                            QObject::tr("BUT_OK") );
496     return false;
497   }
498
499   VISU::Result_i*
500   CheckResult(const SalomeApp_Module* theModule,
501               _PTR(SObject)           theSource,
502               VISU::Result_var&       theResult)
503   {
504     _PTR(SObject) aSObj = theSource->GetFather();
505     if (!aSObj)
506       return NULL;
507
508     aSObj = aSObj->GetFather();
509     if (!aSObj)
510       return NULL;
511
512     aSObj = aSObj->GetFather();
513     if (!aSObj)
514       return NULL;
515
516     CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObj);
517     if (CORBA::is_nil(anObject)) {
518       aSObj = aSObj->GetFather();
519       if (!aSObj)
520         return NULL;
521       anObject = VISU::ClientSObjectToObject(aSObj);
522     }
523
524     if (CORBA::is_nil(anObject))
525       return NULL;
526
527     theResult = VISU::Result::_narrow(anObject);
528     VISU::Result_i* pResult = dynamic_cast<VISU::Result_i*>(VISU::GetServant(anObject).in());
529     if (pResult == NULL)
530       SUIT_MessageBox::warn1(GetDesktop(theModule),
531                              QObject::tr("WRN_VISU"),
532                              QObject::tr("WRN_NO_AVAILABLE_DATA"),
533                              QObject::tr("BUT_OK"));
534     return pResult;
535   }
536
537   //************************************************************
538   // Views
539
540   SUIT_ViewWindow* GetActiveView(const SalomeApp_Module* theModule, QString theType)
541   {
542     if(SalomeApp_Application* anApp = theModule->getApp()){
543       if(SUIT_ViewManager* aViewManager = anApp->activeViewManager()){
544         if (!theType.isNull()) {
545           if (aViewManager->getType() != theType)
546             return 0;
547         }
548         return aViewManager->getActiveView();
549       }
550     }
551     return 0;
552   }
553
554   //************************************************************
555   // VTK View
556
557   TViewWindows
558   GetViews(const SalomeApp_Module* theModule)
559   {
560     TViewWindows aViewWindows;
561     if(SalomeApp_Application* anApp = theModule->getApp()){
562       ViewManagerList aViewManagerList;
563       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
564       QPtrListIterator<SUIT_ViewManager> anIter(aViewManagerList);
565       while(SUIT_ViewManager* aViewManager = anIter.current()){
566         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
567         for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
568           if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
569             if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
570               aViewWindows.push_back(aView);
571         }
572         ++anIter;
573       }
574     }
575     return aViewWindows;
576   }
577
578   SVTK_ViewWindow*
579   GetViewWindow(const SalomeApp_Module* theModule, const bool theCreate )
580   {
581     if(SalomeApp_Application* anApp = theModule->getApp()){
582       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( SVTK_Viewer::Type(), theCreate )){
583         if(SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()){
584           return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
585         }
586       }
587     }
588     return NULL;
589   }
590
591
592   SVTK_ViewWindow*
593   GetViewWindow()
594   {
595     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>
596       (SUIT_Session::session()->activeApplication());
597     if (anApp) {
598       if (SUIT_ViewManager* aViewManager = anApp->activeViewManager()) {
599         if (aViewManager->getType() == SVTK_Viewer::Type()) {
600           if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
601             return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
602           }
603         }
604       }
605     }
606     return NULL;
607   }
608
609   VISU_Actor*
610   PublishInView(const SalomeApp_Module* theModule,
611                 VISU::Prs3d_i* thePrs)
612   {
613     VISU_Actor* aActor = NULL;
614     if(!thePrs)
615       return aActor;
616     if(SVTK_ViewWindow* aView = GetViewWindow(theModule)){
617       QApplication::setOverrideCursor( Qt::waitCursor );
618       if(aActor = thePrs->CreateActor()){
619         aView->AddActor(aActor);
620       }
621       QApplication::restoreOverrideCursor();
622     }
623     return aActor;
624   }
625
626   VISU_Actor*
627   UpdateViewer(const SalomeApp_Module* theModule,
628                VISU::Prs3d_i* thePrs,
629                bool theDispOnly)
630   {
631     SVTK_ViewWindow* vw = GetViewWindow( theModule );
632     if (!vw) return NULL;
633
634     vtkRenderer *aRen = vw->getRenderer();
635     vtkActorCollection *anActColl = aRen->GetActors();
636
637     vtkActor *anActor;
638     VISU_Actor* anVISUActor = NULL;
639     VISU_Actor* aResActor = NULL;
640     for(anActColl->InitTraversal(); (anActor = anActColl->GetNextActor()) != NULL; ){
641       if(!SALOME_Actor::SafeDownCast(anActor))
642         continue;
643       if(anActor->IsA("VISU_Actor")){
644         anVISUActor = VISU_Actor::SafeDownCast(anActor);
645         VISU::Prs3d_i* aPrs = anVISUActor->GetPrs3d();
646         if(aPrs == NULL) continue;
647         if (thePrs == aPrs) {
648           aResActor = anVISUActor->GetParent();
649           thePrs->UpdateActor(aResActor);
650           aResActor->VisibilityOn();
651
652         } else if (theDispOnly) {
653           anVISUActor->GetParent()->VisibilityOff();
654         } else {
655         }
656       } else if (theDispOnly && anActor->GetVisibility()) {
657         anActor->VisibilityOff();
658       } else {
659       }
660     }
661     if (aResActor)
662       return aResActor;
663
664     anVISUActor = PublishInView( theModule, thePrs );
665     return anVISUActor;
666   }
667
668   void
669   RepaintViewWindows (const SalomeApp_Module* theModule,
670                       const Handle(SALOME_InteractiveObject)& theIObject)
671   {
672     TViewWindows aViewWindows;
673     if (SalomeApp_Application* anApp = theModule->getApp()) {
674       ViewManagerList aViewManagerList;
675       anApp->viewManagers(SVTK_Viewer::Type(),aViewManagerList);
676       QPtrListIterator<SUIT_ViewManager> anIter (aViewManagerList);
677       while (SUIT_ViewManager* aViewManager = anIter.current()) {
678         QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
679         for (int i = 0, iEnd = aViews.size(); i < iEnd; i++) {
680           if (SUIT_ViewWindow* aViewWindow = aViews.at(i)) {
681             if (SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
682               if (vw->isVisible(theIObject)) {
683                 vw->getRenderer()->ResetCameraClippingRange();
684                 vw->Repaint();
685                 vw->highlight(theIObject, true, true);
686               }
687             }
688           }
689         }
690         ++anIter;
691       }
692     }
693   }
694
695   VISU_Actor*
696   FindActor(SVTK_ViewWindow* theViewWindow,
697             const char* theEntry)
698   {
699     using namespace VTK;
700     if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
701       if(vtkActorCollection* aCollection = aRenderer->GetActors()){
702         if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
703           return anActor->GetParent();
704         }
705       }
706     }
707     return NULL;
708   }
709
710   VISU_Actor*
711   FindActor(SVTK_ViewWindow* theViewWindow,
712             VISU::Prs3d_i* thePrs)
713   {
714     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
715     CORBA::String_var anEntry = aSObject->GetID();
716     return FindActor(theViewWindow,anEntry.in());
717   }
718
719   void
720   RecreateActor (const SalomeApp_Module* theModule,
721                  VISU::Prs3d_i* thePrs)
722   {
723     QApplication::setOverrideCursor(Qt::waitCursor);
724
725     SALOMEDS::SObject_var aSObject = thePrs->GetSObject();
726     CORBA::String_var anEntry = aSObject->GetID();
727
728     try {
729       thePrs->Update();
730
731       TViewWindows aViewWindows = GetViews(theModule);
732       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
733         SVTK_ViewWindow* aView = aViewWindows[i];
734         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
735           thePrs->UpdateActor(anActor);
736         }
737       }
738     } catch (std::runtime_error& ex) {
739       INFOS(ex.what());
740       QApplication::restoreOverrideCursor();
741       SUIT_MessageBox::warn1 (GetDesktop(theModule), QObject::tr("WRN_VISU"),
742                               QObject::tr("ERR_CANT_BUILD_PRESENTATION") + " " + QObject::tr(ex.what()),
743                               QObject::tr("BUT_OK"));
744
745       TViewWindows aViewWindows = GetViews(theModule);
746       for (int i = 0, iEnd = aViewWindows.size(); i < iEnd; i++) {
747         SVTK_ViewWindow* aView = aViewWindows[i];
748         if (VISU_Actor* anActor = FindActor(aView, anEntry.in())) {
749           aView->RemoveActor(anActor);
750           anActor->Delete();
751         }
752       }
753       return;
754     }
755     QApplication::restoreOverrideCursor();
756   }
757
758   //************************************************************
759   // Plot2d View
760
761   SPlot2d_Viewer*
762   GetPlot2dViewer(const SalomeApp_Module* theModule, const bool theCreate)
763   {
764     if(SalomeApp_Application* anApp = theModule->getApp()){
765       if(SUIT_ViewManager* aViewManager = anApp->getViewManager( Plot2d_Viewer::Type(), theCreate )){
766         return dynamic_cast<SPlot2d_Viewer*>(aViewManager->getViewModel());
767       }
768     }
769     return NULL;
770   }
771
772   // Internal function used by several public functions below
773   void
774   UpdateCurve(VISU::Curve_i* theCurve,
775               Plot2d_ViewFrame* aPlot,
776               SPlot2d_Curve* plotCurve,
777               int theDisplaying)
778   {
779     if ( theDisplaying == VISU::eErase ) {
780       if ( plotCurve )
781         aPlot->eraseCurve( plotCurve, false );
782     }
783     else if ( theDisplaying == VISU::eDisplay || theDisplaying == VISU::eDisplayOnly ) {
784       if ( plotCurve ) {
785         plotCurve->setHorTitle( theCurve->GetHorTitle().c_str() );
786         //plotCurve->setVerTitle( ( theCurve->GetVerTitle().c_str() ) );
787         plotCurve->setVerTitle( theCurve->GetName() );
788         plotCurve->setHorUnits( theCurve->GetHorUnits().c_str() );
789         plotCurve->setVerUnits( theCurve->GetVerUnits().c_str() );
790         double* xList = 0;
791         double* yList = 0;
792         int     nbPoints = theCurve->GetData( xList, yList );
793         if ( nbPoints > 0 && xList && yList ) {
794           plotCurve->setData( xList, yList, nbPoints );
795         }
796         if ( !theCurve->IsAuto() ) {
797           plotCurve->setLine( (Plot2d_Curve::LineType)theCurve->GetLine(), theCurve->GetLineWidth() );
798           plotCurve->setMarker( (Plot2d_Curve::MarkerType)theCurve->GetMarker() );
799           SALOMEDS::Color color = theCurve->GetColor();
800           plotCurve->setColor( QColor( (int)(color.R*255.), (int)(color.G*255.), (int)(color.B*255.) ) );
801         }
802         plotCurve->setAutoAssign( theCurve->IsAuto() );
803         aPlot->displayCurve( plotCurve, false );
804       }
805       else {
806         Plot2d_Curve* crv = theCurve->CreatePresentation();
807         if ( crv ) {
808           aPlot->displayCurve( crv, false );
809           theCurve->SetLine( (VISU::Curve::LineType)crv->getLine(), crv->getLineWidth() );
810           theCurve->SetMarker( (VISU::Curve::MarkerType)crv->getMarker());
811           SALOMEDS::Color newColor;
812           newColor.R = crv->getColor().red()/255.;
813           newColor.G = crv->getColor().green()/255.;
814           newColor.B = crv->getColor().blue()/255.;
815           theCurve->SetColor( newColor );
816           crv->setAutoAssign( theCurve->IsAuto() );
817         }
818       }
819     }
820   }
821
822   void
823   PlotTable(const SalomeApp_Module* theModule,
824             VISU::Table_i* table,
825             int theDisplaying)
826   {
827     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true ); // create if necessary
828     if ( !aView )
829       return;
830     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
831     if ( !aPlot )
832       return;
833
834     if ( theDisplaying == VISU::eDisplayOnly )
835       aPlot->EraseAll();
836     QList<Plot2d_Curve> clist;
837     aPlot->getCurves( clist );
838     _PTR(Study) aStudy = GetCStudy( GetAppStudy( theModule ) );
839     _PTR(SObject) TableSO = aStudy->FindObjectID( table->GetEntry().latin1() );
840     if ( TableSO ) {
841       _PTR(ChildIterator) Iter = aStudy->NewChildIterator( TableSO );
842       for ( ; Iter->More(); Iter->Next() ) {
843         CORBA::Object_var childObject = VISU::ClientSObjectToObject( Iter->Value() );
844         if( !CORBA::is_nil( childObject ) ) {
845           CORBA::Object_ptr aCurve = VISU::Curve::_narrow( childObject );
846           if( !CORBA::is_nil( aCurve ) ) {
847             VISU::Curve_i* theCurve = dynamic_cast<VISU::Curve_i*>(VISU::GetServant(aCurve).in());
848             SPlot2d_Curve* plotCurve = 0;
849             SPlot2d_Curve* tmpCurve;
850             for ( int i = 0; i < clist.count(); i++ ) {
851               tmpCurve = dynamic_cast<SPlot2d_Curve*>( clist.at( i ) );
852               if (tmpCurve && tmpCurve->hasIO() &&
853                   !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
854                 plotCurve = tmpCurve;
855                 break;
856               }
857             }
858
859             UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
860
861             if ( theDisplaying == VISU::eErase && plotCurve ) {
862               clist.remove( plotCurve );
863             }
864           }
865         }
866       }
867       aPlot->Repaint();
868     }
869   }
870
871   void
872   PlotCurve(const SalomeApp_Module* theModule,
873             VISU::Curve_i* theCurve,
874             int theDisplaying)
875   {
876     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
877     if ( !aView )
878       return;
879     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
880     if ( !aPlot )
881       return;
882
883 //  if ( theDisplaying == VISU::eDisplayOnly )
884 //    aPlot->EraseAll();
885     QList<Plot2d_Curve> clist;
886     aPlot->getCurves( clist );
887     SPlot2d_Curve* plotCurve = 0;
888     SPlot2d_Curve* tmpCurve;
889     for (int i = 0; i < clist.count(); i++) {
890       tmpCurve = dynamic_cast<SPlot2d_Curve*>(clist.at(i));
891       if (tmpCurve && tmpCurve->hasIO() &&
892           !strcmp(tmpCurve->getIO()->getEntry(), theCurve->GetEntry())) {
893         plotCurve = tmpCurve;
894       } else if (theDisplaying == VISU::eDisplayOnly) {
895         aPlot->eraseCurve(clist.at(i));
896       }
897     }
898
899     UpdateCurve(theCurve, aPlot, plotCurve, theDisplaying);
900
901     aPlot->Repaint();
902   }
903
904   void
905   PlotContainer(const SalomeApp_Module* theModule,
906                 VISU::Container_i* container,
907                 int theDisplaying)
908   {
909     SPlot2d_Viewer* aView = GetPlot2dViewer( theModule, true );
910     if ( !aView )
911       return;
912     Plot2d_ViewFrame* aPlot = aView->getActiveViewFrame();
913     if ( !aPlot )
914       return;
915
916     if ( theDisplaying == VISU::eDisplayOnly )
917       aPlot->EraseAll();
918     QList<Plot2d_Curve> clist;
919     aPlot->getCurves( clist );
920     if ( container->GetNbCurves() > 0 ) {
921       int nbCurves = container->GetNbCurves();
922       for ( int k = 1; k <= nbCurves; k++ ) {
923         VISU::Curve_i* theCurve = container->GetCurve( k );
924         if ( theCurve && theCurve->IsValid() ) {
925           SPlot2d_Curve* plotCurve = dynamic_cast<SPlot2d_Curve*>
926             (aView->getCurveByIO(new SALOME_InteractiveObject (theCurve->GetEntry(), "", "")));
927
928           UpdateCurve( theCurve, aPlot, plotCurve, theDisplaying );
929
930           if ( plotCurve && theDisplaying == VISU::eErase ) {
931             clist.remove( plotCurve );
932           }
933         }
934       }
935     }
936     aPlot->Repaint();
937   }
938
939   void
940   CreatePlot(SalomeApp_Module* theModule,
941              _PTR(SObject) theTableSO)
942   {
943     if ( IsSObjectTable(theTableSO) ) {
944       CORBA::Object_var aTable = VISU::ClientSObjectToObject(theTableSO);
945       CORBA::Object_var aContainer = GetVisuGen( theModule )->CreateContainer();
946       if ( !CORBA::is_nil( aTable ) && !CORBA::is_nil( aContainer ) ) {
947         VISU::Table_i*     pTable     = dynamic_cast<VISU::Table_i*>(VISU::GetServant(aTable).in());
948         VISU::Container_i* pContainer = dynamic_cast<VISU::Container_i*>(VISU::GetServant(aContainer).in());
949
950         if ( pContainer && pTable ) {
951           for ( int i = 2; i <= pTable->GetNbRows(); i++ ) {
952             CORBA::Object_var aNewCurve = GetVisuGen( theModule )->CreateCurve( pTable->_this(), 1, i );
953             if( !CORBA::is_nil( aNewCurve ) ) {
954               VISU::Curve_i* pCrv = dynamic_cast<VISU::Curve_i*>( VISU::GetServant(aNewCurve).in() );
955               if ( pCrv ) {
956                 pContainer->AddCurve( pCrv->_this() );
957               }
958             }
959           }
960           theModule->updateObjBrowser();
961           PlotContainer( theModule, pContainer, VISU::eDisplay );
962         }
963       }
964     }
965   }
966
967   //************************************************************
968   // Others
969
970   void CreateMesh (const SalomeApp_Module* theModule,
971                     const Handle(SALOME_InteractiveObject)& theIO)
972   {
973     _PTR(Study) aStudy = GetCStudy(GetAppStudy(theModule));
974     //if (CheckLock(aStudy))
975     //  return;
976
977     _PTR(SObject) aResultSObj = aStudy->FindObjectID(theIO->getEntry());
978
979     // Get VISU::Result
980     VISU::Result_var aResult;
981     VISU::Result_i* pResult = CheckResult(theModule, aResultSObj, aResult);
982     if (pResult == NULL)
983       return;
984
985     Storable::TRestoringMap aMap = getMapOfValue(aResultSObj);
986     bool isExist;
987     string aComment = Storable::FindValue(aMap,"myComment",&isExist).latin1();
988     if (!isExist)
989       return;
990
991     CORBA::Object_var aMesh;
992     string aMeshName = Storable::FindValue(aMap,"myMeshName").latin1();
993 #ifdef CHECKTIME
994     Utils_Timer timer;
995     timer.Start();
996 #endif
997     if (aComment == "ENTITY") {
998       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myId").toInt();
999       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity))
1000         aMesh = GetVisuGen(theModule)->MeshOnEntity(aResult,aMeshName.c_str(),anEntity);
1001     } else if (aComment == "FAMILY") {
1002       VISU::Entity anEntity = (VISU::Entity)Storable::FindValue(aMap,"myEntityId").toInt();
1003       string aFamilyName = Storable::FindValue(aMap,"myName").latin1();
1004       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),anEntity,aFamilyName.c_str()))
1005         aMesh = GetVisuGen(theModule)->FamilyMeshOnEntity
1006           (aResult,aMeshName.c_str(),anEntity,aFamilyName.c_str());
1007     } else if (aComment == "GROUP") {
1008       string aGroupName = Storable::FindValue(aMap,"myName").latin1();
1009       if (VISU::Mesh_i::IsPossible(pResult,aMeshName.c_str(),aGroupName.c_str()))
1010         aMesh = GetVisuGen(theModule)->GroupMesh(aResult,aMeshName.c_str(),aGroupName.c_str());
1011     }
1012 #ifdef CHECKTIME
1013     timer.Stop();
1014     MESSAGE("VisuGUI::CreateMesh() - CREATE MESH");
1015     timer.Show();
1016 #endif
1017
1018     QApplication::restoreOverrideCursor();
1019     VISU::Mesh_i* pPresent = NULL;
1020     if (!CORBA::is_nil(aMesh))
1021       pPresent = dynamic_cast<VISU::Mesh_i*>(VISU::GetServant(aMesh).in());
1022     if (pPresent == NULL) {
1023       SUIT_MessageBox::warn1 (GetDesktop(theModule),
1024                               QObject::tr("WRN_VISU"),
1025                               QObject::tr("ERR_CANT_BUILD_PRESENTATION"),
1026                               QObject::tr("BUT_OK"));
1027       return;
1028     }
1029
1030     if (SVTK_ViewWindow* aView = GetViewWindow(theModule)) {
1031       try {
1032 #ifdef CHECKTIME
1033         Utils_Timer timer;
1034         timer.Start();
1035 #endif
1036         PublishInView(theModule, pPresent);
1037         aView->onFitAll();
1038 #ifdef CHECKTIME
1039         timer.Stop();
1040         MESSAGE("VisuGUI::CreateMesh() - DISPLAY MESH");
1041         timer.Show();
1042 #endif
1043         theModule->application()->putInfo(QObject::tr("INF_DONE"));
1044       } catch (std::runtime_error& exc) {
1045         INFOS(exc.what());
1046         SUIT_MessageBox::warn1 (GetDesktop(theModule),
1047                                 QObject::tr("WRN_VISU"),
1048                                 QObject::tr("ERR_CANT_CREATE_ACTOR") + " " + QObject::tr(exc.what()),
1049                                 QObject::tr("BUT_OK"));
1050       }
1051     }
1052   }
1053
1054   // ========================================================================================
1055   // GetPrs3dList: find list of presentations for the given object
1056   // ========================================================================================
1057   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1058                                             const Handle(SALOME_InteractiveObject)& theIO)
1059   {
1060     std::vector<VISU::Prs3d_i*> aList;
1061     if (!theIO.IsNull() && theIO->hasEntry()) {
1062       _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1063       _PTR(SObject) aSObject = aCStudy->FindObjectID(theIO->getEntry());
1064       aList = GetPrs3dList(theModule, aSObject);
1065     }
1066     return aList;
1067   }
1068
1069   std::vector<VISU::Prs3d_i*> GetPrs3dList (const SalomeApp_Module* theModule,
1070                                             _PTR(SObject) theObject)
1071   {
1072     std::vector<VISU::Prs3d_i*> aList; int k = 0;
1073     if (!theObject)
1074       return aList;
1075
1076     _PTR(Study) aCStudy = GetCStudy(GetAppStudy(theModule));
1077
1078     CORBA::Object_var aCORBAObject = VISU::ClientSObjectToObject(theObject);
1079     if (!CORBA::is_nil(aCORBAObject)) {
1080       VISU::Base_var aVisuObj = VISU::Base::_narrow(aCORBAObject);
1081       if (!CORBA::is_nil(aVisuObj)) {
1082         VISU::VISUType aType = aVisuObj->GetType();
1083         switch (aType) {
1084         case VISU::TSCALARMAP:
1085         case VISU::TISOSURFACE:
1086         case VISU::TDEFORMEDSHAPE:
1087         case VISU::TCUTPLANES:
1088         case VISU::TCUTLINES:
1089         case VISU::TVECTORS:
1090         case VISU::TSTREAMLINES:
1091         case VISU::TPLOT3D:
1092           {
1093             PortableServer::ServantBase_var aServant = VISU::GetServant(aCORBAObject);
1094             if (aServant.in()) {
1095               VISU::Prs3d_i* aPrsObject = dynamic_cast<VISU::Prs3d_i*>(aServant.in());
1096               aList.resize(k+1);
1097               aList[k] = aPrsObject;
1098               k++;
1099             }
1100           }
1101           break;
1102         case VISU::TFIELD:
1103           {
1104             _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1105             _PTR(SObject) aTimeStamp;
1106             anIter->Next(); // First is reference on support
1107             for (; anIter->More(); anIter->Next()) {
1108               aTimeStamp = anIter->Value();
1109               if (!aTimeStamp) continue;
1110               std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1111               if (!aSubList.empty()) {
1112                 int n = aSubList.size();
1113                 aList.resize(k+n);
1114                 for (int i = 0; i < n; i++) {
1115                   aList[k] = aSubList[i];
1116                   k++;
1117                 }
1118               }
1119             }
1120           }
1121           break;
1122         }
1123       }
1124     } else {
1125       _PTR(GenericAttribute) anAttr;
1126       if (theObject->FindAttribute(anAttr, "AttributeComment")) {
1127         _PTR(AttributeComment) aComment (anAttr);
1128         string aComm = aComment->Value();
1129         QString strIn (aComm.c_str());
1130         VISU::Storable::TRestoringMap pMap;
1131         VISU::Storable::StrToMap(strIn, pMap);
1132         bool isExist;
1133         VISU::VISUType aType =
1134           (VISU::VISUType)VISU::Storable::FindValue(pMap,"myType",&isExist).toInt();
1135         if (isExist) {
1136           switch (aType) {
1137           case VISU::TFIELD:
1138             {
1139               _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1140               _PTR(SObject) aTimeStamp;
1141               anIter->Next(); // First is reference on support
1142               for (; anIter->More(); anIter->Next()) {
1143                 aTimeStamp = anIter->Value();
1144                 if (!aTimeStamp) continue;
1145                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aTimeStamp);
1146                 if (!aSubList.empty()) {
1147                   int n = aSubList.size();
1148                   aList.resize(k+n);
1149                   for (int i = 0; i < n; i++) {
1150                     aList[k] = aSubList[i];
1151                     k++;
1152                   }
1153                 }
1154               }
1155             }
1156             break;
1157           case VISU::TTIMESTAMP:
1158             {
1159               _PTR(ChildIterator) anIter = aCStudy->NewChildIterator(theObject);
1160               _PTR(SObject) aPrs;
1161               for (; anIter->More(); anIter->Next()) {
1162                 aPrs = anIter->Value();
1163                 if (!aPrs) continue;
1164                 std::vector<VISU::Prs3d_i*> aSubList = GetPrs3dList(theModule, aPrs);
1165                 if (!aSubList.empty()) {
1166                   int n = aSubList.size();
1167                   aList.resize(k+n);
1168                   for (int i = 0; i < n; i++) {
1169                     aList[k] = aSubList[i];
1170                     k++;
1171                   }
1172                 }
1173               }
1174             }
1175             break;
1176           }
1177         }
1178       }
1179     }
1180     return aList;
1181   }
1182 }