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 myIsInternalEditOperation(false),
27 myLastOperationId(""),
28 myPreviousAttributeID(""),
29 myRestartingMode(RM_None)
33 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
37 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::activeWidget() const
39 ModuleBase_ModelWidget* aWidget = 0;
41 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
43 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
44 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
45 if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
46 // finds the first widget which can accept a value
47 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
48 ModuleBase_ModelWidget* aFirstWidget = 0;
49 ModuleBase_ModelWidget* aWgt;
50 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
51 for (aWIt = aWidgets.begin(); aWIt != aWidgets.end() && !aFirstWidget; ++aWIt) {
53 if (aWgt->canSetValue())
57 aWidget = aFirstWidget;
63 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
65 bool aProcessed = false;
66 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
70 FeaturePtr aFeature = aFOperation->feature();
71 std::shared_ptr<SketchPlugin_Feature> aSPFeature =
72 std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
73 if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
74 myRestartingMode == RM_EmptyFeatureUsed)) {
75 myLastOperationId = aFOperation->id().toStdString();
76 myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
77 PartSet_Module* aModule = module();
78 if (!aModule->sketchMgr()->sketchSolverError()) {
79 if (!aFOperation->isEditOperation()) {
80 FeaturePtr anOperationFeature = aFOperation->feature();
81 if (anOperationFeature.get() != NULL) {
82 aModule->editFeature(anOperationFeature);
85 myIsInternalEditOperation = true;
86 // activate selection filters of the first widget in the viewer
87 onInternalActivateFirstWidgetSelection();
89 // activate the last active widget in the Property Panel
90 if (!myPreviousAttributeID.empty()) {
91 ModuleBase_Operation* anEditOperation = aModule->currentOperation();
92 if (anEditOperation) {
93 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
94 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
95 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
96 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
97 if (aWidgets[i]->attributeID() == myPreviousAttributeID)
98 aPreviousAttributeWidget = aWidgets[i];
100 // If the current widget is a selector, do nothing, it processes the mouse press
101 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
102 aPreviousAttributeWidget->focusTo();
108 // the flag should be reset before start to do not react to the widget deactivate
109 myIsInternalEditOperation = false;
110 aModule->launchOperation(myLastOperationId.c_str());
111 breakOperationSequence();
118 breakOperationSequence();
123 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
125 if (myIsInternalEditOperation) {
126 // abort the created feature, which is currently edited
127 SessionPtr aMgr = ModelAPI_Session::get();
128 if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
132 myIsInternalEditOperation = false;
133 breakOperationSequence();
136 bool PartSet_SketcherReetntrantMgr::processMouseMoved()
138 bool isProcessed = false;
139 if (myIsInternalEditOperation) {
140 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
141 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
142 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
143 //if (operationMgr()->isApplyEnabled())
144 anOperation->commit();
151 bool PartSet_SketcherReetntrantMgr::processMousePressed()
153 return myIsInternalEditOperation;
156 bool PartSet_SketcherReetntrantMgr::processMouseReleased()
158 bool isProcessed = false;
159 if (myIsInternalEditOperation) {
160 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
161 //if (operationMgr()->isApplyEnabled())
162 anOperation->commit();
168 void PartSet_SketcherReetntrantMgr::propertyPanelDefined(ModuleBase_Operation* theOperation)
170 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
174 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
175 if (PartSet_SketcherMgr::isSketchOperation(aFOperation) && (aFOperation->isEditOperation())) {
176 // we have to manually activate the sketch label in edit mode
177 aPanel->activateWidget(aPanel->modelWidgets().first());
181 // Restart last operation type
182 if ((aFOperation->id() == myLastOperationId.c_str()) && myLastFeature) {
183 ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
184 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
185 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
186 QList<ModuleBase_ViewerPrs> aSelection;
187 aSelection.append(ModuleBase_ViewerPrs(myLastFeature, TopoDS_Shape(), NULL));
188 if (aPoint2DWdg->setSelection(aSelection, true))
189 aPanel->activateNextWidget(aPoint2DWdg);
194 void PartSet_SketcherReetntrantMgr::noMoreWidgets(const std::string& thePreviousAttributeID)
196 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
198 if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
199 if (myRestartingMode != RM_Forbided) {
200 myRestartingMode = RM_LastFeatureUsed;
201 myPreviousAttributeID = thePreviousAttributeID;
203 XGUI_Workshop* aWorkshop = workshop();
204 XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
205 // do nothing if the feature can not be applyed
206 if (anOpMgr->isApplyEnabled())
207 anOperation->commit();
212 void PartSet_SketcherReetntrantMgr::vertexSelected()
214 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
215 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
216 /// If last line finished on vertex the lines creation sequence has to be break
217 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
218 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
219 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
220 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
221 bool aFoundWidget = false;
222 bool aFoundObligatory = false;
223 for (; anIt != aLast && !aFoundObligatory; anIt++) {
225 aFoundWidget = *anIt == anActiveWidget;
227 aFoundObligatory = (*anIt)->isObligatory();
229 if (!aFoundObligatory)
230 myRestartingMode = RM_Forbided;
234 void PartSet_SketcherReetntrantMgr::enterReleased()
236 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
237 (myWorkshop->currentOperation());
238 if (/*!aFOperation->isEditOperation() || */myIsInternalEditOperation)
239 myRestartingMode = RM_EmptyFeatureUsed;
242 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
244 return myRestartingMode == RM_None;
247 void PartSet_SketcherReetntrantMgr::onInternalActivateFirstWidgetSelection()
249 if (!myIsInternalEditOperation)
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(true);
262 void PartSet_SketcherReetntrantMgr::breakOperationSequence()
264 myLastOperationId = "";
265 myLastFeature = FeaturePtr();
266 myRestartingMode = RM_None;
269 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop()
271 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
272 return aConnector->workshop();
275 PartSet_Module* PartSet_SketcherReetntrantMgr::module()
277 return dynamic_cast<PartSet_Module*>(myWorkshop->module());