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 = QString::fromStdString(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("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 // Launch Sketch operation
386 CompositeFeaturePtr aCompFeature =
387 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
389 // start edit operation for the sketch
390 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
391 (myModule->createOperation("Sketch"));
392 QList<ModuleBase_ViewerPrsPtr> aValues;
393 aValues.push_back(aValue);
394 aFOperation->setPreselection(aValues);
396 myWorkshop->processLaunchOperation(aFOperation);
398 return aSketchStarted;
401 bool PartSet_WidgetSketchCreator::focusTo()
403 // this method is called only in creation mode. In Edition mode this widget is hidden
404 if (isSelectionMode() && !hasSubObjects()) {
405 setVisibleSelectionControl(true);
406 activateSelectionControl();
407 openExtrusionTransaction();
411 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
412 SLOT(onResumed(ModuleBase_Operation*)));
417 void PartSet_WidgetSketchCreator::deactivate()
419 ModuleBase_WidgetSelector::deactivate();
421 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
422 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
424 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
428 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
430 SessionPtr aMgr = ModelAPI_Session::get();
431 bool aIsOp = aMgr->isOperation();
433 // in current implementation, all transactions are closed when resume happens
434 // so, this is a wrong case, which is not checked.
435 // To provide it, make correction in later rows about abort/undo transactions
438 // Set visible only selection control
439 setVisibleSelectionControl(true);
441 // Validate the created sketch. If it is valid, it is set into the composite feature selection
442 // list, otherwise it is removed
443 CompositeFeaturePtr aCompFeature =
444 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
445 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
446 if (aCompFeature->numberOfSubs() > 0) {
447 // set the sub feature to attribute selection list and check whether sketch is valid
448 SessionPtr aMgr = ModelAPI_Session::get();
449 const static std::string aNestedOpID("Set Sketch result into Selection list");
450 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
451 setSketchObjectToList(aCompFeature, anAttrList);
452 aMgr->finishOperation();
454 if (!validateSelectionList()) {
455 #ifdef DEBUG_UNDO_INVALID_SKETCH
456 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
457 aMgr->undo(); /// Sketch creation
459 aMgr->startOperation("Delete invalid Sketch feature", false);
461 // delete invalid sketch
462 CompositeFeaturePtr aSketchFeature =
463 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
464 QObjectPtrList anObjects;
465 anObjects.append(aSketchFeature);
467 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
468 aWorkshop->deleteFeatures(anObjects);
470 aMgr->finishOperation();
474 openExtrusionTransaction();
476 if (aCompFeature->numberOfSubs() == 0) {
477 // call activateWidget() of the parent to connect to the viewer seleciton
478 activateSelectionControl();
481 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
482 /// the attribute selection list will be filled by result of this sketch.
483 setVisibleSelectionControl(false);
485 // Update value in attribute selection list
486 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
487 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
488 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
489 ModuleBase_ModelWidget* aListWidget = 0;
490 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
491 if (aWidget->attributeID() == myAttributeListID) {
492 aListWidget = aWidget;
497 aListWidget->restoreValue();
498 aPropertyPanel->activateNextWidget(aListWidget);
501 // Hide sketcher result
502 CompositeFeaturePtr aSketchFeature =
503 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
504 std::list<ResultPtr> aResults = aSketchFeature->results();
505 std::list<ResultPtr>::const_iterator aIt;
506 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
507 (*aIt)->setDisplayed(false);
509 aSketchFeature->setDisplayed(false);
510 static Events_Loop* aLoop = Events_Loop::loop();
511 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
513 // Add Selected body were created the sketcher to list of selected objects
514 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
515 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
516 if (aSelList.get()) {
517 DataPtr aData = aSketchFeature->data();
518 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
519 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
520 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
522 SessionPtr aMgr = ModelAPI_Session::get();
523 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
524 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
525 std::string aValidatorID;
526 Events_InfoMessage anError;
527 aSelList->append(aRes, GeomShapePtr());
528 if (aFactory->validate(anAttribute, aValidatorID, anError))
529 updateObject(aCompFeature);
538 bool PartSet_WidgetSketchCreator::validateSelectionList() const
540 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
542 SessionPtr aMgr = ModelAPI_Session::get();
543 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
544 std::string aValidatorID;
545 Events_InfoMessage anError;
546 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
547 if (!isValidPComposite) {
548 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
549 // TODO(spo): translate
550 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
551 tr("Sketch is invalid and will be deleted.\nError: %1")
552 .arg(anError.messageString().c_str()),
555 return isValidPComposite;
558 void PartSet_WidgetSketchCreator::setSketchObjectToList(
559 const CompositeFeaturePtr& theCompositeFeature,
560 const AttributePtr& theAttribute)
562 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
565 AttributeSelectionListPtr aBaseObjectsSelectionList =
566 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
567 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
571 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
572 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
576 ResultPtr aSketchRes = aSketchFeature->results().front();
577 ResultConstructionPtr aConstruction =
578 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
579 if(!aConstruction.get()) {
583 if(aBaseObjectsSelectionList->size() == 0) {
584 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
588 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
589 const bool theEnabled)
591 QList<QWidget*> aMyControls = theModelWidget->getControls();
592 foreach(QWidget* eachControl, aMyControls) {
593 eachControl->setEnabled(theEnabled);