]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
b4b926ae4ba4db2d33dc450da01058d1b7cd8ae1
[modules/shaper.git] / src / PartSet / PartSet_SketcherReetntrantMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_SketcherReetntrantMgr.h"
4 #include "PartSet_Module.h"
5 #include "PartSet_SketcherMgr.h"
6 #include "PartSet_WidgetPoint2D.h"
7
8 #include "ModelAPI_Session.h"
9
10 #include <ModuleBase_IPropertyPanel.h>
11 #include <ModuleBase_OperationFeature.h>
12 #include <ModuleBase_ModelWidget.h>
13 #include <ModuleBase_ViewerPrs.h>
14 #include <ModuleBase_WidgetSelector.h>
15
16 #include <SketchPlugin_Feature.h>
17 #include <SketchPlugin_Line.h>
18
19 #include <XGUI_Workshop.h>
20 #include <XGUI_ModuleConnector.h>
21 #include <XGUI_OperationMgr.h>
22
23 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
24 : QObject(theWorkshop),
25   myWorkshop(theWorkshop),
26   myIsInternalEditOperation(false),
27   myLastOperationId(""),
28   myPreviousAttributeID(""),
29   myRestartingMode(RM_None)
30 {
31 }
32
33 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
34 {
35 }
36
37 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::activeWidget() const
38 {
39   ModuleBase_ModelWidget* aWidget = 0;
40
41   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
42   if (aOperation) {
43     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
44     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
45     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
46       // finds the first widget which can accept a value
47       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
48       ModuleBase_ModelWidget* aFirstWidget = 0;
49       ModuleBase_ModelWidget* aWgt;
50       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
51       for (aWIt = aWidgets.begin(); aWIt != aWidgets.end() && !aFirstWidget; ++aWIt) {
52         aWgt = (*aWIt);
53         if (aWgt->canSetValue())
54           aFirstWidget = aWgt;
55       }
56       if (aFirstWidget)
57         aWidget = aFirstWidget;
58     }
59   }
60   return aWidget;
61 }
62
63 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
64 {
65   bool aProcessed = false;
66   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
67   if (!aFOperation)
68     return aProcessed;
69
70   FeaturePtr aFeature = aFOperation->feature();
71   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
72             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
73   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
74                      myRestartingMode == RM_EmptyFeatureUsed)) {
75     myLastOperationId = aFOperation->id().toStdString();
76     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
77     PartSet_Module* aModule = module();
78     if (!aModule->sketchMgr()->sketchSolverError()) {
79       if (!aFOperation->isEditOperation()) {
80         FeaturePtr anOperationFeature = aFOperation->feature();
81         if (anOperationFeature.get() != NULL) {
82           aModule->editFeature(anOperationFeature);
83           aProcessed = true;
84
85           myIsInternalEditOperation = true;
86           // activate selection filters of the first widget in the viewer
87           onInternalActivateFirstWidgetSelection();
88
89           // activate the last active widget in the Property Panel
90           if (!myPreviousAttributeID.empty()) {
91             ModuleBase_Operation* anEditOperation = aModule->currentOperation();
92             if (anEditOperation) {
93               ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
94               ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
95               QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
96               for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
97                 if (aWidgets[i]->attributeID() == myPreviousAttributeID)
98                   aPreviousAttributeWidget = aWidgets[i];
99               }
100               // If the current widget is a selector, do nothing, it processes the mouse press
101               if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
102                 aPreviousAttributeWidget->focusTo();
103             }
104           }
105         }
106       }
107       else {
108         // the flag should be reset before start to do not react to the widget deactivate
109         myIsInternalEditOperation = false;
110         aModule->launchOperation(myLastOperationId.c_str());
111         breakOperationSequence();
112         aProcessed = true;
113       }
114     }
115   }
116
117   if (!aProcessed)
118     breakOperationSequence();
119
120   return aProcessed;
121 }
122
123 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
124 {
125   if (myIsInternalEditOperation) {
126     // abort the created feature, which is currently edited
127     SessionPtr aMgr = ModelAPI_Session::get();
128     if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
129       aMgr->undo();
130     }
131   }
132   myIsInternalEditOperation = false;
133   breakOperationSequence();
134 }
135
136 bool PartSet_SketcherReetntrantMgr::processMouseMoved()
137 {
138   bool isProcessed = false;
139   if  (myIsInternalEditOperation) {
140     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
141     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
142     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
143       //if (operationMgr()->isApplyEnabled())
144       anOperation->commit();
145       isProcessed = true;
146     }
147   }
148   return isProcessed;
149 }
150
151 bool PartSet_SketcherReetntrantMgr::processMousePressed()
152 {
153   return myIsInternalEditOperation;
154 }
155
156 bool PartSet_SketcherReetntrantMgr::processMouseReleased()
157 {
158   bool isProcessed = false;
159   if (myIsInternalEditOperation) {
160     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
161     //if (operationMgr()->isApplyEnabled())
162     anOperation->commit();
163     isProcessed = true;
164   }
165   return isProcessed;
166 }
167
168 void PartSet_SketcherReetntrantMgr::propertyPanelDefined(ModuleBase_Operation* theOperation)
169 {
170   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
171   if (!aFOperation)
172     return;
173
174   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
175   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation())) {
176     // we have to manually activate the sketch label in edit mode
177       aPanel->activateWidget(aPanel->modelWidgets().first());
178       return;
179   }
180
181   // Restart last operation type 
182   if ((aFOperation->id() == myLastOperationId.c_str()) && myLastFeature) {
183     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
184     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
185     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
186       QList<ModuleBase_ViewerPrs> aSelection;
187       aSelection.append(ModuleBase_ViewerPrs(myLastFeature, TopoDS_Shape(), NULL));
188       if (aPoint2DWdg->setSelection(aSelection, true))
189         aPanel->activateNextWidget(aPoint2DWdg);
190     }
191   }
192 }
193
194 void PartSet_SketcherReetntrantMgr::noMoreWidgets(const std::string& thePreviousAttributeID)
195 {
196   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
197   if (anOperation) {
198     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
199       if (myRestartingMode != RM_Forbided) {
200         myRestartingMode = RM_LastFeatureUsed;
201         myPreviousAttributeID = thePreviousAttributeID;
202       }
203       XGUI_Workshop* aWorkshop = workshop();
204       XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
205       // do nothing if the feature can not be applyed
206       if (anOpMgr->isApplyEnabled())
207         anOperation->commit();
208     }
209   }
210 }
211
212 void PartSet_SketcherReetntrantMgr::vertexSelected()
213 {
214   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
215   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
216     /// If last line finished on vertex the lines creation sequence has to be break
217     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
218     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
219     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
220     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
221     bool aFoundWidget = false;
222     bool aFoundObligatory = false;
223     for (; anIt != aLast && !aFoundObligatory; anIt++) {
224       if (!aFoundWidget)
225         aFoundWidget = *anIt == anActiveWidget;
226       else
227         aFoundObligatory = (*anIt)->isObligatory();
228     }
229     if (!aFoundObligatory)
230       myRestartingMode = RM_Forbided;
231   }
232 }
233
234 void PartSet_SketcherReetntrantMgr::enterReleased()
235 {
236   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
237                                                       (myWorkshop->currentOperation());
238   if (/*!aFOperation->isEditOperation() || */myIsInternalEditOperation)
239     myRestartingMode = RM_EmptyFeatureUsed;
240 }
241
242 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
243 {
244   return myRestartingMode == RM_None;
245 }
246
247 void PartSet_SketcherReetntrantMgr::onInternalActivateFirstWidgetSelection()
248 {
249   if (!myIsInternalEditOperation)
250     return;
251
252   PartSet_Module* aModule = module();
253   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
254   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
255   if (aFirstWidget != aPanel->activeWidget()) {
256     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
257     if (aWSelector)
258       aWSelector->activateSelectionAndFilters(true);
259   }
260 }
261
262 void PartSet_SketcherReetntrantMgr::breakOperationSequence()
263 {
264   myLastOperationId = "";
265   myLastFeature = FeaturePtr();
266   myRestartingMode = RM_None;
267 }
268
269 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop()
270 {
271   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
272   return aConnector->workshop();
273 }
274
275 PartSet_Module* PartSet_SketcherReetntrantMgr::module()
276 {
277   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
278 }