1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROGUI_Module.h>
20 #include <HYDROGUI_ProfileOp.h>
21 #include <HYDROGUI_ProfileDlg.h>
22 #include <HYDROGUI_Tool2.h>
23 #include <HYDROGUI_UpdateFlags.h>
24 #include <HYDROGUI_DataObject.h>
25 #include <HYDROData_Document.h>
26 #include <HYDROData_Profile.h>
27 #include <HYDROGUI_CurveCreatorProfile.h>
28 #include <HYDROGUI_DeleteOp.h>
29 #include <HYDROData_Tool.h>
30 #include <CurveCreator_Displayer.hxx>
31 #include <HYDROData_Entity.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_SelectionMgr.h>
37 #include <LightApp_UpdateFlags.h>
39 #include <OCCViewer_ViewManager.h>
40 #include <OCCViewer_ViewModel.h>
41 #include <OCCViewer_ViewWindow.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_Desktop.h>
44 #include <OCCViewer_AISSelector.h>
46 #include <Precision.hxx>
48 //static int ZValueIncrement = 0;
50 static void ProfileUZToCurveCrProfile(const Handle(HYDROData_ProfileUZ)& aProfileUZ,
51 HYDROGUI_CurveCreatorProfile* outProfile)
53 CurveCreator::Coordinates aCurveCoords;
54 CurveCreator::SectionsMap aSectionsMap;
56 HYDROData_ProfileUZ::PointsList aSectPointsList = aProfileUZ->GetPoints();
57 CurveCreator::PosPointsList aPoints;
58 for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
60 const HYDROData_ProfileUZ::Point& aSectPoint = aSectPointsList.Value( k );
62 aCurveCoords.push_back( aSectPoint.X() );
63 aCurveCoords.push_back( aSectPoint.Y() );
65 CurveCreator_PosPoint* aPosPoint = new CurveCreator_PosPoint( k, aCurveCoords );
66 aPoints.push_back( aPosPoint );
69 aSectionsMap[0] = aPoints;
70 outProfile->addPointsInternal( aSectionsMap );
72 HYDROData_ProfileUZ::SectionType aSectType = aProfileUZ->GetSectionType( 0 );
74 CurveCreator::SectionType aCurveType = CurveCreator::Polyline;
75 if( aSectType == HYDROData_ProfileUZ::SECTION_SPLINE )
76 aCurveType = CurveCreator::Spline;
78 outProfile->setSectionType( 0, aCurveType );
81 static int CurveCrProfileToHProfile(const HYDROGUI_CurveCreatorProfile* outProfile,
82 Handle(HYDROData_Profile) theProfileObj,
83 const QString theProfileName,
86 if( theProfileObj.IsNull() )
89 Handle(HYDROData_ProfileUZ) aProfileUZ = theProfileObj->GetProfileUZ();
90 if ( aProfileUZ.IsNull() )
93 theProfileObj->SetName(theProfileName);
95 HYDROData_ProfileUZ::PointsList aProfileParamPoints;
97 Handle(TColgp_HArray1OfPnt) aCurveCoords = outProfile->GetDifferentPoints( 0 );
98 if ( aCurveCoords.IsNull() || aCurveCoords->Size() <= 2 )
101 for ( int k = aCurveCoords->Lower(); k <= aCurveCoords->Upper() ; k++ )
103 HYDROData_ProfileUZ::Point aProfileParamPoint;
105 aProfileParamPoint.SetX( aCurveCoords->Value( k ).X() );
106 aProfileParamPoint.SetY( aCurveCoords->Value( k ).Y() );
108 aProfileParamPoints.Append( aProfileParamPoint );
110 theProfileObj->SetParametricPoints( aProfileParamPoints );
112 HYDROData_ProfileUZ::SectionType aSectType = HYDROData_ProfileUZ::SECTION_POLYLINE;
113 if ( outProfile->getSectionType( 0 ) == CurveCreator::Spline )
114 aSectType = HYDROData_ProfileUZ::SECTION_SPLINE;
116 aProfileUZ->SetSectionType( 0, aSectType );
119 theProfileObj->SetBorderColor( theProfileObj->DefaultBorderColor() );
121 // At first we update the child u,z profile object
122 aProfileUZ->Changed( HYDROData_Entity::Geom_2d );
123 aProfileUZ->Update();
125 // And now we update our edited object
126 theProfileObj->Update();
130 HYDROGUI_ProfileOp::HYDROGUI_ProfileOp( HYDROGUI_Module* theModule, bool theIsEdit )
131 : HYDROGUI_Operation( theModule ), myIsEdit(theIsEdit),
134 setName( theIsEdit ? tr( "EDIT_PROFILE" ) : tr( "CREATE_PROFILE" ) );
137 HYDROGUI_ProfileOp::~HYDROGUI_ProfileOp()
143 * Redirect the delete action to input panel
145 void HYDROGUI_ProfileOp::deleteSelected()
147 HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
148 aPanel->deleteSelected();
152 * Checks whether there are some to delete
154 #include <CurveCreator_Widget.h>
155 bool HYDROGUI_ProfileOp::deleteEnabled()
157 HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
158 return aPanel->deleteEnabled();
161 void HYDROGUI_ProfileOp::startOperation()
163 if (!myProfiles.empty())
165 for (int i = 0; i < myProfiles.size(); i++)
167 delete myProfiles[i];
168 myProfiles[i] = NULL;
174 myEditedObjects.Clear();
176 if (myCurveToProfile.IsEmpty())
177 myCurveToProfile.Clear();
179 if( myIsEdit && isApplyAndClose() )
180 myEditedObjects = HYDROGUI_Tool::GetSelectedObjects(module());
182 int lenP = myEditedObjects.Length();
183 myProfiles.resize(lenP == 0 ? 1 : myEditedObjects.Length());
184 for (int i = 0; i < myProfiles.size(); i++)
185 myProfiles[i] = new HYDROGUI_CurveCreatorProfile();
187 //mySingleProfileMode = myEditedObjects.IsEmpty();
188 HYDROGUI_Operation::startOperation();
190 HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
191 aPanel->myEditorWidget->setCurve(NULL);
193 setPreviewManager( aPanel->viewManager() );
196 //aPanel->SetSingleProfileMode(mySingleProfileMode);
197 QMap<HYDROGUI_CurveCreatorProfile*, QColor> CurveToColor;
200 for (int i = 1; i <= lenP; i++)
202 Handle(HYDROData_Profile) aCProfile = Handle(HYDROData_Profile)::DownCast(myEditedObjects(i));
203 QString aProfileName;
204 if( !aCProfile.IsNull() )
206 Handle(HYDROData_ProfileUZ) aProfileUZ = aCProfile->GetProfileUZ( false );
207 if ( !aProfileUZ.IsNull() )
209 HYDROGUI_CurveCreatorProfile* CP = new HYDROGUI_CurveCreatorProfile();
210 myProfiles[i-1] = CP;
211 ProfileUZToCurveCrProfile(aProfileUZ, CP);
212 myCurveToProfile.Bind(CP, aCProfile);
216 int ext = myCurveToProfile.Extent(); //ext should be equal to lenP
217 QVector<QColor> PColors;
219 HYDROData_Tool::GenerateRepeatableRandColors(ext, PColors);
221 PColors << QColor(0,0,255); //default color
223 for (int i = 0; i < myProfiles.size(); i++)
225 HYDROGUI_CurveCreatorProfile* CC = myProfiles[i];
226 const Handle(HYDROData_Profile)& CP = myCurveToProfile.Find(CC);
227 QColor CurCol = PColors[i];
228 CP->GetProfileColor(CurCol);
229 CurveToColor[CC] = CurCol;
230 const QString& profName = CP->GetName();
231 const QColor& PColor = CurCol;
233 aPanel->addProfileName(profName, PColor);
235 aPanel->setProfileName(profName);
241 QString aProfileName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_PROFILE_NAME" ) );
242 aPanel->setProfileName( aProfileName );
245 if (!myProfiles.empty())
247 aPanel->setProfile( myProfiles[0] );
248 aPanel->setProfilesPointer( &myProfiles );
250 displayPreviews(CurveToColor, 0, myProfiles.size(), true, true );
253 void HYDROGUI_ProfileOp::onAddProfiles()
258 QSet<QString> edObjNamesMap;
259 for (int i = 1; i <= myEditedObjects.Length(); i++)
260 edObjNamesMap.insert( myEditedObjects(i)->GetName());
262 HYDROData_SequenceOfObjects aSelectedObj = HYDROGUI_Tool::GetSelectedObjects( module() );;
263 int ExistingProfLen = myEditedObjects.Length();
264 for (int i = 1; i <= aSelectedObj.Length(); i++)
266 Handle(HYDROData_Profile) CurProf = Handle(HYDROData_Profile)::DownCast(aSelectedObj.Value(i));
267 if (CurProf.IsNull())
269 if (!edObjNamesMap.contains(CurProf->GetName()))
270 myEditedObjects.Append(CurProf);
273 int NewLen = myEditedObjects.Length();
274 bool IsNewPoly = NewLen - ExistingProfLen;
275 if (aSelectedObj.IsEmpty())
277 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "PROFILEOP_WARNING" ), tr ("PROFILES_ARE_NOT_SELECTED") );
282 SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "PROFILEOP_WARNING" ), tr ("PROFILES_ALREADY_PRESENT") );
285 myProfiles.resize(myEditedObjects.Length());
286 for (int i = myProfiles.size() - 1; i < myEditedObjects.Length(); i++)
287 myProfiles[i] = new HYDROGUI_CurveCreatorProfile();
289 HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
290 QMap<HYDROGUI_CurveCreatorProfile*, QColor> CurveToColor;
293 //TODO move to ext func!
294 for (int i = ExistingProfLen + 1; i <= NewLen; i++)
296 Handle(HYDROData_Profile) aCProfile = Handle(HYDROData_Profile)::DownCast(myEditedObjects(i));
298 QString aProfileName;
299 if( !aCProfile.IsNull() )
301 Handle(HYDROData_ProfileUZ) aProfileUZ = aCProfile->GetProfileUZ( false );
302 if ( !aProfileUZ.IsNull() )
304 HYDROGUI_CurveCreatorProfile* CP = new HYDROGUI_CurveCreatorProfile();
305 myProfiles[i-1] = CP;
306 ProfileUZToCurveCrProfile(aProfileUZ, CP);
307 myCurveToProfile.Bind(CP, aCProfile);
311 //int ext = myCurveToProfile.Extent(); //ext should be equal to lenP
312 QVector<QColor> PColors;
313 HYDROData_Tool::GenerateRepeatableRandColors(NewLen - ExistingProfLen, PColors);
315 for (int i = ExistingProfLen; i < NewLen; i++)
317 HYDROGUI_CurveCreatorProfile* CC = myProfiles[i];
318 const Handle(HYDROData_Profile)& CP = myCurveToProfile.Find(CC);
319 QColor CurCol = PColors[i-ExistingProfLen];
320 CP->GetProfileColor(CurCol);
321 CurveToColor[CC] = CurCol;
322 const QString& profName = CP->GetName();
323 const QColor& PColor = CurCol;
325 aPanel->addProfileName(profName, PColor);
327 aPanel->setProfileName(profName);
330 displayPreviews(CurveToColor, ExistingProfLen, NewLen, false, false);
333 void HYDROGUI_ProfileOp::abortOperation()
338 HYDROGUI_Operation::abortOperation();
341 void HYDROGUI_ProfileOp::commitOperation()
346 HYDROGUI_Operation::commitOperation();
349 HYDROGUI_InputPanel* HYDROGUI_ProfileOp::createInputPanel() const
351 HYDROGUI_ProfileDlg* aDlg = new HYDROGUI_ProfileDlg( module(), getName(), myIsEdit );
352 connect( aDlg, SIGNAL( AddProfiles() ), this,
353 SLOT( onAddProfiles() ) );
354 connect( aDlg, SIGNAL( RemoveProfile(int) ), this,
355 SLOT( onRemoveProfile(int) ) );
360 bool HYDROGUI_ProfileOp::processApply( int& theUpdateFlags,
361 QString& theErrorMsg,
362 QStringList& theBrowseObjectsEntries )
364 HYDROGUI_ProfileDlg* aPanel = ::qobject_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
368 QStringList aProfileNames = aPanel->getProfileNames();
369 QVector<QString> aProfileNamesFiltered;
371 // QSet<QString> edObjStrMap = aProfileNames.toSet();
372 QSet<QString> ObjStrMapNE ;
373 HYDROData_SequenceOfObjects allobj = doc()->CollectAllObjects();
374 for (int i=1;i<=allobj.Size();i++ )
375 ObjStrMapNE.insert(allobj(i)->GetName());
376 for (int i=1; i<=myEditedObjects.Size();i++)
377 ObjStrMapNE.remove(myEditedObjects(i)->GetName());
379 QString title = tr( "PROFILEOP_WARNING" );
380 QString mes = tr("PROFILE_RENAMING_NOTIF") + "\n";
381 foreach (QString profName, aProfileNames)
384 if( !myIsEdit || ObjStrMapNE.contains(profName) || profName.isEmpty() )
386 QString newName = HYDROData_Tool::GenerateObjectName(doc(), "Profile");
387 mes += profName + " => " + newName + "\n";
391 aProfileNamesFiltered.append(profName);
393 if (myIsEdit && warn)
395 SUIT_MessageBox::warning( module()->getApp()->desktop(), title, mes );
398 theUpdateFlags = UF_Model;
401 for (int i = 1; i <= myEditedObjects.Size(); i++)
403 Handle(HYDROData_Profile) HProf = Handle(HYDROData_Profile)::DownCast(myEditedObjects(i));
404 int stat = CurveCrProfileToHProfile(myProfiles[i-1], HProf, aProfileNamesFiltered[i-1], true);
405 HProf->SetProfileColor(HYDROData_Tool::toQtColor(myProfiles[i-1]->getColorSection(0)));
410 theErrorMsg = tr( "NUMBER_OF_PROFILE_POINTS_INCORRECT" ); //TODO resolve this
413 module()->setIsToUpdate( HProf );
415 theUpdateFlags |= UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
419 Handle(HYDROData_Profile) aNewProfileObj = Handle(HYDROData_Profile)::DownCast( doc()->CreateObject( KIND_PROFILE ) );
420 int stat = CurveCrProfileToHProfile(myProfiles[0], aNewProfileObj, aProfileNamesFiltered[0], false);
421 aNewProfileObj->SetProfileColor(HYDROData_Tool::toQtColor(myProfiles[0]->getColorSection(0)));
426 theErrorMsg = tr( "NUMBER_OF_PROFILE_POINTS_INCORRECT" );
429 module()->setIsToUpdate( aNewProfileObj );
430 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( aNewProfileObj );
431 theBrowseObjectsEntries.append( anEntry );
437 void HYDROGUI_ProfileOp::displayPreviews(const QMap<HYDROGUI_CurveCreatorProfile*, QColor>& CurveToColor,
438 int firstIndProf, int lastIndProf, bool createNewDisplayer, bool SwitchToFirstProf)
440 HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
443 Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
448 //delete myDisplayer;
449 //myDisplayer = NULL;
451 if (createNewDisplayer)
452 myDisplayer = new CurveCreator_Displayer( aCtx );
453 for (int i = firstIndProf; i < lastIndProf; i++ )
455 HYDROGUI_CurveCreatorProfile* CC = myProfiles[i];
456 QColor QCurCol = QColor(0,0,255); //def color
458 QCurCol = CurveToColor[CC];
459 Quantity_Color CurOCCCol = HYDROData_Tool::toOccColor(QCurCol);
460 CC->setDisplayer( myDisplayer );
461 CC->myPointAspectColor = CurOCCCol;
462 CC->setColorSectionInternal(0, CurOCCCol);
464 myDisplayer->display( CC->getAISObject( true ), true );
467 if (SwitchToFirstProf && myProfiles.size() > 1)
468 aPanel->switchToFirstProfile();
472 void HYDROGUI_ProfileOp::erasePreview()
474 HYDROGUI_ProfileDlg* aPanel = dynamic_cast<HYDROGUI_ProfileDlg*>( inputPanel() );
475 //CurveCreator_Displayer* aDisplayer = myProfiles[0] ? myProfile[0]->getDisplayer() : 0;
476 if( aPanel && myDisplayer )
478 Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
481 myDisplayer->eraseAll( true );
484 myCurveToProfile.Clear();
487 void HYDROGUI_ProfileOp::onRemoveProfile(int index)
489 if (index >= myProfiles.size() )
495 HYDROGUI_CurveCreatorProfile* CP = myProfiles[index];
496 myProfiles.erase (myProfiles.begin()+index);
497 myEditedObjects.Remove(index+1);
498 myCurveToProfile.UnBind(CP);
500 HYDROGUI_ProfileDlg* aPanel = (HYDROGUI_ProfileDlg*)inputPanel();
502 //setPreviewManager( aPanel->viewManager() );
505 aPanel->BlockProfileNameSignals(true);
506 aPanel->eraseProfile(index);
508 Handle(AIS_InteractiveContext) aCtx = aPanel->getAISContext();
509 if( !aCtx.IsNull() && myDisplayer)
510 myDisplayer->erase( CP->getAISObject(false), true);
511 CP->SetEraseAllState(false);
513 int selectedInd = aPanel->GetProfileSelectionIndex();
514 if (selectedInd > -1)
515 aPanel->SwitchToProfile(selectedInd);
516 aPanel->BlockProfileNameSignals(false);
519 bool HYDROGUI_ProfileOp::isValid( SUIT_Operation* theOtherOp ) const
521 HYDROGUI_DeleteOp* aDelOp = dynamic_cast<HYDROGUI_DeleteOp*>( theOtherOp );