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 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
49 ModuleBase_ModelWidget* aFirstWidget = 0;
50 ModuleBase_ModelWidget* aWgt;
51 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
52 for (aWIt = aWidgets.begin(); aWIt != aWidgets.end() && !aFirstWidget; ++aWIt) {
54 if (aWgt->canSetValue())
58 aWidget = aFirstWidget;
64 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
66 bool aProcessed = false;
70 aProcessed = myIsInternalEditOperation;
76 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
84 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
92 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
93 QMouseEvent* /* theEvent*/)
95 bool aProcessed = false;
99 if (myIsInternalEditOperation) {
100 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
101 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
102 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
103 (myWorkshop->currentOperation());
104 FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
109 ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
110 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
111 if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
112 QList<ModuleBase_ViewerPrs> aSelection;
113 aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
114 if (aPoint2DWdg->setSelection(aSelection, true))
115 aPanel->activateNextWidget(aPoint2DWdg);
123 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
124 QMouseEvent* /* theEvent*/)
126 return isActiveMgr() && myIsInternalEditOperation;
129 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
130 QMouseEvent* theEvent)
132 bool aProcessed = false;
136 if (myIsInternalEditOperation) {
137 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
139 ModuleBase_ModelWidget* anActiveWidget = anOperation->propertyPanel()->activeWidget();
140 if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
144 // fill the widget by the mouse event point
145 PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
147 aPoint2DWdg->onMouseRelease(theWnd, theEvent);
155 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
159 if (!myIsInternalEditOperation)
162 PartSet_Module* aModule = module();
163 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
164 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
165 if (aFirstWidget != aPanel->activeWidget()) {
166 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
168 aWSelector->activateSelectionAndFilters(true);
172 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
176 XGUI_OperationMgr* anOpMgr = workshop()->operationMgr();
177 if (!anOpMgr->isApplyEnabled())
180 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
181 (myWorkshop->currentOperation());
183 if (PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
184 XGUI_OperationMgr* anOpMgr = workshop()->operationMgr();
185 if (myRestartingMode != RM_Forbided) {
186 myRestartingMode = RM_LastFeatureUsed;
187 startInternalEdit(thePreviousAttributeID);
190 aFOperation->commit();
195 void PartSet_SketcherReetntrantMgr::onVertexSelected()
200 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
201 if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
202 /// If last line finished on vertex the lines creation sequence has to be break
203 ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
204 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
205 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
206 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
207 bool aFoundWidget = false;
208 bool aFoundObligatory = false;
209 for (; anIt != aLast && !aFoundObligatory; anIt++) {
211 aFoundWidget = *anIt == anActiveWidget;
213 aFoundObligatory = (*anIt)->isObligatory();
215 if (!aFoundObligatory)
216 myRestartingMode = RM_Forbided;
220 void PartSet_SketcherReetntrantMgr::onEnterReleased()
225 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
226 (myWorkshop->currentOperation());
227 if (myIsInternalEditOperation)
228 myRestartingMode = RM_EmptyFeatureUsed;
231 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
233 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
234 (myWorkshop->currentOperation());
236 disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
237 disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
242 if (!myIsInternalEditOperation)
245 PartSet_Module* aModule = module();
246 ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
247 ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
248 if (aFirstWidget != aPanel->activeWidget()) {
249 ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
251 aWSelector->activateSelectionAndFilters(false);
255 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
257 return !isActiveMgr() || myRestartingMode == RM_None;
260 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
262 PartSet_SketcherMgr* aSketcherMgr = module()->sketchMgr();
263 ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
264 return PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
265 PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
268 void PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
270 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
271 (myWorkshop->currentOperation());
273 aFOperation->setEditOperation();
274 FeaturePtr anOperationFeature = aFOperation->feature();
275 if (anOperationFeature.get() != NULL) {
277 myIsInternalEditOperation = true;
278 connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
279 connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
281 // activate selection filters of the first widget in the viewer
284 // activate the last active widget in the Property Panel
285 if (!thePreviousAttributeID.empty()) {
286 ModuleBase_Operation* anEditOperation = module()->currentOperation();
287 if (anEditOperation) {
288 ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
289 ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
290 QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
291 for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
292 if (aWidgets[i]->attributeID() == thePreviousAttributeID)
293 aPreviousAttributeWidget = aWidgets[i];
295 // If the current widget is a selector, do nothing, it processes the mouse press
296 if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
297 aPreviousAttributeWidget->focusTo();
303 void PartSet_SketcherReetntrantMgr::restartOperation()
305 if (myIsInternalEditOperation) {
306 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
307 myWorkshop->currentOperation());
309 myIsFlagsBlocked = true;
310 aFOperation->commit();
311 module()->launchOperation(aFOperation->id());
312 myIsFlagsBlocked = false;
318 void PartSet_SketcherReetntrantMgr::resetFlags()
320 if (!myIsFlagsBlocked) {
321 myIsInternalEditOperation = false;
322 myRestartingMode = RM_None;
326 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
328 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
329 return aConnector->workshop();
332 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
334 return dynamic_cast<PartSet_Module*>(myWorkshop->module());