Salome HOME
Merge branch 'master' into BR_PYTHON_PLUGIN
[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   mySubs.push_back(aNewAttr);
27   mySize->Set(aNewTag);
28   aNewAttr->setValue(theContext, theSubShape);
29   owner()->data()->sendAttributeUpdated(this);
30 }
31
32 int Model_AttributeSelectionList::size()
33 {
34   return mySize->Get();
35 }
36
37 int Model_AttributeSelectionList::selectionType()
38 {
39   return (int) mySelectionType->Get();
40 }
41
42 void Model_AttributeSelectionList::setSelectionType(int theType)
43 {
44   mySelectionType->Set((double) theType);
45 }
46
47 boost::shared_ptr<ModelAPI_AttributeSelection> 
48   Model_AttributeSelectionList::value(const int theIndex)
49 {
50   return mySubs[theIndex];
51 }
52
53 void Model_AttributeSelectionList::clear()
54 {
55   if (!mySubs.empty()) {
56     mySize->Set(0);
57     mySubs.clear();
58     TDF_ChildIterator aSubIter(mySize->Label());
59     for(; aSubIter.More(); aSubIter.Next()) {
60       aSubIter.Value().ForgetAllAttributes(Standard_True);
61     }
62     owner()->data()->sendAttributeUpdated(this);
63   }
64 }
65
66 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
67 {
68   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
69   if (!myIsInitialized) {
70     mySize = TDataStd_Integer::Set(theLabel, 0);
71     mySelectionType = TDataStd_Real::Set(theLabel, 0);
72   } else { // recollect mySubs
73     int aNum = mySize->Get();
74     TDF_ChildIterator aSubIter(theLabel);
75     for(; aSubIter.More(), aNum != 0; aSubIter.Next(), aNum--) {
76       TDF_Label aChildLab = aSubIter.Value();
77       boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
78         boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aChildLab));
79       if (owner())
80         aNewAttr->setObject(owner());
81       mySubs.push_back(aNewAttr);
82     }
83   }
84 }
85
86 void Model_AttributeSelectionList::setObject(const boost::shared_ptr<ModelAPI_Object>& theObject)
87 {
88   ModelAPI_AttributeSelectionList::setObject(theObject);
89   std::vector<boost::shared_ptr<Model_AttributeSelection> >::iterator aSubIter = mySubs.begin();
90   for(; aSubIter != mySubs.end(); aSubIter++) {
91     (*aSubIter)->setObject(theObject);
92   }
93 }