1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "PartSet_WidgetSketchCreator.h"
21 #include "PartSet_Module.h"
22 #include "PartSet_WidgetSketchLabel.h"
23 #include "PartSet_PreviewPlanes.h"
25 #include <Config_Keywords.h>
27 #include <XGUI_ModuleConnector.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_Displayer.h>
30 #include <XGUI_SelectionMgr.h>
31 #include <XGUI_OperationMgr.h>
32 #include <XGUI_PropertyPanel.h>
33 #include <XGUI_Tools.h>
34 #include <XGUI_ViewerProxy.h>
36 #include <GeomAPI_Face.h>
38 #include <Events_InfoMessage.h>
40 #include <ModelAPI_Session.h>
41 #include <ModelAPI_ResultBody.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_Validator.h>
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_ResultConstruction.h>
48 #include <SketchPlugin_SketchEntity.h>
49 #include <FeaturesPlugin_CompositeBoolean.h>
51 #include <ModuleBase_Tools.h>
52 #include <ModuleBase_Operation.h>
53 #include <ModuleBase_IPropertyPanel.h>
54 #include <ModuleBase_OperationFeature.h>
55 #include <ModuleBase_ViewerPrs.h>
57 #include <Config_WidgetAPI.h>
59 #include <Events_Loop.h>
63 #include <QDoubleValidator>
64 //#include <QFormLayout>
65 #include <QVBoxLayout>
66 #include <QMessageBox>
67 #include <QMainWindow>
69 #define DEBUG_UNDO_INVALID_SKETCH
71 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
72 PartSet_Module* theModule,
73 const Config_WidgetAPI* theData)
74 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
75 myModule(theModule), myIsCustomAttribute(false)
77 myAttributeListID = theData->getProperty("attribute_list_id");
79 //QFormLayout* aLayout = new QFormLayout(this);
80 QVBoxLayout* aLayout = new QVBoxLayout(this);
81 ModuleBase_Tools::zeroMargins(aLayout);
83 ModuleBase_Tools::adjustMargins(aLayout);
85 QString aLabelText = translate(theData->widgetLabel());
86 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
88 // Size of the View control
89 mySizeOfViewWidget = new QWidget(this);
90 QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
91 aSizeLayout->addWidget(new QLabel(tr("Size of the view"), mySizeOfViewWidget));
92 mySizeOfView = new QLineEdit(mySizeOfViewWidget);
94 QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
95 aValidator->setLocale(ModuleBase_Tools::doubleLocale());
96 aValidator->setNotation(QDoubleValidator::StandardNotation);
97 mySizeOfView->setValidator(aValidator);
98 aSizeLayout->addWidget(mySizeOfView);
100 myLabel = new QLabel(aLabelText, this);
101 myLabel->setWordWrap(true);
103 aLayout->addWidget(mySizeOfViewWidget);
104 aLayout->addWidget(myLabel);
105 aLayout->addStretch(1);
107 std::string aTypes = theData->getProperty("shape_types");
108 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
110 myPreviewPlanes = new PartSet_PreviewPlanes();
113 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
115 // we need to deactivate here in order to hide preview planes if the selection mode is
120 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
122 QList<QWidget*> aControls;
123 aControls.append(myLabel);
127 bool PartSet_WidgetSketchCreator::restoreValueCustom()
132 bool PartSet_WidgetSketchCreator::storeValueCustom()
137 AttributePtr PartSet_WidgetSketchCreator::attribute() const
139 AttributePtr anAttribute;
140 if (myIsCustomAttribute)
141 anAttribute = myFeature->attribute(myAttributeListID);
143 anAttribute = ModuleBase_WidgetSelector::attribute();
148 //********************************************************************
149 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
151 SessionPtr aMgr = ModelAPI_Session::get();
152 bool aIsOp = aMgr->isOperation();
154 const static std::string aNestedOpID("Parameters modification");
155 aMgr->startOperation(aNestedOpID, true);
159 //********************************************************************
160 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
163 if (myIsCustomAttribute) {
164 // check only suiting of the value to custom attribute (myAttributeListID)
165 // do not cash of validation to avoid using states, stored for XML attribute
166 // there is an alternative is to call clearValidatedCash() in setSelection()
167 aValid = isValidSelectionForAttribute(theValue, attribute());
169 else { /// if the validated attribute is already custom
170 if (getValidState(theValue, aValid)) {
173 aValid = isValidSelectionCustom(theValue);
175 // check selection to create new sketh (XML current attribute)
176 aValid = isValidSelectionForAttribute(theValue, attribute());
178 // check selection to fill list attribute (myAttributeListID)
179 bool isCustomAttribute = myIsCustomAttribute;
180 myIsCustomAttribute = true;
181 aValid = isValidSelectionForAttribute(theValue, attribute());
182 myIsCustomAttribute = isCustomAttribute;
185 storeValidState(theValue, aValid);
189 //********************************************************************
190 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
192 return PartSet_WidgetSketchLabel::canFillSketch(theValue);
195 void PartSet_WidgetSketchCreator::activateSelectionControl()
197 // reset previously set size of view needed on restart extrusion after Sketch
198 if (myModule->sketchMgr()->previewSketchPlane()->isUseSizeOfView())
199 myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(0, false);
201 // we need to call activate here as the widget has no focus accepted controls
202 // if these controls are added here, activate will happens automatically after focusIn()
203 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
204 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
205 aPanel->activateWidget(this, false);
208 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
210 // hide current widget, activate the next widget
211 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
212 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
213 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
214 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
215 if (theSelectionControl) { // hide other controls
217 aWidget->setVisible(false);
219 else { // hide current control
221 aWidget->setVisible(false);
223 aWidget->setVisible(true);
224 if (aWidget->attributeID() == myAttributeListID)
225 setEnabledModelWidget(aWidget, !hasSubObjects());
230 if (theSelectionControl) {
231 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
232 bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
233 if (!aBodyIsVisualized && !aSketchIsVisualized) {
234 // We have to select a plane before any operation
235 myPreviewPlanes->showPreviewPlanes(myWorkshop);
236 mySizeOfViewWidget->setVisible(true);
239 mySizeOfViewWidget->setVisible(false);
243 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
244 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
246 aWorkshop->viewer()->update();
250 QIntList PartSet_WidgetSketchCreator::shapeTypes() const
252 QIntList aShapeTypes;
253 foreach(QString aType, myShapeTypes) {
254 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
259 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
261 ModuleBase_ModelWidget::setEditingMode(isEditing);
263 setVisibleSelectionControl(false);
265 ModuleBase_ModelWidget* anAttributeListWidget = 0;
266 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
267 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
268 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
269 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
270 if (aWidget->attributeID() == myAttributeListID) {
271 anAttributeListWidget = aWidget;
275 if (anAttributeListWidget)
276 setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
280 bool PartSet_WidgetSketchCreator::isSelectionMode() const
282 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
283 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
285 return !aHasValueInList;
288 bool PartSet_WidgetSketchCreator::hasSubObjects() const
290 bool aHasSubObjects = false;
291 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
292 if (aComposite.get())
293 aHasSubObjects = aComposite->numberOfSubs() > 0;
294 return aHasSubObjects;
297 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
298 const bool theToValidate)
301 if (!startSketchOperation(theValues)) {
302 myIsCustomAttribute = true;
303 QList<ModuleBase_ViewerPrsPtr>::const_iterator
304 anIt = theValues.begin(), aLast = theValues.end();
305 bool aProcessed = false;
306 for (; anIt != aLast; anIt++) {
307 ModuleBase_ViewerPrsPtr aValue = *anIt;
308 if (!theToValidate || isValidInFilters(aValue))
309 aProcessed = setSelectionCustom(aValue) || aProcessed;
311 myIsCustomAttribute = false;
314 emit valuesChanged();
315 updateObject(myFeature);
316 setVisibleSelectionControl(false);
317 // manually deactivation because the widget was
318 // not activated as has no focus acceptin controls
320 emit focusOutWidget(this);
326 //********************************************************************
327 bool PartSet_WidgetSketchCreator::processSelection()
329 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
330 bool isDone = setSelection(aSelected, true/*false*/);
335 //********************************************************************
336 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
340 bool PartSet_WidgetSketchCreator::startSketchOperation(
341 const QList<ModuleBase_ViewerPrsPtr>& theValues)
343 bool aSketchStarted = false;
345 if (theValues.size() != 1)
346 return aSketchStarted;
348 ModuleBase_ViewerPrsPtr aValue = theValues.front();
349 if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
350 return aSketchStarted;
352 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
353 /// sketch should not started by object(face) selected as global. If Local face is selected,
354 /// sketch is started
355 if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
356 ResultConstructionPtr aConstruction =
357 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
358 if (!aConstruction.get() || !aConstruction->isInfinite())
359 return aSketchStarted;
361 aSketchStarted = true;
362 // Set View size if a plane is selected
363 if (myPreviewPlanes->isPreviewDisplayed() &&
364 myPreviewPlanes->isPreviewShape(aValue->shape())) {
365 // set default plane size
366 bool isSetSizeOfView = false;
367 double aSizeOfView = 0;
368 QString aSizeOfViewStr = mySizeOfView->text();
369 if (!aSizeOfViewStr.isEmpty()) {
370 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
371 if (isSetSizeOfView && aSizeOfView <= 0) {
372 isSetSizeOfView = false;
376 myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
378 // manually deactivation because the widget was not activated as has no focus acceptin controls
380 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
382 // start edit operation for the sketch
383 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
384 (myModule->createOperation("Sketch"));
385 QList<ModuleBase_ViewerPrsPtr> aValues;
386 aValues.push_back(aValue);
387 aFOperation->setPreselection(aValues);
389 myWorkshop->processLaunchOperation(aFOperation);
391 return aSketchStarted;
394 bool PartSet_WidgetSketchCreator::focusTo()
396 // this method is called only in creation mode. In Edition mode this widget is hidden
397 if (isSelectionMode() && !hasSubObjects()) {
398 setVisibleSelectionControl(true);
399 activateSelectionControl();
400 openExtrusionTransaction();
404 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
405 SLOT(onResumed(ModuleBase_Operation*)));
410 void PartSet_WidgetSketchCreator::deactivate()
412 ModuleBase_WidgetSelector::deactivate();
414 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
415 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
417 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
421 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
423 SessionPtr aMgr = ModelAPI_Session::get();
424 bool aIsOp = aMgr->isOperation();
426 // in current implementation, all transactions are closed when resume happens
427 // so, this is a wrong case, which is not checked.
428 // To provide it, make correction in later rows about abort/undo transactions
431 // Set visible only selection control
432 setVisibleSelectionControl(true);
434 // Validate the created sketch. If it is valid, it is set into the composite feature selection
435 // list, otherwise it is removed
436 CompositeFeaturePtr aCompFeature =
437 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
438 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
439 if (aCompFeature->numberOfSubs() > 0) {
440 // set the sub feature to attribute selection list and check whether sketch is valid
441 const static std::string aNestedOpID("Set Sketch result into Selection list");
442 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
443 setSketchObjectToList(aCompFeature, anAttrList);
444 aMgr->finishOperation();
446 if (!validateSelectionList()) {
447 #ifdef DEBUG_UNDO_INVALID_SKETCH
448 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
449 aMgr->undo(); /// Sketch creation
451 aMgr->startOperation("Delete invalid Sketch feature", false);
453 // delete invalid sketch
454 CompositeFeaturePtr aSketchFeature =
455 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
456 QObjectPtrList anObjects;
457 anObjects.append(aSketchFeature);
459 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
460 aWorkshop->deleteFeatures(anObjects);
462 aMgr->finishOperation();
466 openExtrusionTransaction();
468 if (aCompFeature->numberOfSubs() == 0) {
469 // call activateWidget() of the parent to connect to the viewer seleciton
470 activateSelectionControl();
473 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
474 /// the attribute selection list will be filled by result of this sketch.
475 setVisibleSelectionControl(false);
477 // Update value in attribute selection list
478 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
479 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
480 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
481 ModuleBase_ModelWidget* aListWidget = 0;
482 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
483 if (aWidget->attributeID() == myAttributeListID) {
484 aListWidget = aWidget;
489 aListWidget->restoreValue();
490 aPropertyPanel->activateNextWidget(aListWidget);
493 // Hide sketcher result
494 CompositeFeaturePtr aSketchFeature =
495 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
496 std::list<ResultPtr> aResults = aSketchFeature->results();
497 std::list<ResultPtr>::const_iterator aIt;
498 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
499 (*aIt)->setDisplayed(false);
501 aSketchFeature->setDisplayed(false);
502 static Events_Loop* aLoop = Events_Loop::loop();
503 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
505 // Add Selected body were created the sketcher to list of selected objects
506 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
507 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
508 if (aSelList.get()) {
509 DataPtr aData = aSketchFeature->data();
510 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
511 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
512 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
514 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
515 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
516 std::string aValidatorID;
517 Events_InfoMessage anError;
518 aSelList->append(aRes, GeomShapePtr());
519 if (aFactory->validate(anAttribute, aValidatorID, anError))
520 updateObject(aCompFeature);
529 bool PartSet_WidgetSketchCreator::validateSelectionList() const
531 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
533 SessionPtr aMgr = ModelAPI_Session::get();
534 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
535 std::string aValidatorID;
536 Events_InfoMessage anError;
537 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
538 if (!isValidPComposite) {
539 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
540 // TODO(spo): translate
541 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
542 tr("Sketch is invalid and will be deleted.\nError: %1")
543 .arg(anError.messageString().c_str()),
546 return isValidPComposite;
549 void PartSet_WidgetSketchCreator::setSketchObjectToList(
550 const CompositeFeaturePtr& theCompositeFeature,
551 const AttributePtr& theAttribute)
553 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
556 AttributeSelectionListPtr aBaseObjectsSelectionList =
557 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
558 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
562 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
563 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
567 ResultPtr aSketchRes = aSketchFeature->results().front();
568 ResultConstructionPtr aConstruction =
569 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
570 if(!aConstruction.get()) {
574 if(aBaseObjectsSelectionList->size() == 0) {
575 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
579 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
580 const bool theEnabled)
582 QList<QWidget*> aMyControls = theModelWidget->getControls();
583 foreach(QWidget* eachControl, aMyControls) {
584 eachControl->setEnabled(theEnabled);