Salome HOME
Merge branch 'master' of newgeom:newgeom
[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.setAutoImport(true);
50   aXMLReader.readAll();
51 }
52
53 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
54 {
55   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
56 }
57
58 std::string PartSet_Module::modulePlugin()
59 {
60   Config_ModuleReader aModuleReader = Config_ModuleReader();
61   aModuleReader.readAll();
62   std::map < std::string, std::string > aPluginMap = aModuleReader.plugins();
63   std::string aPluginName = aPluginMap["PartSetPlugin"];
64   return aPluginName;
65 }
66
67 /*
68  *
69  */
70 void PartSet_Module::onFeatureTriggered()
71 {
72   std::string aPluginName = modulePlugin();
73   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginName);
74   aWdgReader.readAll();
75   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
76   QString aCmdId = aCmd->id();
77   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aCmdId.toStdString());
78   std::string aDescription = aWdgReader.featureDescription(aCmdId.toStdString());
79   ModuleBase_PropPanelOperation* aPartSetOp;
80   if (aCmdId == "Sketch" ) {
81     aPartSetOp = new PartSet_OperationSketch(aCmdId, this);
82   } else {
83     aPartSetOp = new ModuleBase_PropPanelOperation(aCmdId, this);
84   }
85   aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
86   aPartSetOp->setDescription(QString::fromStdString(aDescription));
87
88   //TODO(sbh): Implement static method to extract event id [SEID]
89   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
90   Config_PointerMessage aMessage(aModuleEvent, this);
91   aMessage.setPointer(aPartSetOp);
92   Events_Loop::loop()->send(aMessage);
93 }
94
95 void PartSet_Module::onOperationStarted()
96 {
97   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
98
99   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
100   if (aPreviewOp) {
101     connect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
102             aPreviewOp, SLOT(onViewSelectionChanged()));
103     visualizePreview(true);
104   }
105 }
106
107 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
108 {
109   ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
110   if (!anOperation)
111     return;
112
113   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
114   if (aPreviewOp) {
115     disconnect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
116                aPreviewOp, SLOT(onViewSelectionChanged()));
117     visualizePreview(false);
118   }
119 }
120
121 void PartSet_Module::visualizePreview(bool isDisplay)
122 {
123   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
124   if (!anOperation)
125     return;
126
127   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
128   if (!aPreviewOp)
129     return;
130
131   if (isDisplay) {
132     myWorkshop->displayer()->LocalSelection(anOperation->feature(), aPreviewOp->preview(),
133                                             aPreviewOp->getSelectionMode());
134   }
135   else {
136     myWorkshop->displayer()->GlobalSelection(false);
137     myWorkshop->displayer()->Erase(anOperation->feature(), aPreviewOp->preview());
138   }
139 }