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 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
29 : QObject(theWorkshop),
30 myWorkshop(theWorkshop),
31 myRestartingMode(RM_None),
32 myIsFlagsBlocked(false),
33 myIsInternalEditOperation(false),
34 myNoMoreWidgetsAttribute("")
38 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
42 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
44 ModuleBase_ModelWidget* aWidget = 0;
48 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
50 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
51 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
52 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
53 aWidget = myInternalActiveWidget;
58 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
60 return myIsInternalEditOperation;
63 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
65 bool aProcessed = false;
69 aProcessed = myIsInternalEditOperation;
75 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
83 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
91 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
92 QMouseEvent* /* theEvent*/)
94 bool aProcessed = false;
98 if (myIsInternalEditOperation) {
99 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
100 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
101 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
102 (myWorkshop->currentOperation());
103 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
108 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
109 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
110 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
111 QList<ModuleBase_ViewerPrs> aSelection;
112 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
113 if (aPoint2DWdg->setSelection(aSelection, true))
114 aPanel->activateNextWidget(aPoint2DWdg);
122 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
123 QMouseEvent* /* theEvent*/)
125 return isActiveMgr() && myIsInternalEditOperation;
128 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
129 QMouseEvent* theEvent)
131 bool aProcessed = false;
135 if (myIsInternalEditOperation) {
136 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
137 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
139 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
140 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
144 // fill the first widget by the mouse event point
145 // if the active widget is not the first, it means that the restarted operation is filled by
146 // the current preselection.
147 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
148 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
149 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
150 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
158 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
162 if (!myIsInternalEditOperation)
165 PartSet_Module* aModule = module();
166 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
167 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
168 if (aFirstWidget != aPanel->activeWidget()) {
169 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
171 aWSelector->activateSelectionAndFilters(true);
175 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
180 // we should avoid processing of the signal about no more widgets attributes and
181 // do this after the restart operaion is finished if it was called
182 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
183 // if it should be called after restart
184 if (myIsFlagsBlocked) {
185 myNoMoreWidgetsAttribute = thePreviousAttributeID;
189 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
190 (myWorkshop->currentOperation());
191 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
194 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
195 bool isStarted = false;
196 if (!module()->sketchMgr()->sketchSolverError()) {
197 if (myRestartingMode != RM_Forbided) {
198 myRestartingMode = RM_LastFeatureUsed;
199 isStarted = startInternalEdit(thePreviousAttributeID);
203 aFOperation->commit();
207 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
214 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
215 (myWorkshop->currentOperation());
216 if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
219 bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
220 if (!isSketchSolverError) {
221 myRestartingMode = RM_EmptyFeatureUsed;
222 isDone = startInternalEdit(thePreviousAttributeID);
228 void PartSet_SketcherReetntrantMgr::onVertexSelected()
233 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
234 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
235 /// If last line finished on vertex the lines creation sequence has to be break
236 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
237 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
238 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
239 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
240 bool aFoundWidget = false;
241 bool aFoundObligatory = false;
242 for (; anIt != aLast && !aFoundObligatory; anIt++) {
244 aFoundWidget = *anIt == anActiveWidget;
246 aFoundObligatory = (*anIt)->isObligatory();
248 if (!aFoundObligatory)
249 myRestartingMode = RM_Forbided;
253 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
255 if (!isActiveMgr() || !myIsInternalEditOperation)
258 beforeStopInternalEdit();
261 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
263 return !isActiveMgr() || myRestartingMode == RM_None;
266 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
268 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
270 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
272 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
273 if (anActive) { // the manager is not active when the current operation is a usual Edit
274 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
275 (myWorkshop->currentOperation());
276 if (aFOperation->isEditOperation())
277 anActive = myIsInternalEditOperation;
283 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
286 /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
287 /// event comes two times, so we should not start another internal edit operation
288 /// the Apply button becomes disabled becase the second additional internal feature is created
289 if (myIsInternalEditOperation)
292 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
293 (myWorkshop->currentOperation());
295 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
296 aFOperation->setEditOperation(false);
297 workshop()->operationMgr()->updateApplyOfOperations();
299 createInternalFeature();
301 myIsInternalEditOperation = true;
303 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
304 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
306 // activate selection filters of the first widget in the viewer
309 // activate the last active widget in the Property Panel
310 if (!thePreviousAttributeID.empty()) {
311 ModuleBase_Operation* anEditOperation = module()->currentOperation();
312 if (anEditOperation) {
313 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
314 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
315 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
316 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
317 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
318 aPreviousAttributeWidget = aWidgets[i];
320 // If the current widget is a selector, do nothing, it processes the mouse press
321 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector()) {
322 aPreviousAttributeWidget->focusTo();
323 aPreviousAttributeWidget->selectContent();
329 module()->sketchMgr()->clearClickedFlags();
334 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
336 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
337 (myWorkshop->currentOperation());
339 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
340 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
343 deleteInternalFeature();
346 void PartSet_SketcherReetntrantMgr::restartOperation()
348 if (myIsInternalEditOperation) {
349 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
350 myWorkshop->currentOperation());
352 myNoMoreWidgetsAttribute = "";
353 myIsFlagsBlocked = true;
354 aFOperation->commit();
355 module()->launchOperation(aFOperation->id());
356 myIsFlagsBlocked = false;
358 // we should avoid processing of the signal about no more widgets attributes and
359 // do this after the restart operaion is finished if it was called
360 // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
361 // if it should be called after restart
362 if (!myNoMoreWidgetsAttribute.empty()) {
363 onNoMoreWidgets(myNoMoreWidgetsAttribute);
364 myNoMoreWidgetsAttribute = "";
370 void PartSet_SketcherReetntrantMgr::createInternalFeature()
372 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
373 (myWorkshop->currentOperation());
375 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
376 FeaturePtr anOperationFeature = aFOperation->feature();
378 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
379 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
380 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
381 (aFOperation->propertyPanel());
383 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
384 myInternalWidget->setVisible(false);
386 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
388 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
389 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
391 aFactory.createWidget(anInternalPage);
392 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
394 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
395 aWidget->setFeature(myInternalFeature, true);
397 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
400 myInternalActiveWidget = aFirstWidget;
404 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
406 if (myInternalActiveWidget) {
407 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
409 aWSelector->activateSelectionAndFilters(false);
410 myInternalActiveWidget = 0;
412 delete myInternalWidget;
413 myInternalWidget = 0;
415 QObjectPtrList anObjects;
416 anObjects.append(myInternalFeature);
417 workshop()->deleteFeatures(anObjects);
420 void PartSet_SketcherReetntrantMgr::resetFlags()
422 if (!myIsFlagsBlocked) {
423 myIsInternalEditOperation = false;
424 myRestartingMode = RM_None;
428 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
430 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
431 return aConnector->workshop();
434 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
436 return dynamic_cast<PartSet_Module*>(myWorkshop->module());