1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: CurveCreator_Diff.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_Diff.hxx"
24 #include "CurveCreator_Curve.hxx"
28 //=======================================================================
29 // function: Constructor
31 //=======================================================================
32 CurveCreator_Diff::CurveCreator_Diff()
39 //=======================================================================
40 // function: Destructor
42 //=======================================================================
43 CurveCreator_Diff::~CurveCreator_Diff()
48 //=======================================================================
51 //=======================================================================
52 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve)
54 CurveCreator_Operation::Type aType = CurveCreator_Operation::Clear;
57 if (theCurve != NULL) {
61 myPRedo = new CurveCreator_Operation;
63 if (myPRedo->init(aType)) {
66 const int aNbSections = theCurve->getNbSections();
68 // Construct undo for Clear command.
69 if (aNbSections > 0) {
70 setNbUndos(aNbSections);
72 for (int i = 0; i < aNbSections && isOK; i++) {
73 // Add AddSection command.
74 isOK = addSectionToUndo(theCurve, i, myPUndo[i]);
87 //=======================================================================
90 //=======================================================================
91 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
92 const CurveCreator_Operation::Type theType,
93 const int theIntParam)
97 if (theCurve != NULL) {
101 myPRedo = new CurveCreator_Operation;
103 if (myPRedo->init(theType, theIntParam)) {
104 // Construct undo for RemoveSection command.
105 // If the last section is removed, one AddSection command is enough.
106 // If not last section is removed, two commands are requred: AddSection
108 const int aLastIndex = theCurve->getNbSections() - 1;
110 if (theIntParam == aLastIndex) {
116 isOK = addSectionToUndo(theCurve, theIntParam, myPUndo[0]);
118 if (isOK && theIntParam != aLastIndex) {
119 isOK = myPUndo[1].init(CurveCreator_Operation::MoveSection,
120 aLastIndex, theIntParam);
132 //=======================================================================
135 //=======================================================================
136 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
137 const CurveCreator_Operation::Type theType,
138 const int theIntParam1,
139 const int theIntParam2)
143 if (theCurve != NULL) {
147 myPRedo = new CurveCreator_Operation;
149 if (myPRedo->init(theType, theIntParam1, theIntParam2)) {
150 // Construct undo for different commands.
152 case CurveCreator_Operation::SetType:
153 case CurveCreator_Operation::SetClosed:
154 isOK = setTypeOrClosedToUndo
155 (theCurve, theType, theIntParam1, theIntParam2);
157 case CurveCreator_Operation::MoveSection:
159 isOK = myPUndo[0].init(theType, theIntParam2, theIntParam1);
161 case CurveCreator_Operation::Join:
163 // If the last section is removed, one AddSection command is
164 // enough. If not last section is removed, two commands are
165 // requred: AddSection and MoveSection.
166 const int aLastIndex = theCurve->getNbSections() - 1;
167 const int aNbPoints = theCurve->getNbPoints(theIntParam1);
169 if (theIntParam2 == aLastIndex) {
175 // Construct undo for RemovePoints command.
176 int anISection = theIntParam1;
177 const int aNbPoints2 = theCurve->getNbPoints(theIntParam2);
178 CurveCreator_ICurve::SectionToPointList aSectionToPointList;
179 int aJoinedSize = aNbPoints + aNbPoints2;
180 for (int anIPoint = aNbPoints; anIPoint < aJoinedSize; anIPoint++)
181 aSectionToPointList.push_back(std::make_pair( anISection, anIPoint ) );
182 isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
183 aSectionToPointList);
185 //isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
186 // theIntParam1, aNbPoints, -1);
189 isOK = addSectionToUndo(theCurve, theIntParam2, myPUndo[1]);
191 if (isOK && theIntParam2 != aLastIndex) {
192 isOK = myPUndo[2].init(CurveCreator_Operation::MoveSection,
193 aLastIndex, theIntParam2);
211 //=======================================================================
214 //=======================================================================
215 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
216 const CurveCreator_Operation::Type theType,
217 const std::string& theName,
218 const CurveCreator::Coordinates &theCoords,
219 const int theIntParam1,
220 const int theIntParam2)
224 if (theCurve != NULL) {
228 myPRedo = new CurveCreator_Operation;
230 if (myPRedo->init(theType, theName, theCoords, theIntParam1, theIntParam2)) {
231 // Construct undo for different commands.
233 case CurveCreator_Operation::AddSection:
235 isOK = myPUndo[0].init(CurveCreator_Operation::RemoveSection, -1);
245 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
246 const CurveCreator_Operation::Type theType,
247 const std::string &theName,
248 const int theIntParam1 )
251 myPRedo = new CurveCreator_Operation;
253 if (myPRedo->init(theType, theName, theIntParam1 )) {
254 // Construct undo for different commands.
256 case CurveCreator_Operation::RenameSection:
258 isOK = myPUndo[0].init(CurveCreator_Operation::RenameSection,
259 theCurve->getSectionName(theIntParam1), theIntParam1);
269 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
270 const CurveCreator_Operation::Type theType,
271 const CurveCreator_ICurve::SectionToPointList &theParamList1)
275 if (theCurve != NULL) {
279 myPRedo = new CurveCreator_Operation;
281 if (myPRedo->init(theType, theParamList1)) {
282 // Construct undo for different commands.
284 case CurveCreator_Operation::RemovePoints:
286 // Construct undo for RemovePoints command.
287 CurveCreator_ICurve::SectionToPointCoordsList aSectionToPointCoords;
288 CurveCreator::Coordinates aPointsToAdd;
289 const CurveCreator::Dimension aDim = theCurve->getDimension();
290 CurveCreator_ICurve::SectionToPointList::const_iterator anIt = theParamList1.begin(), aLast = theParamList1.end();
291 std::list<int> aPoints;
292 int aSectionId, aPointId;
293 for ( ; anIt != aLast; anIt++ ) {
294 aPointsToAdd.clear();
295 aSectionId = anIt->first;
296 aPointId = anIt->second;
297 const CurveCreator::Coordinates &aPoints =
298 theCurve->getPoints(aSectionId);
299 CurveCreator::Coordinates::const_iterator anIterBegin =
300 aPoints.begin() + (aDim*aPointId);
301 CurveCreator::Coordinates::const_iterator anIterEnd =
303 aPointsToAdd.insert(aPointsToAdd.end(), anIterBegin, anIterEnd);
304 aSectionToPointCoords.push_back(std::make_pair(*anIt, aPointsToAdd));
307 isOK = myPUndo[0].init(CurveCreator_Operation::InsertPoints,
308 aSectionToPointCoords);
324 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
325 const CurveCreator_Operation::Type theType,
326 const CurveCreator_ICurve::SectionToPointCoordsList &theParamList1)
330 if (theCurve != NULL) {
334 myPRedo = new CurveCreator_Operation;
336 if (myPRedo->init(theType, theParamList1)) {
337 // Construct undo for different commands.
339 case CurveCreator_Operation::InsertPoints:
341 // Construct undo for RemovePoints command.
342 CurveCreator_ICurve::SectionToPointList aSectionToPointList;
343 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt = theParamList1.begin(), aLast = theParamList1.end();
344 for ( ; anIt != aLast; anIt++ ) {
345 aSectionToPointList.push_back(anIt->first);
348 isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
349 aSectionToPointList);
352 case CurveCreator_Operation::SetCoordinates:
354 // Construct undo for SetCoordinates command.
355 CurveCreator_ICurve::SectionToPointCoordsList aSectionToPointOldCoords;
356 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt = theParamList1.begin(), aLast = theParamList1.end();
357 for ( ; anIt != aLast; anIt++ ) {
358 CurveCreator::Coordinates anOldCoords = theCurve->getPoint(anIt->first.first, anIt->first.second);
359 aSectionToPointOldCoords.push_back(std::make_pair(anIt->first, anOldCoords));
363 isOK = myPUndo[0].init(CurveCreator_Operation::SetCoordinates,
364 aSectionToPointOldCoords);
380 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
381 const CurveCreator_ICurve::SectionToPointCoordsList &theOldParamList)
385 if (theCurve != NULL && theOldParamList.size() > 0) {
389 myPRedo = new CurveCreator_Operation;
391 // Construct redo for SetCoordinates command.
392 CurveCreator_ICurve::SectionToPointCoordsList aSectionToPointActualCoords;
393 CurveCreator_ICurve::SectionToPointCoordsList::const_iterator anIt =
394 theOldParamList.begin(), aLast = theOldParamList.end();
395 for ( ; anIt != aLast; anIt++ ) {
396 CurveCreator::Coordinates anActualCoords = theCurve->getPoint(anIt->first.first, anIt->first.second);
397 aSectionToPointActualCoords.push_back(std::make_pair(anIt->first, anActualCoords));
400 if (myPRedo->init(CurveCreator_Operation::SetCoordinates, aSectionToPointActualCoords)) {
401 // Undo for SetCoordinates command.
403 isOK = myPUndo[0].init(CurveCreator_Operation::SetCoordinates,
415 //=======================================================================
416 // function: applyUndo
418 //=======================================================================
419 void CurveCreator_Diff::applyUndo(CurveCreator_Curve *theCurve)
421 if (myNbUndos > 0 && myPUndo != NULL) {
422 for (int i = 0; i < myNbUndos; i++) {
423 myPUndo[i].apply(theCurve);
428 //=======================================================================
429 // function: applyRedo
431 //=======================================================================
432 void CurveCreator_Diff::applyRedo(CurveCreator_Curve *theCurve)
434 if (myPRedo != NULL) {
435 myPRedo->apply(theCurve);
439 //=======================================================================
442 //=======================================================================
443 void CurveCreator_Diff::clear()
445 if (myPUndo != NULL) {
452 if (myPRedo != NULL) {
458 //=======================================================================
459 // function: setNbUndos
461 //=======================================================================
462 void CurveCreator_Diff::setNbUndos(const int theNbUndos)
464 myNbUndos = theNbUndos;
465 myPUndo = new CurveCreator_Operation[myNbUndos];
468 //=======================================================================
469 // function: getSectionIndex
471 //=======================================================================
472 int CurveCreator_Diff::getSectionIndex(const CurveCreator_Curve *theCurve,
473 const int theIndex) const
475 return (theIndex == -1 ? theCurve->getNbSections() - 1 : theIndex);
478 //=======================================================================
479 // function: addSectionToUndo
481 //=======================================================================
482 bool CurveCreator_Diff::addSectionToUndo
483 (const CurveCreator_Curve *theCurve,
485 CurveCreator_Operation &theOperation) const
487 const std::string aName = theCurve->getSectionName(theIndex);
488 const CurveCreator::Coordinates &aPnts = theCurve->getPoints(theIndex);
489 const CurveCreator::SectionType aType = theCurve->getSectionType(theIndex);
490 const bool isClosed = theCurve->isClosed(theIndex);
492 bool isOK = theOperation.init(CurveCreator_Operation::AddSection,
493 aName, aPnts, aType, isClosed);
498 //=======================================================================
499 // function: setTypeOrClosedToUndo
501 //=======================================================================
502 bool CurveCreator_Diff::setTypeOrClosedToUndo
503 (const CurveCreator_Curve *theCurve,
504 const CurveCreator_Operation::Type theType,
505 const int theIntParam1,
506 const int theIntParam2)
510 // Compute number of modified sections.
511 const bool isSetType = (theType == CurveCreator_Operation::SetType);
513 std::list<int> aListOfInd;
517 if (theIntParam2 == -1) {
518 // The operation is applied to all sections. We need to collect
519 // really modified sections for undo.
520 const int aNbSections = theCurve->getNbSections();
522 if (aNbSections > 0) {
523 // Get sections to be modified.
524 for (i = 0; i < aNbSections; i++) {
526 aValue = theCurve->getSectionType(i);
528 aValue = theCurve->isClosed(i);
531 if (theIntParam1 != aValue) {
533 aListOfInd.push_back(i);
537 if (aNbSections == aNbModif) {
538 // All sections are modified. We can use one single command
539 // with -1 section index.
542 aListOfInd.push_back(-1);
546 // There is only particular section modified.
547 // Check if there is a real modification required.
549 aValue = theCurve->getSectionType(theIntParam2);
551 aValue = theCurve->isClosed(theIntParam2);
554 if (theIntParam1 != aValue) {
556 aListOfInd.push_back(theIntParam2);
562 std::list<int>::iterator anIter = aListOfInd.begin();
565 aValue = theCurve->getSectionType(*anIter);
567 aValue = theCurve->isClosed(*anIter);
570 setNbUndos(aNbModif);
572 for (i = 0; anIter != aListOfInd.end() && isOK; i++, anIter++) {
573 isOK = myPUndo[i].init(theType, aValue, *anIter);