Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / PartSetPlugin / PartSetPlugin_Plugin.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "PartSetPlugin_Plugin.h"
22 #include "PartSetPlugin_Part.h"
23 #include "PartSetPlugin_Duplicate.h"
24 #include "PartSetPlugin_Remove.h"
25
26 #include <Events_Loop.h>
27
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Document.h>
30
31 #ifdef _DEBUG
32 #include <iostream>
33 #endif
34
35 // the only created instance of this plugin
36 static PartSetPlugin_Plugin* MY_PARTSET_INSTANCE = new PartSetPlugin_Plugin();
37
38 PartSetPlugin_Plugin::PartSetPlugin_Plugin()
39 {
40   // register this plugin
41   ModelAPI_Session::get()->registerPlugin(this);
42 }
43
44 FeaturePtr PartSetPlugin_Plugin::createFeature(std::string theFeatureID)
45 {
46   if (theFeatureID == PartSetPlugin_Part::ID()) {
47     return FeaturePtr(new PartSetPlugin_Part);
48   }
49   if (theFeatureID == PartSetPlugin_Duplicate::ID()) {
50     return FeaturePtr(new PartSetPlugin_Duplicate);
51   }
52   if (theFeatureID == PartSetPlugin_Remove::ID()) {
53     return FeaturePtr(new PartSetPlugin_Remove);
54   }
55   // feature of such kind is not found
56   return FeaturePtr();
57 }
58
59 void PartSetPlugin_Plugin::processEvent(const std::shared_ptr<Events_Message>& theMessage)
60 {
61   const Events_ID kRequestEvent =
62       Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_REQUEST);
63   if (theMessage->eventID() == kRequestEvent) {
64     Events_Loop::loop()->send(getFeaturesState(), false);
65   } else if (theMessage.get()) {
66     #ifdef _DEBUG
67     std::cout << "PartSetPlugin_Plugin::processEvent: unhandled message caught: " << std::endl
68               << theMessage->eventID().eventText() << std::endl;
69     #endif
70   }
71 }
72
73 std::shared_ptr<ModelAPI_FeatureStateMessage> PartSetPlugin_Plugin::getFeaturesState()
74 {
75   const Events_ID kResponseEvent =
76         Events_Loop::loop()->eventByName(EVENT_FEATURE_STATE_RESPONSE);
77   std::shared_ptr<ModelAPI_FeatureStateMessage> aMsg(
78       new ModelAPI_FeatureStateMessage(kResponseEvent, this));
79   std::string aStdDocKind = ModelAPI_Session::get()->activeDocument()->kind();
80   bool aDocIsPart = (aStdDocKind == PartSetPlugin_Part::ID());
81   aMsg->setState(PartSetPlugin_Part::ID(), true);
82   aMsg->setState(PartSetPlugin_Duplicate::ID(), aDocIsPart);
83   aMsg->setState(PartSetPlugin_Remove::ID(), aDocIsPart);
84   return aMsg;
85 }