]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
96050b8781136b0a8118daa7f8f72d00116d1961
[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   if (!isActiveMgr())
41     return aWidget;
42
43   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
44   if (aOperation) {
45     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
46     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
47     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
48       // finds the first widget which can accept a value
49       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
50       ModuleBase_ModelWidget* aFirstWidget = 0;
51       ModuleBase_ModelWidget* aWgt;
52       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
53       for (aWIt = aWidgets.begin(); aWIt != aWidgets.end() && !aFirstWidget; ++aWIt) {
54         aWgt = (*aWIt);
55         if (aWgt->canSetValue())
56           aFirstWidget = aWgt;
57       }
58       if (aFirstWidget)
59         aWidget = aFirstWidget;
60     }
61   }
62   return aWidget;
63 }
64
65 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
66 {
67   bool aProcessed = false;
68   if (!isActiveMgr())
69     return aProcessed;
70
71   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
72   if (!aFOperation)
73     return aProcessed;
74
75   FeaturePtr aFeature = aFOperation->feature();
76   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
77             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
78   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
79                      myRestartingMode == RM_EmptyFeatureUsed)) {
80     myLastOperationId = aFOperation->id().toStdString();
81     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
82     PartSet_Module* aModule = module();
83     if (!aModule->sketchMgr()->sketchSolverError()) {
84       if (!aFOperation->isEditOperation()) {
85         FeaturePtr anOperationFeature = aFOperation->feature();
86         if (anOperationFeature.get() != NULL) {
87           aModule->editFeature(anOperationFeature);
88           aProcessed = true;
89
90           myIsInternalEditOperation = true;
91           // activate selection filters of the first widget in the viewer
92           onInternalActivateFirstWidgetSelection();
93
94           // activate the last active widget in the Property Panel
95           if (!myPreviousAttributeID.empty()) {
96             ModuleBase_Operation* anEditOperation = aModule->currentOperation();
97             if (anEditOperation) {
98               ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
99               ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
100               QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
101               for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
102                 if (aWidgets[i]->attributeID() == myPreviousAttributeID)
103                   aPreviousAttributeWidget = aWidgets[i];
104               }
105               // If the current widget is a selector, do nothing, it processes the mouse press
106               if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
107                 aPreviousAttributeWidget->focusTo();
108             }
109           }
110         }
111       }
112       else {
113         // the flag should be reset before start to do not react to the widget deactivate
114         myIsInternalEditOperation = false;
115         aModule->launchOperation(myLastOperationId.c_str());
116         breakOperationSequence();
117         aProcessed = true;
118       }
119     }
120   }
121
122   if (!aProcessed)
123     breakOperationSequence();
124
125   return aProcessed;
126 }
127
128 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
129 {
130   if (!isActiveMgr())
131     return;
132
133   if (myIsInternalEditOperation) {
134     // abort the created feature, which is currently edited
135     SessionPtr aMgr = ModelAPI_Session::get();
136     if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
137       aMgr->undo();
138     }
139   }
140   myIsInternalEditOperation = false;
141   breakOperationSequence();
142 }
143
144 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow*/* theWnd*/,
145                                                       QMouseEvent*/* theEvent*/)
146 {
147   bool aProcessed = false;
148   if (!isActiveMgr())
149     return aProcessed;
150
151   if  (myIsInternalEditOperation) {
152     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
153     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
154     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
155       //if (operationMgr()->isApplyEnabled())
156       anOperation->commit();
157       aProcessed = true;
158     }
159   }
160   return aProcessed;
161 }
162
163 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow*/* theWnd*/,
164                                                         QMouseEvent*/* theEvent*/)
165 {
166   return isActiveMgr() && myIsInternalEditOperation;
167 }
168
169 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
170                                                          QMouseEvent* theEvent)
171 {
172   bool aProcessed = false;
173   if (!isActiveMgr())
174     return aProcessed;
175
176   if (myIsInternalEditOperation) {
177     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
178     //if (operationMgr()->isApplyEnabled())
179     anOperation->commit();
180     aProcessed = true;
181
182     // fill the widget by the mouse event point
183     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
184     if (aPoint2DWdg) {
185       aPoint2DWdg->onMouseRelease(theWnd, theEvent);
186     }
187   }
188
189   return aProcessed;
190 }
191
192 void PartSet_SketcherReetntrantMgr::propertyPanelDefined(ModuleBase_Operation* theOperation)
193 {
194   if (!isActiveMgr())
195     return;
196   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
197   if (!aFOperation)
198     return;
199
200   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
201   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation())) {
202     // we have to manually activate the sketch label in edit mode
203     aPanel->activateWidget(aPanel->modelWidgets().first());
204   }
205   else if ((aFOperation->id() == myLastOperationId.c_str()) && myLastFeature) {
206     // Restart last operation type 
207     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
208     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
209     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
210       QList<ModuleBase_ViewerPrs> aSelection;
211       aSelection.append(ModuleBase_ViewerPrs(myLastFeature, TopoDS_Shape(), NULL));
212       if (aPoint2DWdg->setSelection(aSelection, true))
213         aPanel->activateNextWidget(aPoint2DWdg);
214     }
215   }
216 }
217
218 void PartSet_SketcherReetntrantMgr::noMoreWidgets(const std::string& thePreviousAttributeID)
219 {
220   if (!isActiveMgr())
221     return;
222
223   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
224   if (anOperation) {
225     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
226       if (myRestartingMode != RM_Forbided) {
227         myRestartingMode = RM_LastFeatureUsed;
228         myPreviousAttributeID = thePreviousAttributeID;
229       }
230       XGUI_Workshop* aWorkshop = workshop();
231       XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
232       // do nothing if the feature can not be applyed
233       if (anOpMgr->isApplyEnabled())
234         anOperation->commit();
235     }
236   }
237 }
238
239 void PartSet_SketcherReetntrantMgr::vertexSelected()
240 {
241   if (!isActiveMgr())
242     return;
243
244   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
245   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
246     /// If last line finished on vertex the lines creation sequence has to be break
247     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
248     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
249     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
250     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
251     bool aFoundWidget = false;
252     bool aFoundObligatory = false;
253     for (; anIt != aLast && !aFoundObligatory; anIt++) {
254       if (!aFoundWidget)
255         aFoundWidget = *anIt == anActiveWidget;
256       else
257         aFoundObligatory = (*anIt)->isObligatory();
258     }
259     if (!aFoundObligatory)
260       myRestartingMode = RM_Forbided;
261   }
262 }
263
264 void PartSet_SketcherReetntrantMgr::enterReleased()
265 {
266   if (!isActiveMgr())
267     return;
268
269   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
270                                                       (myWorkshop->currentOperation());
271   if (/*!aFOperation->isEditOperation() || */myIsInternalEditOperation)
272     myRestartingMode = RM_EmptyFeatureUsed;
273 }
274
275 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
276 {
277   return !isActiveMgr() || myRestartingMode == RM_None;
278 }
279
280 void PartSet_SketcherReetntrantMgr::onInternalActivateFirstWidgetSelection()
281 {
282   if (!isActiveMgr())
283     return;
284
285   if (!myIsInternalEditOperation)
286     return;
287
288   PartSet_Module* aModule = module();
289   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
290   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
291   if (aFirstWidget != aPanel->activeWidget()) {
292     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
293     if (aWSelector)
294       aWSelector->activateSelectionAndFilters(true);
295   }
296 }
297
298 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
299 {
300   PartSet_SketcherMgr* aSketcherMgr = module()->sketchMgr();
301   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
302   return PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
303          PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
304 }
305
306 void PartSet_SketcherReetntrantMgr::breakOperationSequence()
307 {
308   myLastOperationId = "";
309   myLastFeature = FeaturePtr();
310   myRestartingMode = RM_None;
311 }
312
313 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
314 {
315   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
316   return aConnector->workshop();
317 }
318
319 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
320 {
321   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
322 }