]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModelAPI/ModelAPI_Feature.cpp
Salome HOME
Make features history working. Optimization of features and results management and...
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModelAPI_Feature.cpp
4 // Created:     17 Jul 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include "ModelAPI_Feature.h"
8 #include <ModelAPI_Events.h>
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Session.h>
13 #include <Events_Loop.h>
14
15 const std::list<std::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
16 {
17   return myResults;
18 }
19
20 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult()
21 {
22   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
23 }
24
25 std::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
26 {
27   return myResults.empty() ? std::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
28 }
29
30 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult)
31 {
32   static Events_Loop* aLoop = Events_Loop::loop();
33   static Events_ID EVENT_UPD = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
34   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
35
36   if (firstResult() == theResult) {
37     // nothing to change
38   } else if (!myResults.empty()) {  // all except first become disabled
39     std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
40     *aResIter = theResult;
41     aECreator->sendUpdated(theResult, EVENT_UPD);
42     for(aResIter++; aResIter != myResults.end(); aResIter++) {
43       (*aResIter)->setDisabled((*aResIter), true);
44     }
45   } else {
46     myResults.push_back(theResult);
47   }
48   // in any case result decomes enabled
49   theResult->setDisabled(theResult, false);
50 }
51
52 void ModelAPI_Feature::setResult(const std::shared_ptr<ModelAPI_Result>& theResult,
53                                  const int theIndex)
54 {
55   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
56   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
57     aResIter++;
58   }
59   if (aResIter == myResults.end()) {  // append
60     myResults.push_back(theResult);
61   } else {  // update
62     *aResIter = theResult;
63   }
64   theResult->setDisabled(theResult, false);
65 }
66
67 void ModelAPI_Feature::removeResult(const std::shared_ptr<ModelAPI_Result>& theResult)
68 {
69   theResult->setDisabled(theResult, true);
70 }
71
72 void ModelAPI_Feature::removeResults(const int theSinceIndex)
73 {
74   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
75   for(int anIndex = 0; anIndex < theSinceIndex && aResIter != myResults.end(); anIndex++)
76     aResIter++;
77   std::list<std::shared_ptr<ModelAPI_Result> >::iterator aNextIter = aResIter;
78   for(; aNextIter != myResults.end(); aNextIter++) {
79     (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
80   }
81 }
82
83 void ModelAPI_Feature::eraseResults()
84 {
85   removeResults(0);
86 }
87
88 const std::string& ModelAPI_Feature::documentToAdd()
89 {
90   // empty to use the current document
91   static const std::string anEmpty;
92   return anEmpty;
93 }
94
95 void ModelAPI_Feature::erase()
96 {
97   static Events_Loop* aLoop = Events_Loop::loop();
98   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
99   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
100
101   while (!myResults.empty()) {  // remove one by one with messages
102     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
103     myResults.erase(myResults.begin());
104     aECreator->sendDeleted(aRes->document(), aRes->groupName());
105     aECreator->sendUpdated(aRes, EVENT_DISP);
106   }
107   ModelAPI_Object::erase();
108 }
109
110 ModelAPI_Feature::~ModelAPI_Feature()
111 {
112   erase();
113 }
114
115 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
116 {
117   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
118   if (!aFeature) {
119     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
120     if (aResult) {
121       DocumentPtr aDoc = aResult->document();
122       return aDoc->feature(aResult);
123     }
124   }
125   return aFeature;
126 }
127
128
129 bool ModelAPI_Feature::isMacro() const
130 {
131   return false;
132 }
133
134 bool ModelAPI_Feature::setDisabled(const bool theFlag)
135 {
136   if (myIsDisabled != theFlag) {
137     myIsDisabled = theFlag;
138     if (myIsDisabled)
139       eraseResults();
140     return true;
141   }
142   return false;
143 }
144
145 bool ModelAPI_Feature::isDisabled() const
146 {
147   return myIsDisabled;
148 }