1 // Copyright (C) 2014-2019 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 QString aType(aWidget->metaObject()->className());
216 if (aType == "ModuleBase_WidgetChoice")
218 if (theSelectionControl) { // hide other controls
220 aWidget->setVisible(false);
222 else { // hide current control
224 aWidget->setVisible(false);
226 aWidget->setVisible(true);
227 if (aWidget->attributeID() == myAttributeListID)
228 setEnabledModelWidget(aWidget, !hasSubObjects());
233 if (theSelectionControl) {
234 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
235 bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
236 if (!aBodyIsVisualized && !aSketchIsVisualized) {
237 // We have to select a plane before any operation
238 myPreviewPlanes->showPreviewPlanes(myWorkshop);
239 mySizeOfViewWidget->setVisible(true);
242 mySizeOfViewWidget->setVisible(false);
246 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
247 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
249 aWorkshop->viewer()->update();
253 QIntList PartSet_WidgetSketchCreator::shapeTypes() const
255 QIntList aShapeTypes;
256 foreach(QString aType, myShapeTypes) {
257 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
262 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
264 ModuleBase_ModelWidget::setEditingMode(isEditing);
266 setVisibleSelectionControl(false);
268 ModuleBase_ModelWidget* anAttributeListWidget = 0;
269 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
270 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
271 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
272 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
273 if (aWidget->attributeID() == myAttributeListID) {
274 anAttributeListWidget = aWidget;
278 if (anAttributeListWidget)
279 setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
283 bool PartSet_WidgetSketchCreator::isSelectionMode() const
285 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
286 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
288 return !aHasValueInList;
291 bool PartSet_WidgetSketchCreator::hasSubObjects() const
293 bool aHasSubObjects = false;
295 bool aCanSetFocus = true;
296 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
297 if (aComposite.get())
298 aHasSubObjects = aComposite->numberOfSubs() > 0;
299 return aHasSubObjects;
302 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
303 const bool theToValidate)
306 if (!startSketchOperation(theValues)) {
307 myIsCustomAttribute = true;
308 QList<ModuleBase_ViewerPrsPtr>::const_iterator
309 anIt = theValues.begin(), aLast = theValues.end();
310 bool aProcessed = false;
311 for (; anIt != aLast; anIt++) {
312 ModuleBase_ViewerPrsPtr aValue = *anIt;
313 if (!theToValidate || isValidInFilters(aValue))
314 aProcessed = setSelectionCustom(aValue) || aProcessed;
316 myIsCustomAttribute = false;
319 emit valuesChanged();
320 updateObject(myFeature);
321 setVisibleSelectionControl(false);
322 // manually deactivation because the widget was
323 // not activated as has no focus acceptin controls
325 emit focusOutWidget(this);
331 //********************************************************************
332 bool PartSet_WidgetSketchCreator::processSelection()
334 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
335 bool isDone = setSelection(aSelected, true/*false*/);
340 //********************************************************************
341 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
345 bool PartSet_WidgetSketchCreator::startSketchOperation(
346 const QList<ModuleBase_ViewerPrsPtr>& theValues)
348 bool aSketchStarted = false;
350 if (theValues.size() != 1)
351 return aSketchStarted;
353 ModuleBase_ViewerPrsPtr aValue = theValues.front();
354 if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
355 return aSketchStarted;
357 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
358 /// sketch should not started by object(face) selected as global. If Local face is selected,
359 /// sketch is started
360 if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
361 ResultConstructionPtr aConstruction =
362 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
363 if (!aConstruction.get() || !aConstruction->isInfinite())
364 return aSketchStarted;
366 aSketchStarted = true;
367 // Set View size if a plane is selected
368 if (myPreviewPlanes->isPreviewDisplayed() &&
369 myPreviewPlanes->isPreviewShape(aValue->shape())) {
370 // set default plane size
371 bool isSetSizeOfView = false;
372 double aSizeOfView = 0;
373 QString aSizeOfViewStr = mySizeOfView->text();
374 if (!aSizeOfViewStr.isEmpty()) {
375 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
376 if (isSetSizeOfView && aSizeOfView <= 0) {
377 isSetSizeOfView = false;
381 myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
383 // manually deactivation because the widget was not activated as has no focus acceptin controls
385 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
386 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
388 // start edit operation for the sketch
389 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
390 (myModule->createOperation("Sketch"));
391 QList<ModuleBase_ViewerPrsPtr> aValues;
392 aValues.push_back(aValue);
393 aFOperation->setPreselection(aValues);
395 myWorkshop->processLaunchOperation(aFOperation);
397 return aSketchStarted;
400 bool PartSet_WidgetSketchCreator::focusTo()
402 // this method is called only in creation mode. In Edition mode this widget is hidden
403 if (isSelectionMode() && !hasSubObjects()) {
404 setVisibleSelectionControl(true);
405 activateSelectionControl();
406 openExtrusionTransaction();
410 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
411 SLOT(onResumed(ModuleBase_Operation*)));
416 void PartSet_WidgetSketchCreator::deactivate()
418 ModuleBase_WidgetSelector::deactivate();
420 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
421 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
423 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
427 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
429 SessionPtr aMgr = ModelAPI_Session::get();
430 bool aIsOp = aMgr->isOperation();
432 // in current implementation, all transactions are closed when resume happens
433 // so, this is a wrong case, which is not checked.
434 // To provide it, make correction in later rows about abort/undo transactions
437 // Set visible only selection control
438 setVisibleSelectionControl(true);
440 // Validate the created sketch. If it is valid, it is set into the composite feature selection
441 // list, otherwise it is removed
442 CompositeFeaturePtr aCompFeature =
443 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
444 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
445 if (aCompFeature->numberOfSubs() > 0) {
446 // set the sub feature to attribute selection list and check whether sketch is valid
447 SessionPtr aMgr = ModelAPI_Session::get();
448 const static std::string aNestedOpID("Set Sketch result into Selection list");
449 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
450 setSketchObjectToList(aCompFeature, anAttrList);
451 aMgr->finishOperation();
453 if (!validateSelectionList()) {
454 #ifdef DEBUG_UNDO_INVALID_SKETCH
455 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
456 aMgr->undo(); /// Sketch creation
458 aMgr->startOperation("Delete invalid Sketch feature", false);
460 // delete invalid sketch
461 CompositeFeaturePtr aSketchFeature =
462 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
463 QObjectPtrList anObjects;
464 anObjects.append(aSketchFeature);
466 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
467 aWorkshop->deleteFeatures(anObjects);
469 aMgr->finishOperation();
473 openExtrusionTransaction();
475 if (aCompFeature->numberOfSubs() == 0) {
476 // call activateWidget() of the parent to connect to the viewer seleciton
477 activateSelectionControl();
480 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
481 /// the attribute selection list will be filled by result of this sketch.
482 setVisibleSelectionControl(false);
484 // Update value in attribute selection list
485 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
486 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
487 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
488 ModuleBase_ModelWidget* aListWidget = 0;
489 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
490 if (aWidget->attributeID() == myAttributeListID) {
491 aListWidget = aWidget;
496 aListWidget->restoreValue();
497 aPropertyPanel->activateNextWidget(aListWidget);
500 // Hide sketcher result
501 CompositeFeaturePtr aSketchFeature =
502 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
503 std::list<ResultPtr> aResults = aSketchFeature->results();
504 std::list<ResultPtr>::const_iterator aIt;
505 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
506 (*aIt)->setDisplayed(false);
508 aSketchFeature->setDisplayed(false);
509 static Events_Loop* aLoop = Events_Loop::loop();
510 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
512 // Add Selected body were created the sketcher to list of selected objects
513 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
514 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
515 if (aSelList.get()) {
516 DataPtr aData = aSketchFeature->data();
517 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
518 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
519 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
521 SessionPtr aMgr = ModelAPI_Session::get();
522 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
523 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
524 std::string aValidatorID;
525 Events_InfoMessage anError;
526 aSelList->append(aRes, GeomShapePtr());
527 if (aFactory->validate(anAttribute, aValidatorID, anError))
528 updateObject(aCompFeature);
537 bool PartSet_WidgetSketchCreator::validateSelectionList() const
539 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
541 SessionPtr aMgr = ModelAPI_Session::get();
542 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
543 std::string aValidatorID;
544 Events_InfoMessage anError;
545 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
546 if (!isValidPComposite) {
547 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
548 // TODO(spo): translate
549 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
550 tr("Sketch is invalid and will be deleted.\nError: %1")
551 .arg(anError.messageString().c_str()),
554 return isValidPComposite;
557 void PartSet_WidgetSketchCreator::setSketchObjectToList(
558 const CompositeFeaturePtr& theCompositeFeature,
559 const AttributePtr& theAttribute)
561 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
564 AttributeSelectionListPtr aBaseObjectsSelectionList =
565 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
566 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
570 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
571 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
575 ResultPtr aSketchRes = aSketchFeature->results().front();
576 ResultConstructionPtr aConstruction =
577 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
578 if(!aConstruction.get()) {
582 if(aBaseObjectsSelectionList->size() == 0) {
583 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
587 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
588 const bool theEnabled)
590 QList<QWidget*> aMyControls = theModelWidget->getControls();
591 foreach(QWidget* eachControl, aMyControls) {
592 eachControl->setEnabled(theEnabled);