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