Salome HOME
merge BR_hydro_v_1_0_4 on BR_quadtree
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TranslateObstacleOp.cxx
1 // Copyright (C) 2007-2013  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 #include "HYDROGUI_TranslateObstacleOp.h"
24
25 #include "HYDROGUI_TranslateObstacleDlg.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Shape.h"
28 #include "HYDROGUI_Tool.h"
29 #include "HYDROGUI_UpdateFlags.h"
30
31 #include <HYDROData_ShapesTool.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36
37 #include <SUIT_MessageBox.h>
38 #include <SUIT_Desktop.h>
39
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
43
44
45 HYDROGUI_TranslateObstacleOp::HYDROGUI_TranslateObstacleOp( HYDROGUI_Module* theModule )
46 : HYDROGUI_Operation( theModule ), 
47   myPreviewPrs( NULL )
48 {
49   setName( tr( "TRANSLATE_OBSTACLE" ) );
50 }
51
52 HYDROGUI_TranslateObstacleOp::~HYDROGUI_TranslateObstacleOp()
53 {
54   erasePreview();
55 }
56
57 void HYDROGUI_TranslateObstacleOp::startOperation()
58 {
59   HYDROGUI_Operation::startOperation();
60
61   // Get panel and reset its state
62   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
63   aPanel->reset();
64
65   // Get the edited object
66   myEditedObject = Handle(HYDROData_Obstacle)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
67   if ( myEditedObject.IsNull() ) {
68     abort();
69     return;
70   }
71
72   // Set the edited object name to the panel
73   aPanel->setName( myEditedObject->GetName() );
74
75   // Create preview
76   createPreview();
77 }
78
79 void HYDROGUI_TranslateObstacleOp::abortOperation()
80 {
81   erasePreview();
82   abortDocOperation();
83
84   HYDROGUI_Operation::abortOperation();
85 }
86
87 void HYDROGUI_TranslateObstacleOp::commitOperation()
88 {
89   erasePreview();
90
91   HYDROGUI_Operation::commitOperation();
92 }
93
94 HYDROGUI_InputPanel* HYDROGUI_TranslateObstacleOp::createInputPanel() const
95 {
96   HYDROGUI_TranslateObstacleDlg* aPanel = new HYDROGUI_TranslateObstacleDlg( module(), getName() );
97
98   connect( aPanel, SIGNAL( argumentsChanged() ), this, SLOT( onArgumentsChanged() ) );
99
100   return aPanel;
101 }
102
103 bool HYDROGUI_TranslateObstacleOp::processApply( int& theUpdateFlags,
104                                                  QString& theErrorMsg,
105                                                  QStringList& theBrowseObjectsEntries )
106 {
107   HYDROGUI_TranslateObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_TranslateObstacleDlg*>( inputPanel() );
108   if ( !aPanel || myEditedObject.IsNull() ) {
109     return false;
110   }
111
112   // Erase preview
113   erasePreview();
114
115   // Get the translated shape
116   double aDx = aPanel->getDx();
117   double aDy = aPanel->getDy();
118   double aDz = aPanel->getDz();
119
120   // Translate the obstacle
121   myEditedObject->Translate( aDx, aDy, aDz );
122   myEditedObject->Update();
123
124   module()->setIsToUpdate( myEditedObject );
125
126   // Set update flags
127   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
128
129   return true;
130 }
131
132 void HYDROGUI_TranslateObstacleOp::createPreview()
133 {
134   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
135   if ( myEditedObject.IsNull() || !aPanel )
136     return;
137
138   // Create preview presentation if necessary
139   if ( !myPreviewPrs ) {
140     LightApp_Application* anApp = module()->getApp();
141
142     if ( !getPreviewManager() )
143       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
144                          anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
145     OCCViewer_ViewManager* aViewManager = getPreviewManager();
146     if ( aViewManager ) {
147       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
148         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
149         if ( !aCtx.IsNull() ) {
150           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
151           myPreviewPrs->setFillingColor( myEditedObject->GetFillingColor(), false, false );
152           myPreviewPrs->setBorderColor( myEditedObject->GetBorderColor(), false, false );
153         }
154       }
155     }
156   }
157   
158   // Set the translated shape to the preview presentation
159   if ( myPreviewPrs )
160   {
161     double aDx = aPanel->getDx();
162     double aDy = aPanel->getDy();
163     double aDz = aPanel->getDz();
164     TopoDS_Shape anOriShape = myEditedObject->GetShape3D();
165
166     TopoDS_Shape aTranslatedShape = HYDROData_ShapesTool::Translated( anOriShape, aDx, aDy, aDz );
167     myPreviewPrs->setShape( aTranslatedShape );
168   }
169 }
170
171 void HYDROGUI_TranslateObstacleOp::erasePreview()
172 {
173   if( myPreviewPrs ) {
174     delete myPreviewPrs;
175     myPreviewPrs = 0;
176   }
177 }
178
179 void HYDROGUI_TranslateObstacleOp::onArgumentsChanged()
180 {
181   // Update preview
182   createPreview();
183 }
184