Salome HOME
A fix for a bug: clear content in the shape selector by deselection it in the viewer.
[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   while( aNextIter != myResults.end()) {
79     // remove previously erased results: to enable later if needed only actual (of history change)
80     if (theSinceIndex == 0 && (*aNextIter)->isDisabled()) {
81       aNextIter = myResults.erase(aNextIter);
82     } else {
83       (*aNextIter)->setDisabled(*aNextIter, true); // just disable results
84       aNextIter++;
85     }
86   }
87 }
88
89 void ModelAPI_Feature::eraseResults()
90 {
91   removeResults(0);
92 }
93
94 const std::string& ModelAPI_Feature::documentToAdd()
95 {
96   // empty to use the current document
97   static const std::string anEmpty;
98   return anEmpty;
99 }
100
101 void ModelAPI_Feature::erase()
102 {
103   static Events_Loop* aLoop = Events_Loop::loop();
104   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
105   static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
106
107   while (!myResults.empty()) {  // remove one by one with messages
108     std::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
109     myResults.erase(myResults.begin());
110     aECreator->sendDeleted(aRes->document(), aRes->groupName());
111     aECreator->sendUpdated(aRes, EVENT_DISP);
112   }
113   ModelAPI_Object::erase();
114 }
115
116 ModelAPI_Feature::~ModelAPI_Feature()
117 {
118   erase();
119 }
120
121 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
122 {
123   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
124   if (!aFeature) {
125     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
126     if (aResult) {
127       DocumentPtr aDoc = aResult->document();
128       return aDoc->feature(aResult);
129     }
130   }
131   return aFeature;
132 }
133
134
135 bool ModelAPI_Feature::isMacro() const
136 {
137   return false;
138 }
139
140 bool ModelAPI_Feature::setDisabled(const bool theFlag)
141 {
142   if (myIsDisabled != theFlag) {
143     myIsDisabled = theFlag;
144     if (myIsDisabled) {
145       eraseResults();
146     } else {
147       // enable all disabled previously results
148       std::list<std::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
149       for(; aResIter != myResults.end(); aResIter++) {
150         (*aResIter)->setDisabled(*aResIter, false); // just enable results
151       }
152     }
153     return true;
154   }
155   return false;
156 }
157
158 bool ModelAPI_Feature::isDisabled() const
159 {
160   return myIsDisabled;
161 }
162
163 bool ModelAPI_Feature::isPreviewNeeded() const
164 {
165   return true;
166 }