]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
Issue #6 advanced command enable/disable state processing
[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
13 #include <Config_PointerMessage.h>
14 #include <Config_ModuleReader.h>
15 #include <Config_WidgetReader.h>
16 #include <Events_Loop.h>
17 #include <Events_Message.h>
18
19 #include <GeomAPI_Shape.h>
20
21 #include <AIS_ListOfInteractive.hxx>
22
23 #include <QObject>
24 #include <QString>
25
26 #ifdef _DEBUG
27 #include <QDebug>
28 #endif
29
30
31 /*!Create and return new instance of XGUI_Module*/
32 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
33 {
34   return new PartSet_Module(theWshop);
35 }
36
37 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
38 {
39   myWorkshop = theWshop;
40   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
41
42   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
43   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
44           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
45   if (!myWorkshop->isSalomeMode())
46     connect(myWorkshop->mainWindow()->viewer(), 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 QStringList PartSet_Module::nestedFeatures(QString)
67 {
68   return QStringList();
69 }
70
71 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
72 {
73   return myFeaturesInFiles[theFeatureId];
74 }
75
76 /*
77  *
78  */
79 void PartSet_Module::onFeatureTriggered()
80 {
81   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
82   //Do nothing on uncheck
83   if(aCmd->isCheckable() && !aCmd->isChecked())
84     return;
85   launchOperation(aCmd->id());
86 }
87   
88 void PartSet_Module::launchOperation(const QString& theCmdId)
89 {
90   std::string aStdCmdId = theCmdId.toStdString();
91   std::string aPluginFileName = featureFile(aStdCmdId);
92   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
93   aWdgReader.readAll();
94   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
95   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
96   ModuleBase_PropPanelOperation* aPartSetOp;
97   if (theCmdId == "Sketch" ) {
98     aPartSetOp = new PartSet_OperationSketch(theCmdId, this);
99   }
100   else if(theCmdId == "SketchLine") {
101     ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
102     boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
103     if (anOperation)
104       aSketchFeature = anOperation->feature();
105     aPartSetOp = new PartSet_OperationSketchLine(theCmdId, this, aSketchFeature);
106   }
107   else {
108     aPartSetOp = new ModuleBase_PropPanelOperation(theCmdId, this);
109   }
110   aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
111   aPartSetOp->setDescription(QString::fromStdString(aDescription));
112
113   //TODO(sbh): Implement static method to extract event id [SEID]
114   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
115   Config_PointerMessage aMessage(aModuleEvent, this);
116   aMessage.setPointer(aPartSetOp);
117   Events_Loop::loop()->send(aMessage);
118 }
119
120 void PartSet_Module::onOperationStarted()
121 {
122   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
123
124   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
125   if (aPreviewOp) {
126     visualizePreview(true);
127     connect(aPreviewOp, SIGNAL(viewerProjectionChange(double, double, double)),
128             this, SLOT(onViewerProjectionChange(double, double, double)));
129   }
130 }
131
132 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
133 {
134   ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
135   if (!anOperation)
136     return;
137   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
138   if (aPreviewOp)
139     visualizePreview(false);
140 }
141
142 void PartSet_Module::onViewSelectionChanged()
143 {
144   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
145   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
146   if (aPreviewOp) {
147     XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
148     if (aViewer) {
149       NCollection_List<TopoDS_Shape> aList;
150       aViewer->getSelectedShapes(aList);
151       aPreviewOp->setSelectedShapes(aList);
152     }
153   }
154 }
155
156 void PartSet_Module::onViewerProjectionChange(double theX, double theY, double theZ)
157 {
158   XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
159   if (aViewer) {
160     aViewer->setViewProjection(theX, theY, theZ);
161   }
162 }
163
164 void PartSet_Module::visualizePreview(bool isDisplay)
165 {
166   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
167   if (!anOperation)
168     return;
169
170   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
171   if (!aPreviewOp)
172     return;
173
174   if (isDisplay) {
175     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview();
176     if (aPreview)
177       myWorkshop->displayer()->LocalSelection(anOperation->feature(),
178                                    aPreview->impl<TopoDS_Shape>(), aPreviewOp->getSelectionMode());
179   }
180   else {
181     myWorkshop->displayer()->GlobalSelection(false);
182     myWorkshop->displayer()->Erase(anOperation->feature());
183   }
184 }