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