Salome HOME
Reference objetcs are added for chanel.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ProfileOp.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 #include "HYDROGUI_Module.h"
23 #include "HYDROGUI_ProfileOp.h"
24 #include "HYDROGUI_ProfileDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "HYDROGUI_UpdateFlags.h"
27
28 #include "HYDROData_Document.h"
29 #include "HYDROData_Profile.h"
30 #include "CurveCreator_Curve.hxx"
31 #include "CurveCreator_Displayer.h"
32
33 #include <LightApp_Application.h>
34 #include <LightApp_SelectionMgr.h>
35 #include <LightApp_UpdateFlags.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
40
41 #include <OCCViewer_AISSelector.h>
42
43 #include <Precision.hxx>
44
45 //static int ZValueIncrement = 0;
46
47 HYDROGUI_ProfileOp::HYDROGUI_ProfileOp( HYDROGUI_Module* theModule, bool theIsEdit )
48 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL)//, 
49   //myViewManager(NULL)
50 {
51   setName( theIsEdit ? tr( "EDIT_PROFILE" ) : tr( "CREATE_PROFILE" ) );
52 }
53
54 HYDROGUI_ProfileOp::~HYDROGUI_ProfileOp()
55 {
56   //erasePreview();
57 }
58
59 /**
60  * Redirect the delete action to input panel
61  */
62 void HYDROGUI_ProfileOp::deleteSelected()
63 {
64   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
65   aPanel->deleteSelected();
66 }
67
68 /**
69  * Checks whether there are some to delete
70  */
71 bool HYDROGUI_ProfileOp::deleteEnabled()
72 {
73   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
74   return aPanel->deleteEnabled();
75 }
76
77 void HYDROGUI_ProfileOp::startOperation()
78 {
79   if( myCurve )
80   {
81     delete myCurve;
82     myCurve = 0;
83   }
84
85   HYDROGUI_Operation::startOperation();
86
87   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
88   aPanel->reset();
89
90   //LightApp_Application* anApp = module()->getApp();
91   //myViewManager =
92   //  dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
93   //aPanel->setOCCViewer( myViewManager ? myViewManager->getOCCViewer() : 0 );
94
95   if( myIsEdit )
96     myEditedObject = Handle(HYDROData_Profile)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
97   if( !myEditedObject.IsNull() )
98   {
99     /*int anIntDim = myEditedObject->GetDimension();
100     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
101     if( anIntDim == 2 )
102       aDim = CurveCreator::Dim2d;
103     myCurve = new CurveCreator_Curve(aDim);
104     QList<PolylineSection> aPolylineData = myEditedObject->GetPolylineData();
105
106     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
107       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
108       bool isClosed = aPolylineData[i].myIsClosed;
109       CurveCreator::SectionType aType = CurveCreator::Polyline;
110       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
111         aType = CurveCreator::Spline;
112       }
113       CurveCreator::Coordinates aCoords;
114       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
115         aCoords.push_back(aPolylineData[i].myCoords[j]);
116       }
117       myCurve->addSectionInternal( aName, aType, isClosed, aCoords );
118     }
119     aPanel->setProfileName( myEditedObject->GetName() );
120     */
121   }
122   else{
123     /*myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);*/
124     //aPanel->setCurve(myCurve);
125     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
126     aPanel->setProfileName(aNewName);
127   }
128   /*aPanel->setCurve(myCurve);*/
129   //displayPreview();
130 }
131
132 void HYDROGUI_ProfileOp::abortOperation()
133 {
134   //HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
135   //if ( aPanel )
136   //  aPanel->setOCCViewer( 0 );
137   //erasePreview();
138
139   HYDROGUI_Operation::abortOperation();
140 }
141
142 void HYDROGUI_ProfileOp::commitOperation()
143 {
144   //HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
145   //if ( aPanel )
146   //  aPanel->setOCCViewer( 0 );
147   //erasePreview();
148
149   HYDROGUI_Operation::commitOperation();
150 }
151
152 HYDROGUI_InputPanel* HYDROGUI_ProfileOp::createInputPanel() const
153 {
154   HYDROGUI_ProfileDlg* aDlg = new HYDROGUI_ProfileDlg( module(), getName() );
155   //connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
156   return aDlg;
157 }
158
159 bool HYDROGUI_ProfileOp::processApply( int& theUpdateFlags,
160                                         QString& theErrorMsg )
161 {
162   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
163
164   int aStudyId = module()->getStudyId();
165   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
166   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
167   if( aDocument.IsNull() )
168     return false;
169
170   Handle(HYDROData_Profile) aProfileObj;
171   if( myIsEdit ){
172     aProfileObj = myEditedObject;
173   }
174   else{
175     aProfileObj = Handle(HYDROData_Profile)::DownCast( aDocument->CreateObject( KIND_PROFILE ) );
176
177     //double aZValue = double( ++ZValueIncrement ) * 1e-2; // empiric value, to be revised
178     //aProfileObj->SetZValue( aZValue );
179   }
180
181   if( aProfileObj.IsNull() )
182     return false;
183
184   QString aProfileName = aPanel->getProfileName();
185   aProfileObj->SetName(aProfileName);
186   /*
187   int aDimInt = 3;
188   if( myCurve->getDimension() == CurveCreator::Dim2d )
189     aDimInt = 2;
190   aProfileObj->SetDimension(aDimInt);
191   QList<PolylineSection> aPolylineData;
192   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
193     PolylineSection aSect;
194     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
195     aSect.myIsClosed = myCurve->isClosed(i);
196     aSect.myType = PolylineSection::SECTION_POLYLINE;
197     if( myCurve->getSectionType(i) == CurveCreator::Spline ){
198       aSect.myType = PolylineSection::SECTION_SPLINE;
199     }
200     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
201     for( int j = 0 ; j < aCoords.size() ; j++ ){
202       aSect.myCoords << aCoords.at(j);
203     }
204     aPolylineData << aSect;
205   }
206   aProfileObj->SetPolylineData(aPolylineData);
207
208   // the viewer should be release from the widget before the module update it
209   // because it has an opened local context and updated presentation should not be displayed in it
210   //if ( aPanel )
211   //  aPanel->setOCCViewer( 0 );
212
213   if( !myIsEdit )
214     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), aProfileObj, true );
215 */
216   theUpdateFlags = UF_Model;
217   return true;
218 }
219
220 /*void HYDROGUI_ProfileOp::onEditorSelectionChanged()
221 {
222   HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
223   if( !aPanel )
224     return;
225   if( !myCurve )
226     return;
227   CurveCreator_Displayer* aDisplayer = myCurve->getDisplayer();
228   if( !aDisplayer )
229     return;
230   QList<int> aSelSections = aPanel->getSelectedSections();
231   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
232     bool aIsHl = false;
233     if( aSelSections.contains(i) ){
234       aDisplayer->highlight( myCurve->constructSection(i), aIsHl );
235     }
236   }
237 }*/
238
239 /*void HYDROGUI_ProfileOp::displayPreview()
240 {
241   if( myViewManager )
242   {
243     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
244     {
245       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
246       if( !aCtx.IsNull() )
247       {
248         CurveCreator_Displayer* aDisplayer = new CurveCreator_Displayer( aCtx );
249         myCurve->setDisplayer( aDisplayer );
250         aDisplayer->display( myCurve->constructWire() );
251       }
252     }
253   }
254 }*/
255
256 /*void HYDROGUI_ProfileOp::erasePreview()
257 {
258   CurveCreator_Displayer* aDisplayer = myCurve ? myCurve->getDisplayer() : 0;
259   if( myViewManager && aDisplayer )
260   {
261     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
262     {
263       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
264       if( !aCtx.IsNull() )
265       {
266         aDisplayer->erase();
267       }
268     }
269   }
270 }*/