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 <AIS_ListOfInteractive.hxx>
19
20 #include <QObject>
21 #include <QString>
22
23 #ifdef _DEBUG
24 #include <QDebug>
25 #endif
26
27
28 /*!Create and return new instance of XGUI_Module*/
29 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
30 {
31   return new PartSet_Module(theWshop);
32 }
33
34 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
35 {
36   myWorkshop = theWshop;
37   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
38
39   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
40   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
41           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
42   if (!myWorkshop->isSalomeMode())
43     connect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
44             this, SLOT(onViewSelectionChanged()));
45 }
46
47 PartSet_Module::~PartSet_Module()
48 {
49 }
50
51 void PartSet_Module::createFeatures()
52 {
53   Config_ModuleReader aXMLReader = Config_ModuleReader();
54   aXMLReader.readAll();
55   myFeaturesInFiles = aXMLReader.featuresInFiles();
56 }
57
58 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
59 {
60   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
61 }
62
63 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
64 {
65   return myFeaturesInFiles[theFeatureId];
66 }
67
68 /*
69  *
70  */
71 void PartSet_Module::onFeatureTriggered()
72 {
73   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
74   launchOperation(aCmd->id());
75 }
76   
77 void PartSet_Module::launchOperation(const QString& theCmdId)
78 {
79   std::string aStdCmdId = theCmdId.toStdString();
80   std::string aPluginFileName = featureFile(aStdCmdId);
81   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
82   aWdgReader.readAll();
83   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
84   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
85   ModuleBase_PropPanelOperation* aPartSetOp;
86   if (theCmdId == "Sketch" ) {
87     aPartSetOp = new PartSet_OperationSketch(theCmdId, this);
88   } else {
89     aPartSetOp = new ModuleBase_PropPanelOperation(theCmdId, this);
90   }
91   aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
92   aPartSetOp->setDescription(QString::fromStdString(aDescription));
93
94   //TODO(sbh): Implement static method to extract event id [SEID]
95   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
96   Config_PointerMessage aMessage(aModuleEvent, this);
97   aMessage.setPointer(aPartSetOp);
98   Events_Loop::loop()->send(aMessage);
99 }
100
101 void PartSet_Module::onOperationStarted()
102 {
103   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
104
105   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
106   if (aPreviewOp) {
107     visualizePreview(true);
108     connect(aPreviewOp, SIGNAL(viewerProjectionChange(double, double, double)),
109             this, SLOT(onViewerProjectionChange(double, double, double)));
110   }
111 }
112
113 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
114 {
115   ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
116   if (!anOperation)
117     return;
118   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
119   if (aPreviewOp)
120     visualizePreview(false);
121 }
122
123 void PartSet_Module::onViewSelectionChanged()
124 {
125   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
126   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
127   if (aPreviewOp) {
128     XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
129     if (aViewer) {
130       NCollection_List<TopoDS_Shape> aList;
131       aViewer->getSelectedShapes(aList);
132       aPreviewOp->setSelectedShapes(aList);
133     }
134   }
135 }
136
137 void PartSet_Module::onViewerProjectionChange(double theX, double theY, double theZ)
138 {
139   XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
140   if (aViewer) {
141     aViewer->setViewProjection(theX, theY, theZ);
142   }
143 }
144
145 void PartSet_Module::visualizePreview(bool isDisplay)
146 {
147   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
148   if (!anOperation)
149     return;
150
151   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
152   if (!aPreviewOp)
153     return;
154
155   if (isDisplay) {
156     myWorkshop->displayer()->LocalSelection(anOperation->feature(), aPreviewOp->preview(),
157                                             aPreviewOp->getSelectionMode());
158   }
159   else {
160     myWorkshop->displayer()->GlobalSelection(false);
161     myWorkshop->displayer()->Erase(anOperation->feature());
162   }
163 }