]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_PolylineOp.cxx
Salome HOME
Modify creation of curves: 1) using QDockWidget instead of QDialog; 2) selection...
[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 }
56
57 HYDROGUI_InputPanel* HYDROGUI_PolylineOp::createInputPanel() const
58 {
59   HYDROGUI_PolylineDlg* aDlg = new HYDROGUI_PolylineDlg( module(), getName() );
60   connect( aDlg ,SIGNAL( selectionChanged() ), this, SLOT( onEditorSelectionChanged() ) );
61   return aDlg;
62 }
63
64 bool HYDROGUI_PolylineOp::processApply( int& theUpdateFlags,
65                                         QString& theErrorMsg )
66 {
67   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
68
69   int aStudyId = module()->getStudyId();
70   bool aHasDoc = HYDROData_Document::HasDocument(aStudyId);
71   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
72   if( aDocument.IsNull() )
73     return false;
74
75   Handle(HYDROData_Polyline) aPolylineObj;
76   if( myIsEdit ){
77     aPolylineObj = myEditedObject;
78   }
79   else{
80     aPolylineObj = Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
81   }
82
83   if( aPolylineObj.IsNull() )
84     return false;
85
86   QString aPolylineName = aPanel->getPolylineName();
87   aPolylineObj->SetName(aPolylineName);
88   int aDimInt = 3;
89   if( myCurve->getDimension() == CurveCreator::Dim2d )
90     aDimInt = 2;
91   aPolylineObj->setDimension(aDimInt);
92   QList<PolylineSection> aPolylineData;
93   for( int i=0 ; i < myCurve->getNbSections() ; i++ ){
94     PolylineSection aSect;
95     aSect.mySectionName = HYDROGUI_Tool::ToExtString( QString::fromLocal8Bit(myCurve->getSectionName(i).c_str()));
96     aSect.myIsClosed = myCurve->isClosed(i);
97     aSect.myType = PolylineSection::SECTION_POLYLINE;
98     if( myCurve->getType(i) == CurveCreator::BSpline ){
99       aSect.myType = PolylineSection::SECTION_SPLINE;
100     }
101     CurveCreator::Coordinates aCoords = myCurve->getPoints(i);
102     for( int j = 0 ; j < aCoords.size() ; j++ ){
103       aSect.myCoords << aCoords.at(j);
104     }
105     aPolylineData << aSect;
106   }
107   aPolylineObj->setPolylineData(aPolylineData);
108
109   theUpdateFlags = UF_Model;
110   module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), aPolylineObj, true );
111   return true;
112 }
113
114 void HYDROGUI_PolylineOp::onCreatePreview()
115 {
116   LightApp_Application* anApp = module()->getApp();
117
118   myActiveViewManager = anApp->activeViewManager();
119
120   myPreviewViewManager =
121     dynamic_cast<OCCViewer_ViewManager*>( anApp->createViewManager( OCCViewer_Viewer::Type() ) );
122   if( myPreviewViewManager )
123   {
124     //anApp->selectionMgr()->setEnabled(false); // what the hell?!
125     myPreviewViewManager->setTitle( tr( "CREATE_POLYLINE" ) );
126     OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
127     aViewer->enableSelection(true);
128     aViewer->enableMultiselection(true);
129     Handle_AIS_InteractiveContext aCtx = aViewer->getAISContext();
130
131     OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
132     vw->onTopView();
133
134     myAISCurve = new HYDROGUI_AISCurve(myCurve, aCtx);
135
136     myAISCurve->Display();
137   }
138 }
139
140 void HYDROGUI_PolylineOp::startOperation()
141 {
142   CurveCreator_Curve* anOldCurve = myCurve;
143
144   HYDROGUI_Operation::startOperation();
145
146   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
147   aPanel->reset();
148
149   myEditedObject = Handle(HYDROData_Polyline)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
150   if( !myEditedObject.IsNull() )
151   {
152     int anIntDim = myEditedObject->getDimension();
153     CurveCreator::Dimension aDim = CurveCreator::Dim3d;
154     if( anIntDim == 2 )
155       aDim = CurveCreator::Dim2d;
156     myCurve = new CurveCreator_Curve(aDim);
157     QList<PolylineSection> aPolylineData = myEditedObject->getPolylineData();
158
159     CurveCreator_CurveEditor* anEdit = new CurveCreator_CurveEditor(myCurve);
160     for( int i = 0 ; i < aPolylineData.size() ; i++ ){
161       std::string aName = HYDROGUI_Tool::ToQString(aPolylineData[i].mySectionName).toStdString();
162       bool isClosed = aPolylineData[i].myIsClosed;
163       CurveCreator::Type aType = CurveCreator::Polyline;
164       if( aPolylineData[i].myType == PolylineSection::SECTION_SPLINE ){
165         aType = CurveCreator::BSpline;
166       }
167       CurveCreator::Coordinates aCoords;
168       for( int j = 0 ; j < aPolylineData[i].myCoords.size() ; j++ ){
169         aCoords.push_back(aPolylineData[i].myCoords[j]);
170       }
171       anEdit->addSection( aName, aType, isClosed, aCoords );
172     }
173     delete anEdit;
174     aPanel->setPolylineName( myEditedObject->GetName() );
175
176   }
177   else{
178     myCurve = new CurveCreator_Curve(CurveCreator::Dim2d);
179     aPanel->setCurve(myCurve);
180     QString aNewName = HYDROGUI_Tool::GenerateObjectName( module(), "Polyline" );
181     aPanel->setPolylineName(aNewName);
182   }
183   aPanel->setCurve(myCurve);
184   if( myAISCurve )
185     myAISCurve->setCurve(myCurve);
186   if( anOldCurve )
187     delete anOldCurve;
188   onCreatePreview();
189 }
190
191 void HYDROGUI_PolylineOp::onEditorSelectionChanged()
192 {
193   HYDROGUI_PolylineDlg* aPanel = (HYDROGUI_PolylineDlg*)inputPanel();
194   if( !aPanel )
195     return;
196   if( !myCurve )
197     return;
198   if( !myAISCurve )
199     return;
200   QList<int> aSelSections = aPanel->getSelectedSections();
201   for( int i = 0 ; i < myCurve->getNbSections() ; i++ ){
202     bool aIsHl = false;
203     if( aSelSections.contains(i) ){
204       myAISCurve->highlightSection(i, aIsHl);
205     }
206   }
207 }