Salome HOME
Sources formated according to the codeing standards
[modules/shaper.git] / src / ModelAPI / ModelAPI_Feature.cpp
1 // File:        ModelAPI_Feature.cpp
2 // Created:     17 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "ModelAPI_Feature.h"
6 #include <ModelAPI_Events.h>
7 #include <ModelAPI_Result.h>
8 #include <ModelAPI_Document.h>
9 #include <Events_Loop.h>
10
11 const std::list<boost::shared_ptr<ModelAPI_Result> >& ModelAPI_Feature::results()
12 {
13   return myResults;
14 }
15
16 boost::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult()
17 {
18   return myResults.empty() ? boost::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
19 }
20
21 void ModelAPI_Feature::setResult(const boost::shared_ptr<ModelAPI_Result>& theResult)
22 {
23   if (firstResult() == theResult) {  // just updated
24     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
25     ModelAPI_EventCreator::get()->sendUpdated(theResult, anEvent);
26     return;
27   }
28   // created
29   while (!myResults.empty()) {  // remove one by one with messages
30     boost::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
31     myResults.erase(myResults.begin());
32     ModelAPI_EventCreator::get()->sendDeleted(aRes->document(), aRes->groupName());
33   }
34   myResults.push_back(theResult);
35   static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
36   ModelAPI_EventCreator::get()->sendUpdated(theResult, anEvent);
37   // Create event for first Feature 
38   Events_Loop::loop()->flush(anEvent);
39 }
40
41 void ModelAPI_Feature::setResult(const boost::shared_ptr<ModelAPI_Result>& theResult,
42                                  const int theIndex)
43 {
44   std::list<boost::shared_ptr<ModelAPI_Result> >::iterator aResIter = myResults.begin();
45   for (int anIndex = 0; anIndex < theIndex; anIndex++) {
46     aResIter++;
47   }
48   if (aResIter == myResults.end()) {  // append
49     myResults.push_back(theResult);
50     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_CREATED);
51     ModelAPI_EventCreator::get()->sendUpdated(theResult, anEvent);
52     // Create event for first Feature, send it to make "created" earlier than "updated"
53     Events_Loop::loop()->flush(anEvent);
54   } else {  // update
55     *aResIter = theResult;
56     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
57     ModelAPI_EventCreator::get()->sendUpdated(theResult, anEvent);
58   }
59 }
60
61 boost::shared_ptr<ModelAPI_Document> ModelAPI_Feature::documentToAdd()
62 {
63   return ModelAPI_PluginManager::get()->currentDocument();
64 }
65
66 ModelAPI_Feature::~ModelAPI_Feature()
67 {
68   while (!myResults.empty()) {  // remove one by one with messages
69     boost::shared_ptr<ModelAPI_Result> aRes = *(myResults.begin());
70     myResults.erase(myResults.begin());
71     ModelAPI_EventCreator::get()->sendDeleted(aRes->document(), aRes->groupName());
72   }
73 }
74
75 FeaturePtr ModelAPI_Feature::feature(ObjectPtr theObject)
76 {
77   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
78   if (!aFeature) {
79     ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
80     if (aResult) {
81       DocumentPtr aDoc = aResult->document();
82       return aDoc->feature(aResult);
83     }
84   }
85   return aFeature;
86 }