Salome HOME
Using of SelectionMgr instead of using Viewer directly in Sketcher starting operation
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationSketchLine.h>
4
5 #include <ModuleBase_Operation.h>
6
7 #include <XGUI_MainWindow.h>
8 #include <XGUI_Displayer.h>
9 #include <XGUI_Viewer.h>
10 #include <XGUI_Workshop.h>
11 #include <XGUI_OperationMgr.h>
12 #include <XGUI_SelectionMgr.h>
13
14 #include <Config_PointerMessage.h>
15 #include <Config_ModuleReader.h>
16 #include <Config_WidgetReader.h>
17 #include <Events_Loop.h>
18 #include <Events_Message.h>
19
20 #include <GeomAPI_Shape.h>
21
22 #include <AIS_ListOfInteractive.hxx>
23
24 #include <QObject>
25 #include <QString>
26
27 #ifdef _DEBUG
28 #include <QDebug>
29 #endif
30
31
32 /*!Create and return new instance of XGUI_Module*/
33 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
34 {
35   return new PartSet_Module(theWshop);
36 }
37
38 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
39 {
40   myWorkshop = theWshop;
41   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
42
43   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
44   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
45           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
46   connect(myWorkshop->selector(), SIGNAL(selectionChanged()),
47             this, SLOT(onViewSelectionChanged()));
48 }
49
50 PartSet_Module::~PartSet_Module()
51 {
52 }
53
54 void PartSet_Module::createFeatures()
55 {
56   Config_ModuleReader aXMLReader = Config_ModuleReader();
57   aXMLReader.readAll();
58   myFeaturesInFiles = aXMLReader.featuresInFiles();
59 }
60
61 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
62 {
63   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
64 }
65
66 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
67 {
68   return myFeaturesInFiles[theFeatureId];
69 }
70
71 /*
72  *
73  */
74 void PartSet_Module::onFeatureTriggered()
75 {
76   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
77   launchOperation(aCmd->id());
78 }
79   
80 void PartSet_Module::launchOperation(const QString& theCmdId)
81 {
82   std::string aStdCmdId = theCmdId.toStdString();
83   std::string aPluginFileName = featureFile(aStdCmdId);
84   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
85   aWdgReader.readAll();
86   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
87   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
88   ModuleBase_PropPanelOperation* aPartSetOp;
89   if (theCmdId == "Sketch" ) {
90     aPartSetOp = new PartSet_OperationSketch(theCmdId, this);
91   }
92   else if(theCmdId == "SketchLine") {
93     ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
94     boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
95     if (anOperation)
96       aSketchFeature = anOperation->feature();
97     aPartSetOp = new PartSet_OperationSketchLine(theCmdId, this, aSketchFeature);
98   }
99   else {
100     aPartSetOp = new ModuleBase_PropPanelOperation(theCmdId, this);
101   }
102   aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
103   aPartSetOp->setDescription(QString::fromStdString(aDescription));
104
105   //TODO(sbh): Implement static method to extract event id [SEID]
106   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
107   Config_PointerMessage aMessage(aModuleEvent, this);
108   aMessage.setPointer(aPartSetOp);
109   Events_Loop::loop()->send(aMessage);
110 }
111
112 void PartSet_Module::onOperationStarted()
113 {
114   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
115
116   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
117   if (aPreviewOp) {
118     visualizePreview(true);
119     connect(aPreviewOp, SIGNAL(viewerProjectionChange(double, double, double)),
120             this, SLOT(onViewerProjectionChange(double, double, double)));
121   }
122 }
123
124 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
125 {
126   ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
127   if (!anOperation)
128     return;
129   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
130   if (aPreviewOp)
131     visualizePreview(false);
132 }
133
134 void PartSet_Module::onViewSelectionChanged()
135 {
136   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
137   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
138   if (aPreviewOp) {
139     XGUI_SelectionMgr* aSelector = myWorkshop->selector();
140     if (aSelector) {
141       NCollection_List<TopoDS_Shape> aList;
142       aSelector->selectedShapes(aList);
143       aPreviewOp->setSelectedShapes(aList);
144     }
145   }
146 }
147
148 void PartSet_Module::onViewerProjectionChange(double theX, double theY, double theZ)
149 {
150   XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
151   if (aViewer) {
152     aViewer->setViewProjection(theX, theY, theZ);
153   }
154 }
155
156 void PartSet_Module::visualizePreview(bool isDisplay)
157 {
158   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
159   if (!anOperation)
160     return;
161
162   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
163   if (!aPreviewOp)
164     return;
165
166   if (isDisplay) {
167     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview();
168     if (aPreview)
169       myWorkshop->displayer()->LocalSelection(anOperation->feature(),
170                                    aPreview->impl<TopoDS_Shape>(), aPreviewOp->getSelectionMode());
171   }
172   else {
173     myWorkshop->displayer()->GlobalSelection(false);
174     myWorkshop->displayer()->Erase(anOperation->feature());
175   }
176 }