Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3
4 #include <ModuleBase_Operation.h>
5
6 #include <XGUI_MainWindow.h>
7 #include <XGUI_Displayer.h>
8 #include <XGUI_Viewer.h>
9 #include <XGUI_Workshop.h>
10 #include <XGUI_OperationMgr.h>
11
12 #include <Config_PointerMessage.h>
13 #include <Config_ModuleReader.h>
14 #include <Config_WidgetReader.h>
15 #include <Events_Loop.h>
16 #include <Events_Message.h>
17
18 #include <QObject>
19 #include <QString>
20
21 #ifdef _DEBUG
22 #include <QDebug>
23 #endif
24
25
26 /*!Create and return new instance of XGUI_Module*/
27 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
28 {
29   return new PartSet_Module(theWshop);
30 }
31
32 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
33 {
34   myWorkshop = theWshop;
35   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
36
37   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
38   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
39           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
40 }
41
42 PartSet_Module::~PartSet_Module()
43 {
44 }
45
46 void PartSet_Module::createFeatures()
47 {
48   Config_ModuleReader aXMLReader = Config_ModuleReader();
49   aXMLReader.readAll();
50   myFeaturesInFiles = aXMLReader.featuresInFiles();
51 }
52
53 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
54 {
55   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
56 }
57
58 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
59 {
60   return myFeaturesInFiles[theFeatureId];
61 }
62
63 /*
64  *
65  */
66 void PartSet_Module::onFeatureTriggered()
67 {
68   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
69   QString aCmdId = aCmd->id();
70   std::string aStdCmdId = aCmdId.toStdString();
71   std::string aPluginFileName = featureFile(aStdCmdId);
72   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
73   aWdgReader.readAll();
74   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
75   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
76   ModuleBase_PropPanelOperation* aPartSetOp;
77   if (aCmdId == "Sketch" ) {
78     aPartSetOp = new PartSet_OperationSketch(aCmdId, this);
79   } else {
80     aPartSetOp = new ModuleBase_PropPanelOperation(aCmdId, this);
81   }
82   aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
83   aPartSetOp->setDescription(QString::fromStdString(aDescription));
84
85   //TODO(sbh): Implement static method to extract event id [SEID]
86   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
87   Config_PointerMessage aMessage(aModuleEvent, this);
88   aMessage.setPointer(aPartSetOp);
89   Events_Loop::loop()->send(aMessage);
90 }
91
92 void PartSet_Module::onOperationStarted()
93 {
94   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
95
96   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
97   if (aPreviewOp) {
98     connect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
99             aPreviewOp, SLOT(onViewSelectionChanged()));
100     visualizePreview(true);
101   }
102 }
103
104 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
105 {
106   ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
107   if (!anOperation)
108     return;
109
110   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
111   if (aPreviewOp) {
112     disconnect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
113                aPreviewOp, SLOT(onViewSelectionChanged()));
114     visualizePreview(false);
115   }
116 }
117
118 void PartSet_Module::visualizePreview(bool isDisplay)
119 {
120   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
121   if (!anOperation)
122     return;
123
124   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
125   if (!aPreviewOp)
126     return;
127
128   if (isDisplay) {
129     myWorkshop->displayer()->LocalSelection(anOperation->feature(), aPreviewOp->preview(),
130                                             aPreviewOp->getSelectionMode());
131   }
132   else {
133     myWorkshop->displayer()->GlobalSelection(false);
134     myWorkshop->displayer()->Erase(anOperation->feature(), aPreviewOp->preview());
135   }
136 }