Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_AttributeSelectionList.cpp
1 // File:        Model_AttributeSelectionList.h
2 // Created:     22 Oct 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "Model_AttributeSelectionList.h"
6 #include "Model_AttributeSelection.h"
7 #include "Model_Application.h"
8 #include "Model_Events.h"
9 #include "Model_Data.h"
10
11 #include <TDF_ChildIterator.hxx>
12
13 using namespace std;
14
15 void Model_AttributeSelectionList::append(
16     const ResultPtr& theContext, const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
17 {
18   int aNewTag = mySize->Get() + 1;
19   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
20
21   boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
22     boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
23   if (owner()) {
24     aNewAttr->setObject(owner());
25   }
26   mySize->Set(aNewTag);
27   aNewAttr->setValue(theContext, theSubShape);
28   owner()->data()->sendAttributeUpdated(this);
29 }
30
31 int Model_AttributeSelectionList::size()
32 {
33   return mySize->Get();
34 }
35
36 int Model_AttributeSelectionList::selectionType()
37 {
38   return (int) mySelectionType->Get();
39 }
40
41 void Model_AttributeSelectionList::setSelectionType(int theType)
42 {
43   mySelectionType->Set((double) theType);
44 }
45
46 boost::shared_ptr<ModelAPI_AttributeSelection> 
47   Model_AttributeSelectionList::value(const int theIndex)
48 {
49   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
50   // create a new attribute each time, by demand
51   // supporting of old attributes is too slow (synch each time) and buggy on redo
52   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
53   boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
54     boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
55   if (owner()) {
56     aNewAttr->setObject(owner());
57   }
58   return aNewAttr;
59 }
60
61 void Model_AttributeSelectionList::clear()
62 {
63   if (mySize->Get() != 0) {
64     mySize->Set(0);
65     TDF_ChildIterator aSubIter(mySize->Label());
66     for(; aSubIter.More(); aSubIter.Next()) {
67       aSubIter.Value().ForgetAllAttributes(Standard_True);
68     }
69     owner()->data()->sendAttributeUpdated(this);
70   }
71 }
72
73 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
74 {
75   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
76   if (!myIsInitialized) {
77     mySize = TDataStd_Integer::Set(theLabel, 0);
78     mySelectionType = TDataStd_Real::Set(theLabel, 0);
79   } else { // recollect mySubs
80     theLabel.FindAttribute(TDataStd_Real::GetID(), mySelectionType);
81   }
82 }