1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSketchCreator.cpp
4 // Created: 08 June 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_WidgetSketchCreator.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetSketchLabel.h"
10 #include "PartSet_PreviewPlanes.h"
12 #include <Config_Keywords.h>
14 #include <XGUI_ModuleConnector.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <XGUI_OperationMgr.h>
19 #include <XGUI_PropertyPanel.h>
20 #include <XGUI_Tools.h>
21 #include <XGUI_ViewerProxy.h>
23 #include <GeomAPI_Face.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Events.h>
31 #include <ModelAPI_ResultConstruction.h>
33 #include <SketchPlugin_SketchEntity.h>
34 #include <FeaturesPlugin_CompositeBoolean.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_Operation.h>
38 #include <ModuleBase_IPropertyPanel.h>
39 #include <ModuleBase_OperationFeature.h>
40 #include <ModuleBase_ViewerPrs.h>
42 #include <Config_WidgetAPI.h>
44 #include <Events_Loop.h>
48 //#include <QFormLayout>
49 #include <QVBoxLayout>
50 #include <QMessageBox>
51 #include <QMainWindow>
53 #define DEBUG_UNDO_INVALID_SKETCH
55 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
56 PartSet_Module* theModule,
57 const Config_WidgetAPI* theData)
58 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
59 myModule(theModule), myIsCustomAttribute(false)
61 myAttributeListID = theData->getProperty("attribute_list_id");
63 //QFormLayout* aLayout = new QFormLayout(this);
64 QVBoxLayout* aLayout = new QVBoxLayout(this);
65 ModuleBase_Tools::zeroMargins(aLayout);
67 ModuleBase_Tools::adjustMargins(aLayout);
69 QString aLabelText = QString::fromStdString(theData->widgetLabel());
70 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
72 myLabel = new QLabel(aLabelText, this);
73 myLabel->setWordWrap(true);
74 aLayout->addWidget(myLabel);
75 aLayout->addStretch(1);
77 std::string aTypes = theData->getProperty("shape_types");
78 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
80 myPreviewPlanes = new PartSet_PreviewPlanes();
83 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
85 // we need to deactivate here in order to hide preview planes if the selection mode is
90 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
92 QList<QWidget*> aControls;
93 aControls.append(myLabel);
97 bool PartSet_WidgetSketchCreator::restoreValueCustom()
102 bool PartSet_WidgetSketchCreator::storeValueCustom() const
107 AttributePtr PartSet_WidgetSketchCreator::attribute() const
109 AttributePtr anAttribute;
110 if (myIsCustomAttribute)
111 anAttribute = myFeature->attribute(myAttributeListID);
113 anAttribute = ModuleBase_WidgetSelector::attribute();
118 //********************************************************************
119 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
121 SessionPtr aMgr = ModelAPI_Session::get();
122 bool aIsOp = aMgr->isOperation();
124 const static std::string aNestedOpID("Parameters modification");
125 aMgr->startOperation(aNestedOpID, true);
129 //********************************************************************
130 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
133 if (myIsCustomAttribute) {
134 // check only suiting of the value to custom attribute (myAttributeListID)
135 // do not cash of validation to avoid using states, stored for XML attribute
136 // there is an alternative is to call clearValidatedCash() in setSelection()
137 aValid = isValidSelectionForAttribute(theValue, attribute());
139 else { /// if the validated attribute is already custom
140 if (getValidState(theValue, aValid)) {
143 aValid = isValidSelectionCustom(theValue);
145 // check selection to create new sketh (XML current attribute)
146 aValid = isValidSelectionForAttribute(theValue, attribute());
148 // check selection to fill list attribute (myAttributeListID)
149 bool isCustomAttribute = myIsCustomAttribute;
150 myIsCustomAttribute = true;
151 aValid = isValidSelectionForAttribute(theValue, attribute());
152 myIsCustomAttribute = isCustomAttribute;
155 storeValidState(theValue, aValid);
159 //********************************************************************
160 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
162 return PartSet_WidgetSketchLabel::canFillSketch(theValue);
165 void PartSet_WidgetSketchCreator::activateSelectionControl()
167 // we need to call activate here as the widget has no focus accepted controls
168 // if these controls are added here, activate will happens automatically after focusIn()
169 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
170 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
171 aPanel->activateWidget(this, false);
174 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
176 // hide current widget, activate the next widget
177 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
178 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
179 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
180 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
181 if (theSelectionControl) { // hide other controls
183 aWidget->setVisible(false);
185 else { // hide current control
187 aWidget->setVisible(false);
189 aWidget->setVisible(true);
190 if (aWidget->attributeID() == myAttributeListID)
191 setEnabledModelWidget(aWidget, !hasSubObjects());
196 if (theSelectionControl) {
197 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
198 if (!aBodyIsVisualized) {
199 // We have to select a plane before any operation
200 myPreviewPlanes->showPreviewPlanes(myWorkshop);
203 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
204 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
206 aWorkshop->viewer()->update();
210 QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
212 QIntList aShapeTypes;
213 foreach(QString aType, myShapeTypes) {
214 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
219 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
221 ModuleBase_ModelWidget::setEditingMode(isEditing);
223 setVisibleSelectionControl(false);
225 ModuleBase_ModelWidget* anAttributeListWidget = 0;
226 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
227 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
228 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
229 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
230 if (aWidget->attributeID() == myAttributeListID) {
231 anAttributeListWidget = aWidget;
235 if (anAttributeListWidget)
236 setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
240 bool PartSet_WidgetSketchCreator::isSelectionMode() const
242 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
243 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
245 return !aHasValueInList;
248 bool PartSet_WidgetSketchCreator::hasSubObjects() const
250 bool aHasSubObjects = false;
252 bool aCanSetFocus = true;
253 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
254 if (aComposite.get())
255 aHasSubObjects = aComposite->numberOfSubs() > 0;
256 return aHasSubObjects;
259 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
260 const bool theToValidate)
263 if (!startSketchOperation(theValues)) {
264 myIsCustomAttribute = true;
265 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
266 bool aProcessed = false;
267 for (; anIt != aLast; anIt++) {
268 ModuleBase_ViewerPrsPtr aValue = *anIt;
269 if (!theToValidate || isValidInFilters(aValue))
270 aProcessed = setSelectionCustom(aValue) || aProcessed;
272 myIsCustomAttribute = false;
275 emit valuesChanged();
276 updateObject(myFeature);
277 setVisibleSelectionControl(false);
278 // manually deactivation because the widget was not activated as has no focus acceptin controls
280 emit focusOutWidget(this);
286 //********************************************************************
287 void PartSet_WidgetSketchCreator::onSelectionChanged()
289 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
290 bool isDone = setSelection(aSelected, true/*false*/);
293 //********************************************************************
294 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
298 bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrsPtr>& theValues)
300 bool aSketchStarted = false;
302 if (theValues.size() != 1)
303 return aSketchStarted;
305 ModuleBase_ViewerPrsPtr aValue = theValues.front();
306 if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
307 return aSketchStarted;
309 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
310 /// sketch should not started by object(face) selected as global. If Local face is selected,
311 /// sketch is started
312 if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
313 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
314 if (!aConstruction.get() || !aConstruction->isInfinite())
315 return aSketchStarted;
317 aSketchStarted = true;
319 // manually deactivation because the widget was not activated as has no focus acceptin controls
321 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
322 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
324 // Launch Sketch operation
325 CompositeFeaturePtr aCompFeature =
326 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
328 // start edit operation for the sketch
329 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
330 (myModule->createOperation("Sketch"));
331 QList<ModuleBase_ViewerPrsPtr> aValues;
332 aValues.push_back(aValue);
333 aFOperation->setPreselection(aValues);
335 myModule->sendOperation(aFOperation);
337 return aSketchStarted;
340 bool PartSet_WidgetSketchCreator::focusTo()
342 // this method is called only in creation mode. In Edition mode this widget is hidden
343 if (isSelectionMode() && !hasSubObjects()) {
344 setVisibleSelectionControl(true);
345 activateSelectionControl();
346 openExtrusionTransaction();
350 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
355 void PartSet_WidgetSketchCreator::deactivate()
357 ModuleBase_WidgetSelector::deactivate();
359 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
360 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
362 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
366 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
368 SessionPtr aMgr = ModelAPI_Session::get();
369 bool aIsOp = aMgr->isOperation();
371 // in current implementation, all transactions are closed when resume happens
372 // so, this is a wrong case, which is not checked.
373 // To provide it, make correction in later rows about abort/undo transactions
376 // Set visible only selection control
377 setVisibleSelectionControl(true);
379 // Validate the created sketch. If it is valid, it is set into the composite feature selection
380 // list, otherwise it is removed
381 CompositeFeaturePtr aCompFeature =
382 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
383 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
384 if (aCompFeature->numberOfSubs() > 0) {
385 // set the sub feature to attribute selection list and check whether sketch is valid
386 SessionPtr aMgr = ModelAPI_Session::get();
387 const static std::string aNestedOpID("Set Sketch result into Selection list");
388 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
389 setSketchObjectToList(aCompFeature, anAttrList);
390 aMgr->finishOperation();
392 if (!validateSelectionList()) {
393 #ifdef DEBUG_UNDO_INVALID_SKETCH
394 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
395 aMgr->undo(); /// Sketch creation
397 aMgr->startOperation("Delete invalid Sketch feature", false);
399 // delete invalid sketch
400 CompositeFeaturePtr aSketchFeature =
401 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
402 QObjectPtrList anObjects;
403 anObjects.append(aSketchFeature);
405 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
406 aWorkshop->deleteFeatures(anObjects, std::set<FeaturePtr>());
408 aMgr->finishOperation();
412 openExtrusionTransaction();
414 if (aCompFeature->numberOfSubs() == 0) {
415 // call activateWidget() of the parent to connect to the viewer seleciton
416 activateSelectionControl();
419 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
420 /// the attribute selection list will be filled by result of this sketch.
421 setVisibleSelectionControl(false);
423 // Update value in attribute selection list
424 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
425 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
426 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
427 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
428 if (aWidget->attributeID() == myAttributeListID)
429 aWidget->restoreValue();
432 // Hide sketcher result
433 CompositeFeaturePtr aSketchFeature =
434 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
435 std::list<ResultPtr> aResults = aSketchFeature->results();
436 std::list<ResultPtr>::const_iterator aIt;
437 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
438 (*aIt)->setDisplayed(false);
440 aSketchFeature->setDisplayed(false);
441 static Events_Loop* aLoop = Events_Loop::loop();
442 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
444 // Add Selected body were created the sketcher to list of selected objects
445 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
446 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
447 if (aSelList.get()) {
448 DataPtr aData = aSketchFeature->data();
449 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
450 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
451 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
453 SessionPtr aMgr = ModelAPI_Session::get();
454 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
455 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
456 std::string aValidatorID, anError;
457 aSelList->append(aRes, GeomShapePtr());
458 if (aFactory->validate(anAttribute, aValidatorID, anError))
459 updateObject(aCompFeature);
468 bool PartSet_WidgetSketchCreator::validateSelectionList() const
470 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
472 SessionPtr aMgr = ModelAPI_Session::get();
473 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
474 std::string aValidatorID, anError;
475 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
476 if (!isValidPComposite) {
477 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
478 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
479 tr("Sketch is invalid and will be deleted.\nError: %1").arg(anError.c_str()),
482 return isValidPComposite;
485 void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePtr& theCompositeFeature,
486 const AttributePtr& theAttribute)
488 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
491 AttributeSelectionListPtr aBaseObjectsSelectionList =
492 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
493 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
497 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
498 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
502 ResultPtr aSketchRes = aSketchFeature->results().front();
503 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
504 if(!aConstruction.get()) {
508 if(aBaseObjectsSelectionList->size() == 0) {
509 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
513 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
514 const bool theEnabled)
516 QList<QWidget*> aMyControls = theModelWidget->getControls();
517 foreach(QWidget* eachControl, aMyControls) {
518 eachControl->setEnabled(theEnabled);