]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
5e45abfee7253fc32f55e81d05e6249d43b70d1c
[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 #include <ModuleBase_PageWidget.h>
16 #include <ModuleBase_PageBase.h>
17 #include <ModuleBase_WidgetFactory.h>
18 #include <ModuleBase_OperationDescription.h>
19
20 #include <SketchPlugin_Feature.h>
21 #include <SketchPlugin_Line.h>
22
23 #include <XGUI_Workshop.h>
24 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_OperationMgr.h>
26 #include <XGUI_PropertyPanel.h>
27
28 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
29 : QObject(theWorkshop),
30   myWorkshop(theWorkshop),
31   myRestartingMode(RM_None),
32   myIsFlagsBlocked(false),
33   myIsInternalEditOperation(false)
34 {
35 }
36
37 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
38 {
39 }
40
41 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
42 {
43   ModuleBase_ModelWidget* aWidget = 0;
44   if (!isActiveMgr())
45     return aWidget;
46
47   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
48   if (aOperation) {
49     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
50     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
51     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
52       aWidget = myInternalActiveWidget;
53   }
54   return aWidget;
55 }
56
57 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
58 {
59   return myIsInternalEditOperation;
60 }
61
62 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
63 {
64   bool aProcessed = false;
65   if (!isActiveMgr())
66     return aProcessed;
67
68   aProcessed = myIsInternalEditOperation;
69   resetFlags();
70
71   return aProcessed;
72 }
73
74 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
75 {
76   if (!isActiveMgr())
77     return;
78
79   resetFlags();
80 }
81
82 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
83 {
84   if (!isActiveMgr())
85     return;
86
87   resetFlags();
88 }
89
90 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
91                                                       QMouseEvent* /* theEvent*/)
92 {
93   bool aProcessed = false;
94   if (!isActiveMgr())
95     return aProcessed;
96
97   if  (myIsInternalEditOperation) {
98     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
99     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
100       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
101                                                          (myWorkshop->currentOperation());
102       FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
103       restartOperation();
104       aProcessed = true;
105
106       if (aLastFeature) {
107         ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
108         PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
109         if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
110           QList<ModuleBase_ViewerPrs> aSelection;
111           aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
112           if (aPoint2DWdg->setSelection(aSelection, true))
113             aPanel->activateNextWidget(aPoint2DWdg);
114         }
115       }
116     }
117   }
118   return aProcessed;
119 }
120
121 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
122                                                         QMouseEvent* /* theEvent*/)
123 {
124   return isActiveMgr() && myIsInternalEditOperation;
125 }
126
127 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
128                                                          QMouseEvent* theEvent)
129 {
130   bool aProcessed = false;
131   if (!isActiveMgr())
132     return aProcessed;
133
134   if (myIsInternalEditOperation) {
135     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
136     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
137
138     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
139     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
140       restartOperation();
141       aProcessed = true;
142
143       // fill the first widget by the mouse event point
144       // if the active widget is not the first, it means that the restarted operation is filled by
145       // the current preselection.
146       PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
147       ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
148       if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
149         aPoint2DWdg->onMouseRelease(theWnd, theEvent);
150       }
151     }
152   }
153
154   return aProcessed;
155 }
156
157 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
158 {
159   if (!isActiveMgr())
160     return;
161   if (!myIsInternalEditOperation)
162     return;
163
164   PartSet_Module* aModule = module();
165   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
166   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
167   if (aFirstWidget != aPanel->activeWidget()) {
168     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
169     if (aWSelector)
170       aWSelector->activateSelectionAndFilters(true);
171   }
172 }
173
174 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
175 {
176   if (!isActiveMgr())
177     return;
178
179   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
180                                                        (myWorkshop->currentOperation());
181   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
182     return;
183
184   bool isStarted = false;
185   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
186   if (!isSketchSolverError &&
187       aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
188     if (myRestartingMode != RM_Forbided) {
189       myRestartingMode = RM_LastFeatureUsed;
190       isStarted = startInternalEdit(thePreviousAttributeID);
191     }
192   }
193
194   if (!isStarted)
195     aFOperation->commit();
196 }
197
198 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
199 {
200   bool isDone = false;
201
202   if (!isActiveMgr())
203     return isDone;
204
205   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
206                                                        (myWorkshop->currentOperation());
207   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
208     return isDone;
209
210   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
211   if (!isSketchSolverError) {
212     myRestartingMode = RM_EmptyFeatureUsed;
213     isDone = startInternalEdit(thePreviousAttributeID);
214   }
215
216   return isDone;
217 }
218
219 void PartSet_SketcherReetntrantMgr::onVertexSelected()
220 {
221   if (!isActiveMgr())
222     return;
223
224   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
225   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
226     /// If last line finished on vertex the lines creation sequence has to be break
227     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
228     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
229     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
230     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
231     bool aFoundWidget = false;
232     bool aFoundObligatory = false;
233     for (; anIt != aLast && !aFoundObligatory; anIt++) {
234       if (!aFoundWidget)
235         aFoundWidget = *anIt == anActiveWidget;
236       else
237         aFoundObligatory = (*anIt)->isObligatory();
238     }
239     if (!aFoundObligatory)
240       myRestartingMode = RM_Forbided;
241   }
242 }
243
244 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
245 {
246   if (!isActiveMgr() || !myIsInternalEditOperation)
247     return;
248
249   beforeStopInternalEdit();
250 }
251
252 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
253 {
254   return !isActiveMgr() || myRestartingMode == RM_None;
255 }
256
257 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
258 {
259   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
260
261   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
262   if (!anActive) {
263     anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
264     if (anActive) { // the manager is not active when the current operation is a usual Edit
265       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
266                                                        (myWorkshop->currentOperation());
267       if (aFOperation->isEditOperation())
268         anActive = myIsInternalEditOperation;
269     }
270   }
271   return anActive;
272 }
273
274 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
275 {
276   bool isDone = false;
277   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
278   /// event comes two times, so we should not start another internal edit operation
279   /// the Apply button becomes disabled becase the second additional internal feature is created
280   if (myIsInternalEditOperation)
281     return true;
282
283   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
284                                                      (myWorkshop->currentOperation());
285
286   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
287     aFOperation->setEditOperation(false);
288     workshop()->operationMgr()->updateApplyOfOperations();
289
290     createInternalFeature();
291
292     myIsInternalEditOperation = true;
293     isDone = true;
294     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
295     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
296
297     // activate selection filters of the first widget in the viewer
298     onWidgetActivated();
299
300     // activate the last active widget in the Property Panel
301     if (!thePreviousAttributeID.empty()) {
302       ModuleBase_Operation* anEditOperation = module()->currentOperation();
303       if (anEditOperation) {
304         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
305         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
306         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
307         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
308           if (aWidgets[i]->attributeID() == thePreviousAttributeID)
309             aPreviousAttributeWidget = aWidgets[i];
310         }
311         // If the current widget is a selector, do nothing, it processes the mouse press
312         if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector()) {
313           aPreviousAttributeWidget->focusTo();
314           aPreviousAttributeWidget->selectContent();
315         }
316       }
317     }
318   }
319   if (isDone)
320     module()->sketchMgr()->clearClickedFlags();
321
322   return isDone;
323 }
324
325 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
326 {
327   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
328                                                       (myWorkshop->currentOperation());
329   if (aFOperation) {
330     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
331     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
332   }
333
334   deleteInternalFeature();
335 }
336
337 void PartSet_SketcherReetntrantMgr::restartOperation()
338 {
339   if (myIsInternalEditOperation) {
340     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
341                                                                   myWorkshop->currentOperation());
342     if (aFOperation) {
343       myIsFlagsBlocked = true;
344       aFOperation->commit();
345       module()->launchOperation(aFOperation->id());
346       myIsFlagsBlocked = false;
347       resetFlags();
348     }
349   }
350 }
351
352 void PartSet_SketcherReetntrantMgr::createInternalFeature()
353 {
354   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
355                                                      (myWorkshop->currentOperation());
356
357   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
358     FeaturePtr anOperationFeature = aFOperation->feature();
359
360     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
361     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
362     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
363                                                   (aFOperation->propertyPanel());
364
365     myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
366     myInternalWidget->setVisible(false);
367
368     ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
369
370     QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
371     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
372
373     aFactory.createWidget(anInternalPage);
374     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
375
376     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
377       aWidget->setFeature(myInternalFeature, true);
378     }
379     ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
380                                                                                         (aWidgets);
381     if (aFirstWidget)
382       myInternalActiveWidget = aFirstWidget;
383   }
384 }
385
386 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
387 {
388   if (myInternalActiveWidget) {
389     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
390     if (aWSelector)
391       aWSelector->activateSelectionAndFilters(false);
392     myInternalActiveWidget = 0;
393   }
394   delete myInternalWidget;
395   myInternalWidget = 0;
396
397   QObjectPtrList anObjects;
398   anObjects.append(myInternalFeature);
399   workshop()->deleteFeatures(anObjects);
400 }
401
402 void PartSet_SketcherReetntrantMgr::resetFlags()
403 {
404   if (!myIsFlagsBlocked) {
405     myIsInternalEditOperation = false;
406     myRestartingMode = RM_None;
407   }
408 }
409
410 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
411 {
412   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
413   return aConnector->workshop();
414 }
415
416 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
417 {
418   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
419 }
420