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())
184 if (PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
185 if (myRestartingMode != RM_Forbided) {
186 myRestartingMode = RM_LastFeatureUsed;
187 startInternalEdit(thePreviousAttributeID);
190 aFOperation->commit();
196 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
203 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
204 (myWorkshop->currentOperation());
205 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
208 myRestartingMode = RM_EmptyFeatureUsed;
209 startInternalEdit(thePreviousAttributeID);
215 void PartSet_SketcherReetntrantMgr::onVertexSelected()
220 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
221 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
222 /// If last line finished on vertex the lines creation sequence has to be break
223 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
224 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
225 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
226 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
227 bool aFoundWidget = false;
228 bool aFoundObligatory = false;
229 for (; anIt != aLast && !aFoundObligatory; anIt++) {
231 aFoundWidget = *anIt == anActiveWidget;
233 aFoundObligatory = (*anIt)->isObligatory();
235 if (!aFoundObligatory)
236 myRestartingMode = RM_Forbided;
240 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
242 if (!isActiveMgr() || !myIsInternalEditOperation)
245 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
246 (myWorkshop->currentOperation());
248 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
249 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
252 PartSet_Module* aModule = module();
253 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
254 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
255 if (aFirstWidget != aPanel->activeWidget()) {
256 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
258 aWSelector->activateSelectionAndFilters(false);
262 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
264 return !isActiveMgr() || myRestartingMode == RM_None;
267 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
269 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
271 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
273 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
274 if (anActive) { // the manager is not active when the current operation is a usual Edit
275 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
276 (myWorkshop->currentOperation());
277 if (aFOperation->isEditOperation())
278 anActive = myIsInternalEditOperation;
284 void PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
286 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
287 (myWorkshop->currentOperation());
289 aFOperation->setEditOperation(true);
290 FeaturePtr anOperationFeature = aFOperation->feature();
291 if (anOperationFeature.get() != NULL) {
293 myIsInternalEditOperation = true;
294 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
295 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
297 // activate selection filters of the first widget in the viewer
300 // activate the last active widget in the Property Panel
301 if (!thePreviousAttributeID.empty()) {
302 ModuleBase_Operation* anEditOperation = module()->currentOperation();
303 if (anEditOperation) {
304 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
305 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
306 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
307 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
308 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
309 aPreviousAttributeWidget = aWidgets[i];
311 // If the current widget is a selector, do nothing, it processes the mouse press
312 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
313 aPreviousAttributeWidget->focusTo();
319 void PartSet_SketcherReetntrantMgr::restartOperation()
321 if (myIsInternalEditOperation) {
322 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
323 myWorkshop->currentOperation());
325 myIsFlagsBlocked = true;
326 aFOperation->commit();
327 module()->launchOperation(aFOperation->id());
328 myIsFlagsBlocked = false;
334 void PartSet_SketcherReetntrantMgr::resetFlags()
336 if (!myIsFlagsBlocked) {
337 myIsInternalEditOperation = false;
338 myRestartingMode = RM_None;
342 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
344 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
345 return aConnector->workshop();
348 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
350 return dynamic_cast<PartSet_Module*>(myWorkshop->module());