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