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 <Config_WidgetAPI.h>
42 #include <Events_Loop.h>
46 //#include <QFormLayout>
47 #include <QVBoxLayout>
48 #include <QMessageBox>
49 #include <QMainWindow>
51 #define DEBUG_UNDO_INVALID_SKETCH
53 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
54 PartSet_Module* theModule,
55 const Config_WidgetAPI* theData)
56 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
57 myModule(theModule), myIsCustomAttribute(false)
59 myAttributeListID = theData->getProperty("attribute_list_id");
61 //QFormLayout* aLayout = new QFormLayout(this);
62 QVBoxLayout* aLayout = new QVBoxLayout(this);
63 ModuleBase_Tools::zeroMargins(aLayout);
65 ModuleBase_Tools::adjustMargins(aLayout);
67 QString aLabelText = QString::fromStdString(theData->widgetLabel());
68 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
70 myLabel = new QLabel(aLabelText, this);
71 myLabel->setWordWrap(true);
72 aLayout->addWidget(myLabel);
73 aLayout->addStretch(1);
75 std::string aTypes = theData->getProperty("shape_types");
76 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
78 myPreviewPlanes = new PartSet_PreviewPlanes();
81 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
83 // we need to deactivate here in order to hide preview planes if the selection mode is
88 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
90 QList<QWidget*> aControls;
91 aControls.append(myLabel);
95 bool PartSet_WidgetSketchCreator::restoreValueCustom()
100 bool PartSet_WidgetSketchCreator::storeValueCustom() const
105 AttributePtr PartSet_WidgetSketchCreator::attribute() const
107 AttributePtr anAttribute;
108 if (myIsCustomAttribute)
109 anAttribute = myFeature->attribute(myAttributeListID);
111 anAttribute = ModuleBase_WidgetSelector::attribute();
116 //********************************************************************
117 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
119 SessionPtr aMgr = ModelAPI_Session::get();
120 bool aIsOp = aMgr->isOperation();
122 const static std::string aNestedOpID("Parameters modification");
123 aMgr->startOperation(aNestedOpID, true);
127 //********************************************************************
128 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrs& theValue)
131 if (myIsCustomAttribute) {
132 // check only suiting of the value to custom attribute (myAttributeListID)
133 // do not cash of validation to avoid using states, stored for XML attribute
134 // there is an alternative is to call clearValidatedCash() in setSelection()
135 aValid = isValidSelectionForAttribute(theValue, attribute());
137 else { /// if the validated attribute is already custom
138 if (getValidState(theValue, aValid)) {
141 aValid = isValidSelectionCustom(theValue);
143 // check selection to create new sketh (XML current attribute)
144 aValid = isValidSelectionForAttribute(theValue, attribute());
146 // check selection to fill list attribute (myAttributeListID)
147 bool isCustomAttribute = myIsCustomAttribute;
148 myIsCustomAttribute = true;
149 aValid = isValidSelectionForAttribute(theValue, attribute());
150 myIsCustomAttribute = isCustomAttribute;
153 storeValidState(theValue, aValid);
157 //********************************************************************
158 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrs& theValue)
160 return PartSet_WidgetSketchLabel::canFillSketch(theValue);
163 void PartSet_WidgetSketchCreator::activateSelectionControl()
165 // we need to call activate here as the widget has no focus accepted controls
166 // if these controls are added here, activate will happens automatically after focusIn()
167 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
168 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
169 aPanel->activateWidget(this, false);
172 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
174 // hide current widget, activate the next widget
175 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
176 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
177 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
178 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
179 if (theSelectionControl) { // hide other controls
181 aWidget->setVisible(false);
183 else { // hide current control
185 aWidget->setVisible(false);
187 aWidget->setVisible(true);
188 if (aWidget->attributeID() == myAttributeListID)
189 setEnabledModelWidget(aWidget, !hasSubObjects());
194 if (theSelectionControl) {
195 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
196 if (!aBodyIsVisualized) {
197 // We have to select a plane before any operation
198 myPreviewPlanes->showPreviewPlanes(myWorkshop);
201 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
202 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
204 aWorkshop->viewer()->update();
208 QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
210 QIntList aShapeTypes;
211 foreach(QString aType, myShapeTypes) {
212 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
217 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
219 ModuleBase_ModelWidget::setEditingMode(isEditing);
221 setVisibleSelectionControl(false);
223 ModuleBase_ModelWidget* anAttributeListWidget = 0;
224 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
225 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
226 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
227 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
228 if (aWidget->attributeID() == myAttributeListID) {
229 anAttributeListWidget = aWidget;
233 if (anAttributeListWidget)
234 setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
238 bool PartSet_WidgetSketchCreator::isSelectionMode() const
240 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
241 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
243 return !aHasValueInList;
246 bool PartSet_WidgetSketchCreator::hasSubObjects() const
248 bool aHasSubObjects = false;
250 bool aCanSetFocus = true;
251 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
252 if (aComposite.get())
253 aHasSubObjects = aComposite->numberOfSubs() > 0;
254 return aHasSubObjects;
257 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
258 const bool theToValidate)
261 if (!startSketchOperation(theValues)) {
262 myIsCustomAttribute = true;
263 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
264 bool aProcessed = false;
265 for (; anIt != aLast; anIt++) {
266 ModuleBase_ViewerPrs aValue = *anIt;
267 if (!theToValidate || isValidInFilters(aValue))
268 aProcessed = setSelectionCustom(aValue) || aProcessed;
270 myIsCustomAttribute = false;
273 emit valuesChanged();
274 updateObject(myFeature);
275 setVisibleSelectionControl(false);
276 // manually deactivation because the widget was not activated as has no focus acceptin controls
278 emit focusOutWidget(this);
284 //********************************************************************
285 void PartSet_WidgetSketchCreator::onSelectionChanged()
287 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
288 bool isDone = setSelection(aSelected, true/*false*/);
291 //********************************************************************
292 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
296 bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrs>& theValues)
298 bool aSketchStarted = false;
300 if (theValues.size() != 1)
301 return aSketchStarted;
303 ModuleBase_ViewerPrs aValue = theValues.front();
304 if (!PartSet_WidgetSketchLabel::canFillSketch(aValue))
305 return aSketchStarted;
307 aSketchStarted = true;
309 // manually deactivation because the widget was not activated as has no focus acceptin controls
311 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
312 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
314 // Launch Sketch operation
315 CompositeFeaturePtr aCompFeature =
316 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
318 // start edit operation for the sketch
319 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
320 (myModule->createOperation("Sketch"));
321 QList<ModuleBase_ViewerPrs> aValues;
322 aValues.push_back(aValue);
323 aFOperation->setPreselection(aValues);
325 myModule->sendOperation(aFOperation);
327 return aSketchStarted;
330 bool PartSet_WidgetSketchCreator::focusTo()
332 // this method is called only in creation mode. In Edition mode this widget is hidden
333 if (isSelectionMode() && !hasSubObjects()) {
334 setVisibleSelectionControl(true);
335 activateSelectionControl();
336 openExtrusionTransaction();
340 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
345 void PartSet_WidgetSketchCreator::deactivate()
347 ModuleBase_WidgetSelector::deactivate();
349 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
350 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
352 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
356 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
358 SessionPtr aMgr = ModelAPI_Session::get();
359 bool aIsOp = aMgr->isOperation();
361 // in current implementation, all transactions are closed when resume happens
362 // so, this is a wrong case, which is not checked.
363 // To provide it, make correction in later rows about abort/undo transactions
366 // Set visible only selection control
367 setVisibleSelectionControl(true);
369 // Validate the created sketch. If it is valid, it is set into the composite feature selection
370 // list, otherwise it is removed
371 CompositeFeaturePtr aCompFeature =
372 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
373 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
374 if (aCompFeature->numberOfSubs() > 0) {
375 // set the sub feature to attribute selection list and check whether sketch is valid
376 SessionPtr aMgr = ModelAPI_Session::get();
377 const static std::string aNestedOpID("Set Sketch result into Selection list");
378 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
379 setSketchObjectToList(aCompFeature, anAttrList);
380 aMgr->finishOperation();
382 if (!validateSelectionList()) {
383 #ifdef DEBUG_UNDO_INVALID_SKETCH
384 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
385 aMgr->undo(); /// Sketch creation
387 aMgr->startOperation("Delete invalid Sketch feature", false);
389 // delete invalid sketch
390 CompositeFeaturePtr aSketchFeature =
391 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
392 QObjectPtrList anObjects;
393 anObjects.append(aSketchFeature);
395 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
396 aWorkshop->deleteFeatures(anObjects, std::set<FeaturePtr>());
398 aMgr->finishOperation();
402 openExtrusionTransaction();
404 if (aCompFeature->numberOfSubs() == 0) {
405 // call activateWidget() of the parent to connect to the viewer seleciton
406 activateSelectionControl();
409 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
410 /// the attribute selection list will be filled by result of this sketch.
411 setVisibleSelectionControl(false);
413 // Update value in attribute selection list
414 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
415 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
416 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
417 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
418 if (aWidget->attributeID() == myAttributeListID)
419 aWidget->restoreValue();
422 // Hide sketcher result
423 CompositeFeaturePtr aSketchFeature =
424 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
425 std::list<ResultPtr> aResults = aSketchFeature->results();
426 std::list<ResultPtr>::const_iterator aIt;
427 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
428 (*aIt)->setDisplayed(false);
430 aSketchFeature->setDisplayed(false);
431 static Events_Loop* aLoop = Events_Loop::loop();
432 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
434 // Add Selected body were created the sketcher to list of selected objects
435 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
436 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
437 if (aSelList.get()) {
438 DataPtr aData = aSketchFeature->data();
439 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
440 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
441 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
443 SessionPtr aMgr = ModelAPI_Session::get();
444 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
445 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
446 std::string aValidatorID, anError;
447 aSelList->append(aRes, GeomShapePtr());
448 if (aFactory->validate(anAttribute, aValidatorID, anError))
449 updateObject(aCompFeature);
458 bool PartSet_WidgetSketchCreator::validateSelectionList() const
460 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
462 SessionPtr aMgr = ModelAPI_Session::get();
463 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
464 std::string aValidatorID, anError;
465 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
466 if (!isValidPComposite) {
467 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
468 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
469 tr("Sketch is invalid and will be deleted.\nError: %1").arg(anError.c_str()),
472 return isValidPComposite;
475 void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePtr& theCompositeFeature,
476 const AttributePtr& theAttribute)
478 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
481 AttributeSelectionListPtr aBaseObjectsSelectionList =
482 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
483 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
487 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
488 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
492 ResultPtr aSketchRes = aSketchFeature->results().front();
493 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
494 if(!aConstruction.get()) {
498 if(aBaseObjectsSelectionList->size() == 0) {
499 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
503 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
504 const bool theEnabled)
506 QList<QWidget*> aMyControls = theModelWidget->getControls();
507 foreach(QWidget* eachControl, aMyControls) {
508 eachControl->setEnabled(theEnabled);