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