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,
53 const CurveCreator_Operation::Type theType)
57 if (theCurve != NULL) {
61 myPRedo = new CurveCreator_Operation;
63 if (myPRedo->init(theType)) {
66 const int aNbSections = theCurve->getNbSections();
68 if (theType == CurveCreator_Operation::Clear) {
69 // Construct undo for Clear command.
70 if (aNbSections > 0) {
71 setNbUndos(aNbSections);
73 for (int i = 0; i < aNbSections && isOK; i++) {
74 // Add AddSection command.
75 isOK = addSectionToUndo(theCurve, i, myPUndo[i]);
78 } else { // theType == CurveCreator_Operation::Join
79 // Construct undo for Join command.
80 if (aNbSections > 1) {
81 // Add the RemovePoints command to remove points of
82 // the second section fron the first one.
83 const int aNbPoints = theCurve->getNbPoints(0);
85 setNbUndos(aNbSections);
86 isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
89 for (int i = 1; i < aNbSections && isOK; i++) {
90 // Add AddSection command.
91 isOK = addSectionToUndo(theCurve, i, myPUndo[i]);
105 //=======================================================================
108 //=======================================================================
109 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
110 const CurveCreator_Operation::Type theType,
111 const int theIntParam)
115 if (theCurve != NULL) {
119 myPRedo = new CurveCreator_Operation;
121 if (myPRedo->init(theType, theIntParam)) {
122 // Construct undo for RemoveSection command.
123 // If the last section is removed, one AddSection command is enough.
124 // If not last section is removed, two commands are requred: AddSection
126 const int aLastIndex = theCurve->getNbSections() - 1;
128 if (theIntParam == aLastIndex) {
134 isOK = addSectionToUndo(theCurve, theIntParam, myPUndo[0]);
136 if (isOK && theIntParam != aLastIndex) {
137 isOK = myPUndo[1].init(CurveCreator_Operation::MoveSection,
138 aLastIndex, theIntParam);
150 //=======================================================================
153 //=======================================================================
154 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
155 const CurveCreator_Operation::Type theType,
156 const int theIntParam1,
157 const int theIntParam2)
161 if (theCurve != NULL) {
165 myPRedo = new CurveCreator_Operation;
167 if (myPRedo->init(theType, theIntParam1, theIntParam2)) {
168 // Construct undo for different commands.
170 case CurveCreator_Operation::SetType:
171 case CurveCreator_Operation::SetClosed:
172 isOK = setTypeOrClosedToUndo
173 (theCurve, theType, theIntParam1, theIntParam2);
175 case CurveCreator_Operation::MoveSection:
177 isOK = myPUndo[0].init(theType, theIntParam2, theIntParam1);
179 case CurveCreator_Operation::Join:
181 // If the last section is removed, one AddSection command is
182 // enough. If not last section is removed, two commands are
183 // requred: AddSection and MoveSection.
184 const int aLastIndex = theCurve->getNbSections() - 1;
185 const int aNbPoints = theCurve->getNbPoints(theIntParam1);
187 if (theIntParam2 == aLastIndex) {
193 isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
194 theIntParam1, aNbPoints, -1);
197 isOK = addSectionToUndo(theCurve, theIntParam2, myPUndo[1]);
199 if (isOK && theIntParam2 != aLastIndex) {
200 isOK = myPUndo[2].init(CurveCreator_Operation::MoveSection,
201 aLastIndex, theIntParam2);
219 //=======================================================================
222 //=======================================================================
223 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
224 const CurveCreator_Operation::Type theType,
225 const int theIntParam1,
226 const int theIntParam2,
227 const int theIntParam3)
231 if (theCurve != NULL) {
235 myPRedo = new CurveCreator_Operation;
237 if (myPRedo->init(theType, theIntParam1, theIntParam2, theIntParam3)) {
238 // Construct undo for RemovePoints command.
239 const CurveCreator::Dimension aDim = theCurve->getDimension();
240 const CurveCreator::Coordinates &aPoints =
241 theCurve->getPoints(theIntParam1);
242 CurveCreator::Coordinates::const_iterator anIterBegin =
243 aPoints.begin() + (aDim*theIntParam2);
244 CurveCreator::Coordinates::const_iterator anIterEnd;
246 if (theIntParam3 == -1) {
247 anIterEnd = aPoints.end();
249 anIterEnd = anIterBegin + (aDim*theIntParam3);
252 CurveCreator::Coordinates aPointsToAdd;
255 aPointsToAdd.insert(aPointsToAdd.end(), anIterBegin, anIterEnd);
256 isOK = myPUndo[0].init(CurveCreator_Operation::InsertPoints,
257 aPointsToAdd, theIntParam1, theIntParam2);
268 //=======================================================================
271 //=======================================================================
272 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
273 const CurveCreator_Operation::Type theType,
274 const CurveCreator::Coordinates &theCoords,
275 const int theIntParam)
279 if (theCurve != NULL) {
283 myPRedo = new CurveCreator_Operation;
285 if (myPRedo->init(theType, theCoords, theIntParam)) {
286 // Construct undo for AddPoints command.
287 const int aSectionInd = getSectionIndex(theCurve, theIntParam);
288 const CurveCreator::Dimension aDim = theCurve->getDimension();
289 const CurveCreator::Coordinates &aPoints =
290 theCurve->getPoints(aSectionInd);
291 const int aNbPoints = (aPoints.size()/aDim);
294 isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
295 aSectionInd, aNbPoints, -1);
306 //=======================================================================
309 //=======================================================================
310 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
311 const CurveCreator_Operation::Type theType,
312 const std::string& theName,
313 const CurveCreator::Coordinates &theCoords,
314 const int theIntParam1,
315 const int theIntParam2)
319 if (theCurve != NULL) {
323 myPRedo = new CurveCreator_Operation;
325 if (myPRedo->init(theType, theName, theCoords, theIntParam1, theIntParam2)) {
326 // Construct undo for different commands.
328 case CurveCreator_Operation::AddSection:
330 isOK = myPUndo[0].init(CurveCreator_Operation::RemoveSection, -1);
340 //=======================================================================
343 //=======================================================================
344 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
345 const CurveCreator_Operation::Type theType,
346 const CurveCreator::Coordinates &theCoords,
347 const int theIntParam1,
348 const int theIntParam2)
352 if (theCurve != NULL) {
356 myPRedo = new CurveCreator_Operation;
358 if (myPRedo->init(theType, theCoords, theIntParam1, theIntParam2)) {
359 // Construct undo for different commands.
361 case CurveCreator_Operation::InsertPoints:
363 const CurveCreator::Dimension aDim = theCurve->getDimension();
364 const int aNbPoints = (theCoords.size()/aDim);
365 const int aSectionInd = getSectionIndex(theCurve, theIntParam1);
368 if (theIntParam2 == -1) {
369 aPointInd = theCurve->getNbPoints(aSectionInd);
371 aPointInd = theIntParam2;
375 isOK = myPUndo[0].init(CurveCreator_Operation::RemovePoints,
376 aSectionInd, aPointInd, aNbPoints);
379 case CurveCreator_Operation::SetCoordinates:
381 const CurveCreator::Coordinates anOldCoords =
382 theCurve->getPoint(theIntParam1, theIntParam2);
385 isOK = myPUndo[0].init(CurveCreator_Operation::SetCoordinates,
386 anOldCoords, theIntParam1, theIntParam2);
402 bool CurveCreator_Diff::init(const CurveCreator_Curve *theCurve,
403 const CurveCreator_Operation::Type theType,
404 const std::string &theName,
405 const int theIntParam1 )
408 myPRedo = new CurveCreator_Operation;
410 if (myPRedo->init(theType, theName, theIntParam1 )) {
411 // Construct undo for different commands.
413 case CurveCreator_Operation::RenameSection:
415 isOK = myPUndo[0].init(CurveCreator_Operation::RenameSection,
416 theCurve->getSectionName(theIntParam1), theIntParam1);
426 //=======================================================================
427 // function: applyUndo
429 //=======================================================================
430 void CurveCreator_Diff::applyUndo(CurveCreator_Curve *theCurve)
432 if (myNbUndos > 0 && myPUndo != NULL) {
433 for (int i = 0; i < myNbUndos; i++) {
434 myPUndo[i].apply(theCurve);
439 //=======================================================================
440 // function: applyRedo
442 //=======================================================================
443 void CurveCreator_Diff::applyRedo(CurveCreator_Curve *theCurve)
445 if (myPRedo != NULL) {
446 myPRedo->apply(theCurve);
450 //=======================================================================
453 //=======================================================================
454 void CurveCreator_Diff::clear()
456 if (myPUndo != NULL) {
463 if (myPRedo != NULL) {
469 //=======================================================================
470 // function: setNbUndos
472 //=======================================================================
473 void CurveCreator_Diff::setNbUndos(const int theNbUndos)
475 myNbUndos = theNbUndos;
476 myPUndo = new CurveCreator_Operation[myNbUndos];
479 //=======================================================================
480 // function: getSectionIndex
482 //=======================================================================
483 int CurveCreator_Diff::getSectionIndex(const CurveCreator_Curve *theCurve,
484 const int theIndex) const
486 return (theIndex == -1 ? theCurve->getNbSections() - 1 : theIndex);
489 //=======================================================================
490 // function: addSectionToUndo
492 //=======================================================================
493 bool CurveCreator_Diff::addSectionToUndo
494 (const CurveCreator_Curve *theCurve,
496 CurveCreator_Operation &theOperation) const
498 const CurveCreator::Coordinates &aPnts = theCurve->getPoints(theIndex);
499 const CurveCreator::SectionType aType = theCurve->getSectionType(theIndex);
500 const bool isClosed = theCurve->isClosed(theIndex);
502 bool isOK = theOperation.init(CurveCreator_Operation::AddSection,
503 aPnts, aType, isClosed);
508 //=======================================================================
509 // function: setTypeOrClosedToUndo
511 //=======================================================================
512 bool CurveCreator_Diff::setTypeOrClosedToUndo
513 (const CurveCreator_Curve *theCurve,
514 const CurveCreator_Operation::Type theType,
515 const int theIntParam1,
516 const int theIntParam2)
520 // Compute number of modified sections.
521 const bool isSetType = (theType == CurveCreator_Operation::SetType);
523 std::list<int> aListOfInd;
527 if (theIntParam2 == -1) {
528 // The operation is applied to all sections. We need to collect
529 // really modified sections for undo.
530 const int aNbSections = theCurve->getNbSections();
532 if (aNbSections > 0) {
533 // Get sections to be modified.
534 for (i = 0; i < aNbSections; i++) {
536 aValue = theCurve->getSectionType(i);
538 aValue = theCurve->isClosed(i);
541 if (theIntParam1 != aValue) {
543 aListOfInd.push_back(i);
547 if (aNbSections == aNbModif) {
548 // All sections are modified. We can use one single command
549 // with -1 section index.
552 aListOfInd.push_back(-1);
556 // There is only particular section modified.
557 // Check if there is a real modification required.
559 aValue = theCurve->getSectionType(theIntParam2);
561 aValue = theCurve->isClosed(theIntParam2);
564 if (theIntParam1 != aValue) {
566 aListOfInd.push_back(theIntParam2);
572 std::list<int>::iterator anIter = aListOfInd.begin();
575 aValue = theCurve->getSectionType(*anIter);
577 aValue = theCurve->isClosed(*anIter);
580 setNbUndos(aNbModif);
582 for (i = 0; anIter != aListOfInd.end() && isOK; i++, anIter++) {
583 isOK = myPUndo[i].init(theType, aValue, *anIter);