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