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