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 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
74 bool aProcessed = false;
78 aProcessed = myIsInternalEditOperation;
84 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
92 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
100 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
101 QMouseEvent* /* theEvent*/)
103 bool aProcessed = false;
107 if (myIsInternalEditOperation) {
108 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
109 (myWorkshop->currentOperation());
110 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature()
113 ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
114 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
115 bool aWidgetIsFilled = false;
117 //bool aCanBeActivatedByMove = false;
118 FeaturePtr aCurrentFeature = anActiveWidget->feature();
119 bool isLineFeature = false, isArcFeature = false;
120 if (aCurrentFeature->getKind() == SketchPlugin_Line::ID())
121 isLineFeature = anActiveWidget->attributeID() == SketchPlugin_Line::START_ID();
122 else if (isTangentArc(aFOperation))
123 isArcFeature = anActiveWidget->attributeID() == SketchPlugin_Arc::TANGENT_POINT_ID();
125 bool aCanBeActivatedByMove = isLineFeature || isArcFeature;
126 if (aCanBeActivatedByMove) {
129 anActiveWidget = module()->activeWidget();
130 aCurrentFeature = anActiveWidget->feature();
133 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
134 if (aPoint2DWdg) { // line, start point should be equal last point of the last feature line
135 QList<ModuleBase_ViewerPrs> aSelection;
136 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
137 aWidgetIsFilled = aPoint2DWdg->setSelection(aSelection, true);
140 else if (isArcFeature) { // arc, start point should be equal last point of the last feature arc
141 if (aCurrentFeature->getKind() == SketchPlugin_Arc::ID()) {
142 // get the last point of the previuos arc feature(geom point 2d)
143 std::shared_ptr<ModelAPI_Data> aData = aLastFeature->data();
144 std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
145 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
146 aData->attribute(SketchPlugin_Arc::END_ID()));
147 // get point attribute on the current feature
148 AttributeRefAttrPtr aTangentPointAttr = aCurrentFeature->data()->refattr(
149 SketchPlugin_Arc::TANGENT_POINT_ID());
150 aTangentPointAttr->setAttr(aPointAttr);
151 aWidgetIsFilled = true;
156 aPanel->activateNextWidget(anActiveWidget);
162 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
163 QMouseEvent* /* theEvent*/)
165 return isActiveMgr() && myIsInternalEditOperation;
168 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
169 QMouseEvent* theEvent)
171 bool aProcessed = false;
175 if (myIsInternalEditOperation) {
176 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
177 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
179 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
180 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
182 // block of viewer update
183 // we need to block update content of the viewer because of Sketch Point feature
184 // in activate() the value of the point is initialized and it can be displayed
185 // but the default value is [0, 0]. So, we block update viewer contentent until
186 // onMouseRelease happens, which correct the point position
187 ModuleBase_ModelWidget::blockUpdateViewer(true);
192 // fill the first widget by the mouse event point
193 // if the active widget is not the first, it means that the restarted operation is filled by
194 // the current preselection.
195 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
196 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
197 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
198 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
200 // unblock viewer update
201 ModuleBase_ModelWidget::blockUpdateViewer(false);
208 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
212 if (!myIsInternalEditOperation)
215 PartSet_Module* aModule = module();
216 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
217 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
218 if (aFirstWidget != aPanel->activeWidget()) {
219 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
221 aWSelector->activateSelectionAndFilters(true);
225 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
230 // we should avoid processing of the signal about no more widgets attributes and
231 // do this after the restart operaion is finished if it was called
232 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
233 // if it should be called after restart
234 if (myIsFlagsBlocked) {
235 myNoMoreWidgetsAttribute = thePreviousAttributeID;
239 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
240 (myWorkshop->currentOperation());
241 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
244 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
245 bool isStarted = false;
246 if (!module()->sketchMgr()->sketchSolverError()) {
247 if (myRestartingMode != RM_Forbided) {
248 myRestartingMode = RM_LastFeatureUsed;
249 isStarted = startInternalEdit(thePreviousAttributeID);
253 aFOperation->commit();
257 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
264 // empty previous attribute means that the Apply/Ok button has focus and the enter
265 // should not lead to start edition mode of the previous operation
266 if (thePreviousAttributeID.empty())
269 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
270 (myWorkshop->currentOperation());
271 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
274 bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
276 if (!isSketchSolverError) {
277 myRestartingMode = RM_EmptyFeatureUsed;
278 isDone = startInternalEdit(thePreviousAttributeID);
284 void PartSet_SketcherReetntrantMgr::onVertexSelected()
289 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
290 std::string anOperationId = anOperation->id().toStdString();
291 if (anOperationId == SketchPlugin_Line::ID()/* || isTangentArc(anOperation)*/) {
292 /// If last line finished on vertex the lines creation sequence has to be break
293 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
294 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
295 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
296 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
297 bool aFoundWidget = false;
298 bool aFoundObligatory = false;
299 for (; anIt != aLast && !aFoundObligatory; anIt++) {
301 aFoundWidget = *anIt == anActiveWidget;
303 aFoundObligatory = (*anIt)->isObligatory();
305 if (!aFoundObligatory)
306 myRestartingMode = RM_Forbided;
310 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
312 if (!isActiveMgr() || !myIsInternalEditOperation)
315 beforeStopInternalEdit();
318 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
320 return !isActiveMgr() || myRestartingMode == RM_None;
323 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
325 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
327 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
329 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
330 if (anActive) { // the manager is not active when the current operation is a usual Edit
331 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
332 (myWorkshop->currentOperation());
333 if (aFOperation->isEditOperation())
334 anActive = myIsInternalEditOperation;
340 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
343 /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
344 /// event comes two times, so we should not start another internal edit operation
345 /// the Apply button becomes disabled becase the second additional internal feature is created
346 if (myIsInternalEditOperation)
349 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
350 (myWorkshop->currentOperation());
352 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
353 aFOperation->setEditOperation(false);
354 workshop()->operationMgr()->updateApplyOfOperations();
356 createInternalFeature();
358 myIsInternalEditOperation = true;
360 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
361 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
363 // activate selection filters of the first widget in the viewer
366 // activate the last active widget in the Property Panel
367 if (!thePreviousAttributeID.empty()) {
368 ModuleBase_Operation* anEditOperation = module()->currentOperation();
369 if (anEditOperation) {
370 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
371 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
372 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
373 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
374 if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
375 /// workaround for the same attributes used in different stacked widgets(attribute types)
376 if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
378 aPreviousAttributeWidget = aWidgets[i];
381 // If the current widget is a selector, do nothing, it processes the mouse press
382 if (aPreviousAttributeWidget) {
383 if (!aPreviousAttributeWidget->isViewerSelector()) {
384 aPreviousAttributeWidget->focusTo();
385 aPreviousAttributeWidget->selectContent();
388 // in case of shape multi selector, the widget does not lose focus by filling
389 // like it is in shape selector. So, if enter is pressed, the multi shape selector
390 // control should be deactivated. The focus is moved to Apply button and there
391 // should not be active control visualized in property panel
392 if (aPreviousAttributeWidget == aPanel->activeWidget()) {
393 aPanel->activateWidget(NULL, false);
395 // if there is no the next widget to be automatically activated, the Ok button in property
396 // panel should accept the focus(example is parallel constraint on sketch lines)
397 QToolButton* anOkBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_OK);
399 anOkBtn->setFocus(Qt::TabFocusReason);
406 module()->sketchMgr()->clearClickedFlags();
411 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
413 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
414 (myWorkshop->currentOperation());
416 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
417 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
420 deleteInternalFeature();
423 void PartSet_SketcherReetntrantMgr::restartOperation()
425 if (myIsInternalEditOperation) {
426 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
427 myWorkshop->currentOperation());
429 myNoMoreWidgetsAttribute = "";
430 myIsFlagsBlocked = true;
431 FeaturePtr aPrevFeature = aFOperation->feature();
432 aFOperation->commit();
433 module()->launchOperation(aFOperation->id());
434 // allow the same attribute values in restarted operation
435 ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
436 myWorkshop->currentOperation());
437 copyReetntrantAttributes(aPrevFeature, aCurrentOperation->feature());
439 myIsFlagsBlocked = false;
441 // we should avoid processing of the signal about no more widgets attributes and
442 // do this after the restart operaion is finished if it was called
443 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
444 // if it should be called after restart
445 if (!myNoMoreWidgetsAttribute.empty()) {
446 onNoMoreWidgets(myNoMoreWidgetsAttribute);
447 myNoMoreWidgetsAttribute = "";
453 void PartSet_SketcherReetntrantMgr::createInternalFeature()
455 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
456 (myWorkshop->currentOperation());
458 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
459 FeaturePtr anOperationFeature = aFOperation->feature();
461 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
462 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
464 bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature);
465 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
466 (aFOperation->propertyPanel());
468 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
469 myInternalWidget->setVisible(false);
471 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
473 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
474 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
476 aFactory.createWidget(anInternalPage);
477 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
479 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
480 bool isStoreValue = !aFOperation->isEditOperation() &&
481 !aWidget->getDefaultValue().empty() &&
482 !aWidget->isComputedDefault();
483 aWidget->setFeature(myInternalFeature, isStoreValue);
484 if (!isStoreValue && isFeatureChanged)
485 aWidget->restoreValue();
488 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
491 myInternalActiveWidget = aFirstWidget;
495 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
497 if (myInternalActiveWidget) {
498 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
500 aWSelector->activateSelectionAndFilters(false);
501 myInternalActiveWidget = 0;
503 delete myInternalWidget;
504 myInternalWidget = 0;
506 QObjectPtrList anObjects;
507 anObjects.append(myInternalFeature);
508 workshop()->deleteFeatures(anObjects);
511 void PartSet_SketcherReetntrantMgr::resetFlags()
513 if (!myIsFlagsBlocked) {
514 myIsInternalEditOperation = false;
515 myRestartingMode = RM_None;
519 bool PartSet_SketcherReetntrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
520 const FeaturePtr& theNewFeature)
522 bool aChanged = false;
523 std::string aTypeAttributeId;
524 if (theSourceFeature->getKind() == SketchPlugin_Circle::ID()) {
525 aTypeAttributeId = SketchPlugin_Circle::CIRCLE_TYPE();
527 if (theSourceFeature->getKind() == SketchPlugin_Arc::ID()) {
528 aTypeAttributeId = SketchPlugin_Arc::ARC_TYPE();
530 if (!aTypeAttributeId.empty()) {
531 AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
532 AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
533 aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
534 ModuleBase_ModelWidget::updateObject(theNewFeature);
540 bool PartSet_SketcherReetntrantMgr::isTangentArc(ModuleBase_Operation* theOperation)
542 bool aTangentArc = false;
543 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
545 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
546 FeaturePtr aFeature = aFOperation->feature();
547 if (aFeature.get() && aFeature->getKind() == SketchPlugin_Arc::ID()) {
548 AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_Arc::ARC_TYPE());
549 std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
550 aTangentArc = anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT();
556 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
558 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
559 return aConnector->workshop();
562 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
564 return dynamic_cast<PartSet_Module*>(myWorkshop->module());