Salome HOME
Implementation of the issue "20830: EDF 1357 GUI : Hide/Show Icon" for Post-Pro module.
[modules/visu.git] / src / VISUGUI / VisuGUI_ViewTools.cxx
1 //  Copyright (C) 2007-2010  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
23 //  VISU VISUGUI : GUI of VISU component
24 //  File   : VisuGUI_Tools.cxx
25 //  Author : Sergey Anikin
26 //  Module : VISU
27 //
28 #include "VisuGUI_ViewTools.h"
29
30 #include "VISU_Actor.h"
31
32 #include "SVTK_ViewModel.h"
33 #include "SVTK_ViewWindow.h"
34
35 #include <VISU_Tools.h>
36
37 #include <SUIT_Desktop.h>
38
39 #include <SalomeApp_Study.h>
40
41 namespace VISU 
42 {
43   //----------------------------------------------------------------------------
44   VISU_Actor*
45   PublishInView(VisuGUI* theModule,
46                 Prs3d_i* thePrs,
47                 SVTK_ViewWindow* theViewWindow,
48                 bool theIsHighlight)
49   {
50     if (theViewWindow) {
51       QApplication::setOverrideCursor( Qt::WaitCursor );
52       try {
53         if (VISU_Actor* anActor = thePrs->CreateActor()) {
54           theViewWindow->AddActor(anActor);
55           if (theIsHighlight)
56             theViewWindow->highlight(anActor->getIO(),true);
57           theViewWindow->getRenderer()->ResetCameraClippingRange();
58           theViewWindow->Repaint();
59           QApplication::restoreOverrideCursor();
60           
61           if(!thePrs->GetEntry().empty())
62             VISU::SetVisibilityState(thePrs->GetEntry(), Qtx::ShownState);
63           else {
64             VISU::ColoredPrs3d_i* prs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
65             if(prs && !prs->GetHolderEntry().empty()) {
66               VISU::SetVisibilityState(prs->GetHolderEntry(), Qtx::ShownState);
67             }
68           }
69           return anActor;
70         }
71       } catch(std::exception& exc) {
72         thePrs->RemoveActors();
73
74         QApplication::restoreOverrideCursor();
75         INFOS(exc.what());
76         SUIT_MessageBox::warning
77           (GetDesktop(theModule), QObject::tr("WRN_VISU"),
78            QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()));
79       }
80     }
81     return NULL;
82   }
83
84
85   //---------------------------------------------------------------
86   VISU_Actor*
87   UpdateViewer(VisuGUI* theModule,
88                VISU::Prs3d_i* thePrs,
89                bool theDispOnly,
90                const bool theIsHighlight)
91   {
92     if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
93       vtkRenderer *aRen = aViewWindow->getRenderer();
94       VTK::ActorCollectionCopy aCopy(aRen->GetActors());
95       vtkActorCollection *anActColl = aCopy.GetActors();
96       anActColl->InitTraversal();
97       VISU_Actor* aResActor = NULL;
98       bool isOk = true;
99       while (vtkActor *anAct = anActColl->GetNextActor()) {
100         if (VISU_Actor* anActor = dynamic_cast<VISU_Actor*>(anAct)) {
101           if (VISU::Prs3d_i* aPrs3d = anActor->GetPrs3d()) {
102             if (thePrs == aPrs3d) {
103               try {
104                 aResActor = anActor;
105                 thePrs->UpdateActors();
106                 aResActor->VisibilityOn();
107               } catch (std::runtime_error& exc) {
108                 thePrs->RemoveActors();
109                 isOk = false;
110
111                 INFOS(exc.what());
112                 SUIT_MessageBox::warning
113                   (GetDesktop(theModule), QObject::tr("WRN_VISU"),
114                    QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()) );
115               }
116             } else if (theDispOnly) {
117               anActor->VisibilityOff();
118             }
119           } else if (theDispOnly && anActor->GetVisibility()) {
120             anActor->VisibilityOff();
121           }
122         }
123       }
124       if (aResActor) {
125         if (theIsHighlight && isOk)
126           aViewWindow->highlight(aResActor->getIO(), true);
127         aViewWindow->getRenderer()->ResetCameraClippingRange();
128         aViewWindow->Repaint();
129         return aResActor;
130       }
131       return PublishInView(theModule, thePrs, aViewWindow, theIsHighlight);
132     }
133     return NULL;
134   }
135
136
137   //---------------------------------------------------------------
138   void
139   ErasePrs3d(VisuGUI* theModule,
140              VISU::Prs3d_i* thePrs,
141              bool theDoRepaint)
142   {
143     if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)){
144       if(VISU_Actor* anActor = FindActor(aViewWindow, thePrs)){
145         anActor->VisibilityOff();
146         if(theDoRepaint)
147           aViewWindow->Repaint();
148       }
149     }
150   }
151
152   //----------------------------------------------------------------------------
153   void
154   ErasePrs(VisuGUI* theModule,
155            Base_i* theBase, 
156            bool theDoRepaint)
157   {    
158     if(!theBase)
159       return;
160
161     bool updateState = true;
162
163     switch (theBase->GetType()) {
164     case VISU::TCURVE: {
165       if (VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(theBase)) {
166         PlotCurve(theModule, aCurve, VISU::eErase );
167         updateState = false;
168       }
169       break;
170     }
171     case VISU::TCONTAINER: {
172       if (VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(theBase)) {
173         PlotContainer(theModule, aContainer, VISU::eErase );
174         updateState = false;
175       }
176       break;
177     }
178     case VISU::TTABLE: {
179       if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(theBase)) {
180         PlotTable(theModule, aTable, VISU::eErase );
181         updateState = false;
182       }
183       break;
184     }
185     case VISU::TPOINTMAP3D: {
186       if (VISU::PointMap3d_i* aTable3d = dynamic_cast<VISU::PointMap3d_i*>(theBase)) {
187         if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
188           if(VISU_ActorBase* anActor = FindActorBase(aViewWindow, aTable3d)){
189             anActor->VisibilityOff();
190             if(theDoRepaint)
191             aViewWindow->Repaint();
192           }
193         } else {
194           if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(theBase)) {
195             PlotTable(theModule, aTable, VISU::eErase );
196             updateState = false;
197           }
198         }
199       }
200       break;
201     }
202     default: {
203       if(VISU::Prs3d_i* aPrs3d = VISU::GetPrs3dFromBase(theBase)){
204         if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)){
205           RemoveScalarBarPosition(theModule, aViewWindow, aPrs3d);
206           ErasePrs3d(theModule, aPrs3d, theDoRepaint);
207         }
208       }
209     }} // switch (aType)
210     
211     /* Update visibility state */
212     if(updateState) {
213       if( RemovableObject_i* obj = dynamic_cast<RemovableObject_i*>(theBase)) {
214         SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>(theModule->application());
215         if(anApp) {
216           SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(anApp->activeStudy());
217           std::string entry = obj->GetEntry();
218           if(aStudy && !entry.empty()) 
219             aStudy->setVisibilityState(QString(entry.c_str()), Qtx::HiddenState);
220         }
221       } 
222     }
223   }
224 }