Salome HOME
fbe2485324858d246d998b2db7a6a749967377cb
[modules/shaper.git] / src / PartSet / PartSet_Listener.cpp
1 // File:        PartSet_Listener.h
2 // Created:     28 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_Listener.h>
6
7 #include <PartSet_Module.h>
8
9 #include <XGUI_Displayer.h>
10
11 #include <Events_Loop.h>
12 #include <Model_Events.h>
13
14 #ifdef _DEBUG
15 #include <QDebug>
16 #endif
17
18 using namespace std;
19
20 PartSet_Listener::PartSet_Listener(PartSet_Module* theModule)
21 : myModule(theModule)
22 {
23   Events_Loop* aLoop = Events_Loop::loop();
24   aLoop->registerListener(this, aLoop->eventByName(EVENT_FEATURE_UPDATED));
25   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
26   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
27 }
28
29 PartSet_Listener::~PartSet_Listener()
30 {
31 }
32
33 //******************************************************
34 void PartSet_Listener::processEvent(const Events_Message* theMessage)
35 {
36   QString aType = QString(theMessage->eventID().eventText());
37   if (aType == EVENT_FEATURE_UPDATED ||
38       aType == EVENT_FEATURE_CREATED)
39   {
40     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>
41                                                                                     (theMessage);
42     std::set<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aUpdMsg->features();
43     std::set<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
44                                                                    aLast = aFeatures.end();
45     for (; anIt != aLast; anIt++) {
46       boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
47       if (myModule->workshop()->displayer()->IsVisible(aFeature) ||
48           aType == EVENT_FEATURE_CREATED) {
49         myModule->visualizePreview(aFeature, true, false);
50         //if (aType == EVENT_FEATURE_CREATED)
51           myModule->activateFeature(aFeature, true);
52       }
53     }
54     myModule->workshop()->displayer()->UpdateViewer();
55   }
56   if (aType == EVENT_FEATURE_DELETED)
57   {
58     const Model_FeatureDeletedMessage* aDelMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
59     boost::shared_ptr<ModelAPI_Document> aDoc = aDelMsg->document();
60
61     std::set<std::string> aGroups = aDelMsg->groups();
62     std::set<std::string>::const_iterator anIt = aGroups.begin(), aLast = aGroups.end();
63     for (; anIt != aLast; anIt++) {
64       std::string aGroup = *anIt;
65       if (aGroup.compare("Sketch") == 0) { // Update only Sketch group
66         myModule->workshop()->displayer()->EraseDeletedFeatures();
67         myModule->updateCurrentPreview(aGroup);
68       }
69     }
70   }
71 }