Salome HOME
Merge branch 'BR_H2018_3' into BR_2018_V8_5
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_TranslateObstacleOp.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_TranslateObstacleOp.h"
20
21 #include "HYDROGUI_TranslateObstacleDlg.h"
22 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_Shape.h"
24 #include "HYDROGUI_Tool2.h"
25 #include "HYDROGUI_UpdateFlags.h"
26
27 #include <HYDROData_ShapesTool.h>
28
29 #include <LightApp_Application.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <LightApp_UpdateFlags.h>
32
33 #include <SUIT_MessageBox.h>
34 #include <SUIT_Desktop.h>
35
36 #include <OCCViewer_ViewManager.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39
40
41 HYDROGUI_TranslateObstacleOp::HYDROGUI_TranslateObstacleOp( HYDROGUI_Module* theModule )
42 : HYDROGUI_Operation( theModule ), 
43   myPreviewPrs( NULL )
44 {
45   setName( tr( "TRANSLATE_OBSTACLE" ) );
46 }
47
48 HYDROGUI_TranslateObstacleOp::~HYDROGUI_TranslateObstacleOp()
49 {
50   erasePreview();
51 }
52
53 void HYDROGUI_TranslateObstacleOp::startOperation()
54 {
55   HYDROGUI_Operation::startOperation();
56
57   // Get panel and reset its state
58   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
59   aPanel->reset();
60
61   // Get the edited object
62   if ( isApplyAndClose() )
63     myEditedObject = Handle(HYDROData_Obstacle)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
64   if ( myEditedObject.IsNull() ) {
65     abort();
66     return;
67   }
68
69   // Set the edited object name to the panel
70   aPanel->setName( myEditedObject->GetName() );
71
72   // Create preview
73   createPreview();
74 }
75
76 void HYDROGUI_TranslateObstacleOp::abortOperation()
77 {
78   erasePreview();
79   abortDocOperation();
80
81   HYDROGUI_Operation::abortOperation();
82 }
83
84 void HYDROGUI_TranslateObstacleOp::commitOperation()
85 {
86   erasePreview();
87
88   HYDROGUI_Operation::commitOperation();
89 }
90
91 HYDROGUI_InputPanel* HYDROGUI_TranslateObstacleOp::createInputPanel() const
92 {
93   HYDROGUI_TranslateObstacleDlg* aPanel = new HYDROGUI_TranslateObstacleDlg( module(), getName() );
94
95   connect( aPanel, SIGNAL( argumentsChanged() ), this, SLOT( onArgumentsChanged() ) );
96
97   return aPanel;
98 }
99
100 bool HYDROGUI_TranslateObstacleOp::processApply( int& theUpdateFlags,
101                                                  QString& theErrorMsg,
102                                                  QStringList& theBrowseObjectsEntries )
103 {
104   HYDROGUI_TranslateObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_TranslateObstacleDlg*>( inputPanel() );
105   if ( !aPanel || myEditedObject.IsNull() ) {
106     return false;
107   }
108
109   // Erase preview
110   erasePreview();
111
112   // Get the translated shape
113   double aDx = aPanel->getDx();
114   double aDy = aPanel->getDy();
115   double aDz = aPanel->getDz();
116
117   // Translate the obstacle
118   myEditedObject->Translate( aDx, aDy, aDz );
119   myEditedObject->Update();
120
121   module()->setIsToUpdate( myEditedObject );
122
123   // Set update flags
124   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
125
126   return true;
127 }
128
129 void HYDROGUI_TranslateObstacleOp::createPreview()
130 {
131   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
132   if ( myEditedObject.IsNull() || !aPanel )
133     return;
134
135   // Create preview presentation if necessary
136   if ( !myPreviewPrs ) {
137     LightApp_Application* anApp = module()->getApp();
138
139     if ( !getPreviewManager() )
140       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
141                          anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
142     OCCViewer_ViewManager* aViewManager = getPreviewManager();
143     if ( aViewManager ) {
144       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
145         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
146         if ( !aCtx.IsNull() ) {
147           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
148           myPreviewPrs->setFillingColor( myEditedObject->GetFillingColor(), false, false );
149           myPreviewPrs->setBorderColor( myEditedObject->GetBorderColor(), false, false );
150         }
151       }
152     }
153   }
154   
155   // Set the translated shape to the preview presentation
156   if ( myPreviewPrs )
157   {
158     double aDx = aPanel->getDx();
159     double aDy = aPanel->getDy();
160     double aDz = aPanel->getDz();
161     TopoDS_Shape anOriShape = myEditedObject->GetShape3D();
162
163     TopoDS_Shape aTranslatedShape = HYDROData_ShapesTool::Translated( anOriShape, aDx, aDy, aDz );
164     myPreviewPrs->setShape( aTranslatedShape );
165   }
166 }
167
168 void HYDROGUI_TranslateObstacleOp::erasePreview()
169 {
170   if( myPreviewPrs ) {
171     delete myPreviewPrs;
172     myPreviewPrs = 0;
173   }
174 }
175
176 void HYDROGUI_TranslateObstacleOp::onArgumentsChanged()
177 {
178   // Update preview
179   createPreview();
180 }
181