Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[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_OperationEditLine.h>
5 #include <ModuleBase_Operation.h>
6 #include <ModuleBase_OperationDescription.h>
7 #include <PartSet_Listener.h>
8
9 #include <ModuleBase_Operation.h>
10
11 #include <XGUI_MainWindow.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_Viewer.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_OperationMgr.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <XGUI_ViewPort.h>
18 #include <XGUI_ActionsMgr.h>
19 #include <XGUI_ViewerProxy.h>
20
21 #include <Config_PointerMessage.h>
22 #include <Config_ModuleReader.h>
23 #include <Config_WidgetReader.h>
24 #include <Events_Loop.h>
25 #include <Events_Message.h>
26 #include <Events_Error.h>
27
28 #include <GeomAPI_Shape.h>
29
30 #include <AIS_ListOfInteractive.hxx>
31
32 #include <QObject>
33 #include <QMouseEvent>
34 #include <QString>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40
41 /*!Create and return new instance of XGUI_Module*/
42 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
43 {
44   return new PartSet_Module(theWshop);
45 }
46
47 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
48 {
49   myWorkshop = theWshop;
50   myListener = new PartSet_Listener(this);
51
52   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
53
54   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
55           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
56
57   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
58           this, SLOT(onMousePressed(QMouseEvent*)));
59   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
60           this, SLOT(onMouseReleased(QMouseEvent*)));
61   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
62           this, SLOT(onMouseMoved(QMouseEvent*)));
63   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
64           this, SLOT(onKeyRelease(QKeyEvent*)));
65 }
66
67 PartSet_Module::~PartSet_Module()
68 {
69 }
70
71 XGUI_Workshop* PartSet_Module::workshop() const
72 {
73   return myWorkshop;
74 }
75
76 void PartSet_Module::createFeatures()
77 {
78   Config_ModuleReader aXMLReader = Config_ModuleReader();
79   aXMLReader.readAll();
80   myFeaturesInFiles = aXMLReader.featuresInFiles();
81 }
82
83 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
84 {
85   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
86 }
87
88 QStringList PartSet_Module::nestedFeatures(QString)
89 {
90   return QStringList();
91 }
92
93 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
94 {
95   return myFeaturesInFiles[theFeatureId];
96 }
97
98 /*
99  *
100  */
101 void PartSet_Module::onFeatureTriggered()
102 {
103   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
104   //Do nothing on uncheck
105   if(aCmd->isCheckable() && !aCmd->isChecked())
106     return;
107   launchOperation(aCmd->id());
108 }
109   
110 void PartSet_Module::launchOperation(const QString& theCmdId)
111 {
112   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
113   sendOperation(anOperation);
114 }
115
116 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
117 {
118   if (!theOperation)
119     return;
120   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
121   if (aPreviewOp) {
122   }
123 }
124
125 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
126 {
127   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
128                                        myWorkshop->operationMgr()->currentOperation());
129   if (aPreviewOp)
130   {
131     std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
132     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
133   }
134 }
135
136 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
137 {
138   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
139                                        myWorkshop->operationMgr()->currentOperation());
140   if (aPreviewOp)
141   {
142     std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
143     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
144   }
145 }
146
147 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
148 {
149   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
150                                        myWorkshop->operationMgr()->currentOperation());
151   if (aPreviewOp)
152     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
153 }
154
155 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
156 {
157   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
158   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
159   if (aPreviewOp) {
160     aPreviewOp->keyReleased(theEvent->key());
161   }
162 }
163
164 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
165 {
166   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
167
168   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
169   if (anOperation) {
170     PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
171     if (aPreviewOp) {
172       visualizePreview(aPreviewOp->feature(), false);
173     }
174   }
175
176   myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
177 }
178
179 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
180 {
181   ModuleBase_Operation* anOperation = createOperation(theName.c_str());
182   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
183   if (aPreviewOp)
184   {
185     std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
186     aPreviewOp->init(theFeature, aPresentations);
187   }
188   myWorkshop->actionsMgr()->setActionChecked(anOperation->getDescription()->operationId(), true);
189   sendOperation(anOperation);
190 }
191
192 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
193 {
194   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
195   aViewer->enableMultiselection(theEnabled);
196 }
197
198 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
199 {
200   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
201   if (!isStop) {
202     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
203     boost::shared_ptr<ModelAPI_Feature> aFeature;
204     for (; anIt != aLast; anIt++) {
205       activateFeature((*anIt).feature(), false);
206     }
207   }
208   aDisplayer->StopSelection(theFeatures, isStop, false);
209   aDisplayer->UpdateViewer();
210 }
211
212 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
213 {
214   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
215   aDisplayer->SetSelected(theFeatures, false);
216   aDisplayer->UpdateViewer();
217 }
218
219 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
220                                           int theMode)
221 {
222   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Abort;
223   visualizePreview(theFeature, isDisplay, false);
224
225   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
226       theMode == PartSet_OperationSketchBase::FM_Deactivation)
227     activateFeature(theFeature, true);
228 }
229
230 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
231 {
232   // get operation xml description
233   std::string aStdCmdId = theCmdId;
234   if (aStdCmdId == PartSet_OperationEditLine::Type())
235     aStdCmdId = PartSet_OperationSketchLine::Type();
236   std::string aPluginFileName = featureFile(aStdCmdId);
237   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
238   aWdgReader.readAll();
239   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
240   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
241
242   // create the operation
243   ModuleBase_Operation* anOperation;
244   if (theCmdId == PartSet_OperationSketch::Type()) {
245     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
246   }
247   else if(theCmdId == PartSet_OperationSketchLine::Type() ||
248           theCmdId == PartSet_OperationEditLine::Type()) {
249     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
250     boost::shared_ptr<ModelAPI_Feature> aSketch;
251     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
252     if (aPrevOp)
253       aSketch = aPrevOp->sketch();
254     if (theCmdId == PartSet_OperationSketchLine::Type())
255       anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
256     else
257       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
258   }
259   else {
260     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
261   }
262   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
263   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
264
265   // connect the operation
266   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
267   if (aPreviewOp) {
268     connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
269             this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
270     connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
271             this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
272     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
273             this, SLOT(onMultiSelectionEnabled(bool)));
274
275     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
276             this, SLOT(onMultiSelectionEnabled(bool)));
277     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
278             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
279     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
280             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
281
282     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
283     if (aSketchOp) {
284       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
285               this, SLOT(onPlaneSelected(double, double, double)));
286     }
287   }
288
289   return anOperation;
290 }
291
292 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
293 {
294   //TODO(sbh): Implement static method to extract event id [SEID]
295   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
296   Config_PointerMessage aMessage(aModuleEvent, this);
297   aMessage.setPointer(theOperation);
298   Events_Loop::loop()->send(aMessage);
299 }
300
301 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
302                                       const bool isUpdateViewer)
303 {
304   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
305   if (!anOperation)
306     return;
307
308   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
309   if (!aPreviewOp)
310     return;
311
312   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
313   if (isDisplay) {
314     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
315     aDisplayer->Redisplay(theFeature,
316                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
317   }
318   else
319     aDisplayer->Erase(anOperation->feature(), false);
320
321   if (isUpdateViewer)
322     aDisplayer->UpdateViewer();
323 }
324
325 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
326                                      const bool isUpdateViewer)
327 {
328   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
329   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
330   if (aPreviewOp) {
331     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
332     aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
333                                        isUpdateViewer);
334   }
335 }
336
337 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
338 {
339   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
340   if (!anOperation)
341     return;
342
343   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
344   if (!aPreviewOp)
345     return;
346
347   boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
348   if (!aFeature || aFeature->getKind() != theCmdId)
349     return;
350
351   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
352                                                                      aList = aPreviewOp->preview();
353   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
354   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
355
356   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
357                                                          anIt = aList.begin(), aLast = aList.end();
358   for (; anIt != aLast; anIt++) {
359     boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
360     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
361     aDisplayer->Redisplay(aFeature,
362                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
363     aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
364   }
365   aDisplayer->UpdateViewer();
366 }
367