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