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 #include <PartSet_OperationSketchLine.h>
4 #include <PartSet_Listener.h>
5 #include <PartSet_Tools.h>
6
7 #include <ModuleBase_Operation.h>
8
9 #include <XGUI_MainWindow.h>
10 #include <XGUI_Displayer.h>
11 #include <XGUI_Viewer.h>
12 #include <XGUI_Workshop.h>
13 #include <XGUI_OperationMgr.h>
14 #include <XGUI_ViewWindow.h>
15 #include <XGUI_SelectionMgr.h>
16 #include <XGUI_ViewPort.h>
17 #include <XGUI_ActionsMgr.h>
18
19 #include <Config_PointerMessage.h>
20 #include <Config_ModuleReader.h>
21 #include <Config_WidgetReader.h>
22 #include <Events_Loop.h>
23 #include <Events_Message.h>
24 #include <Events_Error.h>
25
26 #include <GeomAPI_Shape.h>
27
28 #include <AIS_ListOfInteractive.hxx>
29
30 #include <QObject>
31 #include <QMouseEvent>
32 #include <QString>
33
34 #ifdef _DEBUG
35 #include <QDebug>
36 #endif
37
38
39 /*!Create and return new instance of XGUI_Module*/
40 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
41 {
42   return new PartSet_Module(theWshop);
43 }
44
45 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
46 {
47   myWorkshop = theWshop;
48   myListener = new PartSet_Listener(this);
49
50   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
51
52   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
53   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
54           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
55   if (!myWorkshop->isSalomeMode()) {
56     XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
57     connect(aViewer, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
58     connect(aViewer, SIGNAL(mouseRelease(XGUI_ViewWindow*, QMouseEvent*)),
59             this, SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
60     connect(aViewer, SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)),
61             this, SLOT(onMouseMoved(XGUI_ViewWindow*, QMouseEvent*)));
62   }
63 }
64
65 PartSet_Module::~PartSet_Module()
66 {
67 }
68
69 void PartSet_Module::createFeatures()
70 {
71   Config_ModuleReader aXMLReader = Config_ModuleReader();
72   aXMLReader.readAll();
73   myFeaturesInFiles = aXMLReader.featuresInFiles();
74 }
75
76 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
77 {
78   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
79 }
80
81 QStringList PartSet_Module::nestedFeatures(QString)
82 {
83   return QStringList();
84 }
85
86 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
87 {
88   return myFeaturesInFiles[theFeatureId];
89 }
90
91 /*
92  *
93  */
94 void PartSet_Module::onFeatureTriggered()
95 {
96   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
97   //Do nothing on uncheck
98   if(aCmd->isCheckable() && !aCmd->isChecked())
99     return;
100   launchOperation(aCmd->id());
101 }
102   
103 void PartSet_Module::launchOperation(const QString& theCmdId)
104 {
105   std::string aStdCmdId = theCmdId.toStdString();
106   std::string aPluginFileName = featureFile(aStdCmdId);
107   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
108   aWdgReader.readAll();
109   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
110   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
111   ModuleBase_PropPanelOperation* aPartSetOp;
112   if (theCmdId == "Sketch" ) {
113     aPartSetOp = new PartSet_OperationSketch(theCmdId, this);
114   }
115   else if(theCmdId == "SketchLine") {
116     ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
117     boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
118     if (anOperation)
119       aSketchFeature = anOperation->feature();
120     aPartSetOp = new PartSet_OperationSketchLine(theCmdId, this, aSketchFeature);
121   }
122   else {
123     aPartSetOp = new ModuleBase_PropPanelOperation(theCmdId, this);
124   }
125   aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
126   aPartSetOp->setDescription(QString::fromStdString(aDescription));
127
128   //TODO(sbh): Implement static method to extract event id [SEID]
129   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
130   Config_PointerMessage aMessage(aModuleEvent, this);
131   aMessage.setPointer(aPartSetOp);
132   Events_Loop::loop()->send(aMessage);
133 }
134
135 void PartSet_Module::onOperationStarted()
136 {
137   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
138
139   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
140   if (aPreviewOp) {
141     visualizePreview(true);
142
143     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
144     if (aSketchOp) {
145       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
146               this, SLOT(onPlaneSelected(double, double, double)));
147     }
148   }
149 }
150
151 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
152 {
153   ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
154   if (!anOperation)
155     return;
156   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
157   //if (aPreviewOp)
158   //  visualizePreview(false);
159 }
160
161 void PartSet_Module::onSelectionChanged()
162 {
163   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
164   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
165   if (aPreviewOp) {
166     XGUI_SelectionMgr* aSelector = myWorkshop->selector();
167     if (aSelector) {
168       NCollection_List<TopoDS_Shape> aList;
169       aSelector->selectedShapes(aList);
170       aPreviewOp->setSelectedShapes(aList);
171     }
172   }
173 }
174
175 void PartSet_Module::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
176 {
177   QPoint aPoint = theEvent->pos();
178   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
179   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
180   if (aPreviewOp) {
181     XGUI_SelectionMgr* aSelector = myWorkshop->selector();
182     if (aSelector) {
183       XGUI_ViewWindow* aWindow = myWorkshop->mainWindow()->viewer()->activeViewWindow();
184       if (aWindow) {
185         Handle(V3d_View) aView3d = aWindow->viewPort()->getView();
186         if ( !aView3d.IsNull() ) {
187           gp_Pnt aPnt = PartSet_Tools::ConvertClickToPoint(aPoint, aView3d);
188           aPreviewOp->mouseReleased(aPnt);
189         }
190       }
191     }
192   }
193 }
194
195 void PartSet_Module::onMouseMoved(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
196 {
197   QPoint aPoint = theEvent->pos();
198   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
199   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
200   if (aPreviewOp) {
201     XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
202     if (aViewer) {
203       XGUI_ViewWindow* aWindow = aViewer->activeViewWindow();
204       if (aWindow) {
205         Handle(V3d_View) aView3d = aWindow->viewPort()->getView();
206         if ( !aView3d.IsNull() ) {
207           gp_Pnt aPnt = PartSet_Tools::ConvertClickToPoint(aPoint, aView3d);
208           aPreviewOp->mouseMoved(aPnt);
209         }
210       }
211     }
212   }
213 }
214
215 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
216 {
217   XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
218   if (aViewer) {
219     aViewer->setViewProjection(theX, theY, theZ);
220   }
221
222   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
223   if (anOperation) {
224     PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
225     if (aPreviewOp) {
226       aPreviewOp->setEditMode(true);
227       // the preview should be shown in another local context
228       visualizePreview(false);
229       visualizePreview(true);
230     }
231   }
232
233   myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
234 }
235
236 void PartSet_Module::visualizePreview(bool isDisplay)
237 {
238   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
239   if (!anOperation)
240     return;
241
242   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
243   if (!aPreviewOp)
244     return;
245
246   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
247   if (isDisplay) {
248     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview();
249     if (aPreview) {
250       aDisplayer->LocalSelection(anOperation->feature(),
251                                    aPreview->impl<TopoDS_Shape>(), aPreviewOp->getSelectionMode());
252     }
253   }
254   else {
255     aDisplayer->GlobalSelection(false);
256     aDisplayer->Erase(anOperation->feature());
257   }
258 }