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   updateSubs();
19
20   int aNewTag = mySize->Get() + 1;
21   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
22
23   boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
24     boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
25   if (owner()) {
26     aNewAttr->setObject(owner());
27   }
28   mySubs.push_back(aNewAttr);
29   mySize->Set(aNewTag);
30   aNewAttr->setValue(theContext, theSubShape);
31   owner()->data()->sendAttributeUpdated(this);
32 }
33
34 int Model_AttributeSelectionList::size()
35 {
36   return mySize->Get();
37 }
38
39 int Model_AttributeSelectionList::selectionType()
40 {
41   return (int) mySelectionType->Get();
42 }
43
44 void Model_AttributeSelectionList::setSelectionType(int theType)
45 {
46   mySelectionType->Set((double) theType);
47 }
48
49 boost::shared_ptr<ModelAPI_AttributeSelection> 
50   Model_AttributeSelectionList::value(const int theIndex)
51 {
52   updateSubs();
53   return mySubs[theIndex];
54 }
55
56 void Model_AttributeSelectionList::clear()
57 {
58   if (mySize->Get() != 0) {
59     mySize->Set(0);
60     mySubs.clear();
61     TDF_ChildIterator aSubIter(mySize->Label());
62     for(; aSubIter.More(); aSubIter.Next()) {
63       aSubIter.Value().ForgetAllAttributes(Standard_True);
64     }
65     owner()->data()->sendAttributeUpdated(this);
66   }
67 }
68
69 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
70 {
71   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
72   if (!myIsInitialized) {
73     mySize = TDataStd_Integer::Set(theLabel, 0);
74     mySelectionType = TDataStd_Real::Set(theLabel, 0);
75   } else { // recollect mySubs
76     theLabel.FindAttribute(TDataStd_Real::GetID(), mySelectionType);
77     updateSubs();
78   }
79 }
80
81 void Model_AttributeSelectionList::setObject(const boost::shared_ptr<ModelAPI_Object>& theObject)
82 {
83   ModelAPI_AttributeSelectionList::setObject(theObject);
84   std::vector<boost::shared_ptr<Model_AttributeSelection> >::iterator aSubIter = mySubs.begin();
85   for(; aSubIter != mySubs.end(); aSubIter++) {
86     (*aSubIter)->setObject(theObject);
87   }
88 }
89
90 void Model_AttributeSelectionList::updateSubs()
91 {
92   unsigned int aNum = mySize->Get();
93   if (aNum > mySubs.size()) { // add subs what are not yet created
94     TDF_ChildIterator aSubIter(mySize->Label());
95     for(int aExisting = mySubs.size(); aExisting > 0; aSubIter.Next()) aExisting--;
96     for(; aSubIter.More(); aSubIter.Next()) {
97       TDF_Label aChildLab = aSubIter.Value();
98       boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
99         boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aChildLab));
100       if (owner())
101         aNewAttr->setObject(owner());
102       mySubs.push_back(aNewAttr);
103     }
104   } else if (aNum < mySubs.size()) { // remove excess subs from the end
105     if (aNum == 0) {
106       mySubs.clear();
107     } else {
108       std::vector<boost::shared_ptr<Model_AttributeSelection> >::iterator aSubIter = mySubs.begin();
109       for(int aExisting = aNum; aExisting != 0; aSubIter++) aExisting--;
110       mySubs.erase(aSubIter, mySubs.end());
111     }
112   }
113 }