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_RefAttrValidator.h>
39 #include <Config_WidgetAPI.h>
40 #include <Events_Error.h>
42 #include <GeomAPI_Shape.h>
44 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopExp_Explorer.hxx>
52 #include <QToolButton>
55 #include <QDockWidget>
56 #include <QApplication>
58 #include <TopExp_Explorer.hxx>
59 #include <TopoDS_Shape.hxx>
66 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
67 static ShapeTypes MyShapeTypes;
69 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
71 if (MyShapeTypes.count() == 0) {
72 MyShapeTypes["face"] = TopAbs_FACE;
73 MyShapeTypes["faces"] = TopAbs_FACE;
74 MyShapeTypes["vertex"] = TopAbs_VERTEX;
75 MyShapeTypes["vertices"] = TopAbs_VERTEX;
76 MyShapeTypes["wire"] = TopAbs_WIRE;
77 MyShapeTypes["edge"] = TopAbs_EDGE;
78 MyShapeTypes["edges"] = TopAbs_EDGE;
79 MyShapeTypes["shell"] = TopAbs_SHELL;
80 MyShapeTypes["solid"] = TopAbs_SOLID;
81 MyShapeTypes["solids"] = TopAbs_SOLID;
83 QString aType = theType.toLower();
84 if (MyShapeTypes.contains(aType))
85 return MyShapeTypes[aType];
86 Events_Error::send("Shape type defined in XML is not implemented!");
90 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
91 ModuleBase_IWorkshop* theWorkshop,
92 const Config_WidgetAPI* theData,
93 const std::string& theParentId)
94 : ModuleBase_ModelWidget(theParent, theData, theParentId),
95 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
97 myContainer = new QWidget(theParent);
98 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
99 ModuleBase_Tools::adjustMargins(aLayout);
101 QString aLabelText = QString::fromStdString(theData->widgetLabel());
102 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
103 myLabel = new QLabel(aLabelText, myContainer);
104 if (!aLabelIcon.isEmpty())
105 myLabel->setPixmap(QPixmap(aLabelIcon));
107 aLayout->addWidget(myLabel);
109 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
110 myTextLine = new QLineEdit(myContainer);
111 myTextLine->setReadOnly(true);
112 myTextLine->setToolTip(aToolTip);
113 myTextLine->installEventFilter(this);
115 aLayout->addWidget(myTextLine, 1);
117 std::string aTypes = theData->getProperty("shape_types");
118 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
120 std::string aObjTypes = theData->getProperty("object_types");
121 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
123 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
126 //********************************************************************
127 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
129 activateSelection(false);
132 //********************************************************************
133 bool ModuleBase_WidgetShapeSelector::storeValue() const
135 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
136 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
139 DataPtr aData = myFeature->data();
140 if (myUseSubShapes) {
141 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
143 AttributePtr aAttr = aData->attribute(attributeID());
145 // We have to check several attributes types
146 AttributeSelectionPtr aSelectAttr =
147 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
149 aSelectAttr->setValue(aBody, myShape);
150 updateObject(myFeature);
153 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
155 aRefAttr->setObject(mySelectedObject);
156 updateObject(myFeature);
162 AttributeReferencePtr aRef = aData->reference(attributeID());
164 ObjectPtr aObject = aRef->value();
165 if (!(aObject && aObject->isSame(mySelectedObject))) {
166 aRef->setValue(mySelectedObject);
167 updateObject(myFeature);
171 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
173 ObjectPtr aObject = aRefAttr->object();
174 if (!(aObject && aObject->isSame(mySelectedObject))) {
175 aRefAttr->setObject(mySelectedObject);
176 updateObject(myFeature);
185 //********************************************************************
186 void ModuleBase_WidgetShapeSelector::clearAttribute()
188 DataPtr aData = myFeature->data();
189 AttributeSelectionPtr aSelect = aData->selection(attributeID());
191 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
194 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
196 aRefAttr->setObject(ObjectPtr());
199 AttributeReferencePtr aRef = aData->reference(attributeID());
201 aRef->setObject(ObjectPtr());
205 //********************************************************************
206 bool ModuleBase_WidgetShapeSelector::restoreValue()
208 DataPtr aData = myFeature->data();
209 bool isBlocked = this->blockSignals(true);
210 if (myUseSubShapes) {
211 AttributeSelectionPtr aSelect = aData->selection(attributeID());
213 mySelectedObject = aSelect->context();
214 myShape = aSelect->value();
216 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
218 mySelectedObject = aRefAttr->object();
222 AttributeReferencePtr aRef = aData->reference(attributeID());
224 mySelectedObject = aRef->value();
226 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
228 mySelectedObject = aRefAttr->object();
231 updateSelectionName();
233 this->blockSignals(isBlocked);
237 //********************************************************************
238 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
240 QList<QWidget*> aControls;
241 aControls.append(myTextLine);
245 //********************************************************************
246 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
248 // In order to make reselection possible
249 // TODO: check with MPV clearAttribute();
251 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
252 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
253 if (aSelected.size() > 0) {
254 if (setSelection(aSelected.first()))
255 emit focusOutWidget(this);
259 //********************************************************************
260 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
262 ObjectPtr aObject = theValue.object();
263 if ((!mySelectedObject) && (!aObject))
266 // Check that the selected object is result (others can not be accepted)
267 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
272 // We can not select a result of our feature
273 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
274 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
275 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
280 // Check that object belongs to active document or PartSet
281 DocumentPtr aDoc = aRes->document();
282 SessionPtr aMgr = ModelAPI_Session::get();
283 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
286 // Check that the result has a shape
287 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
291 /// Check that object has acceptable type
292 if (!acceptObjectType(aObject))
295 // Get sub-shapes from local selection
296 if (myUseSubShapes) {
297 if (!theValue.shape().IsNull()) {
298 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
299 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
303 // Check that the selection corresponds to selection type
304 if (myUseSubShapes) {
305 if (!acceptSubShape(aShape))
308 if (!acceptObjectShape(aObject))
311 if (isValid(aObject, aShape)) {
312 setObject(aObject, aShape);
318 //********************************************************************
319 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
321 mySelectedObject = theObj;
323 if (mySelectedObject) {
326 updateSelectionName();
327 emit valuesChanged();
330 //********************************************************************
331 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
333 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
335 // Check that the shape of necessary type
336 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
339 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
343 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
344 if (aShapeType == TopAbs_COMPOUND) {
345 foreach (QString aType, myShapeTypes) {
346 TopExp_Explorer aEx(aShape, shapeType(aType));
351 foreach (QString aType, myShapeTypes) {
352 if (shapeType(aType) == aShapeType)
359 //********************************************************************
360 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
362 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
363 foreach (QString aType, myShapeTypes) {
364 if (aShape.ShapeType() == shapeType(aType))
370 //********************************************************************
371 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
373 // Definition of types is not obligatory. If types are not defined then
374 // it means that accepted any type
375 if (myObjectTypes.isEmpty())
378 foreach (QString aType, myObjectTypes) {
379 if (aType.toLower() == "construction") {
380 ResultConstructionPtr aConstr =
381 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
382 return (aConstr != NULL);
383 } // ToDo: Process other types of objects
385 // Object type is defined but not found
390 //********************************************************************
391 void ModuleBase_WidgetShapeSelector::updateSelectionName()
393 if (mySelectedObject) {
394 std::string aName = mySelectedObject->data()->name();
395 myTextLine->setText(QString::fromStdString(aName));
398 myTextLine->setText("");
404 //********************************************************************
405 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
407 if (myIsActive == toActivate)
409 myIsActive = toActivate;
410 updateSelectionName();
411 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
414 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
415 if (myUseSubShapes) {
418 foreach (QString aType, myShapeTypes)
419 aList.append(shapeType(aType));
420 myWorkshop->activateSubShapesSelection(aList);
421 if (!myObjectTypes.isEmpty()) {
422 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
423 aViewer->clearSelectionFilters();
424 aViewer->addSelectionFilter(myObjTypeFilter);
428 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
429 if (myUseSubShapes) {
430 if (!myObjTypeFilter.IsNull()) {
431 aViewer->removeSelectionFilter(myObjTypeFilter);
432 myObjTypeFilter.Nullify();
434 myWorkshop->deactivateSubShapesSelection();
437 // apply filters loaded from the XML definition of the widget
438 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
439 SelectMgr_ListOfFilter aFilters;
440 aFactory->filters(parentID(), attributeID(), aFilters);
441 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
442 for (; aIt.More(); aIt.Next()) {
443 Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
444 if (aSelFilter.IsNull())
447 //Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
448 //if (aFilter.IsNull())
451 aViewer->addSelectionFilter(aSelFilter);
453 aViewer->removeSelectionFilter(aSelFilter);
457 //********************************************************************
458 void ModuleBase_WidgetShapeSelector::raisePanel() const
460 QWidget* aParent = myContainer->parentWidget();
461 QWidget* aLastPanel = 0;
462 while (!aParent->inherits("QDockWidget")) {
463 aLastPanel = aParent;
464 aParent = aParent->parentWidget();
468 if (aParent->inherits("QDockWidget")) {
469 QDockWidget* aTabWgt = (QDockWidget*) aParent;
474 //********************************************************************
475 //bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
477 // if (theValue.object()) {
478 // ObjectPtr aObject = theValue.object();
479 // if (acceptObjectShape(aObject)) {
480 // setObject(aObject);
487 //********************************************************************
488 void ModuleBase_WidgetShapeSelector::activate()
490 activateSelection(true);
493 //********************************************************************
494 void ModuleBase_WidgetShapeSelector::deactivate()
496 activateSelection(false);
499 //********************************************************************
500 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
502 SessionPtr aMgr = ModelAPI_Session::get();
503 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
504 std::list<ModelAPI_Validator*> aValidators;
505 std::list<std::list<std::string> > anArguments;
506 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
508 // Check the type of selected object
509 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
511 for (; aValidator != aValidators.end(); aValidator++) {
512 const ModelAPI_ResultValidator* aResValidator =
513 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
516 if (aResValidator->isValid(theObj)) {
525 // Check the acceptability of the object as attribute
526 aValidator = aValidators.begin();
527 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
528 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
529 const ModelAPI_RefAttrValidator* aAttrValidator =
530 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
531 if (aAttrValidator) {
532 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {