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)
37 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
41 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
43 ModuleBase_ModelWidget* aWidget = 0;
47 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
49 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
50 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
51 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
52 aWidget = myInternalActiveWidget;
57 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
59 return myIsInternalEditOperation;
62 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
64 bool aProcessed = false;
68 aProcessed = myIsInternalEditOperation;
74 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
82 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
90 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
91 QMouseEvent* /* theEvent*/)
93 bool aProcessed = false;
97 if (myIsInternalEditOperation) {
98 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
99 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
100 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
101 (myWorkshop->currentOperation());
102 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
107 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
108 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
109 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
110 QList<ModuleBase_ViewerPrs> aSelection;
111 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
112 if (aPoint2DWdg->setSelection(aSelection, true))
113 aPanel->activateNextWidget(aPoint2DWdg);
121 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
122 QMouseEvent* /* theEvent*/)
124 return isActiveMgr() && myIsInternalEditOperation;
127 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
128 QMouseEvent* theEvent)
130 bool aProcessed = false;
134 if (myIsInternalEditOperation) {
135 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
136 ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
138 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
139 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
143 // fill the first widget by the mouse event point
144 // if the active widget is not the first, it means that the restarted operation is filled by
145 // the current preselection.
146 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
147 ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
148 if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
149 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
157 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
161 if (!myIsInternalEditOperation)
164 PartSet_Module* aModule = module();
165 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
166 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
167 if (aFirstWidget != aPanel->activeWidget()) {
168 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
170 aWSelector->activateSelectionAndFilters(true);
174 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
179 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
180 (myWorkshop->currentOperation());
181 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
184 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
185 bool isStarted = false;
186 if (myRestartingMode != RM_Forbided) {
187 myRestartingMode = RM_LastFeatureUsed;
188 isStarted = startInternalEdit(thePreviousAttributeID);
191 aFOperation->commit();
195 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
202 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
203 (myWorkshop->currentOperation());
204 if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
207 myRestartingMode = RM_EmptyFeatureUsed;
208 isDone = startInternalEdit(thePreviousAttributeID);
213 void PartSet_SketcherReetntrantMgr::onVertexSelected()
218 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
219 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
220 /// If last line finished on vertex the lines creation sequence has to be break
221 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
222 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
223 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
224 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
225 bool aFoundWidget = false;
226 bool aFoundObligatory = false;
227 for (; anIt != aLast && !aFoundObligatory; anIt++) {
229 aFoundWidget = *anIt == anActiveWidget;
231 aFoundObligatory = (*anIt)->isObligatory();
233 if (!aFoundObligatory)
234 myRestartingMode = RM_Forbided;
238 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
240 if (!isActiveMgr() || !myIsInternalEditOperation)
243 beforeStopInternalEdit();
246 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
248 return !isActiveMgr() || myRestartingMode == RM_None;
251 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
253 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
255 bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
257 anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
258 if (anActive) { // the manager is not active when the current operation is a usual Edit
259 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
260 (myWorkshop->currentOperation());
261 if (aFOperation->isEditOperation())
262 anActive = myIsInternalEditOperation;
268 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
271 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
272 (myWorkshop->currentOperation());
274 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
275 aFOperation->setEditOperation(true);
276 workshop()->operationMgr()->updateApplyOfOperations();
278 createInternalFeature();
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 deleteInternalFeature();
320 void PartSet_SketcherReetntrantMgr::restartOperation()
322 if (myIsInternalEditOperation) {
323 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
324 myWorkshop->currentOperation());
326 myIsFlagsBlocked = true;
327 aFOperation->commit();
328 module()->launchOperation(aFOperation->id());
329 myIsFlagsBlocked = false;
335 void PartSet_SketcherReetntrantMgr::createInternalFeature()
337 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
338 (myWorkshop->currentOperation());
340 if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
341 aFOperation->setEditOperation(true);
342 FeaturePtr anOperationFeature = aFOperation->feature();
344 CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
345 myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
346 XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
347 (aFOperation->propertyPanel());
349 myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
350 myInternalWidget->setVisible(false);
352 ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
354 QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
355 ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
357 aFactory.createWidget(anInternalPage);
358 QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
360 foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
361 aWidget->setFeature(myInternalFeature, true);
363 ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
366 myInternalActiveWidget = aFirstWidget;
370 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
372 if (myInternalActiveWidget) {
373 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
375 aWSelector->activateSelectionAndFilters(false);
376 myInternalActiveWidget = 0;
378 delete myInternalWidget;
379 myInternalWidget = 0;
381 QObjectPtrList anObjects;
382 anObjects.append(myInternalFeature);
383 workshop()->deleteFeatures(anObjects);
386 void PartSet_SketcherReetntrantMgr::resetFlags()
388 if (!myIsFlagsBlocked) {
389 myIsInternalEditOperation = false;
390 myRestartingMode = RM_None;
394 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
396 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
397 return aConnector->workshop();
400 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
402 return dynamic_cast<PartSet_Module*>(myWorkshop->module());