Salome HOME
Some slight improvements and bugs fixing.
[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 #include <XGUI_Workshop.h>
11
12 #include <Events_Loop.h>
13 #include <Model_Events.h>
14
15 #include <SketchPlugin_Sketch.h>
16
17 #ifdef _DEBUG
18 #include <QDebug>
19 #endif
20
21 using namespace std;
22
23 PartSet_Listener::PartSet_Listener(PartSet_Module* theModule)
24 : myModule(theModule)
25 {
26   Events_Loop* aLoop = Events_Loop::loop();
27   aLoop->registerListener(this, aLoop->eventByName(EVENT_FEATURE_UPDATED));
28   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
29   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
30 }
31
32 PartSet_Listener::~PartSet_Listener()
33 {
34 }
35
36 //******************************************************
37 void PartSet_Listener::processEvent(const Events_Message* theMessage)
38 {
39   QString aType = QString(theMessage->eventID().eventText());
40   if (aType == EVENT_FEATURE_UPDATED ||
41       aType == EVENT_FEATURE_CREATED)
42   {
43     const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>
44                                                                                     (theMessage);
45     std::set<FeaturePtr > aFeatures = aUpdMsg->features();
46     std::set<FeaturePtr >::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
47     for (; anIt != aLast; anIt++) {
48       FeaturePtr aFeature = *anIt;
49       if (myModule->workshop()->displayer()->isVisible(aFeature) ||
50           aType == EVENT_FEATURE_CREATED) {
51         myModule->visualizePreview(aFeature, true, false);
52         //if (aType == EVENT_FEATURE_CREATED)
53           myModule->activateFeature(aFeature, true);
54       }
55     }
56     myModule->workshop()->displayer()->updateViewer();
57   }
58   if (aType == EVENT_FEATURE_DELETED)
59   {
60     const Model_FeatureDeletedMessage* aDelMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
61     boost::shared_ptr<ModelAPI_Document> aDoc = aDelMsg->document();
62
63     std::set<std::string> aGroups = aDelMsg->groups();
64     std::set<std::string>::const_iterator anIt = aGroups.begin(), aLast = aGroups.end();
65     for (; anIt != aLast; anIt++) {
66       std::string aGroup = *anIt;
67       if (aGroup.compare(SKETCH_KIND) == 0) { // Update only Sketch group
68         myModule->workshop()->displayer()->eraseDeletedFeatures();
69         myModule->updateCurrentPreview(aGroup);
70       }
71     }
72   }
73 }