Salome HOME
New architecture debugging
[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 <ModelAPI_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_OBJECT_UPDATED));
28   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
29   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_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_OBJECT_UPDATED ||
41       aType == EVENT_OBJECT_CREATED)
42   {
43     const ModelAPI_ObjectUpdatedMessage* aUpdMsg = 
44       dynamic_cast<const ModelAPI_ObjectUpdatedMessage*>(theMessage);
45     std::set<ObjectPtr > aFeatures = aUpdMsg->objects();
46     std::set<ObjectPtr >::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
47     for (; anIt != aLast; anIt++) {
48       ObjectPtr aObject = *anIt;
49       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
50       if (aFeature) {
51         if (myModule->workshop()->displayer()->isVisible(aFeature->firstResult()) ||
52             aType == EVENT_OBJECT_CREATED) {
53           myModule->visualizePreview(aFeature, true, false);
54           //if (aType == EVENT_OBJECT_CREATED)
55           myModule->activateFeature(aFeature, true);
56         }
57       }
58     }
59     myModule->workshop()->displayer()->updateViewer();
60   }
61   if (aType == EVENT_OBJECT_DELETED)
62   {
63     const ModelAPI_ObjectDeletedMessage* aDelMsg = 
64       dynamic_cast<const ModelAPI_ObjectDeletedMessage*>(theMessage);
65     boost::shared_ptr<ModelAPI_Document> aDoc = aDelMsg->document();
66
67     std::set<std::string> aGroups = aDelMsg->groups();
68     std::set<std::string>::const_iterator anIt = aGroups.begin(), aLast = aGroups.end();
69     for (; anIt != aLast; anIt++) {
70       std::string aGroup = *anIt;
71       if (aGroup.compare(SketchPlugin_Sketch::ID()) == 0) { // Update only Sketch group
72         myModule->workshop()->displayer()->eraseDeletedResults();
73         myModule->updateCurrentPreview(aGroup);
74       }
75     }
76   }
77 }