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>
35 #include <QToolButton>
37 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
38 : QObject(theWorkshop),
39 myWorkshop(theWorkshop),
40 myRestartingMode(RM_None),
41 myIsFlagsBlocked(false),
42 myIsInternalEditOperation(false),
43 myNoMoreWidgetsAttribute("")
47 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
51 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
53 ModuleBase_ModelWidget* aWidget = 0;
57 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
59 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
60 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
61 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
62 aWidget = myInternalActiveWidget;
67 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
69 return myIsInternalEditOperation;
72 void PartSet_SketcherReetntrantMgr::updateInternalEditActiveState()
74 if (myIsInternalEditOperation) {
75 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
76 (myWorkshop->currentOperation());
78 FeaturePtr aFeature = aFOperation->feature();
79 QString anError = myWorkshop->module()->getFeatureError(aFeature);
80 // stop started internal edit operation as soon as the operation becomes invalid
81 // it is especially important for the sketch tangent arc feature
82 if (!anError.isEmpty()) {
83 aFOperation->setEditOperation(false);
84 //workshop()->operationMgr()->updateApplyOfOperations();
85 beforeStopInternalEdit();
86 myIsInternalEditOperation = false;
92 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
94 bool aProcessed = false;
98 aProcessed = myIsInternalEditOperation;
104 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
112 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
120 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
121 QMouseEvent* /* theEvent*/)
123 bool aProcessed = false;
127 if (myIsInternalEditOperation) {
128 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
129 (myWorkshop->currentOperation());
130 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature()
133 ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
134 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
135 bool aWidgetIsFilled = false;
137 //bool aCanBeActivatedByMove = false;
138 FeaturePtr aCurrentFeature = aFOperation->feature();
139 bool isLineFeature = false, isArcFeature = false;
140 if (aCurrentFeature->getKind() == SketchPlugin_Line::ID())
141 isLineFeature = anActiveWidget->attributeID() == SketchPlugin_Line::START_ID();
142 else if (isTangentArc(aFOperation))
143 isArcFeature = anActiveWidget->attributeID() == SketchPlugin_Arc::TANGENT_POINT_ID();
145 bool aCanBeActivatedByMove = isLineFeature || isArcFeature;
146 if (aCanBeActivatedByMove) {
149 anActiveWidget = module()->activeWidget();
150 aCurrentFeature = anActiveWidget->feature();
153 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
154 if (aPoint2DWdg) { // line, start point should be equal last point of the last feature line
155 QList<ModuleBase_ViewerPrsPtr> aSelection;
156 aSelection.append(std::shared_ptr<ModuleBase_ViewerPrs>(
157 new ModuleBase_ViewerPrs(aLastFeature, GeomShapePtr(), NULL)));
158 aWidgetIsFilled = aPoint2DWdg->setSelection(aSelection, true);
161 else if (isArcFeature) { // arc, start point should be equal last point of the last feature arc
162 if (aCurrentFeature->getKind() == SketchPlugin_Arc::ID()) {
163 // get the last point of the previuos arc feature(geom point 2d)
164 std::shared_ptr<ModelAPI_Data> aData = aLastFeature->data();
165 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
166 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
167 aData->attribute(SketchPlugin_Arc::END_ID()));
168 // get point attribute on the current feature
169 AttributeRefAttrPtr aTangentPointAttr = aCurrentFeature->data()->refattr(
170 SketchPlugin_Arc::TANGENT_POINT_ID());
171 aTangentPointAttr->setAttr(aPointAttr);
172 aWidgetIsFilled = true;
177 aPanel->activateNextWidget(anActiveWidget);
183 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
184 QMouseEvent* /* theEvent*/)
186 return isActiveMgr() && myIsInternalEditOperation;
189 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
190 QMouseEvent* theEvent)
192 bool aProcessed = false;
196 if (myIsInternalEditOperation) {
197 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
198 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
200 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
201 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
203 // block of viewer update
204 // we need to block update content of the viewer because of Sketch Point feature
205 // in activate() the value of the point is initialized and it can be displayed
206 // but the default value is [0, 0]. So, we block update viewer contentent until
207 // onMouseRelease happens, which correct the point position
208 ModuleBase_Tools::blockUpdateViewer(true);
213 // fill the first widget by the mouse event point
214 // if the active widget is not the first, it means that the restarted operation is filled by
215 // the current preselection.
216 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
217 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
218 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
219 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
221 // unblock viewer update
222 ModuleBase_Tools::blockUpdateViewer(false);
229 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
233 if (!myIsInternalEditOperation)
236 PartSet_Module* aModule = module();
237 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
238 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
239 if (aFirstWidget != aPanel->activeWidget()) {
240 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
242 aWSelector->activateSelectionAndFilters(true);
246 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
251 // we should avoid processing of the signal about no more widgets attributes and
252 // do this after the restart operaion is finished if it was called
253 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
254 // if it should be called after restart
255 if (myIsFlagsBlocked) {
256 myNoMoreWidgetsAttribute = thePreviousAttributeID;
260 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
261 (myWorkshop->currentOperation());
262 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
265 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
266 bool isStarted = false;
267 if (!module()->sketchMgr()->sketchSolverError()) {
268 if (myRestartingMode != RM_Forbided) {
269 myRestartingMode = RM_LastFeatureUsed;
270 isStarted = startInternalEdit(thePreviousAttributeID);
274 aFOperation->commit();
278 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
285 // empty previous attribute means that the Apply/Ok button has focus and the enter
286 // should not lead to start edition mode of the previous operation
287 if (thePreviousAttributeID.empty())
290 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
291 (myWorkshop->currentOperation());
292 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
295 bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
297 if (!isSketchSolverError) {
298 myRestartingMode = RM_EmptyFeatureUsed;
299 isDone = startInternalEdit(thePreviousAttributeID);
305 void PartSet_SketcherReetntrantMgr::onVertexSelected()
310 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
311 std::string anOperationId = anOperation->id().toStdString();
312 if (anOperationId == SketchPlugin_Line::ID() || isTangentArc(anOperation)) {
313 /// If last line finished on vertex the lines creation sequence has to be break
314 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
315 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
316 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
317 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
318 bool aFoundWidget = false;
319 bool aFoundObligatory = false;
320 for (; anIt != aLast && !aFoundObligatory; anIt++) {
322 aFoundWidget = *anIt == anActiveWidget;
324 aFoundObligatory = (*anIt)->isObligatory();
326 if (!aFoundObligatory)
327 myRestartingMode = RM_Forbided;
331 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
333 if (!isActiveMgr() || !myIsInternalEditOperation)
336 beforeStopInternalEdit();
339 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
341 return !isActiveMgr() || myRestartingMode == RM_None;
344 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
346 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
348 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
350 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
351 if (anActive) { // the manager is not active when the current operation is a usual Edit
352 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
353 (myWorkshop->currentOperation());
354 if (aFOperation->isEditOperation())
355 anActive = myIsInternalEditOperation;
361 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
364 /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
365 /// event comes two times, so we should not start another internal edit operation
366 /// the Apply button becomes disabled becase the second additional internal feature is created
367 if (myIsInternalEditOperation)
370 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
371 (myWorkshop->currentOperation());
373 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
374 aFOperation->setEditOperation(true/*, false*/);
375 workshop()->operationMgr()->updateApplyOfOperations();
377 createInternalFeature();
379 myIsInternalEditOperation = true;
381 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
382 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
384 // activate selection filters of the first widget in the viewer
387 // activate the last active widget in the Property Panel
388 if (!thePreviousAttributeID.empty()) {
389 ModuleBase_Operation* anEditOperation = module()->currentOperation();
390 if (anEditOperation) {
391 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
392 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
393 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
394 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
395 if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
396 /// workaround for the same attributes used in different stacked widgets(attribute types)
397 if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
399 aPreviousAttributeWidget = aWidgets[i];
402 // If the current widget is a selector, do nothing, it processes the mouse press
403 if (aPreviousAttributeWidget) {
404 if (!aPreviousAttributeWidget->isViewerSelector()) {
405 aPreviousAttributeWidget->focusTo();
406 aPreviousAttributeWidget->selectContent();
409 // in case of shape multi selector, the widget does not lose focus by filling
410 // like it is in shape selector. So, if enter is pressed, the multi shape selector
411 // control should be deactivated. The focus is moved to Apply button and there
412 // should not be active control visualized in property panel
413 if (aPreviousAttributeWidget == aPanel->activeWidget()) {
414 aPanel->activateWidget(NULL, false);
416 // if there is no the next widget to be automatically activated, the Ok button in property
417 // panel should accept the focus(example is parallel constraint on sketch lines)
418 QToolButton* anOkBtn = dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
420 anOkBtn->setFocus(Qt::TabFocusReason);
427 module()->sketchMgr()->clearClickedFlags();
432 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
434 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
435 (myWorkshop->currentOperation());
437 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
438 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
441 deleteInternalFeature();
444 void PartSet_SketcherReetntrantMgr::restartOperation()
446 if (myIsInternalEditOperation) {
447 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
448 myWorkshop->currentOperation());
450 myNoMoreWidgetsAttribute = "";
451 myIsFlagsBlocked = true;
452 FeaturePtr aPrevFeature = aFOperation->feature();
453 aFOperation->commit();
454 module()->launchOperation(aFOperation->id(), false);
455 // allow the same attribute values in restarted operation
456 ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
457 myWorkshop->currentOperation());
458 copyReetntrantAttributes(aPrevFeature, aCurrentOperation->feature());
460 // update property panel: it should be done because in launchOperation, the 'false' is given
461 workshop()->propertyPanel()->updateContentWidget(aCurrentOperation->feature());
462 workshop()->propertyPanel()->createContentPanel(aCurrentOperation->feature());
464 myIsFlagsBlocked = false;
466 // we should avoid processing of the signal about no more widgets attributes and
467 // do this after the restart operaion is finished if it was called
468 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
469 // if it should be called after restart
470 if (!myNoMoreWidgetsAttribute.empty()) {
471 onNoMoreWidgets(myNoMoreWidgetsAttribute);
472 myNoMoreWidgetsAttribute = "";
478 void PartSet_SketcherReetntrantMgr::createInternalFeature()
480 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
481 (myWorkshop->currentOperation());
483 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
484 FeaturePtr anOperationFeature = aFOperation->feature();
486 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
487 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
489 bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature);
490 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
491 (aFOperation->propertyPanel());
493 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
494 myInternalWidget->setVisible(false);
496 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
498 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
499 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
501 aFactory.createWidget(anInternalPage);
502 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
504 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
505 bool isStoreValue = !aFOperation->isEditOperation() &&
506 !aWidget->getDefaultValue().empty() &&
507 !aWidget->isComputedDefault();
508 aWidget->setFeature(myInternalFeature, isStoreValue);
509 if (!isStoreValue && isFeatureChanged)
510 aWidget->restoreValue();
513 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
516 myInternalActiveWidget = aFirstWidget;
520 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
522 if (myInternalActiveWidget) {
523 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
525 aWSelector->activateSelectionAndFilters(false);
526 myInternalActiveWidget = 0;
528 delete myInternalWidget;
529 myInternalWidget = 0;
531 QObjectPtrList anObjects;
532 anObjects.append(myInternalFeature);
533 workshop()->deleteFeatures(anObjects);
536 void PartSet_SketcherReetntrantMgr::resetFlags()
538 if (!myIsFlagsBlocked) {
539 myIsInternalEditOperation = false;
540 myRestartingMode = RM_None;
544 bool PartSet_SketcherReetntrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
545 const FeaturePtr& theNewFeature)
547 bool aChanged = false;
548 std::string aTypeAttributeId;
549 if (theSourceFeature->getKind() == SketchPlugin_Circle::ID()) {
550 aTypeAttributeId = SketchPlugin_Circle::CIRCLE_TYPE();
552 if (theSourceFeature->getKind() == SketchPlugin_Arc::ID()) {
553 aTypeAttributeId = SketchPlugin_Arc::ARC_TYPE();
555 if (!aTypeAttributeId.empty()) {
556 AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
557 AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
558 aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
559 ModuleBase_Tools::flushUpdated(theNewFeature);
565 bool PartSet_SketcherReetntrantMgr::isTangentArc(ModuleBase_Operation* theOperation)
567 bool aTangentArc = false;
568 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
570 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
571 FeaturePtr aFeature = aFOperation->feature();
572 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Arc::ID()) {
573 AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_Arc::ARC_TYPE());
574 std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
575 aTangentArc = anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT();
581 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
583 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
584 return aConnector->workshop();
587 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
589 return dynamic_cast<PartSet_Module*>(myWorkshop->module());