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 <Events_InfoMessage.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_AttributeSelection.h>
30 #include <ModelAPI_AttributeSelectionList.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_Events.h>
33 #include <ModelAPI_ResultConstruction.h>
35 #include <SketchPlugin_SketchEntity.h>
36 #include <FeaturesPlugin_CompositeBoolean.h>
38 #include <ModuleBase_Tools.h>
39 #include <ModuleBase_Operation.h>
40 #include <ModuleBase_IPropertyPanel.h>
41 #include <ModuleBase_OperationFeature.h>
42 #include <ModuleBase_ViewerPrs.h>
44 #include <Config_WidgetAPI.h>
46 #include <Events_Loop.h>
50 //#include <QFormLayout>
51 #include <QVBoxLayout>
52 #include <QMessageBox>
53 #include <QMainWindow>
55 #define DEBUG_UNDO_INVALID_SKETCH
57 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
58 PartSet_Module* theModule,
59 const Config_WidgetAPI* theData)
60 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
61 myModule(theModule), myIsCustomAttribute(false)
63 myAttributeListID = theData->getProperty("attribute_list_id");
65 //QFormLayout* aLayout = new QFormLayout(this);
66 QVBoxLayout* aLayout = new QVBoxLayout(this);
67 ModuleBase_Tools::zeroMargins(aLayout);
69 ModuleBase_Tools::adjustMargins(aLayout);
71 QString aLabelText = QString::fromStdString(theData->widgetLabel());
72 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
74 myLabel = new QLabel(aLabelText, this);
75 myLabel->setWordWrap(true);
76 aLayout->addWidget(myLabel);
77 aLayout->addStretch(1);
79 std::string aTypes = theData->getProperty("shape_types");
80 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
82 myPreviewPlanes = new PartSet_PreviewPlanes();
85 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
87 // we need to deactivate here in order to hide preview planes if the selection mode is
92 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
94 QList<QWidget*> aControls;
95 aControls.append(myLabel);
99 bool PartSet_WidgetSketchCreator::restoreValueCustom()
104 bool PartSet_WidgetSketchCreator::storeValueCustom()
109 AttributePtr PartSet_WidgetSketchCreator::attribute() const
111 AttributePtr anAttribute;
112 if (myIsCustomAttribute)
113 anAttribute = myFeature->attribute(myAttributeListID);
115 anAttribute = ModuleBase_WidgetSelector::attribute();
120 //********************************************************************
121 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
123 SessionPtr aMgr = ModelAPI_Session::get();
124 bool aIsOp = aMgr->isOperation();
126 const static std::string aNestedOpID("Parameters modification");
127 aMgr->startOperation(aNestedOpID, true);
131 //********************************************************************
132 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
135 if (myIsCustomAttribute) {
136 // check only suiting of the value to custom attribute (myAttributeListID)
137 // do not cash of validation to avoid using states, stored for XML attribute
138 // there is an alternative is to call clearValidatedCash() in setSelection()
139 aValid = isValidSelectionForAttribute(theValue, attribute());
141 else { /// if the validated attribute is already custom
142 if (getValidState(theValue, aValid)) {
145 aValid = isValidSelectionCustom(theValue);
147 // check selection to create new sketh (XML current attribute)
148 aValid = isValidSelectionForAttribute(theValue, attribute());
150 // check selection to fill list attribute (myAttributeListID)
151 bool isCustomAttribute = myIsCustomAttribute;
152 myIsCustomAttribute = true;
153 aValid = isValidSelectionForAttribute(theValue, attribute());
154 myIsCustomAttribute = isCustomAttribute;
157 storeValidState(theValue, aValid);
161 //********************************************************************
162 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
164 return PartSet_WidgetSketchLabel::canFillSketch(theValue);
167 void PartSet_WidgetSketchCreator::activateSelectionControl()
169 // we need to call activate here as the widget has no focus accepted controls
170 // if these controls are added here, activate will happens automatically after focusIn()
171 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
172 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
173 aPanel->activateWidget(this, false);
176 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
178 // hide current widget, activate the next widget
179 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
180 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
181 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
182 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
183 if (theSelectionControl) { // hide other controls
185 aWidget->setVisible(false);
187 else { // hide current control
189 aWidget->setVisible(false);
191 aWidget->setVisible(true);
192 if (aWidget->attributeID() == myAttributeListID)
193 setEnabledModelWidget(aWidget, !hasSubObjects());
198 if (theSelectionControl) {
199 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
200 bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
201 if (!aBodyIsVisualized && !aSketchIsVisualized) {
202 // We have to select a plane before any operation
203 myPreviewPlanes->showPreviewPlanes(myWorkshop);
206 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
207 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
209 aWorkshop->viewer()->update();
213 QIntList PartSet_WidgetSketchCreator::shapeTypes() const
215 QIntList aShapeTypes;
216 foreach(QString aType, myShapeTypes) {
217 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
222 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
224 ModuleBase_ModelWidget::setEditingMode(isEditing);
226 setVisibleSelectionControl(false);
228 ModuleBase_ModelWidget* anAttributeListWidget = 0;
229 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
230 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
231 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
232 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
233 if (aWidget->attributeID() == myAttributeListID) {
234 anAttributeListWidget = aWidget;
238 if (anAttributeListWidget)
239 setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
243 bool PartSet_WidgetSketchCreator::isSelectionMode() const
245 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
246 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
248 return !aHasValueInList;
251 bool PartSet_WidgetSketchCreator::hasSubObjects() const
253 bool aHasSubObjects = false;
255 bool aCanSetFocus = true;
256 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
257 if (aComposite.get())
258 aHasSubObjects = aComposite->numberOfSubs() > 0;
259 return aHasSubObjects;
262 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
263 const bool theToValidate)
266 if (!startSketchOperation(theValues)) {
267 myIsCustomAttribute = true;
268 QList<ModuleBase_ViewerPrsPtr>::const_iterator
269 anIt = theValues.begin(), aLast = theValues.end();
270 bool aProcessed = false;
271 for (; anIt != aLast; anIt++) {
272 ModuleBase_ViewerPrsPtr aValue = *anIt;
273 if (!theToValidate || isValidInFilters(aValue))
274 aProcessed = setSelectionCustom(aValue) || aProcessed;
276 myIsCustomAttribute = false;
279 emit valuesChanged();
280 updateObject(myFeature);
281 setVisibleSelectionControl(false);
282 // manually deactivation because the widget was
283 // not activated as has no focus acceptin controls
285 emit focusOutWidget(this);
291 //********************************************************************
292 void PartSet_WidgetSketchCreator::onSelectionChanged()
294 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
295 bool isDone = setSelection(aSelected, true/*false*/);
298 //********************************************************************
299 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
303 bool PartSet_WidgetSketchCreator::startSketchOperation(
304 const QList<ModuleBase_ViewerPrsPtr>& theValues)
306 bool aSketchStarted = false;
308 if (theValues.size() != 1)
309 return aSketchStarted;
311 ModuleBase_ViewerPrsPtr aValue = theValues.front();
312 if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
313 return aSketchStarted;
315 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
316 /// sketch should not started by object(face) selected as global. If Local face is selected,
317 /// sketch is started
318 if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
319 ResultConstructionPtr aConstruction =
320 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
321 if (!aConstruction.get() || !aConstruction->isInfinite())
322 return aSketchStarted;
324 aSketchStarted = true;
326 // manually deactivation because the widget was not activated as has no focus acceptin controls
328 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
329 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
331 // Launch Sketch operation
332 CompositeFeaturePtr aCompFeature =
333 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
335 // start edit operation for the sketch
336 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
337 (myModule->createOperation("Sketch"));
338 QList<ModuleBase_ViewerPrsPtr> aValues;
339 aValues.push_back(aValue);
340 aFOperation->setPreselection(aValues);
342 myWorkshop->processLaunchOperation(aFOperation);
344 return aSketchStarted;
347 bool PartSet_WidgetSketchCreator::focusTo()
349 // this method is called only in creation mode. In Edition mode this widget is hidden
350 if (isSelectionMode() && !hasSubObjects()) {
351 setVisibleSelectionControl(true);
352 activateSelectionControl();
353 openExtrusionTransaction();
357 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
358 SLOT(onResumed(ModuleBase_Operation*)));
363 void PartSet_WidgetSketchCreator::deactivate()
365 ModuleBase_WidgetSelector::deactivate();
367 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
368 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
370 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
374 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
376 SessionPtr aMgr = ModelAPI_Session::get();
377 bool aIsOp = aMgr->isOperation();
379 // in current implementation, all transactions are closed when resume happens
380 // so, this is a wrong case, which is not checked.
381 // To provide it, make correction in later rows about abort/undo transactions
384 // Set visible only selection control
385 setVisibleSelectionControl(true);
387 // Validate the created sketch. If it is valid, it is set into the composite feature selection
388 // list, otherwise it is removed
389 CompositeFeaturePtr aCompFeature =
390 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
391 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
392 if (aCompFeature->numberOfSubs() > 0) {
393 // set the sub feature to attribute selection list and check whether sketch is valid
394 SessionPtr aMgr = ModelAPI_Session::get();
395 const static std::string aNestedOpID("Set Sketch result into Selection list");
396 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
397 setSketchObjectToList(aCompFeature, anAttrList);
398 aMgr->finishOperation();
400 if (!validateSelectionList()) {
401 #ifdef DEBUG_UNDO_INVALID_SKETCH
402 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
403 aMgr->undo(); /// Sketch creation
405 aMgr->startOperation("Delete invalid Sketch feature", false);
407 // delete invalid sketch
408 CompositeFeaturePtr aSketchFeature =
409 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
410 QObjectPtrList anObjects;
411 anObjects.append(aSketchFeature);
413 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
414 aWorkshop->deleteFeatures(anObjects);
416 aMgr->finishOperation();
420 openExtrusionTransaction();
422 if (aCompFeature->numberOfSubs() == 0) {
423 // call activateWidget() of the parent to connect to the viewer seleciton
424 activateSelectionControl();
427 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
428 /// the attribute selection list will be filled by result of this sketch.
429 setVisibleSelectionControl(false);
431 // Update value in attribute selection list
432 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
433 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
434 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
435 ModuleBase_ModelWidget* aListWidget = 0;
436 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
437 if (aWidget->attributeID() == myAttributeListID) {
438 aListWidget = aWidget;
443 aListWidget->restoreValue();
444 aPropertyPanel->activateNextWidget(aListWidget);
447 // Hide sketcher result
448 CompositeFeaturePtr aSketchFeature =
449 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
450 std::list<ResultPtr> aResults = aSketchFeature->results();
451 std::list<ResultPtr>::const_iterator aIt;
452 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
453 (*aIt)->setDisplayed(false);
455 aSketchFeature->setDisplayed(false);
456 static Events_Loop* aLoop = Events_Loop::loop();
457 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
459 // Add Selected body were created the sketcher to list of selected objects
460 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
461 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
462 if (aSelList.get()) {
463 DataPtr aData = aSketchFeature->data();
464 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
465 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
466 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
468 SessionPtr aMgr = ModelAPI_Session::get();
469 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
470 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
471 std::string aValidatorID;
472 Events_InfoMessage anError;
473 aSelList->append(aRes, GeomShapePtr());
474 if (aFactory->validate(anAttribute, aValidatorID, anError))
475 updateObject(aCompFeature);
484 bool PartSet_WidgetSketchCreator::validateSelectionList() const
486 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
488 SessionPtr aMgr = ModelAPI_Session::get();
489 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
490 std::string aValidatorID;
491 Events_InfoMessage anError;
492 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
493 if (!isValidPComposite) {
494 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
495 // TODO(spo): translate
496 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
497 tr("Sketch is invalid and will be deleted.\nError: %1")
498 .arg(anError.messageString().c_str()),
501 return isValidPComposite;
504 void PartSet_WidgetSketchCreator::setSketchObjectToList(
505 const CompositeFeaturePtr& theCompositeFeature,
506 const AttributePtr& theAttribute)
508 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
511 AttributeSelectionListPtr aBaseObjectsSelectionList =
512 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
513 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
517 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
518 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
522 ResultPtr aSketchRes = aSketchFeature->results().front();
523 ResultConstructionPtr aConstruction =
524 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
525 if(!aConstruction.get()) {
529 if(aBaseObjectsSelectionList->size() == 0) {
530 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
534 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
535 const bool theEnabled)
537 QList<QWidget*> aMyControls = theModelWidget->getControls();
538 foreach(QWidget* eachControl, aMyControls) {
539 eachControl->setEnabled(theEnabled);