Salome HOME
Merge from V5_1_main 14/05/2010
[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 <SUIT_Desktop.h>
36
37 namespace VISU 
38 {
39   //----------------------------------------------------------------------------
40   VISU_Actor*
41   PublishInView(VisuGUI* theModule,
42                 Prs3d_i* thePrs,
43                 SVTK_ViewWindow* theViewWindow,
44                 bool theIsHighlight)
45   {
46     if (theViewWindow) {
47       QApplication::setOverrideCursor( Qt::WaitCursor );
48       try {
49         if (VISU_Actor* anActor = thePrs->CreateActor()) {
50           theViewWindow->AddActor(anActor);
51           if (theIsHighlight)
52             theViewWindow->highlight(anActor->getIO(),true);
53           theViewWindow->getRenderer()->ResetCameraClippingRange();
54           theViewWindow->Repaint();
55           QApplication::restoreOverrideCursor();
56           return anActor;
57         }
58       } catch(std::exception& exc) {
59         thePrs->RemoveActors();
60
61         QApplication::restoreOverrideCursor();
62         INFOS(exc.what());
63         SUIT_MessageBox::warning
64           (GetDesktop(theModule), QObject::tr("WRN_VISU"),
65            QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()));
66       }
67     }
68     return NULL;
69   }
70
71
72   //---------------------------------------------------------------
73   VISU_Actor*
74   UpdateViewer(VisuGUI* theModule,
75                VISU::Prs3d_i* thePrs,
76                bool theDispOnly,
77                const bool theIsHighlight)
78   {
79     if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
80       vtkRenderer *aRen = aViewWindow->getRenderer();
81       VTK::ActorCollectionCopy aCopy(aRen->GetActors());
82       vtkActorCollection *anActColl = aCopy.GetActors();
83       anActColl->InitTraversal();
84       VISU_Actor* aResActor = NULL;
85       bool isOk = true;
86       while (vtkActor *anAct = anActColl->GetNextActor()) {
87         if (VISU_Actor* anActor = dynamic_cast<VISU_Actor*>(anAct)) {
88           if (VISU::Prs3d_i* aPrs3d = anActor->GetPrs3d()) {
89             if (thePrs == aPrs3d) {
90               try {
91                 aResActor = anActor;
92                 thePrs->UpdateActors();
93                 aResActor->VisibilityOn();
94               } catch (std::runtime_error& exc) {
95                 thePrs->RemoveActors();
96                 isOk = false;
97
98                 INFOS(exc.what());
99                 SUIT_MessageBox::warning
100                   (GetDesktop(theModule), QObject::tr("WRN_VISU"),
101                    QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()) );
102               }
103             } else if (theDispOnly) {
104               anActor->VisibilityOff();
105             }
106           } else if (theDispOnly && anActor->GetVisibility()) {
107             anActor->VisibilityOff();
108           }
109         }
110       }
111       if (aResActor) {
112         if (theIsHighlight && isOk)
113           aViewWindow->highlight(aResActor->getIO(), true);
114         aViewWindow->getRenderer()->ResetCameraClippingRange();
115         aViewWindow->Repaint();
116         return aResActor;
117       }
118       return PublishInView(theModule, thePrs, aViewWindow, theIsHighlight);
119     }
120     return NULL;
121   }
122
123
124   //---------------------------------------------------------------
125   void
126   ErasePrs3d(VisuGUI* theModule,
127              VISU::Prs3d_i* thePrs,
128              bool theDoRepaint)
129   {
130     if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)){
131       if(VISU_Actor* anActor = FindActor(aViewWindow, thePrs)){
132         anActor->VisibilityOff();
133         if(theDoRepaint)
134           aViewWindow->Repaint();
135       }
136     }
137   }
138
139   //----------------------------------------------------------------------------
140   void
141   ErasePrs(VisuGUI* theModule,
142            Base_i* theBase, 
143            bool theDoRepaint)
144   {
145     if(!theBase)
146       return;
147
148     switch (theBase->GetType()) {
149     case VISU::TCURVE: {
150       if (VISU::Curve_i* aCurve = dynamic_cast<VISU::Curve_i*>(theBase))
151         PlotCurve(theModule, aCurve, VISU::eErase );
152       break;
153     }
154     case VISU::TCONTAINER: {
155       if (VISU::Container_i* aContainer = dynamic_cast<VISU::Container_i*>(theBase))
156         PlotContainer(theModule, aContainer, VISU::eErase );
157       break;
158     }
159     case VISU::TTABLE: {
160       if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(theBase))
161         PlotTable(theModule, aTable, VISU::eErase );
162       break;
163     }
164     case VISU::TPOINTMAP3D: {
165       if (VISU::PointMap3d_i* aTable3d = dynamic_cast<VISU::PointMap3d_i*>(theBase)) {
166         if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
167           if(VISU_ActorBase* anActor = FindActorBase(aViewWindow, aTable3d)){
168             anActor->VisibilityOff();
169             if(theDoRepaint)
170             aViewWindow->Repaint();
171           }
172         } else {
173           if (VISU::Table_i* aTable = dynamic_cast<VISU::Table_i*>(theBase))
174             PlotTable(theModule, aTable, VISU::eErase );
175         }
176       }
177       break;
178     }
179     default: {
180       if(VISU::Prs3d_i* aPrs3d = VISU::GetPrs3dFromBase(theBase)){
181         if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)){
182           RemoveScalarBarPosition(theModule, aViewWindow, aPrs3d);
183           ErasePrs3d(theModule, aPrs3d, theDoRepaint);
184         }
185       }
186     }} // switch (aType)
187   }
188
189 }