Salome HOME
Correction of icon
[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 #include <TopAbs_ShapeEnum.hxx>
15
16 using namespace std;
17
18 void Model_AttributeSelectionList::append(
19     const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
20 {
21   int aNewTag = mySize->Get() + 1;
22   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
23
24   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
25     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
26   if (owner()) {
27     aNewAttr->setObject(owner());
28   }
29   mySize->Set(aNewTag);
30   aNewAttr->setValue(theContext, theSubShape);
31   owner()->data()->sendAttributeUpdated(this);
32 }
33
34 void Model_AttributeSelectionList::append(std::string theNamingName)
35 {
36   int aNewTag = mySize->Get() + 1;
37   TDF_Label aNewLab = mySize->Label().FindChild(aNewTag);
38
39   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
40     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aNewLab));
41   if (owner()) {
42     aNewAttr->setObject(owner());
43   }
44   mySize->Set(aNewTag);
45   aNewAttr->selectSubShape(selectionType(), theNamingName);
46   owner()->data()->sendAttributeUpdated(this);
47 }
48
49 int Model_AttributeSelectionList::size()
50 {
51   return mySize->Get();
52 }
53
54 const std::string Model_AttributeSelectionList::selectionType() const
55 {
56   return TCollection_AsciiString(mySelectionType->Get()).ToCString();
57 }
58
59 void Model_AttributeSelectionList::setSelectionType(const std::string& theType)
60 {
61   mySelectionType->Set(theType.c_str());
62 }
63
64 std::shared_ptr<ModelAPI_AttributeSelection> 
65   Model_AttributeSelectionList::value(const int theIndex)
66 {
67   TDF_Label aLabel = mySize->Label().FindChild(theIndex + 1);
68   // create a new attribute each time, by demand
69   // supporting of old attributes is too slow (synch each time) and buggy on redo
70   // (if attribute is deleted and created, the abort updates attriute and makes the Attr invalid)
71   std::shared_ptr<Model_AttributeSelection> aNewAttr = 
72     std::shared_ptr<Model_AttributeSelection>(new Model_AttributeSelection(aLabel));
73   if (owner()) {
74     aNewAttr->setObject(owner());
75   }
76   return aNewAttr;
77 }
78
79 void Model_AttributeSelectionList::clear()
80 {
81   if (mySize->Get() != 0) {
82     mySize->Set(0);
83     TDF_ChildIterator aSubIter(mySize->Label());
84     for(; aSubIter.More(); aSubIter.Next()) {
85       aSubIter.Value().ForgetAllAttributes(Standard_True);
86     }
87     owner()->data()->sendAttributeUpdated(this);
88   }
89 }
90
91 Model_AttributeSelectionList::Model_AttributeSelectionList(TDF_Label& theLabel)
92 {
93   myIsInitialized = theLabel.FindAttribute(TDataStd_Integer::GetID(), mySize) == Standard_True;
94   if (!myIsInitialized) {
95     mySize = TDataStd_Integer::Set(theLabel, 0);
96     mySelectionType = TDataStd_Comment::Set(theLabel, "");
97   } else { // recollect mySubs
98     theLabel.FindAttribute(TDataStd_Comment::GetID(), mySelectionType);
99   }
100 }