1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "PartSet_WidgetSketchCreator.h"
22 #include "PartSet_Module.h"
23 #include "PartSet_WidgetSketchLabel.h"
24 #include "PartSet_PreviewPlanes.h"
26 #include <Config_Keywords.h>
28 #include <XGUI_ModuleConnector.h>
29 #include <XGUI_Workshop.h>
30 #include <XGUI_Displayer.h>
31 #include <XGUI_SelectionMgr.h>
32 #include <XGUI_OperationMgr.h>
33 #include <XGUI_PropertyPanel.h>
34 #include <XGUI_Tools.h>
35 #include <XGUI_ViewerProxy.h>
37 #include <GeomAPI_Face.h>
39 #include <Events_InfoMessage.h>
41 #include <ModelAPI_Session.h>
42 #include <ModelAPI_ResultBody.h>
43 #include <ModelAPI_AttributeSelection.h>
44 #include <ModelAPI_AttributeSelectionList.h>
45 #include <ModelAPI_Validator.h>
46 #include <ModelAPI_Events.h>
47 #include <ModelAPI_ResultConstruction.h>
49 #include <SketchPlugin_SketchEntity.h>
50 #include <FeaturesPlugin_CompositeBoolean.h>
52 #include <ModuleBase_Tools.h>
53 #include <ModuleBase_Operation.h>
54 #include <ModuleBase_IPropertyPanel.h>
55 #include <ModuleBase_OperationFeature.h>
56 #include <ModuleBase_ViewerPrs.h>
58 #include <Config_WidgetAPI.h>
60 #include <Events_Loop.h>
64 #include <QDoubleValidator>
65 //#include <QFormLayout>
66 #include <QVBoxLayout>
67 #include <QMessageBox>
68 #include <QMainWindow>
70 #define DEBUG_UNDO_INVALID_SKETCH
72 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
73 PartSet_Module* theModule,
74 const Config_WidgetAPI* theData)
75 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
76 myModule(theModule), myIsCustomAttribute(false)
78 myAttributeListID = theData->getProperty("attribute_list_id");
80 //QFormLayout* aLayout = new QFormLayout(this);
81 QVBoxLayout* aLayout = new QVBoxLayout(this);
82 ModuleBase_Tools::zeroMargins(aLayout);
84 ModuleBase_Tools::adjustMargins(aLayout);
86 QString aLabelText = QString::fromStdString(theData->widgetLabel());
87 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
89 // Size of the View control
90 mySizeOfViewWidget = new QWidget(this);
91 QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
92 aSizeLayout->addWidget(new QLabel("Size of the view", mySizeOfViewWidget));
93 mySizeOfView = new QLineEdit(mySizeOfViewWidget);
95 QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
96 aValidator->setLocale(ModuleBase_Tools::doubleLocale());
97 aValidator->setNotation(QDoubleValidator::StandardNotation);
98 mySizeOfView->setValidator(aValidator);
99 aSizeLayout->addWidget(mySizeOfView);
101 myLabel = new QLabel(aLabelText, this);
102 myLabel->setWordWrap(true);
104 aLayout->addWidget(mySizeOfViewWidget);
105 aLayout->addWidget(myLabel);
106 aLayout->addStretch(1);
108 std::string aTypes = theData->getProperty("shape_types");
109 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
111 myPreviewPlanes = new PartSet_PreviewPlanes();
114 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
116 // we need to deactivate here in order to hide preview planes if the selection mode is
121 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
123 QList<QWidget*> aControls;
124 aControls.append(myLabel);
128 bool PartSet_WidgetSketchCreator::restoreValueCustom()
133 bool PartSet_WidgetSketchCreator::storeValueCustom()
138 AttributePtr PartSet_WidgetSketchCreator::attribute() const
140 AttributePtr anAttribute;
141 if (myIsCustomAttribute)
142 anAttribute = myFeature->attribute(myAttributeListID);
144 anAttribute = ModuleBase_WidgetSelector::attribute();
149 //********************************************************************
150 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
152 SessionPtr aMgr = ModelAPI_Session::get();
153 bool aIsOp = aMgr->isOperation();
155 const static std::string aNestedOpID("Parameters modification");
156 aMgr->startOperation(aNestedOpID, true);
160 //********************************************************************
161 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
164 if (myIsCustomAttribute) {
165 // check only suiting of the value to custom attribute (myAttributeListID)
166 // do not cash of validation to avoid using states, stored for XML attribute
167 // there is an alternative is to call clearValidatedCash() in setSelection()
168 aValid = isValidSelectionForAttribute(theValue, attribute());
170 else { /// if the validated attribute is already custom
171 if (getValidState(theValue, aValid)) {
174 aValid = isValidSelectionCustom(theValue);
176 // check selection to create new sketh (XML current attribute)
177 aValid = isValidSelectionForAttribute(theValue, attribute());
179 // check selection to fill list attribute (myAttributeListID)
180 bool isCustomAttribute = myIsCustomAttribute;
181 myIsCustomAttribute = true;
182 aValid = isValidSelectionForAttribute(theValue, attribute());
183 myIsCustomAttribute = isCustomAttribute;
186 storeValidState(theValue, aValid);
190 //********************************************************************
191 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
193 return PartSet_WidgetSketchLabel::canFillSketch(theValue);
196 void PartSet_WidgetSketchCreator::activateSelectionControl()
198 // reset previously set size of view needed on restart extrusion after Sketch
199 if (myModule->sketchMgr()->previewSketchPlane()->isUseSizeOfView())
200 myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(0, false);
202 // we need to call activate here as the widget has no focus accepted controls
203 // if these controls are added here, activate will happens automatically after focusIn()
204 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
205 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
206 aPanel->activateWidget(this, false);
209 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
211 // hide current widget, activate the next widget
212 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
213 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
214 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
215 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
216 if (theSelectionControl) { // hide other controls
218 aWidget->setVisible(false);
220 else { // hide current control
222 aWidget->setVisible(false);
224 aWidget->setVisible(true);
225 if (aWidget->attributeID() == myAttributeListID)
226 setEnabledModelWidget(aWidget, !hasSubObjects());
231 if (theSelectionControl) {
232 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
233 bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
234 if (!aBodyIsVisualized && !aSketchIsVisualized) {
235 // We have to select a plane before any operation
236 myPreviewPlanes->showPreviewPlanes(myWorkshop);
237 mySizeOfViewWidget->setVisible(true);
240 mySizeOfViewWidget->setVisible(false);
244 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
245 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
247 aWorkshop->viewer()->update();
251 QIntList PartSet_WidgetSketchCreator::shapeTypes() const
253 QIntList aShapeTypes;
254 foreach(QString aType, myShapeTypes) {
255 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
260 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
262 ModuleBase_ModelWidget::setEditingMode(isEditing);
264 setVisibleSelectionControl(false);
266 ModuleBase_ModelWidget* anAttributeListWidget = 0;
267 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
268 XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
269 const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
270 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
271 if (aWidget->attributeID() == myAttributeListID) {
272 anAttributeListWidget = aWidget;
276 if (anAttributeListWidget)
277 setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
281 bool PartSet_WidgetSketchCreator::isSelectionMode() const
283 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
284 bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
286 return !aHasValueInList;
289 bool PartSet_WidgetSketchCreator::hasSubObjects() const
291 bool aHasSubObjects = false;
293 bool aCanSetFocus = true;
294 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
295 if (aComposite.get())
296 aHasSubObjects = aComposite->numberOfSubs() > 0;
297 return aHasSubObjects;
300 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
301 const bool theToValidate)
304 if (!startSketchOperation(theValues)) {
305 myIsCustomAttribute = true;
306 QList<ModuleBase_ViewerPrsPtr>::const_iterator
307 anIt = theValues.begin(), aLast = theValues.end();
308 bool aProcessed = false;
309 for (; anIt != aLast; anIt++) {
310 ModuleBase_ViewerPrsPtr aValue = *anIt;
311 if (!theToValidate || isValidInFilters(aValue))
312 aProcessed = setSelectionCustom(aValue) || aProcessed;
314 myIsCustomAttribute = false;
317 emit valuesChanged();
318 updateObject(myFeature);
319 setVisibleSelectionControl(false);
320 // manually deactivation because the widget was
321 // not activated as has no focus acceptin controls
323 emit focusOutWidget(this);
329 //********************************************************************
330 bool PartSet_WidgetSketchCreator::processSelection()
332 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
333 bool isDone = setSelection(aSelected, true/*false*/);
338 //********************************************************************
339 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
343 bool PartSet_WidgetSketchCreator::startSketchOperation(
344 const QList<ModuleBase_ViewerPrsPtr>& theValues)
346 bool aSketchStarted = false;
348 if (theValues.size() != 1)
349 return aSketchStarted;
351 ModuleBase_ViewerPrsPtr aValue = theValues.front();
352 if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
353 return aSketchStarted;
355 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
356 /// sketch should not started by object(face) selected as global. If Local face is selected,
357 /// sketch is started
358 if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
359 ResultConstructionPtr aConstruction =
360 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
361 if (!aConstruction.get() || !aConstruction->isInfinite())
362 return aSketchStarted;
364 aSketchStarted = true;
365 // Set View size if a plane is selected
366 if (myPreviewPlanes->isPreviewDisplayed() &&
367 myPreviewPlanes->isPreviewShape(aValue->shape())) {
368 // set default plane size
369 bool isSetSizeOfView = false;
370 double aSizeOfView = 0;
371 QString aSizeOfViewStr = mySizeOfView->text();
372 if (!aSizeOfViewStr.isEmpty()) {
373 aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
374 if (isSetSizeOfView && aSizeOfView <= 0) {
375 isSetSizeOfView = false;
379 myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
381 // manually deactivation because the widget was not activated as has no focus acceptin controls
383 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
384 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
386 // Launch Sketch operation
387 CompositeFeaturePtr aCompFeature =
388 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
390 // start edit operation for the sketch
391 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
392 (myModule->createOperation("Sketch"));
393 QList<ModuleBase_ViewerPrsPtr> aValues;
394 aValues.push_back(aValue);
395 aFOperation->setPreselection(aValues);
397 myWorkshop->processLaunchOperation(aFOperation);
399 return aSketchStarted;
402 bool PartSet_WidgetSketchCreator::focusTo()
404 // this method is called only in creation mode. In Edition mode this widget is hidden
405 if (isSelectionMode() && !hasSubObjects()) {
406 setVisibleSelectionControl(true);
407 activateSelectionControl();
408 openExtrusionTransaction();
412 connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
413 SLOT(onResumed(ModuleBase_Operation*)));
418 void PartSet_WidgetSketchCreator::deactivate()
420 ModuleBase_WidgetSelector::deactivate();
422 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
423 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
425 XGUI_Tools::workshop(myWorkshop)->viewer()->update();
429 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
431 SessionPtr aMgr = ModelAPI_Session::get();
432 bool aIsOp = aMgr->isOperation();
434 // in current implementation, all transactions are closed when resume happens
435 // so, this is a wrong case, which is not checked.
436 // To provide it, make correction in later rows about abort/undo transactions
439 // Set visible only selection control
440 setVisibleSelectionControl(true);
442 // Validate the created sketch. If it is valid, it is set into the composite feature selection
443 // list, otherwise it is removed
444 CompositeFeaturePtr aCompFeature =
445 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
446 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
447 if (aCompFeature->numberOfSubs() > 0) {
448 // set the sub feature to attribute selection list and check whether sketch is valid
449 SessionPtr aMgr = ModelAPI_Session::get();
450 const static std::string aNestedOpID("Set Sketch result into Selection list");
451 aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
452 setSketchObjectToList(aCompFeature, anAttrList);
453 aMgr->finishOperation();
455 if (!validateSelectionList()) {
456 #ifdef DEBUG_UNDO_INVALID_SKETCH
457 aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
458 aMgr->undo(); /// Sketch creation
460 aMgr->startOperation("Delete invalid Sketch feature", false);
462 // delete invalid sketch
463 CompositeFeaturePtr aSketchFeature =
464 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
465 QObjectPtrList anObjects;
466 anObjects.append(aSketchFeature);
468 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
469 aWorkshop->deleteFeatures(anObjects);
471 aMgr->finishOperation();
475 openExtrusionTransaction();
477 if (aCompFeature->numberOfSubs() == 0) {
478 // call activateWidget() of the parent to connect to the viewer seleciton
479 activateSelectionControl();
482 // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
483 /// the attribute selection list will be filled by result of this sketch.
484 setVisibleSelectionControl(false);
486 // Update value in attribute selection list
487 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
488 XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
489 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
490 ModuleBase_ModelWidget* aListWidget = 0;
491 foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
492 if (aWidget->attributeID() == myAttributeListID) {
493 aListWidget = aWidget;
498 aListWidget->restoreValue();
499 aPropertyPanel->activateNextWidget(aListWidget);
502 // Hide sketcher result
503 CompositeFeaturePtr aSketchFeature =
504 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
505 std::list<ResultPtr> aResults = aSketchFeature->results();
506 std::list<ResultPtr>::const_iterator aIt;
507 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
508 (*aIt)->setDisplayed(false);
510 aSketchFeature->setDisplayed(false);
511 static Events_Loop* aLoop = Events_Loop::loop();
512 aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
514 // Add Selected body were created the sketcher to list of selected objects
515 std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
516 AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
517 if (aSelList.get()) {
518 DataPtr aData = aSketchFeature->data();
519 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
520 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
521 ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
523 SessionPtr aMgr = ModelAPI_Session::get();
524 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
525 AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
526 std::string aValidatorID;
527 Events_InfoMessage anError;
528 aSelList->append(aRes, GeomShapePtr());
529 if (aFactory->validate(anAttribute, aValidatorID, anError))
530 updateObject(aCompFeature);
539 bool PartSet_WidgetSketchCreator::validateSelectionList() const
541 AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
543 SessionPtr aMgr = ModelAPI_Session::get();
544 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
545 std::string aValidatorID;
546 Events_InfoMessage anError;
547 bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
548 if (!isValidPComposite) {
549 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
550 // TODO(spo): translate
551 QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
552 tr("Sketch is invalid and will be deleted.\nError: %1")
553 .arg(anError.messageString().c_str()),
556 return isValidPComposite;
559 void PartSet_WidgetSketchCreator::setSketchObjectToList(
560 const CompositeFeaturePtr& theCompositeFeature,
561 const AttributePtr& theAttribute)
563 if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
566 AttributeSelectionListPtr aBaseObjectsSelectionList =
567 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
568 if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
572 FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
573 if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
577 ResultPtr aSketchRes = aSketchFeature->results().front();
578 ResultConstructionPtr aConstruction =
579 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
580 if(!aConstruction.get()) {
584 if(aBaseObjectsSelectionList->size() == 0) {
585 aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
589 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
590 const bool theEnabled)
592 QList<QWidget*> aMyControls = theModelWidget->getControls();
593 foreach(QWidget* eachControl, aMyControls) {
594 eachControl->setEnabled(theEnabled);