Salome HOME
Issue #79 Move Events messages to boost pointers: XGUI, PartSet
[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 #include <PartSet_OperationSketchBase.h>
9
10 #include <XGUI_Displayer.h>
11 #include <XGUI_Workshop.h>
12 #include <XGUI_OperationMgr.h>
13
14 #include <ModuleBase_Operation.h>
15
16 #include <Events_Loop.h>
17 #include <ModelAPI_Events.h>
18
19 #include <SketchPlugin_Sketch.h>
20
21 #ifdef _DEBUG
22 #include <QDebug>
23 #endif
24
25 using namespace std;
26
27 PartSet_Listener::PartSet_Listener(PartSet_Module* theModule)
28     : myModule(theModule)
29 {
30   Events_Loop* aLoop = Events_Loop::loop();
31   //aLoop->registerListener(this, aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
32   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_CREATED));
33   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_DELETED));
34 }
35
36 PartSet_Listener::~PartSet_Listener()
37 {
38 }
39
40 //******************************************************
41 void PartSet_Listener::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
42 {
43   ModuleBase_Operation* anOperation = myModule->workshop()->operationMgr()->currentOperation();
44   PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
45   if (!aSketchOp)
46     return;
47
48   XGUI_Displayer* aDisplayer = myModule->workshop()->displayer();
49   QString aType = QString(theMessage->eventID().eventText());
50   if (aType == EVENT_OBJECT_CREATED) {
51     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
52         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
53     std::set<ObjectPtr> aFeatures = aUpdMsg->objects();
54     std::set<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
55     for (; anIt != aLast; anIt++) {
56       aDisplayer->deactivate(*anIt);
57       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
58       if (aFeature)
59         myModule->activateFeature(aFeature, false);
60     }
61
62   } else if (aType == EVENT_OBJECT_DELETED) {
63     boost::shared_ptr<ModelAPI_ObjectDeletedMessage> aDelMsg =
64         boost::dynamic_pointer_cast<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 }