Salome HOME
PR: 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 {
106   HYDROGUI_TranslateObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_TranslateObstacleDlg*>( inputPanel() );
107   if ( !aPanel || myEditedObject.IsNull() ) {
108     return false;
109   }
110
111   // Erase preview
112   erasePreview();
113
114   // Get the translated shape
115   double aDx = aPanel->getDx();
116   double aDy = aPanel->getDy();
117   double aDz = aPanel->getDz();
118
119   // Translate the obstacle
120   myEditedObject->Translate( aDx, aDy, aDz );
121   myEditedObject->Update();
122
123   module()->setIsToUpdate( myEditedObject );
124
125   // Set update flags
126   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
127
128   return true;
129 }
130
131 void HYDROGUI_TranslateObstacleOp::createPreview()
132 {
133   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
134   if ( myEditedObject.IsNull() || !aPanel )
135     return;
136
137   // Create preview presentation if necessary
138   if ( !myPreviewPrs ) {
139     LightApp_Application* anApp = module()->getApp();
140
141     if ( !getPreviewManager() )
142       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
143                          anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
144     OCCViewer_ViewManager* aViewManager = getPreviewManager();
145     if ( aViewManager ) {
146       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
147         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
148         if ( !aCtx.IsNull() ) {
149           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
150           myPreviewPrs->setFillingColor( myEditedObject->GetFillingColor(), false, false );
151           myPreviewPrs->setBorderColor( myEditedObject->GetBorderColor(), false, false );
152         }
153       }
154     }
155   }
156   
157   // Set the translated shape to the preview presentation
158   if ( myPreviewPrs )
159   {
160     double aDx = aPanel->getDx();
161     double aDy = aPanel->getDy();
162     double aDz = aPanel->getDz();
163     TopoDS_Shape anOriShape = myEditedObject->GetShape3D();
164
165     TopoDS_Shape aTranslatedShape = HYDROData_ShapesTool::Translated( anOriShape, aDx, aDy, aDz );
166     myPreviewPrs->setShape( aTranslatedShape );
167   }
168 }
169
170 void HYDROGUI_TranslateObstacleOp::erasePreview()
171 {
172   if( myPreviewPrs ) {
173     delete myPreviewPrs;
174     myPreviewPrs = 0;
175   }
176 }
177
178 void HYDROGUI_TranslateObstacleOp::onArgumentsChanged()
179 {
180   // Update preview
181   createPreview();
182 }
183