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 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;
292 bool aCanSetFocus = true;
293 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
294 if (aComposite.get())
295 aHasSubObjects = aComposite->numberOfSubs() > 0;
296 return aHasSubObjects;
299 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
300 const bool theToValidate)
303 if (!startSketchOperation(theValues)) {
304 myIsCustomAttribute = true;
305 QList<ModuleBase_ViewerPrsPtr>::const_iterator
306 anIt = theValues.begin(), aLast = theValues.end();
307 bool aProcessed = false;
308 for (; anIt != aLast; anIt++) {
309 ModuleBase_ViewerPrsPtr aValue = *anIt;
310 if (!theToValidate || isValidInFilters(aValue))
311 aProcessed = setSelectionCustom(aValue) || aProcessed;
313 myIsCustomAttribute = false;
316 emit valuesChanged();
317 updateObject(myFeature);
318 setVisibleSelectionControl(false);
319 // manually deactivation because the widget was
320 // not activated as has no focus acceptin controls
322 emit focusOutWidget(this);
328 //********************************************************************
329 bool PartSet_WidgetSketchCreator::processSelection()
331 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
332 bool isDone = setSelection(aSelected, true/*false*/);
337 //********************************************************************
338 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
342 bool PartSet_WidgetSketchCreator::startSketchOperation(
343 const QList<ModuleBase_ViewerPrsPtr>& theValues)
345 bool aSketchStarted = false;
347 if (theValues.size() != 1)
348 return aSketchStarted;
350 ModuleBase_ViewerPrsPtr aValue = theValues.front();
351 if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
352 return aSketchStarted;
354 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
355 /// sketch should not started by object(face) selected as global. If Local face is selected,
356 /// sketch is started
357 if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
358 ResultConstructionPtr aConstruction =
359 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
360 if (!aConstruction.get() || !aConstruction->isInfinite())
361 return aSketchStarted;
363 aSketchStarted = true;
364 // Set View size if a plane is selected
365 if (myPreviewPlanes->isPreviewDisplayed() &&
366 myPreviewPlanes->isPreviewShape(aValue->shape())) {
367 // set default plane size
368 bool isSetSizeOfView = false;
369 double aSizeOfView = 0;
370 QString aSizeOfViewStr = mySizeOfView->text();
371 if (!aSizeOfViewStr.isEmpty()) {
372 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
373 if (isSetSizeOfView && aSizeOfView <= 0) {
374 isSetSizeOfView = false;
378 myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
380 // manually deactivation because the widget was not activated as has no focus acceptin controls
382 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
383 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
385 // start edit operation for the sketch
386 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
387 (myModule->createOperation("Sketch"));
388 QList<ModuleBase_ViewerPrsPtr> aValues;
389 aValues.push_back(aValue);
390 aFOperation->setPreselection(aValues);
392 myWorkshop->processLaunchOperation(aFOperation);
394 return aSketchStarted;
397 bool PartSet_WidgetSketchCreator::focusTo()
399 // this method is called only in creation mode. In Edition mode this widget is hidden
400 if (isSelectionMode() && !hasSubObjects()) {
401 setVisibleSelectionControl(true);
402 activateSelectionControl();
403 openExtrusionTransaction();
407 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
408 SLOT(onResumed(ModuleBase_Operation*)));
413 void PartSet_WidgetSketchCreator::deactivate()
415 ModuleBase_WidgetSelector::deactivate();
417 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
418 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
420 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
424 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
426 SessionPtr aMgr = ModelAPI_Session::get();
427 bool aIsOp = aMgr->isOperation();
429 // in current implementation, all transactions are closed when resume happens
430 // so, this is a wrong case, which is not checked.
431 // To provide it, make correction in later rows about abort/undo transactions
434 // Set visible only selection control
435 setVisibleSelectionControl(true);
437 // Validate the created sketch. If it is valid, it is set into the composite feature selection
438 // list, otherwise it is removed
439 CompositeFeaturePtr aCompFeature =
440 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
441 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
442 if (aCompFeature->numberOfSubs() > 0) {
443 // set the sub feature to attribute selection list and check whether sketch is valid
444 SessionPtr aMgr = ModelAPI_Session::get();
445 const static std::string aNestedOpID("Set Sketch result into Selection list");
446 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
447 setSketchObjectToList(aCompFeature, anAttrList);
448 aMgr->finishOperation();
450 if (!validateSelectionList()) {
451 #ifdef DEBUG_UNDO_INVALID_SKETCH
452 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
453 aMgr->undo(); /// Sketch creation
455 aMgr->startOperation("Delete invalid Sketch feature", false);
457 // delete invalid sketch
458 CompositeFeaturePtr aSketchFeature =
459 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
460 QObjectPtrList anObjects;
461 anObjects.append(aSketchFeature);
463 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
464 aWorkshop->deleteFeatures(anObjects);
466 aMgr->finishOperation();
470 openExtrusionTransaction();
472 if (aCompFeature->numberOfSubs() == 0) {
473 // call activateWidget() of the parent to connect to the viewer seleciton
474 activateSelectionControl();
477 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
478 /// the attribute selection list will be filled by result of this sketch.
479 setVisibleSelectionControl(false);
481 // Update value in attribute selection list
482 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
483 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
484 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
485 ModuleBase_ModelWidget* aListWidget = 0;
486 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
487 if (aWidget->attributeID() == myAttributeListID) {
488 aListWidget = aWidget;
493 aListWidget->restoreValue();
494 aPropertyPanel->activateNextWidget(aListWidget);
497 // Hide sketcher result
498 CompositeFeaturePtr aSketchFeature =
499 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
500 std::list<ResultPtr> aResults = aSketchFeature->results();
501 std::list<ResultPtr>::const_iterator aIt;
502 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
503 (*aIt)->setDisplayed(false);
505 aSketchFeature->setDisplayed(false);
506 static Events_Loop* aLoop = Events_Loop::loop();
507 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
509 // Add Selected body were created the sketcher to list of selected objects
510 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
511 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
512 if (aSelList.get()) {
513 DataPtr aData = aSketchFeature->data();
514 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
515 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
516 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
518 SessionPtr aMgr = ModelAPI_Session::get();
519 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
520 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
521 std::string aValidatorID;
522 Events_InfoMessage anError;
523 aSelList->append(aRes, GeomShapePtr());
524 if (aFactory->validate(anAttribute, aValidatorID, anError))
525 updateObject(aCompFeature);
534 bool PartSet_WidgetSketchCreator::validateSelectionList() const
536 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
538 SessionPtr aMgr = ModelAPI_Session::get();
539 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
540 std::string aValidatorID;
541 Events_InfoMessage anError;
542 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
543 if (!isValidPComposite) {
544 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
545 // TODO(spo): translate
546 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
547 tr("Sketch is invalid and will be deleted.\nError: %1")
548 .arg(anError.messageString().c_str()),
551 return isValidPComposite;
554 void PartSet_WidgetSketchCreator::setSketchObjectToList(
555 const CompositeFeaturePtr& theCompositeFeature,
556 const AttributePtr& theAttribute)
558 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
561 AttributeSelectionListPtr aBaseObjectsSelectionList =
562 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
563 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
567 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
568 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
572 ResultPtr aSketchRes = aSketchFeature->results().front();
573 ResultConstructionPtr aConstruction =
574 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
575 if(!aConstruction.get()) {
579 if(aBaseObjectsSelectionList->size() == 0) {
580 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
584 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
585 const bool theEnabled)
587 QList<QWidget*> aMyControls = theModelWidget->getControls();
588 foreach(QWidget* eachControl, aMyControls) {
589 eachControl->setEnabled(theEnabled);