Salome HOME
a95510118475b39a4e03d102df82725f09fa8de9
[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 boost::shared_ptr<ModelAPI_AttributeSelection> 
38   Model_AttributeSelectionList::value(const int theIndex)
39 {
40   return mySubs[theIndex];
41 }
42
43 void Model_AttributeSelectionList::clear()
44 {
45   if (!mySubs.empty()) {
46     mySize->Set(0);
47     mySubs.clear();
48     TDF_ChildIterator aSubIter(mySize->Label());
49     for(; aSubIter.More(); aSubIter.Next()) {
50       aSubIter.Value().ForgetAllAttributes(Standard_True);
51     }
52     owner()->data()->sendAttributeUpdated(this);
53   }
54 }
55
56 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
57 {
58   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
59   if (!myIsInitialized) {
60     mySize = TDataStd_Integer::Set(theLabel, 0);
61   } else { // recollect mySubs
62     int aNum = mySize->Get();
63     TDF_ChildIterator aSubIter(theLabel);
64     for(; aSubIter.More(), aNum != 0; aSubIter.Next(), aNum--) {
65       TDF_Label aChildLab = aSubIter.Value();
66       boost::shared_ptr<Model_AttributeSelection> aNewAttr = 
67         boost::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aChildLab));
68       if (owner())
69         aNewAttr->setObject(owner());
70       mySubs.push_back(aNewAttr);
71     }
72   }
73 }
74
75 void Model_AttributeSelectionList::setObject(const boost::shared_ptr<ModelAPI_Object>& theObject)
76 {
77   ModelAPI_AttributeSelectionList::setObject(theObject);
78   std::vector<boost::shared_ptr<Model_AttributeSelection> >::iterator aSubIter = mySubs.begin();
79   for(; aSubIter != mySubs.end(); aSubIter++) {
80     (*aSubIter)->setObject(theObject);
81   }
82 }