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 #include <QToolButton>
30 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
31 : QObject(theWorkshop),
32 myWorkshop(theWorkshop),
33 myRestartingMode(RM_None),
34 myIsFlagsBlocked(false),
35 myIsInternalEditOperation(false),
36 myNoMoreWidgetsAttribute("")
40 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
44 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
46 ModuleBase_ModelWidget* aWidget = 0;
50 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
52 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
53 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
54 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
55 aWidget = myInternalActiveWidget;
60 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
62 return myIsInternalEditOperation;
65 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
67 bool aProcessed = false;
71 aProcessed = myIsInternalEditOperation;
77 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
85 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
93 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
94 QMouseEvent* /* theEvent*/)
96 bool aProcessed = false;
100 if (myIsInternalEditOperation) {
101 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
102 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
103 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
104 (myWorkshop->currentOperation());
105 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
110 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
111 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
112 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
113 QList<ModuleBase_ViewerPrs> aSelection;
114 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
115 if (aPoint2DWdg->setSelection(aSelection, true))
116 aPanel->activateNextWidget(aPoint2DWdg);
124 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
125 QMouseEvent* /* theEvent*/)
127 return isActiveMgr() && myIsInternalEditOperation;
130 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
131 QMouseEvent* theEvent)
133 bool aProcessed = false;
137 if (myIsInternalEditOperation) {
138 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
139 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
141 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
142 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
146 // fill the first widget by the mouse event point
147 // if the active widget is not the first, it means that the restarted operation is filled by
148 // the current preselection.
149 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
150 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
151 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
152 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
160 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
164 if (!myIsInternalEditOperation)
167 PartSet_Module* aModule = module();
168 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
169 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
170 if (aFirstWidget != aPanel->activeWidget()) {
171 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
173 aWSelector->activateSelectionAndFilters(true);
177 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
182 // we should avoid processing of the signal about no more widgets attributes and
183 // do this after the restart operaion is finished if it was called
184 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
185 // if it should be called after restart
186 if (myIsFlagsBlocked) {
187 myNoMoreWidgetsAttribute = thePreviousAttributeID;
191 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
192 (myWorkshop->currentOperation());
193 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
196 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
197 bool isStarted = false;
198 if (!module()->sketchMgr()->sketchSolverError()) {
199 if (myRestartingMode != RM_Forbided) {
200 myRestartingMode = RM_LastFeatureUsed;
201 isStarted = startInternalEdit(thePreviousAttributeID);
205 aFOperation->commit();
209 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
216 // empty previous attribute means that the Apply/Ok button has focus and the enter
217 // should not lead to start edition mode of the previous operation
218 if (thePreviousAttributeID.empty())
221 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
222 (myWorkshop->currentOperation());
223 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
226 bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
228 if (!isSketchSolverError) {
229 myRestartingMode = RM_EmptyFeatureUsed;
230 isDone = startInternalEdit(thePreviousAttributeID);
236 void PartSet_SketcherReetntrantMgr::onVertexSelected()
241 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
242 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
243 /// If last line finished on vertex the lines creation sequence has to be break
244 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
245 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
246 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
247 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
248 bool aFoundWidget = false;
249 bool aFoundObligatory = false;
250 for (; anIt != aLast && !aFoundObligatory; anIt++) {
252 aFoundWidget = *anIt == anActiveWidget;
254 aFoundObligatory = (*anIt)->isObligatory();
256 if (!aFoundObligatory)
257 myRestartingMode = RM_Forbided;
261 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
263 if (!isActiveMgr() || !myIsInternalEditOperation)
266 beforeStopInternalEdit();
269 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
271 return !isActiveMgr() || myRestartingMode == RM_None;
274 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
276 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
278 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
280 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
281 if (anActive) { // the manager is not active when the current operation is a usual Edit
282 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
283 (myWorkshop->currentOperation());
284 if (aFOperation->isEditOperation())
285 anActive = myIsInternalEditOperation;
291 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
294 /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
295 /// event comes two times, so we should not start another internal edit operation
296 /// the Apply button becomes disabled becase the second additional internal feature is created
297 if (myIsInternalEditOperation)
300 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
301 (myWorkshop->currentOperation());
303 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
304 aFOperation->setEditOperation(false);
305 workshop()->operationMgr()->updateApplyOfOperations();
307 createInternalFeature();
309 myIsInternalEditOperation = true;
311 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
312 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
314 // activate selection filters of the first widget in the viewer
317 // activate the last active widget in the Property Panel
318 if (!thePreviousAttributeID.empty()) {
319 ModuleBase_Operation* anEditOperation = module()->currentOperation();
320 if (anEditOperation) {
321 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
322 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
323 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
324 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
325 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
326 aPreviousAttributeWidget = aWidgets[i];
328 // If the current widget is a selector, do nothing, it processes the mouse press
329 if (aPreviousAttributeWidget) {
330 if (!aPreviousAttributeWidget->isViewerSelector()) {
331 aPreviousAttributeWidget->focusTo();
332 aPreviousAttributeWidget->selectContent();
335 // in case of shape multi selector, the widget does not lose focus by filling
336 // like it is in shape selector. So, if enter is pressed, the multi shape selector
337 // control should be deactivated. The focus is moved to Apply button and there
338 // should not be active control visualized in property panel
339 if (aPreviousAttributeWidget == aPanel->activeWidget()) {
340 aPanel->activateWidget(NULL, false);
342 // if there is no the next widget to be automatically activated, the Ok button in property
343 // panel should accept the focus(example is parallel constraint on sketch lines)
344 QToolButton* anOkBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_OK);
346 anOkBtn->setFocus(Qt::TabFocusReason);
353 module()->sketchMgr()->clearClickedFlags();
358 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
360 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
361 (myWorkshop->currentOperation());
363 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
364 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
367 deleteInternalFeature();
370 void PartSet_SketcherReetntrantMgr::restartOperation()
372 if (myIsInternalEditOperation) {
373 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
374 myWorkshop->currentOperation());
376 myNoMoreWidgetsAttribute = "";
377 myIsFlagsBlocked = true;
378 aFOperation->commit();
379 module()->launchOperation(aFOperation->id());
380 myIsFlagsBlocked = false;
382 // we should avoid processing of the signal about no more widgets attributes and
383 // do this after the restart operaion is finished if it was called
384 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
385 // if it should be called after restart
386 if (!myNoMoreWidgetsAttribute.empty()) {
387 onNoMoreWidgets(myNoMoreWidgetsAttribute);
388 myNoMoreWidgetsAttribute = "";
394 void PartSet_SketcherReetntrantMgr::createInternalFeature()
396 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
397 (myWorkshop->currentOperation());
399 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
400 FeaturePtr anOperationFeature = aFOperation->feature();
402 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
403 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
404 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
405 (aFOperation->propertyPanel());
407 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
408 myInternalWidget->setVisible(false);
410 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
412 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
413 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
415 aFactory.createWidget(anInternalPage);
416 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
418 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
419 bool isStoreValue = !aFOperation->isEditOperation() &&
420 !aWidget->getDefaultValue().empty() &&
421 !aWidget->isComputedDefault();
422 aWidget->setFeature(myInternalFeature, isStoreValue);
424 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
427 myInternalActiveWidget = aFirstWidget;
431 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
433 if (myInternalActiveWidget) {
434 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
436 aWSelector->activateSelectionAndFilters(false);
437 myInternalActiveWidget = 0;
439 delete myInternalWidget;
440 myInternalWidget = 0;
442 QObjectPtrList anObjects;
443 anObjects.append(myInternalFeature);
444 workshop()->deleteFeatures(anObjects);
447 void PartSet_SketcherReetntrantMgr::resetFlags()
449 if (!myIsFlagsBlocked) {
450 myIsInternalEditOperation = false;
451 myRestartingMode = RM_None;
455 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
457 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
458 return aConnector->workshop();
461 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
463 return dynamic_cast<PartSet_Module*>(myWorkshop->module());