1 // Copyright (C) 2013-2014 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, or (at your option) any later version.
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_CurveEditor.cxx
21 // Author: Sergey KHROMOV
23 #include "CurveCreator_CurveEditor.hxx"
25 //=======================================================================
26 // function: Constructor
28 //=======================================================================
29 CurveCreator_CurveEditor::CurveCreator_CurveEditor
30 (CurveCreator_Curve* thePCurve)
37 if (myPCurve != NULL) {
38 if (myPCurve->isLocked()) {
39 // This curve is locked by another editor. Invalid case.
43 myPCurve->myIsLocked = true;
44 myCurrenPos = myListDiffs.end();
49 //=======================================================================
50 // function: Destructor
52 //=======================================================================
53 CurveCreator_CurveEditor::~CurveCreator_CurveEditor()
55 if (myPCurve != NULL) {
57 myPCurve->myIsLocked = false;
61 //=======================================================================
64 //=======================================================================
65 CurveCreator_Curve *CurveCreator_CurveEditor::getCurve() const
70 //=======================================================================
71 // function: isAttached
73 //=======================================================================
74 bool CurveCreator_CurveEditor::isAttached() const
76 return (myPCurve != NULL);
79 //=======================================================================
82 //=======================================================================
83 void CurveCreator_CurveEditor::undo()
89 myCurrenPos->applyUndo(myPCurve);
93 //=======================================================================
96 //=======================================================================
97 void CurveCreator_CurveEditor::redo()
100 myCurrenPos->applyRedo(myPCurve);
107 //=======================================================================
108 // function: getNbUndo
110 //=======================================================================
111 int CurveCreator_CurveEditor::getNbUndo() const
116 //=======================================================================
117 // function: getNbRedo
119 //=======================================================================
120 int CurveCreator_CurveEditor::getNbRedo() const
125 //=======================================================================
126 // function: setUndoDepth
128 //=======================================================================
129 void CurveCreator_CurveEditor::setUndoDepth(const int theDepth)
132 // Reset all undo/redo data.
136 myCurrenPos = myListDiffs.end();
138 } else if (theDepth == -1) {
139 // There is nothing to do as the depth become unlimited.
141 } else if (theDepth > 0) {
142 // The new "real" depth is set.
143 if (theDepth < myNbRedos) {
144 // The new depth is less then number of redos. Remove the latest redos.
145 int aShift = (myNbRedos - theDepth);
146 ListDiff::iterator aFromPos = myListDiffs.end();
152 myListDiffs.erase(aFromPos, myListDiffs.end());
153 myNbRedos = theDepth;
156 if (theDepth < myNbUndos + myNbRedos) {
157 // The new depth is less then the total number of differences.
158 // Remove the first undos.
159 int aShift = (myNbUndos + myNbRedos - theDepth);
160 ListDiff::iterator aToPos = myListDiffs.begin();
166 myListDiffs.erase(myListDiffs.begin(), aToPos);
167 myNbUndos = theDepth - myNbRedos;
170 myUndoDepth = theDepth;
174 //=======================================================================
175 // function: getUndoDepth
177 //=======================================================================
178 int CurveCreator_CurveEditor::getUndoDepth() const
183 //=======================================================================
186 //=======================================================================
187 void CurveCreator_CurveEditor::setType(const CurveCreator::Type theType,
188 const int theISection)
190 if (myPCurve != NULL) {
192 // Set the difference.
193 if (addEmptyDiff()) {
194 myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetType,
195 theType, theISection);
199 myPCurve->setType(theType, theISection);
204 //=======================================================================
205 // function: addPoints
207 //=======================================================================
208 void CurveCreator_CurveEditor::addPoints
209 (const CurveCreator::Coordinates &thePoints,
210 const int theISection)
212 if (myPCurve != NULL) {
213 // Set the difference.
215 if (addEmptyDiff()) {
216 myListDiffs.back().init(myPCurve, CurveCreator_Operation::AddPoints,
217 thePoints, theISection);
221 myPCurve->addPoints(thePoints, theISection);
226 //=======================================================================
227 // function: addSection
229 //=======================================================================
230 void CurveCreator_CurveEditor::addSection
231 (const std::string& theName, const CurveCreator::Type theType,
232 const bool theIsClosed,
233 const CurveCreator::Coordinates &thePoints)
235 if (myPCurve != NULL) {
236 // Set the difference.
238 if (addEmptyDiff()) {
239 myListDiffs.back().init(myPCurve, CurveCreator_Operation::AddSection,
240 theName, thePoints, theType, theIsClosed);
244 myPCurve->addSection(theName, theType, theIsClosed, thePoints);
249 //=======================================================================
250 // function: removeSection
252 //=======================================================================
253 void CurveCreator_CurveEditor::removeSection(const int theISection)
255 if (myPCurve != NULL) {
256 // Set the difference.
258 if (addEmptyDiff()) {
259 myListDiffs.back().init(myPCurve, CurveCreator_Operation::RemoveSection,
264 myPCurve->removeSection(theISection);
269 //=======================================================================
270 // function: insertPoints
272 //=======================================================================
273 void CurveCreator_CurveEditor::insertPoints
274 (const CurveCreator::Coordinates &thePoints,
275 const int theISection,
278 if (myPCurve != NULL) {
279 // Set the difference.
281 if (addEmptyDiff()) {
282 myListDiffs.back().init(myPCurve, CurveCreator_Operation::InsertPoints,
283 thePoints, theISection, theIPnt);
287 myPCurve->insertPoints(thePoints, theISection, theIPnt);
292 //=======================================================================
293 // function: movePoints
295 //=======================================================================
296 void CurveCreator_CurveEditor::movePoint(const int theISection,
297 const int theOrigIPnt,
298 const int theNewIPnt )
301 myPCurve->movePoint(theISection, theOrigIPnt, theNewIPnt);
305 //=======================================================================
306 // function: removePoints
308 //=======================================================================
309 void CurveCreator_CurveEditor::removePoints
310 (const int theISection,
312 const int theNbPoints)
314 if (myPCurve != NULL) {
315 // Set the difference.
317 if (addEmptyDiff()) {
318 myListDiffs.back().init(myPCurve, CurveCreator_Operation::RemovePoints,
319 theISection, theIPnt, theNbPoints);
323 myPCurve->removePoints(theISection, theIPnt, theNbPoints);
328 //=======================================================================
331 //=======================================================================
332 void CurveCreator_CurveEditor::clear()
334 if (myPCurve != NULL) {
336 // Set the difference.
337 if (addEmptyDiff()) {
338 myListDiffs.back().init(myPCurve, CurveCreator_Operation::Clear);
347 //=======================================================================
348 // function: setCoordinates
350 //=======================================================================
351 void CurveCreator_CurveEditor::setCoordinates
352 (const CurveCreator::Coordinates &theCoords,
353 const int theISection,
356 if (myPCurve != NULL) {
357 // Set the difference.
359 if (addEmptyDiff()) {
360 myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetCoordinates,
361 theCoords, theISection, theIPnt);
365 myPCurve->setCoordinates(theCoords, theISection, theIPnt);
370 //=======================================================================
371 // function: setClosed
373 //=======================================================================
374 void CurveCreator_CurveEditor::setClosed(const bool theIsClosed,
375 const int theISection)
377 if (myPCurve != NULL) {
378 // Set the difference.
380 if (addEmptyDiff()) {
381 myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetClosed,
382 theIsClosed, theISection);
386 myPCurve->setClosed(theIsClosed, theISection);
391 //=======================================================================
394 //=======================================================================
395 void CurveCreator_CurveEditor::setName(const std::string& theName,
396 const int theISection)
398 if (myPCurve != NULL) {
399 // Set the difference.
401 if (addEmptyDiff()) {
402 myListDiffs.back().init(myPCurve, CurveCreator_Operation::RenameSection,
403 theName, theISection);
405 myPCurve->setName( theName, theISection );
410 //=======================================================================
411 // function: moveSection
413 //=======================================================================
414 void CurveCreator_CurveEditor::moveSection(const int theISection,
415 const int theNewIndex)
417 if (myPCurve != NULL) {
418 // Set the difference.
420 if (addEmptyDiff()) {
421 myListDiffs.back().init(myPCurve, CurveCreator_Operation::MoveSection,
422 theISection, theNewIndex);
426 myPCurve->moveSection(theISection, theNewIndex);
431 //=======================================================================
434 //=======================================================================
435 void CurveCreator_CurveEditor::join(const int theISectionTo,
436 const int theISectionFrom)
438 if (myPCurve != NULL) {
439 // Set the difference.
441 if (addEmptyDiff()) {
442 myListDiffs.back().init(myPCurve, CurveCreator_Operation::Join,
443 theISectionTo, theISectionFrom);
447 myPCurve->join(theISectionTo, theISectionFrom);
452 //=======================================================================
455 //=======================================================================
456 void CurveCreator_CurveEditor::join()
458 if (myPCurve != NULL) {
459 // Set the difference.
461 if (addEmptyDiff()) {
462 myListDiffs.back().init(myPCurve, CurveCreator_Operation::Join);
471 //=======================================================================
474 //=======================================================================
475 bool CurveCreator_CurveEditor::addEmptyDiff()
477 bool isEnabled = false;
479 if (myUndoDepth != 0) {
480 // Forget all Redos after the current one.
483 myListDiffs.erase(myCurrenPos, myListDiffs.end());
486 if (myUndoDepth == -1 || myNbUndos < myUndoDepth) {
487 // Increase the number of undos.
490 // If there are too many differences, remove the first one.
491 myListDiffs.pop_front();
494 // Add new difference.
495 myListDiffs.push_back(CurveCreator_Diff());
496 myCurrenPos = myListDiffs.end();
503 void CurveCreator_CurveEditor::startOperation()
508 void CurveCreator_CurveEditor::finishOperation()