]> SALOME platform Git repositories - modules/geom.git/blob - src/CurveCreator/CurveCreator_CurveEditor.cxx
Salome HOME
Images neccessary for CurveEditor were added
[modules/geom.git] / src / CurveCreator / CurveCreator_CurveEditor.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        CurveCreator_CurveEditor.cxx
24 // Created:     Mon Jun  24 14:33:50 2013
25 // Author:      Sergey KHROMOV
26 //
27
28 #include <CurveCreator_CurveEditor.hxx>
29
30
31 //=======================================================================
32 // function: Constructor
33 // purpose:
34 //=======================================================================
35 CurveCreator_CurveEditor::CurveCreator_CurveEditor
36                                 (CurveCreator_Curve* thePCurve)
37  : myNbUndos   (0),
38    myNbRedos   (0),
39    myPCurve    (thePCurve),
40    myUndoDepth (-1)
41 {
42   if (myPCurve != NULL) {
43     if (myPCurve->isLocked()) {
44       // This curve is locked by another editor. Invalid case.
45       myPCurve = NULL;
46     } else {
47       // Lock the curve.
48       myPCurve->myIsLocked = true;
49       myCurrenPos = myListDiffs.end();
50     }
51   }
52 }
53
54 //=======================================================================
55 // function: Destructor
56 // purpose:
57 //=======================================================================
58 CurveCreator_CurveEditor::~CurveCreator_CurveEditor()
59 {
60   if (myPCurve != NULL) {
61     // Unlock the curve.
62     myPCurve->myIsLocked = false;
63   }
64 }
65
66 //=======================================================================
67 // function: getCurve
68 // purpose:
69 //=======================================================================
70 CurveCreator_Curve *CurveCreator_CurveEditor::getCurve() const
71 {
72   return myPCurve;
73 }
74
75 //=======================================================================
76 // function: isAttached
77 // purpose:
78 //=======================================================================
79 bool CurveCreator_CurveEditor::isAttached() const
80 {
81   return (myPCurve != NULL);
82 }
83
84 //=======================================================================
85 // function: undo
86 // purpose:
87 //=======================================================================
88 void CurveCreator_CurveEditor::undo()
89 {
90   if (myNbUndos > 0) {
91     myNbUndos--;
92     myNbRedos++;
93     myCurrenPos--;
94     myCurrenPos->applyUndo(myPCurve);
95   }
96 }
97
98 //=======================================================================
99 // function: redo
100 // purpose:
101 //=======================================================================
102 void CurveCreator_CurveEditor::redo()
103 {
104   if (myNbRedos > 0) {
105     myCurrenPos->applyRedo(myPCurve);
106     myCurrenPos++;
107     myNbRedos--;
108     myNbUndos++;
109   }
110 }
111
112 //=======================================================================
113 // function: getNbUndo
114 // purpose:
115 //=======================================================================
116 int CurveCreator_CurveEditor::getNbUndo() const
117 {
118   return myNbUndos;
119 }
120
121 //=======================================================================
122 // function: getNbRedo
123 // purpose:
124 //=======================================================================
125 int CurveCreator_CurveEditor::getNbRedo() const
126 {
127   return myNbRedos;
128 }
129
130 //=======================================================================
131 // function: setUndoDepth
132 // purpose:
133 //=======================================================================
134 void CurveCreator_CurveEditor::setUndoDepth(const int theDepth)
135 {
136   if (theDepth == 0) {
137     // Reset all undo/redo data.
138     myNbUndos = 0;
139     myNbRedos = 0;
140     myListDiffs.clear();
141     myCurrenPos = myListDiffs.end();
142     myUndoDepth = 0;
143   } else if (theDepth == -1) {
144     // There is nothing to do as the depth become unlimited.
145     myUndoDepth = -1;
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();
152
153       while (aShift--) {
154         aFromPos--;
155       }
156
157       myListDiffs.erase(aFromPos, myListDiffs.end());
158       myNbRedos = theDepth;
159     }
160
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();
166
167       while (aShift--) {
168         aToPos++;
169       }
170
171       myListDiffs.erase(myListDiffs.begin(), aToPos);
172       myNbUndos = theDepth - myNbRedos;
173     }
174
175     myUndoDepth = theDepth;
176   }
177 }
178
179 //=======================================================================
180 // function: getUndoDepth
181 // purpose:
182 //=======================================================================
183 int CurveCreator_CurveEditor::getUndoDepth() const
184 {
185   return myUndoDepth;
186 }
187
188 //=======================================================================
189 // function: setType
190 // purpose:
191 //=======================================================================
192 void CurveCreator_CurveEditor::setType(const CurveCreator::Type theType,
193                                        const int theISection)
194 {
195   if (myPCurve != NULL) {
196     // Set the difference.
197     if (addEmptyDiff()) {
198       myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetType,
199                               theType, theISection);
200     }
201
202     // Update the curve.
203     myPCurve->setType(theType, theISection);
204   }
205 }
206
207 //=======================================================================
208 // function: addPoints
209 // purpose:
210 //=======================================================================
211 void CurveCreator_CurveEditor::addPoints
212                       (const CurveCreator::Coordinates &thePoints,
213                        const int theISection)
214 {
215   if (myPCurve != NULL) {
216     // Set the difference.
217     if (addEmptyDiff()) {
218       myListDiffs.back().init(myPCurve, CurveCreator_Operation::AddPoints,
219                               thePoints, theISection);
220     }
221
222     // Update the curve.
223     myPCurve->addPoints(thePoints, theISection);
224   }
225 }
226
227 //=======================================================================
228 // function: addSection
229 // purpose:
230 //=======================================================================
231 void CurveCreator_CurveEditor::addSection
232         (const CurveCreator::Type theType,
233          const bool theIsClosed,
234          const CurveCreator::Coordinates &thePoints)
235 {
236   if (myPCurve != NULL) {
237     // Set the difference.
238     if (addEmptyDiff()) {
239       myListDiffs.back().init(myPCurve, CurveCreator_Operation::AddSection,
240                               thePoints, theType, theIsClosed);
241     }
242
243     // Update the curve.
244     myPCurve->addSection(theType, theIsClosed, thePoints);
245   }
246 }
247
248 //=======================================================================
249 // function: removeSection
250 // purpose:
251 //=======================================================================
252 void CurveCreator_CurveEditor::removeSection(const int theISection)
253 {
254   if (myPCurve != NULL) {
255     // Set the difference.
256     if (addEmptyDiff()) {
257       myListDiffs.back().init(myPCurve, CurveCreator_Operation::RemoveSection,
258                               theISection);
259     }
260
261     // Update the curve.
262     myPCurve->removeSection(theISection);
263   }
264 }
265
266 //=======================================================================
267 // function: insertPoints
268 // purpose:
269 //=======================================================================
270 void CurveCreator_CurveEditor::insertPoints
271         (const CurveCreator::Coordinates &thePoints,
272          const int theISection,
273          const int theIPnt)
274 {
275   if (myPCurve != NULL) {
276     // Set the difference.
277     if (addEmptyDiff()) {
278       myListDiffs.back().init(myPCurve, CurveCreator_Operation::InsertPoints,
279                               thePoints, theISection, theIPnt);
280     }
281
282     // Update the curve.
283     myPCurve->insertPoints(thePoints, theISection, theIPnt);
284   }
285 }
286
287 //=======================================================================
288 // function: removePoints
289 // purpose:
290 //=======================================================================
291 void CurveCreator_CurveEditor::removePoints
292               (const int theISection,
293                const int theIPnt,
294                const int theNbPoints)
295 {
296   if (myPCurve != NULL) {
297     // Set the difference.
298     if (addEmptyDiff()) {
299       myListDiffs.back().init(myPCurve, CurveCreator_Operation::RemovePoints,
300                               theISection, theIPnt, theNbPoints);
301     }
302
303     // Update the curve.
304     myPCurve->removePoints(theISection, theIPnt, theNbPoints);
305   }
306 }
307
308 //=======================================================================
309 // function: clear
310 // purpose:
311 //=======================================================================
312 void CurveCreator_CurveEditor::clear()
313 {
314   if (myPCurve != NULL) {
315     // Set the difference.
316     if (addEmptyDiff()) {
317       myListDiffs.back().init(myPCurve, CurveCreator_Operation::Clear);
318     }
319
320     // Update the curve.
321     myPCurve->clear();
322   }
323 }
324
325 //=======================================================================
326 // function: setCoordinates
327 // purpose:
328 //=======================================================================
329 void CurveCreator_CurveEditor::setCoordinates
330                      (const CurveCreator::Coordinates &theCoords,
331                       const int theISection,
332                       const int theIPnt)
333 {
334   if (myPCurve != NULL) {
335     // Set the difference.
336     if (addEmptyDiff()) {
337       myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetCoordinates,
338                               theCoords, theISection, theIPnt);
339     }
340
341     // Update the curve.
342     myPCurve->setCoordinates(theCoords, theISection, theIPnt);
343   }
344 }
345
346 //=======================================================================
347 // function: setClosed
348 // purpose:
349 //=======================================================================
350 void CurveCreator_CurveEditor::setClosed(const bool theIsClosed,
351                                          const int theISection)
352 {
353   if (myPCurve != NULL) {
354     // Set the difference.
355     if (addEmptyDiff()) {
356       myListDiffs.back().init(myPCurve, CurveCreator_Operation::SetClosed,
357                               theIsClosed, theISection);
358     }
359
360     // Update the curve.
361     myPCurve->setClosed(theIsClosed, theISection);
362   }
363 }
364
365 //=======================================================================
366 // function: moveSection
367 // purpose:
368 //=======================================================================
369 void CurveCreator_CurveEditor::moveSection(const int theISection,
370                                            const int theNewIndex)
371 {
372   if (myPCurve != NULL) {
373     // Set the difference.
374     if (addEmptyDiff()) {
375       myListDiffs.back().init(myPCurve, CurveCreator_Operation::MoveSection,
376                               theISection, theNewIndex);
377     }
378
379     // Update the curve.
380     myPCurve->moveSection(theISection, theNewIndex);
381   }
382 }
383
384 //=======================================================================
385 // function: join
386 // purpose:
387 //=======================================================================
388 void CurveCreator_CurveEditor::join(const int theISectionTo,
389                                     const int theISectionFrom)
390 {
391   if (myPCurve != NULL) {
392     // Set the difference.
393     if (addEmptyDiff()) {
394       myListDiffs.back().init(myPCurve, CurveCreator_Operation::Join,
395                               theISectionTo, theISectionFrom);
396     }
397
398     // Update the curve.
399     myPCurve->join(theISectionTo, theISectionFrom);
400   }
401 }
402
403 //=======================================================================
404 // function: join
405 // purpose:
406 //=======================================================================
407 void CurveCreator_CurveEditor::join()
408 {
409   if (myPCurve != NULL) {
410     // Set the difference.
411     if (addEmptyDiff()) {
412       myListDiffs.back().init(myPCurve, CurveCreator_Operation::Join);
413     }
414
415     // Update the curve.
416     myPCurve->join();
417   }
418 }
419
420 //=======================================================================
421 // function: addDiff
422 // purpose:
423 //=======================================================================
424 bool CurveCreator_CurveEditor::addEmptyDiff()
425 {
426   bool isEnabled = false;
427
428   if (myUndoDepth != 0) {
429     // Forget all Redos after the current one.
430     if (myNbRedos > 0) {
431       myNbRedos = 0;
432       myListDiffs.erase(myCurrenPos, myListDiffs.end());
433     }
434
435     if (myUndoDepth == -1 || myNbUndos < myUndoDepth) {
436       // Increase the number of undos.
437       myNbUndos++;
438     } else {
439       // If there are too many differences, remove the first one.
440       myListDiffs.pop_front();
441     }
442
443     // Add new difference.
444     myListDiffs.push_back(CurveCreator_Diff());
445     myCurrenPos = myListDiffs.end();
446     isEnabled = true;
447   }
448
449   return isEnabled;
450 }