1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File: CurveCreator_CurveEditor.cxx
24 // Created: Mon Jun 24 14:33:50 2013
25 // Author: Sergey KHROMOV
28 #include <CurveCreator_CurveEditor.hxx>
31 //=======================================================================
32 // function: Constructor
34 //=======================================================================
35 CurveCreator_CurveEditor::CurveCreator_CurveEditor
36 (CurveCreator_Curve* thePCurve)
42 if (myPCurve != NULL) {
43 if (myPCurve->isLocked()) {
44 // This curve is locked by another editor. Invalid case.
48 myPCurve->myIsLocked = true;
49 myCurrenPos = myListDiffs.end();
54 //=======================================================================
55 // function: Destructor
57 //=======================================================================
58 CurveCreator_CurveEditor::~CurveCreator_CurveEditor()
60 if (myPCurve != NULL) {
62 myPCurve->myIsLocked = false;
66 //=======================================================================
69 //=======================================================================
70 CurveCreator_Curve *CurveCreator_CurveEditor::getCurve() const
75 //=======================================================================
76 // function: isAttached
78 //=======================================================================
79 bool CurveCreator_CurveEditor::isAttached() const
81 return (myPCurve != NULL);
84 //=======================================================================
87 //=======================================================================
88 void CurveCreator_CurveEditor::undo()
94 myCurrenPos->applyUndo(myPCurve);
98 //=======================================================================
101 //=======================================================================
102 void CurveCreator_CurveEditor::redo()
105 myCurrenPos->applyRedo(myPCurve);
112 //=======================================================================
113 // function: getNbUndo
115 //=======================================================================
116 int CurveCreator_CurveEditor::getNbUndo() const
121 //=======================================================================
122 // function: getNbRedo
124 //=======================================================================
125 int CurveCreator_CurveEditor::getNbRedo() const
130 //=======================================================================
131 // function: setUndoDepth
133 //=======================================================================
134 void CurveCreator_CurveEditor::setUndoDepth(const int theDepth)
137 // Reset all undo/redo data.
141 myCurrenPos = myListDiffs.end();
143 } else if (theDepth == -1) {
144 // There is nothing to do as the depth become unlimited.
146 } else if (theDepth > 0) {
147 // The new "real" depth is set.
148 if (theDepth < myNbRedos) {
149 // The new depth is less then number of redos. Remove the latest redos.
150 int aShift = (myNbRedos - theDepth);
151 ListDiff::iterator aFromPos = myListDiffs.end();
157 myListDiffs.erase(aFromPos, myListDiffs.end());
158 myNbRedos = theDepth;
161 if (theDepth < myNbUndos + myNbRedos) {
162 // The new depth is less then the total number of differences.
163 // Remove the first undos.
164 int aShift = (myNbUndos + myNbRedos - theDepth);
165 ListDiff::iterator aToPos = myListDiffs.begin();
171 myListDiffs.erase(myListDiffs.begin(), aToPos);
172 myNbUndos = theDepth - myNbRedos;
175 myUndoDepth = theDepth;
179 //=======================================================================
180 // function: getUndoDepth
182 //=======================================================================
183 int CurveCreator_CurveEditor::getUndoDepth() const
188 //=======================================================================
191 //=======================================================================
192 void CurveCreator_CurveEditor::setType(const CurveCreator::Type theType,
193 const int theISection)
195 if (myPCurve != NULL) {
196 // Set the difference.
197 if (addEmptyDiff()) {
198 myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetType,
199 theType, theISection);
203 myPCurve->setType(theType, theISection);
207 //=======================================================================
208 // function: addPoints
210 //=======================================================================
211 void CurveCreator_CurveEditor::addPoints
212 (const CurveCreator::Coordinates &thePoints,
213 const int theISection)
215 if (myPCurve != NULL) {
216 // Set the difference.
217 if (addEmptyDiff()) {
218 myListDiffs.back().init(myPCurve, CurveCreator_Operation::AddPoints,
219 thePoints, theISection);
223 myPCurve->addPoints(thePoints, theISection);
227 //=======================================================================
228 // function: addSection
230 //=======================================================================
231 void CurveCreator_CurveEditor::addSection
232 (const CurveCreator::Type theType,
233 const bool theIsClosed,
234 const CurveCreator::Coordinates &thePoints)
236 if (myPCurve != NULL) {
237 // Set the difference.
238 if (addEmptyDiff()) {
239 myListDiffs.back().init(myPCurve, CurveCreator_Operation::AddSection,
240 thePoints, theType, theIsClosed);
244 myPCurve->addSection(theType, theIsClosed, thePoints);
248 //=======================================================================
249 // function: removeSection
251 //=======================================================================
252 void CurveCreator_CurveEditor::removeSection(const int theISection)
254 if (myPCurve != NULL) {
255 // Set the difference.
256 if (addEmptyDiff()) {
257 myListDiffs.back().init(myPCurve, CurveCreator_Operation::RemoveSection,
262 myPCurve->removeSection(theISection);
266 //=======================================================================
267 // function: insertPoints
269 //=======================================================================
270 void CurveCreator_CurveEditor::insertPoints
271 (const CurveCreator::Coordinates &thePoints,
272 const int theISection,
275 if (myPCurve != NULL) {
276 // Set the difference.
277 if (addEmptyDiff()) {
278 myListDiffs.back().init(myPCurve, CurveCreator_Operation::InsertPoints,
279 thePoints, theISection, theIPnt);
283 myPCurve->insertPoints(thePoints, theISection, theIPnt);
287 //=======================================================================
288 // function: removePoints
290 //=======================================================================
291 void CurveCreator_CurveEditor::removePoints
292 (const int theISection,
294 const int theNbPoints)
296 if (myPCurve != NULL) {
297 // Set the difference.
298 if (addEmptyDiff()) {
299 myListDiffs.back().init(myPCurve, CurveCreator_Operation::RemovePoints,
300 theISection, theIPnt, theNbPoints);
304 myPCurve->removePoints(theISection, theIPnt, theNbPoints);
308 //=======================================================================
311 //=======================================================================
312 void CurveCreator_CurveEditor::clear()
314 if (myPCurve != NULL) {
315 // Set the difference.
316 if (addEmptyDiff()) {
317 myListDiffs.back().init(myPCurve, CurveCreator_Operation::Clear);
325 //=======================================================================
326 // function: setCoordinates
328 //=======================================================================
329 void CurveCreator_CurveEditor::setCoordinates
330 (const CurveCreator::Coordinates &theCoords,
331 const int theISection,
334 if (myPCurve != NULL) {
335 // Set the difference.
336 if (addEmptyDiff()) {
337 myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetCoordinates,
338 theCoords, theISection, theIPnt);
342 myPCurve->setCoordinates(theCoords, theISection, theIPnt);
346 //=======================================================================
347 // function: setClosed
349 //=======================================================================
350 void CurveCreator_CurveEditor::setClosed(const bool theIsClosed,
351 const int theISection)
353 if (myPCurve != NULL) {
354 // Set the difference.
355 if (addEmptyDiff()) {
356 myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetClosed,
357 theIsClosed, theISection);
361 myPCurve->setClosed(theIsClosed, theISection);
365 //=======================================================================
366 // function: moveSection
368 //=======================================================================
369 void CurveCreator_CurveEditor::moveSection(const int theISection,
370 const int theNewIndex)
372 if (myPCurve != NULL) {
373 // Set the difference.
374 if (addEmptyDiff()) {
375 myListDiffs.back().init(myPCurve, CurveCreator_Operation::MoveSection,
376 theISection, theNewIndex);
380 myPCurve->moveSection(theISection, theNewIndex);
384 //=======================================================================
387 //=======================================================================
388 void CurveCreator_CurveEditor::join(const int theISectionTo,
389 const int theISectionFrom)
391 if (myPCurve != NULL) {
392 // Set the difference.
393 if (addEmptyDiff()) {
394 myListDiffs.back().init(myPCurve, CurveCreator_Operation::Join,
395 theISectionTo, theISectionFrom);
399 myPCurve->join(theISectionTo, theISectionFrom);
403 //=======================================================================
406 //=======================================================================
407 void CurveCreator_CurveEditor::join()
409 if (myPCurve != NULL) {
410 // Set the difference.
411 if (addEmptyDiff()) {
412 myListDiffs.back().init(myPCurve, CurveCreator_Operation::Join);
420 //=======================================================================
423 //=======================================================================
424 bool CurveCreator_CurveEditor::addEmptyDiff()
426 bool isEnabled = false;
428 if (myUndoDepth != 0) {
429 // Forget all Redos after the current one.
432 myListDiffs.erase(myCurrenPos, myListDiffs.end());
435 if (myUndoDepth == -1 || myNbUndos < myUndoDepth) {
436 // Increase the number of undos.
439 // If there are too many differences, remove the first one.
440 myListDiffs.pop_front();
443 // Add new difference.
444 myListDiffs.push_back(CurveCreator_Diff());
445 myCurrenPos = myListDiffs.end();