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::operationCommitted(ModuleBase_Operation* theOperation)
58 bool aProcessed = false;
62 aProcessed = myIsInternalEditOperation;
68 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
76 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
84 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
85 QMouseEvent* /* theEvent*/)
87 bool aProcessed = false;
91 if (myIsInternalEditOperation) {
92 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
93 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
94 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
95 (myWorkshop->currentOperation());
96 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
101 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
102 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
103 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
104 QList<ModuleBase_ViewerPrs> aSelection;
105 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
106 if (aPoint2DWdg->setSelection(aSelection, true))
107 aPanel->activateNextWidget(aPoint2DWdg);
115 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
116 QMouseEvent* /* theEvent*/)
118 return isActiveMgr() && myIsInternalEditOperation;
121 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
122 QMouseEvent* theEvent)
124 bool aProcessed = false;
128 if (myIsInternalEditOperation) {
129 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
130 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
132 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
133 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
137 // fill the first widget by the mouse event point
138 // if the active widget is not the first, it means that the restarted operation is filled by
139 // the current preselection.
140 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
141 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
142 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
143 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
151 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
155 if (!myIsInternalEditOperation)
158 PartSet_Module* aModule = module();
159 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
160 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
161 if (aFirstWidget != aPanel->activeWidget()) {
162 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
164 aWSelector->activateSelectionAndFilters(true);
168 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
173 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
174 (myWorkshop->currentOperation());
175 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
179 if (PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
180 if (myRestartingMode != RM_Forbided) {
181 myRestartingMode = RM_LastFeatureUsed;
182 startInternalEdit(thePreviousAttributeID);
185 aFOperation->commit();
191 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
198 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
199 (myWorkshop->currentOperation());
200 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
203 myRestartingMode = RM_EmptyFeatureUsed;
204 startInternalEdit(thePreviousAttributeID);
210 void PartSet_SketcherReetntrantMgr::onVertexSelected()
215 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
216 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
217 /// If last line finished on vertex the lines creation sequence has to be break
218 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
219 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
220 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
221 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
222 bool aFoundWidget = false;
223 bool aFoundObligatory = false;
224 for (; anIt != aLast && !aFoundObligatory; anIt++) {
226 aFoundWidget = *anIt == anActiveWidget;
228 aFoundObligatory = (*anIt)->isObligatory();
230 if (!aFoundObligatory)
231 myRestartingMode = RM_Forbided;
235 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
237 if (!isActiveMgr() || !myIsInternalEditOperation)
240 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
241 (myWorkshop->currentOperation());
243 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
244 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
247 PartSet_Module* aModule = module();
248 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
249 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
250 if (aFirstWidget != aPanel->activeWidget()) {
251 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
253 aWSelector->activateSelectionAndFilters(false);
257 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
259 return !isActiveMgr() || myRestartingMode == RM_None;
262 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
264 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
266 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
268 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
269 if (anActive) { // the manager is not active when the current operation is a usual Edit
270 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
271 (myWorkshop->currentOperation());
272 if (aFOperation->isEditOperation())
273 anActive = myIsInternalEditOperation;
279 void PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
281 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
282 (myWorkshop->currentOperation());
284 aFOperation->setEditOperation(true);
285 FeaturePtr anOperationFeature = aFOperation->feature();
286 if (anOperationFeature.get() != NULL) {
288 myIsInternalEditOperation = true;
289 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
290 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
292 // activate selection filters of the first widget in the viewer
295 // activate the last active widget in the Property Panel
296 if (!thePreviousAttributeID.empty()) {
297 ModuleBase_Operation* anEditOperation = module()->currentOperation();
298 if (anEditOperation) {
299 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
300 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
301 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
302 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
303 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
304 aPreviousAttributeWidget = aWidgets[i];
306 // If the current widget is a selector, do nothing, it processes the mouse press
307 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
308 aPreviousAttributeWidget->focusTo();
314 void PartSet_SketcherReetntrantMgr::restartOperation()
316 if (myIsInternalEditOperation) {
317 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
318 myWorkshop->currentOperation());
320 myIsFlagsBlocked = true;
321 aFOperation->commit();
322 module()->launchOperation(aFOperation->id());
323 myIsFlagsBlocked = false;
329 void PartSet_SketcherReetntrantMgr::resetFlags()
331 if (!myIsFlagsBlocked) {
332 myIsInternalEditOperation = false;
333 myRestartingMode = RM_None;
337 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
339 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
340 return aConnector->workshop();
343 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
345 return dynamic_cast<PartSet_Module*>(myWorkshop->module());