Salome HOME
Feature 32: Image presentation in OCCViewer.
[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   myViewManager(NULL), myAISCurve(NULL)
49 {
50   setName( theIsEdit ? tr( "EDIT_POLYLINE" ) : tr( "CREATE_POLYLINE" ) );
51 }
52
53 HYDROGUI_PolylineOp::~HYDROGUI_PolylineOp()
54 {
55   erasePreview();
56 }
57
58 void HYDROGUI_PolylineOp::startOperation()
59 {
60   CurveCreator_Curve* anOldCurve = myCurve;
61
62   HYDROGUI_Operation::startOperation();
63
64   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
65   aPanel->reset();
66
67   myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
68   if( !myEditedObject.IsNull() )
69   {
70     int anIntDim = myEditedObject->getDimension();
71     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
72     if( anIntDim == 2 )
73       aDim = CurveCreator::Dim2d;
74     myCurve = new CurveCreator_Curve(aDim);
75     QList<PolylineSection> aPolylineData = myEditedObject->getPolylineData();
76
77     CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
78     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
79       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
80       bool isClosed = aPolylineData[i].myIsClosed;
81       CurveCreator::Type aType = CurveCreator::Polyline;
82       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
83         aType = CurveCreator::BSpline;
84       }
85       CurveCreator::Coordinates aCoords;
86       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
87         aCoords.push_back(aPolylineData[i].myCoords[j]);
88       }
89       anEdit->addSection( aName, aType, isClosed, aCoords );
90     }
91     delete anEdit;
92     aPanel->setPolylineName( myEditedObject->GetName() );
93
94   }
95   else{
96     myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
97     aPanel->setCurve(myCurve);
98     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), "Polyline" );
99     aPanel->setPolylineName(aNewName);
100   }
101   aPanel->setCurve(myCurve);
102   if( myAISCurve )
103     myAISCurve->setCurve(myCurve);
104   if( anOldCurve )
105     delete anOldCurve;
106   displayPreview();
107 }
108
109 void HYDROGUI_PolylineOp::abortOperation()
110 {
111   erasePreview();
112
113   HYDROGUI_Operation::abortOperation();
114 }
115
116 void HYDROGUI_PolylineOp::commitOperation()
117 {
118   erasePreview();
119
120   HYDROGUI_Operation::commitOperation();
121 }
122
123 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
124 {
125   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
126   connect( aDlg, SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
127   return aDlg;
128 }
129
130 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
131                                         QString& theErrorMsg )
132 {
133   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
134
135   int aStudyId = module()->getStudyId();
136   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
137   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
138   if( aDocument.IsNull() )
139     return false;
140
141   Handle(HYDROData_Polyline) aPolylineObj;
142   if( myIsEdit ){
143     aPolylineObj = myEditedObject;
144   }
145   else{
146     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
147   }
148
149   if( aPolylineObj.IsNull() )
150     return false;
151
152   QString aPolylineName = aPanel->getPolylineName();
153   aPolylineObj->SetName(aPolylineName);
154   int aDimInt = 3;
155   if( myCurve->getDimension() == CurveCreator::Dim2d )
156     aDimInt = 2;
157   aPolylineObj->setDimension(aDimInt);
158   QList<PolylineSection> aPolylineData;
159   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
160     PolylineSection aSect;
161     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
162     aSect.myIsClosed = myCurve->isClosed(i);
163     aSect.myType = PolylineSection::SECTION_POLYLINE;
164     if( myCurve->getType(i) == CurveCreator::BSpline ){
165       aSect.myType = PolylineSection::SECTION_SPLINE;
166     }
167     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
168     for( int j = 0 ; j < aCoords.size() ; j++ ){
169       aSect.myCoords << aCoords.at(j);
170     }
171     aPolylineData << aSect;
172   }
173   aPolylineObj->setPolylineData(aPolylineData);
174
175   theUpdateFlags = UF_Model;
176   module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
177   return true;
178 }
179
180 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
181 {
182   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
183   if( !aPanel )
184     return;
185   if( !myCurve )
186     return;
187   if( !myAISCurve )
188     return;
189   QList<int> aSelSections = aPanel->getSelectedSections();
190   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
191     bool aIsHl = false;
192     if( aSelSections.contains(i) ){
193       myAISCurve->highlightSection(i, aIsHl);
194     }
195   }
196 }
197
198 void HYDROGUI_PolylineOp::displayPreview()
199 {
200   LightApp_Application* anApp = module()->getApp();
201
202   myViewManager =
203     dynamic_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
204   if( myViewManager )
205   {
206     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
207     {
208       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
209       if( !aCtx.IsNull() )
210       {
211         myAISCurve = new HYDROGUI_AISCurve( myCurve, aCtx );
212         myAISCurve->Display();
213       }
214     }
215   }
216 }
217
218 void HYDROGUI_PolylineOp::erasePreview()
219 {
220   if( myViewManager )
221   {
222     if( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() )
223     {
224       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
225       if( !aCtx.IsNull() && myAISCurve )
226       {
227         myAISCurve->Erase();
228         delete myAISCurve;
229         myAISCurve = 0;
230       }
231     }
232   }
233 }