1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetShapeSelector.h
4 // Created: 2 June 2014
5 // Author: Vitaly Smetannikov
7 #include <ModuleBase_WidgetShapeSelector.h>
8 #include <ModuleBase_Definitions.h>
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_Tools.h>
13 #include <ModuleBase_FilterFactory.h>
14 #include <ModuleBase_Filter.h>
16 #include <Config_WidgetAPI.h>
17 #include <Events_Loop.h>
18 #include <Events_Message.h>
19 #include <GeomAPI_Interface.h>
20 #include <GeomAPI_Shape.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeReference.h>
30 #include <ModelAPI_AttributeSelection.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_ResultValidator.h>
37 #include <ModelAPI_AttributeValidator.h>
38 #include <ModelAPI_ShapeValidator.h>
40 #include <Config_WidgetAPI.h>
41 #include <Events_Error.h>
43 #include <GeomAPI_Shape.h>
45 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopExp_Explorer.hxx>
53 #include <QToolButton>
56 #include <QDockWidget>
57 #include <QApplication>
58 #include <QFormLayout>
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Shape.hxx>
68 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
69 static ShapeTypes MyShapeTypes;
71 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
73 if (MyShapeTypes.count() == 0) {
74 MyShapeTypes["face"] = TopAbs_FACE;
75 MyShapeTypes["faces"] = TopAbs_FACE;
76 MyShapeTypes["vertex"] = TopAbs_VERTEX;
77 MyShapeTypes["vertices"] = TopAbs_VERTEX;
78 MyShapeTypes["wire"] = TopAbs_WIRE;
79 MyShapeTypes["edge"] = TopAbs_EDGE;
80 MyShapeTypes["edges"] = TopAbs_EDGE;
81 MyShapeTypes["shell"] = TopAbs_SHELL;
82 MyShapeTypes["solid"] = TopAbs_SOLID;
83 MyShapeTypes["solids"] = TopAbs_SOLID;
85 QString aType = theType.toLower();
86 if (MyShapeTypes.contains(aType))
87 return MyShapeTypes[aType];
88 Events_Error::send("Shape type defined in XML is not implemented!");
92 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
93 ModuleBase_IWorkshop* theWorkshop,
94 const Config_WidgetAPI* theData,
95 const std::string& theParentId)
96 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
97 myWorkshop(theWorkshop), myIsActive(false)
99 QFormLayout* aLayout = new QFormLayout(this);
100 ModuleBase_Tools::adjustMargins(aLayout);
102 QString aLabelText = QString::fromStdString(theData->widgetLabel());
103 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
104 myLabel = new QLabel(aLabelText, this);
105 if (!aLabelIcon.isEmpty())
106 myLabel->setPixmap(QPixmap(aLabelIcon));
109 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
110 myTextLine = new QLineEdit(this);
111 myTextLine->setReadOnly(true);
112 myTextLine->setToolTip(aToolTip);
113 myTextLine->installEventFilter(this);
115 aLayout->addRow(myLabel, myTextLine);
117 std::string aTypes = theData->getProperty("shape_types");
118 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
121 //********************************************************************
122 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
124 activateSelection(false);
127 //********************************************************************
128 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
130 // the value is stored on the selection changed signal processing
134 //********************************************************************
135 bool ModuleBase_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject,
136 GeomShapePtr theShape) const
138 bool isChanged = false;
139 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
140 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
143 DataPtr aData = myFeature->data();
144 AttributeReferencePtr aRef = aData->reference(attributeID());
146 ObjectPtr aObject = aRef->value();
147 if (!(aObject && aObject->isSame(theSelectedObject))) {
148 aRef->setValue(theSelectedObject);
152 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
154 ObjectPtr aObject = aRefAttr->object();
155 if (!(aObject && aObject->isSame(theSelectedObject))) {
156 aRefAttr->setObject(theSelectedObject);
160 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
161 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
162 if (aSelectAttr && aBody && (theShape.get() != NULL)) {
163 aSelectAttr->setValue(aBody, theShape);
171 //********************************************************************
172 void ModuleBase_WidgetShapeSelector::clearAttribute()
174 DataPtr aData = myFeature->data();
175 AttributeSelectionPtr aSelect = aData->selection(attributeID());
177 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
180 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
182 aRefAttr->setObject(ObjectPtr());
185 AttributeReferencePtr aRef = aData->reference(attributeID());
187 aRef->setObject(ObjectPtr());
191 //********************************************************************
192 bool ModuleBase_WidgetShapeSelector::restoreValue()
194 bool isBlocked = this->blockSignals(true);
195 updateSelectionName();
196 this->blockSignals(isBlocked);
201 //********************************************************************
202 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
204 QList<QWidget*> aControls;
205 aControls.append(myTextLine);
209 //********************************************************************
210 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
212 // In order to make reselection possible
213 // TODO: check with MPV clearAttribute();
215 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
216 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
217 if (aSelected.size() > 0) {
218 Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
219 if (isValid(anOwner)) {
220 setSelection(anOwner);
221 emit focusOutWidget(this);
226 //********************************************************************
227 bool ModuleBase_WidgetShapeSelector::setSelectionPrs(ModuleBase_ViewerPrs theValue)
229 ObjectPtr aObject = theValue.object();
230 ObjectPtr aCurrentObject = getObject(myFeature->attribute(attributeID()));
231 if ((!aCurrentObject) && (!aObject))
234 // Check that the selected object is result (others can not be accepted)
235 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
240 // We can not select a result of our feature
241 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
242 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
243 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
248 // Check that object belongs to active document or PartSet
249 DocumentPtr aDoc = aRes->document();
250 SessionPtr aMgr = ModelAPI_Session::get();
251 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
254 // Check that the result has a shape
255 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
259 // Get sub-shapes from local selection
260 if (!theValue.shape().IsNull()) {
261 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
262 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
264 // Check that the selection corresponds to selection type
265 if (!acceptSubShape(aShape))
268 setObject(aObject, aShape);
272 //********************************************************************
273 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
276 storeAttributeValues(theObj, theShape);//)
277 // updateObject(myFeature);
282 //updateSelectionName();
283 //emit valuesChanged();
286 //********************************************************************
287 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
289 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
291 // // Check that the shape of necessary type
292 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
295 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
296 // if (aShape.IsNull())
299 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
300 // if (aShapeType == TopAbs_COMPOUND) {
301 // foreach (QString aType,
303 // TopExp_Explorer aEx(aShape, shapeType(aType));
308 // foreach (QString aType, myShapeTypes) {
309 // if (shapeType(aType) == aShapeType)
316 //********************************************************************
317 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
319 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
320 foreach (QString aType, myShapeTypes) {
321 if (aShape.ShapeType() == shapeType(aType))
327 ObjectPtr ModuleBase_WidgetShapeSelector::getObject(const AttributePtr& theAttribute)
330 std::string anAttrType = theAttribute->attributeType();
331 if (anAttrType == ModelAPI_AttributeRefAttr::type()) {
332 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
333 if (anAttr != NULL && anAttr->isObject())
334 anObject = anAttr->object();
336 if (anAttrType == ModelAPI_AttributeSelection::type()) {
337 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
338 if (anAttr != NULL && anAttr->isInitialized())
339 anObject = anAttr->context();
341 if (anAttrType == ModelAPI_AttributeReference::type()) {
342 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
343 if (anAttr.get() != NULL && anAttr->isInitialized())
344 anObject = anAttr->value();
350 //********************************************************************
351 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
354 DataPtr aData = myFeature->data();
355 if (aData.get() == NULL)
358 AttributeSelectionPtr aSelect = aData->selection(attributeID());
360 aShape = aSelect->value();
365 //********************************************************************
366 void ModuleBase_WidgetShapeSelector::updateSelectionName()
368 DataPtr aData = myFeature->data();
369 if (aData.get() == NULL)
372 bool isNameUpdated = false;
373 AttributeSelectionPtr aSelect = aData->selection(attributeID());
375 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
376 isNameUpdated = true;
378 if (!isNameUpdated) {
379 ObjectPtr anObject = getObject(myFeature->attribute(attributeID()));
380 if (anObject.get() != NULL) {
381 std::string aName = anObject->data()->name();
382 myTextLine->setText(QString::fromStdString(aName));
384 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
385 if (aRefAttr && aRefAttr->attr().get() != NULL) {
386 //myIsObject = aRefAttr->isObject();
387 AttributePtr anAttr = aRefAttr->attr();
388 if (anAttr.get() != NULL) {
389 std::stringstream aName;
390 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
391 myTextLine->setText(QString::fromStdString(aName.str()));
394 else if (myIsActive) {
395 myTextLine->setText("");
402 //********************************************************************
403 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
405 if (myIsActive == toActivate)
407 myIsActive = toActivate;
408 updateSelectionName();
412 foreach (QString aType, myShapeTypes) {
413 aList.append(shapeType(aType));
415 myWorkshop->activateSubShapesSelection(aList);
417 myWorkshop->deactivateSubShapesSelection();
420 activateFilters(myWorkshop, myIsActive);
423 //********************************************************************
424 void ModuleBase_WidgetShapeSelector::raisePanel() const
426 QWidget* aParent = this->parentWidget();
427 QWidget* aLastPanel = 0;
428 while (!aParent->inherits("QDockWidget")) {
429 aLastPanel = aParent;
430 aParent = aParent->parentWidget();
434 if (aParent->inherits("QDockWidget")) {
435 QDockWidget* aTabWgt = (QDockWidget*) aParent;
440 //********************************************************************
441 void ModuleBase_WidgetShapeSelector::activateCustom()
443 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
444 activateSelection(true);
447 //********************************************************************
448 void ModuleBase_WidgetShapeSelector::backupAttributeValue(const bool isBackup)
450 DataPtr aData = myFeature->data();
451 AttributePtr anAttribute = myFeature->attribute(attributeID());
454 myObject = getObject(anAttribute);
455 myShape = getShape();
456 myRefAttribute = NULL;
458 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
460 myIsObject = aRefAttr->isObject();
461 myRefAttribute = aRefAttr->attr();
465 storeAttributeValues(myObject, myShape);
466 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
469 aRefAttr->setAttr(myRefAttribute);
474 //********************************************************************
475 bool ModuleBase_WidgetShapeSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
478 //QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
479 //if (aSelected.size() > 0) {
480 ModuleBase_ViewerPrs aPrs;
481 myWorkshop->selection()->fillPresentation(aPrs, theOwner);
482 isDone = setSelectionPrs(aPrs);
487 //********************************************************************
488 void ModuleBase_WidgetShapeSelector::deactivate()
490 activateSelection(false);
491 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
494 //********************************************************************
495 /*bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
497 bool isValid = ModuleBase_WidgetValidated::isValid(theObj, theShape);
501 SessionPtr aMgr = ModelAPI_Session::get();
502 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
503 std::list<ModelAPI_Validator*> aValidators;
504 std::list<std::list<std::string> > anArguments;
505 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
507 DataPtr aData = myFeature->data();
508 AttributePtr anAttribute = myFeature->attribute(attributeID());
510 // 1. make a backup of the previous attribute values
511 backupAttributeValue(true);
512 // 2. store the current values, disable the model's update
513 aData->blockSendAttributeUpdated(true);
515 // 3. check the acceptability of the current values
516 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
517 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
519 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
520 const ModelAPI_AttributeValidator* aAttrValidator =
521 dynamic_cast<const ModelAPI_AttributeValidator*>(*aValidator);
522 if (aAttrValidator) {
523 aValid = aAttrValidator->isValid(anAttribute, *aArgs);
527 // 4. if the values are not valid, restore the previous values to the attribute
528 backupAttributeValue(false);
530 // 5. enable the model's update
531 aData->blockSendAttributeUpdated(false);