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>
16 #include <SketchPlugin_Feature.h>
17 #include <SketchPlugin_Line.h>
19 #include <XGUI_Workshop.h>
20 #include <XGUI_ModuleConnector.h>
21 #include <XGUI_OperationMgr.h>
23 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
24 : QObject(theWorkshop),
25 myWorkshop(theWorkshop),
26 myRestartingMode(RM_None),
27 myIsFlagsBlocked(false),
28 myIsInternalEditOperation(false)
32 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
36 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
38 ModuleBase_ModelWidget* aWidget = 0;
42 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
44 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
45 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
46 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
47 // finds the first widget which can accept a value
48 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
50 aWidget = aFirstWidget;
56 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
58 return myIsInternalEditOperation;
61 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
63 bool aProcessed = false;
67 aProcessed = myIsInternalEditOperation;
73 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
81 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
89 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
90 QMouseEvent* /* theEvent*/)
92 bool aProcessed = false;
96 if (myIsInternalEditOperation) {
97 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
98 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
99 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
100 (myWorkshop->currentOperation());
101 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
106 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
107 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
108 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
109 QList<ModuleBase_ViewerPrs> aSelection;
110 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
111 if (aPoint2DWdg->setSelection(aSelection, true))
112 aPanel->activateNextWidget(aPoint2DWdg);
120 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
121 QMouseEvent* /* theEvent*/)
123 return isActiveMgr() && myIsInternalEditOperation;
126 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
127 QMouseEvent* theEvent)
129 bool aProcessed = false;
133 if (myIsInternalEditOperation) {
134 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
135 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
137 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
138 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
142 // fill the first widget by the mouse event point
143 // if the active widget is not the first, it means that the restarted operation is filled by
144 // the current preselection.
145 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
146 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
147 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
148 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
156 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
160 if (!myIsInternalEditOperation)
163 PartSet_Module* aModule = module();
164 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
165 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
166 if (aFirstWidget != aPanel->activeWidget()) {
167 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
169 aWSelector->activateSelectionAndFilters(true);
173 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
178 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
179 (myWorkshop->currentOperation());
180 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
183 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
184 bool isStarted = false;
185 if (myRestartingMode != RM_Forbided) {
186 myRestartingMode = RM_LastFeatureUsed;
187 isStarted = startInternalEdit(thePreviousAttributeID);
190 aFOperation->commit();
194 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
201 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
202 (myWorkshop->currentOperation());
203 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
206 myRestartingMode = RM_EmptyFeatureUsed;
207 isDone = startInternalEdit(thePreviousAttributeID);
212 void PartSet_SketcherReetntrantMgr::onVertexSelected()
217 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
218 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
219 /// If last line finished on vertex the lines creation sequence has to be break
220 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
221 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
222 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
223 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
224 bool aFoundWidget = false;
225 bool aFoundObligatory = false;
226 for (; anIt != aLast && !aFoundObligatory; anIt++) {
228 aFoundWidget = *anIt == anActiveWidget;
230 aFoundObligatory = (*anIt)->isObligatory();
232 if (!aFoundObligatory)
233 myRestartingMode = RM_Forbided;
237 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
239 if (!isActiveMgr() || !myIsInternalEditOperation)
242 beforeStopInternalEdit();
245 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
247 return !isActiveMgr() || myRestartingMode == RM_None;
250 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
252 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
254 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
256 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
257 if (anActive) { // the manager is not active when the current operation is a usual Edit
258 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
259 (myWorkshop->currentOperation());
260 if (aFOperation->isEditOperation())
261 anActive = myIsInternalEditOperation;
267 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
270 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
271 (myWorkshop->currentOperation());
273 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
274 aFOperation->setEditOperation(true);
275 FeaturePtr anOperationFeature = aFOperation->feature();
277 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
278 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
280 myIsInternalEditOperation = true;
282 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
283 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
285 // activate selection filters of the first widget in the viewer
288 // activate the last active widget in the Property Panel
289 if (!thePreviousAttributeID.empty()) {
290 ModuleBase_Operation* anEditOperation = module()->currentOperation();
291 if (anEditOperation) {
292 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
293 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
294 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
295 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
296 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
297 aPreviousAttributeWidget = aWidgets[i];
299 // If the current widget is a selector, do nothing, it processes the mouse press
300 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
301 aPreviousAttributeWidget->focusTo();
308 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
310 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
311 (myWorkshop->currentOperation());
313 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
314 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
317 QObjectPtrList anObjects;
318 anObjects.append(myInternalFeature);
319 workshop()->deleteFeatures(anObjects);
321 PartSet_Module* aModule = module();
322 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
323 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
324 if (aFirstWidget != aPanel->activeWidget()) {
325 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
327 aWSelector->activateSelectionAndFilters(false);
331 void PartSet_SketcherReetntrantMgr::restartOperation()
333 if (myIsInternalEditOperation) {
334 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
335 myWorkshop->currentOperation());
337 myIsFlagsBlocked = true;
338 aFOperation->commit();
339 module()->launchOperation(aFOperation->id());
340 myIsFlagsBlocked = false;
346 void PartSet_SketcherReetntrantMgr::resetFlags()
348 if (!myIsFlagsBlocked) {
349 myIsInternalEditOperation = false;
350 myRestartingMode = RM_None;
354 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
356 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
357 return aConnector->workshop();
360 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
362 return dynamic_cast<PartSet_Module*>(myWorkshop->module());