]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
61284fd7bae77ad7a9ce427af361fc9f78bc53d7
[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   myRestartingMode(RM_None),
27   myIsFlagsBlocked(false),
28   myIsInternalEditOperation(false)
29 {
30 }
31
32 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
33 {
34 }
35
36 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
37 {
38   ModuleBase_ModelWidget* aWidget = 0;
39   if (!isActiveMgr())
40     return aWidget;
41
42   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
43   if (aOperation) {
44     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
45     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
46     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
47       // finds the first widget which can accept a value
48       ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
49       if (aFirstWidget)
50         aWidget = aFirstWidget;
51     }
52   }
53   return aWidget;
54 }
55
56 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
57 {
58   bool aProcessed = false;
59   if (!isActiveMgr())
60     return aProcessed;
61
62   aProcessed = myIsInternalEditOperation;
63   resetFlags();
64
65   return aProcessed;
66 }
67
68 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
69 {
70   if (!isActiveMgr())
71     return;
72
73   resetFlags();
74 }
75
76 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
77 {
78   if (!isActiveMgr())
79     return;
80
81   resetFlags();
82 }
83
84 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
85                                                       QMouseEvent* /* theEvent*/)
86 {
87   bool aProcessed = false;
88   if (!isActiveMgr())
89     return aProcessed;
90
91   if  (myIsInternalEditOperation) {
92     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
93     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
94       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
95                                                          (myWorkshop->currentOperation());
96       FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
97       restartOperation();
98       aProcessed = true;
99
100       if (aLastFeature) {
101         ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
102         PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
103         if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
104           QList<ModuleBase_ViewerPrs> aSelection;
105           aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
106           if (aPoint2DWdg->setSelection(aSelection, true))
107             aPanel->activateNextWidget(aPoint2DWdg);
108         }
109       }
110     }
111   }
112   return aProcessed;
113 }
114
115 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
116                                                         QMouseEvent* /* theEvent*/)
117 {
118   return isActiveMgr() && myIsInternalEditOperation;
119 }
120
121 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
122                                                          QMouseEvent* theEvent)
123 {
124   bool aProcessed = false;
125   if (!isActiveMgr())
126     return aProcessed;
127
128   if (myIsInternalEditOperation) {
129     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
130     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
131
132     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
133     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
134       restartOperation();
135       aProcessed = true;
136
137       // fill the first widget by the mouse event point
138       // if the active widget is not the first, it means that the restarted operation is filled by
139       // the current preselection.
140       PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
141       ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
142       if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
143         aPoint2DWdg->onMouseRelease(theWnd, theEvent);
144       }
145     }
146   }
147
148   return aProcessed;
149 }
150
151 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
152 {
153   if (!isActiveMgr())
154     return;
155   if (!myIsInternalEditOperation)
156     return;
157
158   PartSet_Module* aModule = module();
159   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
160   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
161   if (aFirstWidget != aPanel->activeWidget()) {
162     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
163     if (aWSelector)
164       aWSelector->activateSelectionAndFilters(true);
165   }
166 }
167
168 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
169 {
170   if (!isActiveMgr())
171     return;
172
173   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
174                                                        (myWorkshop->currentOperation());
175   if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
176     return;
177
178   if (aFOperation) {
179     if (PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
180       if (myRestartingMode != RM_Forbided) {
181         myRestartingMode = RM_LastFeatureUsed;
182         startInternalEdit(thePreviousAttributeID);
183       }
184       else {
185         aFOperation->commit();
186       }
187     }
188   }
189 }
190
191 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
192 {
193   bool isDone = false;
194
195   if (!isActiveMgr())
196     return isDone;
197
198   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
199                                                        (myWorkshop->currentOperation());
200   if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
201     return isDone;
202
203   myRestartingMode = RM_EmptyFeatureUsed;
204   startInternalEdit(thePreviousAttributeID);
205   isDone = true;
206
207   return isDone;
208 }
209
210 void PartSet_SketcherReetntrantMgr::onVertexSelected()
211 {
212   if (!isActiveMgr())
213     return;
214
215   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
216   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
217     /// If last line finished on vertex the lines creation sequence has to be break
218     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
219     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
220     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
221     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
222     bool aFoundWidget = false;
223     bool aFoundObligatory = false;
224     for (; anIt != aLast && !aFoundObligatory; anIt++) {
225       if (!aFoundWidget)
226         aFoundWidget = *anIt == anActiveWidget;
227       else
228         aFoundObligatory = (*anIt)->isObligatory();
229     }
230     if (!aFoundObligatory)
231       myRestartingMode = RM_Forbided;
232   }
233 }
234
235 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
236 {
237   if (!isActiveMgr() || !myIsInternalEditOperation)
238     return;
239
240   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
241                                                       (myWorkshop->currentOperation());
242   if (aFOperation) {
243     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
244     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
245   }
246
247   PartSet_Module* aModule = module();
248   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
249   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
250   if (aFirstWidget != aPanel->activeWidget()) {
251     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
252     if (aWSelector)
253       aWSelector->activateSelectionAndFilters(false);
254   }
255 }
256
257 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
258 {
259   return !isActiveMgr() || myRestartingMode == RM_None;
260 }
261
262 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
263 {
264   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
265
266   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
267   if (!anActive) {
268     anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
269     if (anActive) { // the manager is not active when the current operation is a usual Edit
270       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
271                                                        (myWorkshop->currentOperation());
272       if (aFOperation->isEditOperation())
273         anActive = myIsInternalEditOperation;
274     }
275   }
276   return anActive;
277 }
278
279 void PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
280 {
281   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
282                                                      (myWorkshop->currentOperation());
283
284   aFOperation->setEditOperation(true);
285   FeaturePtr anOperationFeature = aFOperation->feature();
286   if (anOperationFeature.get() != NULL) {
287
288     myIsInternalEditOperation = true;
289     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
290     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
291
292     // activate selection filters of the first widget in the viewer
293     onWidgetActivated();
294
295     // activate the last active widget in the Property Panel
296     if (!thePreviousAttributeID.empty()) {
297       ModuleBase_Operation* anEditOperation = module()->currentOperation();
298       if (anEditOperation) {
299         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
300         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
301         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
302         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
303           if (aWidgets[i]->attributeID() == thePreviousAttributeID)
304             aPreviousAttributeWidget = aWidgets[i];
305         }
306         // If the current widget is a selector, do nothing, it processes the mouse press
307         if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
308           aPreviousAttributeWidget->focusTo();
309       }
310     }
311   }
312 }
313
314 void PartSet_SketcherReetntrantMgr::restartOperation()
315 {
316   if (myIsInternalEditOperation) {
317     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
318                                                                   myWorkshop->currentOperation());
319     if (aFOperation) {
320       myIsFlagsBlocked = true;
321       aFOperation->commit();
322       module()->launchOperation(aFOperation->id());
323       myIsFlagsBlocked = false;
324       resetFlags();
325     }
326   }
327 }
328
329 void PartSet_SketcherReetntrantMgr::resetFlags()
330 {
331   if (!myIsFlagsBlocked) {
332     myIsInternalEditOperation = false;
333     myRestartingMode = RM_None;
334   }
335 }
336
337 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
338 {
339   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
340   return aConnector->workshop();
341 }
342
343 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
344 {
345   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
346 }
347