1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "PartSet_SketcherReetntrantMgr.h"
4 #include "PartSet_Module.h"
5 #include "PartSet_SketcherMgr.h"
6 #include "PartSet_WidgetPoint2d.h"
8 #include "ModelAPI_Session.h"
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>
20 #include <SketchPlugin_Feature.h>
21 #include <SketchPlugin_Line.h>
23 #include <XGUI_Workshop.h>
24 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_OperationMgr.h>
26 #include <XGUI_PropertyPanel.h>
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 myNoMoreWidgetsAttribute("")
38 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
42 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
44 ModuleBase_ModelWidget* aWidget = 0;
48 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
50 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
51 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
52 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
53 aWidget = myInternalActiveWidget;
58 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
60 return myIsInternalEditOperation;
63 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
65 bool aProcessed = false;
69 aProcessed = myIsInternalEditOperation;
75 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
83 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
91 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
92 QMouseEvent* /* theEvent*/)
94 bool aProcessed = false;
98 if (myIsInternalEditOperation) {
99 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
100 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
101 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
102 (myWorkshop->currentOperation());
103 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
108 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
109 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
110 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
111 QList<ModuleBase_ViewerPrs> aSelection;
112 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
113 if (aPoint2DWdg->setSelection(aSelection, true))
114 aPanel->activateNextWidget(aPoint2DWdg);
122 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
123 QMouseEvent* /* theEvent*/)
125 return isActiveMgr() && myIsInternalEditOperation;
128 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
129 QMouseEvent* theEvent)
131 bool aProcessed = false;
135 if (myIsInternalEditOperation) {
136 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
137 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
139 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
140 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
144 // fill the first widget by the mouse event point
145 // if the active widget is not the first, it means that the restarted operation is filled by
146 // the current preselection.
147 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
148 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
149 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
150 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
158 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
162 if (!myIsInternalEditOperation)
165 PartSet_Module* aModule = module();
166 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
167 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
168 if (aFirstWidget != aPanel->activeWidget()) {
169 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
171 aWSelector->activateSelectionAndFilters(true);
175 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
180 // we should avoid processing of the signal about no more widgets attributes and
181 // do this after the restart operaion is finished if it was called
182 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
183 // if it should be called after restart
184 if (myIsFlagsBlocked) {
185 myNoMoreWidgetsAttribute = thePreviousAttributeID;
189 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
190 (myWorkshop->currentOperation());
191 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
194 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
195 bool isStarted = false;
196 if (!module()->sketchMgr()->sketchSolverError()) {
197 if (myRestartingMode != RM_Forbided) {
198 myRestartingMode = RM_LastFeatureUsed;
199 isStarted = startInternalEdit(thePreviousAttributeID);
203 aFOperation->commit();
207 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
214 // empty previous attribute means that the Apply/Ok button has focus and the enter
215 // should not lead to start edition mode of the previous operation
216 if (thePreviousAttributeID.empty())
219 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
220 (myWorkshop->currentOperation());
221 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
224 bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
226 if (!isSketchSolverError) {
227 myRestartingMode = RM_EmptyFeatureUsed;
228 isDone = startInternalEdit(thePreviousAttributeID);
234 void PartSet_SketcherReetntrantMgr::onVertexSelected()
239 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
240 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
241 /// If last line finished on vertex the lines creation sequence has to be break
242 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
243 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
244 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
245 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
246 bool aFoundWidget = false;
247 bool aFoundObligatory = false;
248 for (; anIt != aLast && !aFoundObligatory; anIt++) {
250 aFoundWidget = *anIt == anActiveWidget;
252 aFoundObligatory = (*anIt)->isObligatory();
254 if (!aFoundObligatory)
255 myRestartingMode = RM_Forbided;
259 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
261 if (!isActiveMgr() || !myIsInternalEditOperation)
264 beforeStopInternalEdit();
267 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
269 return !isActiveMgr() || myRestartingMode == RM_None;
272 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
274 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
276 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
278 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
279 if (anActive) { // the manager is not active when the current operation is a usual Edit
280 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
281 (myWorkshop->currentOperation());
282 if (aFOperation->isEditOperation())
283 anActive = myIsInternalEditOperation;
289 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
292 /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
293 /// event comes two times, so we should not start another internal edit operation
294 /// the Apply button becomes disabled becase the second additional internal feature is created
295 if (myIsInternalEditOperation)
298 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
299 (myWorkshop->currentOperation());
301 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
302 aFOperation->setEditOperation(false);
303 workshop()->operationMgr()->updateApplyOfOperations();
305 createInternalFeature();
307 myIsInternalEditOperation = true;
309 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
310 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
312 // activate selection filters of the first widget in the viewer
315 // activate the last active widget in the Property Panel
316 if (!thePreviousAttributeID.empty()) {
317 ModuleBase_Operation* anEditOperation = module()->currentOperation();
318 if (anEditOperation) {
319 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
320 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
321 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
322 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
323 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
324 aPreviousAttributeWidget = aWidgets[i];
326 // If the current widget is a selector, do nothing, it processes the mouse press
327 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector()) {
328 aPreviousAttributeWidget->focusTo();
329 aPreviousAttributeWidget->selectContent();
335 module()->sketchMgr()->clearClickedFlags();
340 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
342 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
343 (myWorkshop->currentOperation());
345 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
346 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
349 deleteInternalFeature();
352 void PartSet_SketcherReetntrantMgr::restartOperation()
354 if (myIsInternalEditOperation) {
355 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
356 myWorkshop->currentOperation());
358 myNoMoreWidgetsAttribute = "";
359 myIsFlagsBlocked = true;
360 aFOperation->commit();
361 module()->launchOperation(aFOperation->id());
362 myIsFlagsBlocked = false;
364 // we should avoid processing of the signal about no more widgets attributes and
365 // do this after the restart operaion is finished if it was called
366 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
367 // if it should be called after restart
368 if (!myNoMoreWidgetsAttribute.empty()) {
369 onNoMoreWidgets(myNoMoreWidgetsAttribute);
370 myNoMoreWidgetsAttribute = "";
376 void PartSet_SketcherReetntrantMgr::createInternalFeature()
378 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
379 (myWorkshop->currentOperation());
381 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
382 FeaturePtr anOperationFeature = aFOperation->feature();
384 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
385 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
386 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
387 (aFOperation->propertyPanel());
389 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
390 myInternalWidget->setVisible(false);
392 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
394 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
395 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
397 aFactory.createWidget(anInternalPage);
398 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
400 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
401 bool isStoreValue = !aFOperation->isEditOperation() &&
402 !aWidget->getDefaultValue().empty() &&
403 !aWidget->isComputedDefault();
404 aWidget->setFeature(myInternalFeature, isStoreValue);
406 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
409 myInternalActiveWidget = aFirstWidget;
413 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
415 if (myInternalActiveWidget) {
416 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
418 aWSelector->activateSelectionAndFilters(false);
419 myInternalActiveWidget = 0;
421 delete myInternalWidget;
422 myInternalWidget = 0;
424 QObjectPtrList anObjects;
425 anObjects.append(myInternalFeature);
426 workshop()->deleteFeatures(anObjects);
429 void PartSet_SketcherReetntrantMgr::resetFlags()
431 if (!myIsFlagsBlocked) {
432 myIsInternalEditOperation = false;
433 myRestartingMode = RM_None;
437 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
439 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
440 return aConnector->workshop();
443 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
445 return dynamic_cast<PartSet_Module*>(myWorkshop->module());