Salome HOME
b8b19f763778e2b1a2cbb63b3c2aef813004fc0a
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PolylineOp.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_PolylineOp.h"
24 #include "HYDROGUI_PolylineDlg.h"
25 #include "HYDROGUI_Tool.h"
26 #include "CurveCreator.hxx"
27 #include "CurveCreator_Curve.hxx"
28 #include "CurveCreator_CurveEditor.hxx"
29 #include "HYDROGUI_AISCurve.h"
30
31 #include <HYDROData_Document.h>
32 #include <HYDROData_Polyline.h>
33 #include <CurveCreator_Curve.hxx>
34 #include <CurveCreator_CurveEditor.hxx>
35
36 #include <LightApp_Application.h>
37 #include <LightApp_SelectionMgr.h>
38 #include <LightApp_UpdateFlags.h>
39
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
43
44 #include <OCCViewer_AISSelector.h>
45
46 HYDROGUI_PolylineOp::HYDROGUI_PolylineOp( HYDROGUI_Module* theModule, bool theIsEdit )
47 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit), myCurve(NULL), 
48   myActiveViewManager(NULL), myPreviewViewManager(NULL), myAISCurve(NULL)
49 {
50   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
51 }
52
53 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
54 {
55   closePreview();
56 }
57
58 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
59 {
60   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
61   connect( aDlg ,SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
62   return aDlg;
63 }
64
65 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
66                                         QString& theErrorMsg )
67 {
68   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
69
70   int aStudyId = module()->getStudyId();
71   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
72   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
73   if( aDocument.IsNull() )
74     return false;
75
76   Handle(HYDROData_Polyline) aPolylineObj;
77   if( myIsEdit ){
78     aPolylineObj = myEditedObject;
79   }
80   else{
81     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
82   }
83
84   if( aPolylineObj.IsNull() )
85     return false;
86
87   QString aPolylineName = aPanel->getPolylineName();
88   aPolylineObj->SetName(aPolylineName);
89   int aDimInt = 3;
90   if( myCurve->getDimension() == CurveCreator::Dim2d )
91     aDimInt = 2;
92   aPolylineObj->setDimension(aDimInt);
93   QList<PolylineSection> aPolylineData;
94   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
95     PolylineSection aSect;
96     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
97     aSect.myIsClosed = myCurve->isClosed(i);
98     aSect.myType = PolylineSection::SECTION_POLYLINE;
99     if( myCurve->getType(i) == CurveCreator::BSpline ){
100       aSect.myType = PolylineSection::SECTION_SPLINE;
101     }
102     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
103     for( int j = 0 ; j < aCoords.size() ; j++ ){
104       aSect.myCoords << aCoords.at(j);
105     }
106     aPolylineData << aSect;
107   }
108   aPolylineObj->setPolylineData(aPolylineData);
109
110   theUpdateFlags = UF_Model;
111   module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
112   return true;
113 }
114
115 void HYDROGUI_PolylineOp::onCreatePreview()
116 {
117   LightApp_Application* anApp = module()->getApp();
118
119   myActiveViewManager = anApp->activeViewManager();
120
121   myPreviewViewManager =
122     dynamic_cast<OCCViewer_ViewManager*>( anApp->createViewManager( OCCViewer_Viewer::Type() ) );
123   if( myPreviewViewManager )
124   {
125     connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
126              this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
127
128     //anApp->selectionMgr()->setEnabled(false); // what the hell?!
129     myPreviewViewManager->setTitle( tr( "CREATE_POLYLINE" ) );
130     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
131     aViewer->enableSelection(true);
132     aViewer->enableMultiselection(true);
133     Handle_AIS_InteractiveContext aCtx = aViewer->getAISContext();
134
135     OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
136     vw->onTopView();
137
138     myAISCurve = new HYDROGUI_AISCurve(myCurve, aCtx);
139
140     myAISCurve->Display();
141   }
142 }
143
144 void HYDROGUI_PolylineOp::startOperation()
145 {
146   CurveCreator_Curve* anOldCurve = myCurve;
147
148   HYDROGUI_Operation::startOperation();
149
150   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
151   aPanel->reset();
152
153   myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
154   if( !myEditedObject.IsNull() )
155   {
156     int anIntDim = myEditedObject->getDimension();
157     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
158     if( anIntDim == 2 )
159       aDim = CurveCreator::Dim2d;
160     myCurve = new CurveCreator_Curve(aDim);
161     QList<PolylineSection> aPolylineData = myEditedObject->getPolylineData();
162
163     CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
164     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
165       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
166       bool isClosed = aPolylineData[i].myIsClosed;
167       CurveCreator::Type aType = CurveCreator::Polyline;
168       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
169         aType = CurveCreator::BSpline;
170       }
171       CurveCreator::Coordinates aCoords;
172       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
173         aCoords.push_back(aPolylineData[i].myCoords[j]);
174       }
175       anEdit->addSection( aName, aType, isClosed, aCoords );
176     }
177     delete anEdit;
178     aPanel->setPolylineName( myEditedObject->GetName() );
179
180   }
181   else{
182     myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
183     aPanel->setCurve(myCurve);
184     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), "Polyline" );
185     aPanel->setPolylineName(aNewName);
186   }
187   aPanel->setCurve(myCurve);
188   if( myAISCurve )
189     myAISCurve->setCurve(myCurve);
190   if( anOldCurve )
191     delete anOldCurve;
192   onCreatePreview();
193 }
194
195 void HYDROGUI_PolylineOp::abortOperation()
196 {
197   closePreview();
198
199   HYDROGUI_Operation::abortOperation();
200 }
201
202 void HYDROGUI_PolylineOp::commitOperation()
203 {
204   closePreview();
205
206   HYDROGUI_Operation::commitOperation();
207 }
208
209 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
210 {
211   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
212   if( !aPanel )
213     return;
214   if( !myCurve )
215     return;
216   if( !myAISCurve )
217     return;
218   QList<int> aSelSections = aPanel->getSelectedSections();
219   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
220     bool aIsHl = false;
221     if( aSelSections.contains(i) ){
222       myAISCurve->highlightSection(i, aIsHl);
223     }
224   }
225 }
226
227 void HYDROGUI_PolylineOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
228 {
229   closePreview();
230 }
231
232 void HYDROGUI_PolylineOp::closePreview()
233 {
234   if( myPreviewViewManager )
235   {
236     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
237                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
238
239     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
240     myPreviewViewManager = 0;
241   }
242
243   if( myActiveViewManager )
244   {
245     HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
246     myActiveViewManager = 0;
247   }
248 }