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"
9 #include "ModelAPI_AttributeString.h"
10 #include "ModelAPI_AttributeRefAttr.h"
12 #include "GeomDataAPI_Point2D.h"
14 #include <ModuleBase_IPropertyPanel.h>
15 #include <ModuleBase_OperationFeature.h>
16 #include <ModuleBase_ModelWidget.h>
17 #include <ModuleBase_ViewerPrs.h>
18 #include <ModuleBase_WidgetSelector.h>
19 #include <ModuleBase_PageWidget.h>
20 #include <ModuleBase_PageBase.h>
21 #include <ModuleBase_WidgetFactory.h>
22 #include <ModuleBase_OperationDescription.h>
23 #include "ModuleBase_ToolBox.h"
25 #include <SketchPlugin_Feature.h>
26 #include <SketchPlugin_Line.h>
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_Circle.h>
30 #include <XGUI_Workshop.h>
31 #include <XGUI_ModuleConnector.h>
32 #include <XGUI_OperationMgr.h>
33 #include <XGUI_PropertyPanel.h>
34 #include <XGUI_ErrorMgr.h>
36 #include <QToolButton>
38 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
39 : QObject(theWorkshop),
40 myWorkshop(theWorkshop),
41 myRestartingMode(RM_None),
42 myIsFlagsBlocked(false),
43 myIsInternalEditOperation(false),
44 myNoMoreWidgetsAttribute("")
48 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
52 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
54 ModuleBase_ModelWidget* aWidget = 0;
58 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
60 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
61 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
62 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
63 aWidget = myInternalActiveWidget;
68 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
70 return myIsInternalEditOperation;
73 void PartSet_SketcherReetntrantMgr::updateInternalEditActiveState()
75 if (myIsInternalEditOperation) {
76 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
77 (myWorkshop->currentOperation());
79 FeaturePtr aFeature = aFOperation->feature();
80 QString anError = myWorkshop->module()->getFeatureError(aFeature);
81 // stop started internal edit operation as soon as the operation becomes invalid
82 // it is especially important for the sketch tangent arc feature
83 if (!anError.isEmpty()) {
84 aFOperation->setEditOperation(false);
85 //workshop()->operationMgr()->updateApplyOfOperations();
86 beforeStopInternalEdit();
87 myIsInternalEditOperation = false;
88 updateAcceptAllAction();
94 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
96 bool aProcessed = false;
100 aProcessed = myIsInternalEditOperation;
106 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
114 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
122 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
123 QMouseEvent* /* theEvent*/)
125 bool aProcessed = false;
129 if (myIsInternalEditOperation) {
130 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
131 (myWorkshop->currentOperation());
132 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature()
135 ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
136 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
137 bool aWidgetIsFilled = false;
139 //bool aCanBeActivatedByMove = false;
140 FeaturePtr aCurrentFeature = aFOperation->feature();
141 bool isLineFeature = false, isArcFeature = false;
142 if (aCurrentFeature->getKind() == SketchPlugin_Line::ID())
143 isLineFeature = anActiveWidget->attributeID() == SketchPlugin_Line::START_ID();
144 else if (isTangentArc(aFOperation))
145 isArcFeature = anActiveWidget->attributeID() == SketchPlugin_Arc::TANGENT_POINT_ID();
147 bool aCanBeActivatedByMove = isLineFeature || isArcFeature;
148 if (aCanBeActivatedByMove) {
151 anActiveWidget = module()->activeWidget();
152 aCurrentFeature = anActiveWidget->feature();
155 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
156 if (aPoint2DWdg) { // line, start point should be equal last point of the last feature line
157 QList<ModuleBase_ViewerPrsPtr> aSelection;
158 aSelection.append(std::shared_ptr<ModuleBase_ViewerPrs>(
159 new ModuleBase_ViewerPrs(aLastFeature, GeomShapePtr(), NULL)));
160 aWidgetIsFilled = aPoint2DWdg->setSelection(aSelection, true);
163 else if (isArcFeature) { // arc, start point should be equal last point of the last feature arc
164 if (aCurrentFeature->getKind() == SketchPlugin_Arc::ID()) {
165 // get the last point of the previuos arc feature(geom point 2d)
166 std::shared_ptr<ModelAPI_Data> aData = aLastFeature->data();
167 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
168 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
169 aData->attribute(SketchPlugin_Arc::END_ID()));
170 // get point attribute on the current feature
171 AttributeRefAttrPtr aTangentPointAttr = aCurrentFeature->data()->refattr(
172 SketchPlugin_Arc::TANGENT_POINT_ID());
173 aTangentPointAttr->setAttr(aPointAttr);
174 aWidgetIsFilled = true;
179 aPanel->activateNextWidget(anActiveWidget);
185 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
186 QMouseEvent* /* theEvent*/)
188 return isActiveMgr() && myIsInternalEditOperation;
191 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
192 QMouseEvent* theEvent)
194 bool aProcessed = false;
198 if (myIsInternalEditOperation) {
199 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
200 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
202 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
203 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
205 // block of viewer update
206 // we need to block update content of the viewer because of Sketch Point feature
207 // in activate() the value of the point is initialized and it can be displayed
208 // but the default value is [0, 0]. So, we block update viewer contentent until
209 // onMouseRelease happens, which correct the point position
210 ModuleBase_Tools::blockUpdateViewer(true);
215 // fill the first widget by the mouse event point
216 // if the active widget is not the first, it means that the restarted operation is filled by
217 // the current preselection.
218 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
219 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
220 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
221 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
223 // unblock viewer update
224 ModuleBase_Tools::blockUpdateViewer(false);
231 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
235 if (!myIsInternalEditOperation)
238 PartSet_Module* aModule = module();
239 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
240 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
241 if (aFirstWidget != aPanel->activeWidget()) {
242 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
244 aWSelector->activateSelectionAndFilters(true);
248 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
253 // we should avoid processing of the signal about no more widgets attributes and
254 // do this after the restart operaion is finished if it was called
255 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
256 // if it should be called after restart
257 if (myIsFlagsBlocked) {
258 myNoMoreWidgetsAttribute = thePreviousAttributeID;
262 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
263 (myWorkshop->currentOperation());
264 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
267 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
268 bool isStarted = false;
269 if (!module()->sketchMgr()->sketchSolverError()) {
270 if (myRestartingMode != RM_Forbided) {
271 myRestartingMode = RM_LastFeatureUsed;
272 isStarted = startInternalEdit(thePreviousAttributeID);
276 aFOperation->commit();
280 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
287 // empty previous attribute means that the Apply/Ok button has focus and the enter
288 // should not lead to start edition mode of the previous operation
289 if (thePreviousAttributeID.empty())
292 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
293 (myWorkshop->currentOperation());
294 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
297 bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
299 if (!isSketchSolverError) {
300 myRestartingMode = RM_EmptyFeatureUsed;
301 isDone = startInternalEdit(thePreviousAttributeID);
307 void PartSet_SketcherReetntrantMgr::onVertexSelected()
312 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
313 std::string anOperationId = anOperation->id().toStdString();
314 if (anOperationId == SketchPlugin_Line::ID() || isTangentArc(anOperation)) {
315 /// If last line finished on vertex the lines creation sequence has to be break
316 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
317 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
318 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
319 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
320 bool aFoundWidget = false;
321 bool aFoundObligatory = false;
322 for (; anIt != aLast && !aFoundObligatory; anIt++) {
324 aFoundWidget = *anIt == anActiveWidget;
326 aFoundObligatory = (*anIt)->isObligatory();
328 if (!aFoundObligatory)
329 myRestartingMode = RM_Forbided;
333 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
335 if (!isActiveMgr() || !myIsInternalEditOperation)
338 beforeStopInternalEdit();
341 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
343 return !isActiveMgr() || myRestartingMode == RM_None;
346 bool PartSet_SketcherReetntrantMgr::isInternalEditStarted() const
348 return myIsInternalEditOperation;
351 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
353 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
355 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
357 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
358 if (anActive) { // the manager is not active when the current operation is a usual Edit
359 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
360 (myWorkshop->currentOperation());
361 if (aFOperation->isEditOperation())
362 anActive = myIsInternalEditOperation;
368 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
371 /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
372 /// event comes two times, so we should not start another internal edit operation
373 /// the Apply button becomes disabled becase the second additional internal feature is created
374 if (myIsInternalEditOperation)
377 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
378 (myWorkshop->currentOperation());
380 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
381 aFOperation->setEditOperation(true/*, false*/);
382 createInternalFeature();
384 myIsInternalEditOperation = true;
385 updateAcceptAllAction();
388 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
389 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
391 // activate selection filters of the first widget in the viewer
394 // activate the last active widget in the Property Panel
395 if (!thePreviousAttributeID.empty()) {
396 ModuleBase_Operation* anEditOperation = module()->currentOperation();
397 if (anEditOperation) {
398 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
399 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
400 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
401 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
402 if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
403 /// workaround for the same attributes used in different stacked widgets(attribute types)
404 if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
406 aPreviousAttributeWidget = aWidgets[i];
409 // If the current widget is a selector, do nothing, it processes the mouse press
410 if (aPreviousAttributeWidget) {
411 if (!aPreviousAttributeWidget->isViewerSelector()) {
412 aPreviousAttributeWidget->focusTo();
413 aPreviousAttributeWidget->selectContent();
416 // in case of shape multi selector, the widget does not lose focus by filling
417 // like it is in shape selector. So, if enter is pressed, the multi shape selector
418 // control should be deactivated. The focus is moved to Apply button and there
419 // should not be active control visualized in property panel
420 if (aPreviousAttributeWidget == aPanel->activeWidget()) {
421 aPanel->activateWidget(NULL, false);
423 // if there is no the next widget to be automatically activated, the Ok button in property
424 // panel should accept the focus(example is parallel constraint on sketch lines)
425 QToolButton* anOkBtn = dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
427 anOkBtn->setFocus(Qt::TabFocusReason);
434 module()->sketchMgr()->clearClickedFlags();
439 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
441 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
442 (myWorkshop->currentOperation());
444 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
445 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
448 deleteInternalFeature();
451 void PartSet_SketcherReetntrantMgr::restartOperation()
453 if (myIsInternalEditOperation) {
454 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
455 myWorkshop->currentOperation());
457 myNoMoreWidgetsAttribute = "";
458 myIsFlagsBlocked = true;
459 FeaturePtr aPrevFeature = aFOperation->feature();
460 aFOperation->commit();
461 module()->launchOperation(aFOperation->id(), false);
462 // allow the same attribute values in restarted operation
463 ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
464 myWorkshop->currentOperation());
465 copyReetntrantAttributes(aPrevFeature, aCurrentOperation->feature());
467 // update property panel: it should be done because in launchOperation, the 'false' is given
468 workshop()->propertyPanel()->updateContentWidget(aCurrentOperation->feature());
469 workshop()->propertyPanel()->createContentPanel(aCurrentOperation->feature());
471 myIsFlagsBlocked = false;
473 // we should avoid processing of the signal about no more widgets attributes and
474 // do this after the restart operaion is finished if it was called
475 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
476 // if it should be called after restart
477 if (!myNoMoreWidgetsAttribute.empty()) {
478 onNoMoreWidgets(myNoMoreWidgetsAttribute);
479 myNoMoreWidgetsAttribute = "";
485 void PartSet_SketcherReetntrantMgr::createInternalFeature()
487 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
488 (myWorkshop->currentOperation());
490 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
491 FeaturePtr anOperationFeature = aFOperation->feature();
493 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
494 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
496 bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature);
497 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
498 (aFOperation->propertyPanel());
500 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
501 myInternalWidget->setVisible(false);
503 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
505 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
506 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
508 aFactory.createWidget(anInternalPage);
509 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
511 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
512 bool isStoreValue = !aFOperation->isEditOperation() &&
513 !aWidget->getDefaultValue().empty() &&
514 !aWidget->isComputedDefault();
515 aWidget->setFeature(myInternalFeature, isStoreValue);
516 if (!isStoreValue && isFeatureChanged)
517 aWidget->restoreValue();
520 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
523 myInternalActiveWidget = aFirstWidget;
527 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
529 if (myInternalActiveWidget) {
530 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
532 aWSelector->activateSelectionAndFilters(false);
533 myInternalActiveWidget = 0;
535 delete myInternalWidget;
536 myInternalWidget = 0;
538 QObjectPtrList anObjects;
539 anObjects.append(myInternalFeature);
540 workshop()->deleteFeatures(anObjects);
543 void PartSet_SketcherReetntrantMgr::resetFlags()
545 if (!myIsFlagsBlocked) {
546 myIsInternalEditOperation = false;
547 updateAcceptAllAction();
548 myRestartingMode = RM_None;
552 bool PartSet_SketcherReetntrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
553 const FeaturePtr& theNewFeature)
555 bool aChanged = false;
556 std::string aTypeAttributeId;
557 if (theSourceFeature->getKind() == SketchPlugin_Circle::ID()) {
558 aTypeAttributeId = SketchPlugin_Circle::CIRCLE_TYPE();
560 if (theSourceFeature->getKind() == SketchPlugin_Arc::ID()) {
561 aTypeAttributeId = SketchPlugin_Arc::ARC_TYPE();
563 if (!aTypeAttributeId.empty()) {
564 AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
565 AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
566 aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
567 ModuleBase_Tools::flushUpdated(theNewFeature);
573 bool PartSet_SketcherReetntrantMgr::isTangentArc(ModuleBase_Operation* theOperation)
575 bool aTangentArc = false;
576 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
578 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
579 FeaturePtr aFeature = aFOperation->feature();
580 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Arc::ID()) {
581 AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_Arc::ARC_TYPE());
582 std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
583 aTangentArc = anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT();
589 void PartSet_SketcherReetntrantMgr::updateAcceptAllAction()
591 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
593 workshop()->errorMgr()->updateAcceptAllAction(aSketch);
596 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
598 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
599 return aConnector->workshop();
602 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
604 return dynamic_cast<PartSet_Module*>(myWorkshop->module());