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 // if there is no the next widget to be automatically activated, the Ok button in property
336 // panel should accept the focus(example is parallel constraint on sketch lines)
337 QToolButton* anOkBtn = aPanel->findChild<QToolButton*>(PROP_PANEL_OK);
339 anOkBtn->setFocus(Qt::TabFocusReason);
346 module()->sketchMgr()->clearClickedFlags();
351 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
353 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
354 (myWorkshop->currentOperation());
356 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
357 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
360 deleteInternalFeature();
363 void PartSet_SketcherReetntrantMgr::restartOperation()
365 if (myIsInternalEditOperation) {
366 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
367 myWorkshop->currentOperation());
369 myNoMoreWidgetsAttribute = "";
370 myIsFlagsBlocked = true;
371 aFOperation->commit();
372 module()->launchOperation(aFOperation->id());
373 myIsFlagsBlocked = false;
375 // we should avoid processing of the signal about no more widgets attributes and
376 // do this after the restart operaion is finished if it was called
377 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
378 // if it should be called after restart
379 if (!myNoMoreWidgetsAttribute.empty()) {
380 onNoMoreWidgets(myNoMoreWidgetsAttribute);
381 myNoMoreWidgetsAttribute = "";
387 void PartSet_SketcherReetntrantMgr::createInternalFeature()
389 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
390 (myWorkshop->currentOperation());
392 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
393 FeaturePtr anOperationFeature = aFOperation->feature();
395 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
396 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
397 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
398 (aFOperation->propertyPanel());
400 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
401 myInternalWidget->setVisible(false);
403 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
405 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
406 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
408 aFactory.createWidget(anInternalPage);
409 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
411 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
412 bool isStoreValue = !aFOperation->isEditOperation() &&
413 !aWidget->getDefaultValue().empty() &&
414 !aWidget->isComputedDefault();
415 aWidget->setFeature(myInternalFeature, isStoreValue);
417 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
420 myInternalActiveWidget = aFirstWidget;
424 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
426 if (myInternalActiveWidget) {
427 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
429 aWSelector->activateSelectionAndFilters(false);
430 myInternalActiveWidget = 0;
432 delete myInternalWidget;
433 myInternalWidget = 0;
435 QObjectPtrList anObjects;
436 anObjects.append(myInternalFeature);
437 workshop()->deleteFeatures(anObjects);
440 void PartSet_SketcherReetntrantMgr::resetFlags()
442 if (!myIsFlagsBlocked) {
443 myIsInternalEditOperation = false;
444 myRestartingMode = RM_None;
448 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
450 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
451 return aConnector->workshop();
454 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
456 return dynamic_cast<PartSet_Module*>(myWorkshop->module());