]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_TranslateObstacleOp.cxx
Salome HOME
merge master 2015/06/04
[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_Tool.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   myEditedObject = Handle(HYDROData_Obstacle)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
63   if ( myEditedObject.IsNull() ) {
64     abort();
65     return;
66   }
67
68   // Set the edited object name to the panel
69   aPanel->setName( myEditedObject->GetName() );
70
71   // Create preview
72   createPreview();
73 }
74
75 void HYDROGUI_TranslateObstacleOp::abortOperation()
76 {
77   erasePreview();
78   abortDocOperation();
79
80   HYDROGUI_Operation::abortOperation();
81 }
82
83 void HYDROGUI_TranslateObstacleOp::commitOperation()
84 {
85   erasePreview();
86
87   HYDROGUI_Operation::commitOperation();
88 }
89
90 HYDROGUI_InputPanel* HYDROGUI_TranslateObstacleOp::createInputPanel() const
91 {
92   HYDROGUI_TranslateObstacleDlg* aPanel = new HYDROGUI_TranslateObstacleDlg( module(), getName() );
93
94   connect( aPanel, SIGNAL( argumentsChanged() ), this, SLOT( onArgumentsChanged() ) );
95
96   return aPanel;
97 }
98
99 bool HYDROGUI_TranslateObstacleOp::processApply( int& theUpdateFlags,
100                                                  QString& theErrorMsg,
101                                                  QStringList& theBrowseObjectsEntries )
102 {
103   HYDROGUI_TranslateObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_TranslateObstacleDlg*>( inputPanel() );
104   if ( !aPanel || myEditedObject.IsNull() ) {
105     return false;
106   }
107
108   // Erase preview
109   erasePreview();
110
111   // Get the translated shape
112   double aDx = aPanel->getDx();
113   double aDy = aPanel->getDy();
114   double aDz = aPanel->getDz();
115
116   // Translate the obstacle
117   myEditedObject->Translate( aDx, aDy, aDz );
118   myEditedObject->Update();
119
120   module()->setIsToUpdate( myEditedObject );
121
122   // Set update flags
123   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
124
125   return true;
126 }
127
128 void HYDROGUI_TranslateObstacleOp::createPreview()
129 {
130   HYDROGUI_TranslateObstacleDlg* aPanel = (HYDROGUI_TranslateObstacleDlg*)inputPanel();
131   if ( myEditedObject.IsNull() || !aPanel )
132     return;
133
134   // Create preview presentation if necessary
135   if ( !myPreviewPrs ) {
136     LightApp_Application* anApp = module()->getApp();
137
138     if ( !getPreviewManager() )
139       setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
140                          anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
141     OCCViewer_ViewManager* aViewManager = getPreviewManager();
142     if ( aViewManager ) {
143       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
144         Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
145         if ( !aCtx.IsNull() ) {
146           myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
147           myPreviewPrs->setFillingColor( myEditedObject->GetFillingColor(), false, false );
148           myPreviewPrs->setBorderColor( myEditedObject->GetBorderColor(), false, false );
149         }
150       }
151     }
152   }
153   
154   // Set the translated shape to the preview presentation
155   if ( myPreviewPrs )
156   {
157     double aDx = aPanel->getDx();
158     double aDy = aPanel->getDy();
159     double aDz = aPanel->getDz();
160     TopoDS_Shape anOriShape = myEditedObject->GetShape3D();
161
162     TopoDS_Shape aTranslatedShape = HYDROData_ShapesTool::Translated( anOriShape, aDx, aDy, aDz );
163     myPreviewPrs->setShape( aTranslatedShape );
164   }
165 }
166
167 void HYDROGUI_TranslateObstacleOp::erasePreview()
168 {
169   if( myPreviewPrs ) {
170     delete myPreviewPrs;
171     myPreviewPrs = 0;
172   }
173 }
174
175 void HYDROGUI_TranslateObstacleOp::onArgumentsChanged()
176 {
177   // Update preview
178   createPreview();
179 }
180