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