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>
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>
59 #include <TopExp_Explorer.hxx>
60 #include <TopoDS_Shape.hxx>
67 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
68 static ShapeTypes MyShapeTypes;
70 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
72 if (MyShapeTypes.count() == 0) {
73 MyShapeTypes["face"] = TopAbs_FACE;
74 MyShapeTypes["faces"] = TopAbs_FACE;
75 MyShapeTypes["vertex"] = TopAbs_VERTEX;
76 MyShapeTypes["vertices"] = TopAbs_VERTEX;
77 MyShapeTypes["wire"] = TopAbs_WIRE;
78 MyShapeTypes["edge"] = TopAbs_EDGE;
79 MyShapeTypes["edges"] = TopAbs_EDGE;
80 MyShapeTypes["shell"] = TopAbs_SHELL;
81 MyShapeTypes["solid"] = TopAbs_SOLID;
82 MyShapeTypes["solids"] = TopAbs_SOLID;
84 QString aType = theType.toLower();
85 if (MyShapeTypes.contains(aType))
86 return MyShapeTypes[aType];
87 Events_Error::send("Shape type defined in XML is not implemented!");
91 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
92 ModuleBase_IWorkshop* theWorkshop,
93 const Config_WidgetAPI* theData,
94 const std::string& theParentId)
95 : ModuleBase_ModelWidget(theParent, theData, theParentId),
96 myWorkshop(theWorkshop), myIsActive(false)
98 myContainer = new QWidget(theParent);
99 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
100 ModuleBase_Tools::adjustMargins(aLayout);
102 QString aLabelText = QString::fromStdString(theData->widgetLabel());
103 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
104 myLabel = new QLabel(aLabelText, myContainer);
105 if (!aLabelIcon.isEmpty())
106 myLabel->setPixmap(QPixmap(aLabelIcon));
108 aLayout->addWidget(myLabel);
110 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
111 myTextLine = new QLineEdit(myContainer);
112 myTextLine->setReadOnly(true);
113 myTextLine->setToolTip(aToolTip);
114 myTextLine->installEventFilter(this);
116 aLayout->addWidget(myTextLine, 1);
118 std::string aTypes = theData->getProperty("shape_types");
119 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
121 std::string aObjTypes = theData->getProperty("object_types");
122 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
125 //********************************************************************
126 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
128 activateSelection(false);
131 //********************************************************************
132 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
134 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
135 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
138 DataPtr aData = myFeature->data();
139 AttributeReferencePtr aRef = aData->reference(attributeID());
141 ObjectPtr aObject = aRef->value();
142 if (!(aObject && aObject->isSame(mySelectedObject))) {
143 aRef->setValue(mySelectedObject);
144 updateObject(myFeature);
148 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
150 ObjectPtr aObject = aRefAttr->object();
151 if (!(aObject && aObject->isSame(mySelectedObject))) {
152 aRefAttr->setObject(mySelectedObject);
153 updateObject(myFeature);
157 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
158 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
159 if (aSelectAttr && aBody && (myShape.get() != NULL)) {
160 aSelectAttr->setValue(aBody, myShape);
161 updateObject(myFeature);
169 //********************************************************************
170 void ModuleBase_WidgetShapeSelector::clearAttribute()
172 DataPtr aData = myFeature->data();
173 AttributeSelectionPtr aSelect = aData->selection(attributeID());
175 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
178 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
180 aRefAttr->setObject(ObjectPtr());
183 AttributeReferencePtr aRef = aData->reference(attributeID());
185 aRef->setObject(ObjectPtr());
189 //********************************************************************
190 bool ModuleBase_WidgetShapeSelector::restoreValue()
192 DataPtr aData = myFeature->data();
193 bool isBlocked = this->blockSignals(true);
195 AttributeSelectionPtr aSelect = aData->selection(attributeID());
197 mySelectedObject = aSelect->context();
198 myShape = aSelect->value();
200 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
202 mySelectedObject = aRefAttr->object();
204 AttributeReferencePtr aRef = aData->reference(attributeID());
206 mySelectedObject = aRef->value();
210 updateSelectionName();
212 this->blockSignals(isBlocked);
216 //********************************************************************
217 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
219 QList<QWidget*> aControls;
220 aControls.append(myTextLine);
224 //********************************************************************
225 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
227 // In order to make reselection possible
228 // TODO: check with MPV clearAttribute();
230 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
231 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
232 if (aSelected.size() > 0) {
233 if (setSelection(aSelected.first()))
234 emit focusOutWidget(this);
238 //********************************************************************
239 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
241 ObjectPtr aObject = theValue.object();
242 if ((!mySelectedObject) && (!aObject))
245 // Check that the selected object is result (others can not be accepted)
246 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
251 // We can not select a result of our feature
252 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
253 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
254 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
259 // Check that object belongs to active document or PartSet
260 DocumentPtr aDoc = aRes->document();
261 SessionPtr aMgr = ModelAPI_Session::get();
262 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
265 // Check that the result has a shape
266 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
270 /// Check that object has acceptable type
271 if (!acceptObjectType(aObject))
274 // Get sub-shapes from local selection
275 if (!theValue.shape().IsNull()) {
276 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
277 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
280 // Check that the selection corresponds to selection type
281 if (!acceptSubShape(aShape))
283 // if (!acceptObjectShape(aObject))
286 // Check whether the value is valid for the viewer selection filters
287 Handle(SelectMgr_EntityOwner) anOwner = theValue.owner();
288 if (!anOwner.IsNull()) {
289 SelectMgr_ListOfFilter aFilters;
290 selectionFilters(aFilters);
291 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
292 for (; aIt.More(); aIt.Next()) {
293 const Handle(SelectMgr_Filter)& aFilter = aIt.Value();
294 if (!aFilter->IsOk(anOwner))
298 if (isValid(aObject, aShape)) {
299 setObject(aObject, aShape);
305 //********************************************************************
306 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
308 mySelectedObject = theObj;
310 if (mySelectedObject) {
313 updateSelectionName();
314 emit valuesChanged();
317 //********************************************************************
318 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
320 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
322 // // Check that the shape of necessary type
323 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
326 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
327 // if (aShape.IsNull())
330 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
331 // if (aShapeType == TopAbs_COMPOUND) {
332 // foreach (QString aType,
334 // TopExp_Explorer aEx(aShape, shapeType(aType));
339 // foreach (QString aType, myShapeTypes) {
340 // if (shapeType(aType) == aShapeType)
347 //********************************************************************
348 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
350 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
351 foreach (QString aType, myShapeTypes) {
352 if (aShape.ShapeType() == shapeType(aType))
358 //********************************************************************
359 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
361 // Definition of types is not obligatory. If types are not defined then
362 // it means that accepted any type
363 if (myObjectTypes.isEmpty())
366 foreach (QString aType, myObjectTypes) {
367 if (aType.toLower() == "construction") {
368 ResultConstructionPtr aConstr =
369 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
370 return (aConstr != NULL);
371 } // ToDo: Process other types of objects
373 // Object type is defined but not found
378 //********************************************************************
379 void ModuleBase_WidgetShapeSelector::updateSelectionName()
381 DataPtr aData = myFeature->data();
382 bool isNameUpdated = false;
383 if (aData.get() != NULL) {
384 AttributeSelectionPtr aSelect = aData->selection(attributeID());
386 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
387 isNameUpdated = true;
390 if (!isNameUpdated) {
391 if (mySelectedObject) {
392 std::string aName = mySelectedObject->data()->name();
393 myTextLine->setText(QString::fromStdString(aName));
396 myTextLine->setText("");
403 //********************************************************************
404 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
406 if (myIsActive == toActivate)
408 myIsActive = toActivate;
409 updateSelectionName();
410 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
413 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
415 foreach (QString aType, myShapeTypes) {
416 aList.append(shapeType(aType));
418 myWorkshop->activateSubShapesSelection(aList);
420 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
421 myWorkshop->deactivateSubShapesSelection();
424 // the initial code is moved here in order to clear filters, it is possible, that it is excessive and
425 // is performed outside
426 if (myIsActive && !myObjectTypes.isEmpty()) {
427 aViewer->clearSelectionFilters();
429 SelectMgr_ListOfFilter aFilters;
430 selectionFilters(aFilters);
431 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
432 for (; aIt.More(); aIt.Next()) {
433 const Handle(SelectMgr_Filter)& aSelFilter = aIt.Value();
435 aViewer->addSelectionFilter(aSelFilter);
437 aViewer->removeSelectionFilter(aSelFilter);
439 // the internal filter should be removed by the widget deactivation, it is done historically
440 if (!myIsActive && !myObjTypeFilter.IsNull()) {
441 myObjTypeFilter.Nullify();
445 //********************************************************************
446 void ModuleBase_WidgetShapeSelector::selectionFilters(SelectMgr_ListOfFilter& theFilters)
448 if (!myObjectTypes.isEmpty()) {
449 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
450 theFilters.Append(myObjTypeFilter);
452 // apply filters loaded from the XML definition of the widget
453 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
454 SelectMgr_ListOfFilter aFilters;
455 aFactory->filters(parentID(), attributeID(), aFilters);
456 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
457 for (; aIt.More(); aIt.Next()) {
458 Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
459 if (aSelFilter.IsNull())
462 theFilters.Append(aSelFilter);
466 //********************************************************************
467 void ModuleBase_WidgetShapeSelector::raisePanel() const
469 QWidget* aParent = myContainer->parentWidget();
470 QWidget* aLastPanel = 0;
471 while (!aParent->inherits("QDockWidget")) {
472 aLastPanel = aParent;
473 aParent = aParent->parentWidget();
477 if (aParent->inherits("QDockWidget")) {
478 QDockWidget* aTabWgt = (QDockWidget*) aParent;
483 //********************************************************************
484 void ModuleBase_WidgetShapeSelector::activateCustom()
486 activateSelection(true);
489 //********************************************************************
490 void ModuleBase_WidgetShapeSelector::deactivate()
492 activateSelection(false);
495 //********************************************************************
496 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
498 SessionPtr aMgr = ModelAPI_Session::get();
499 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
500 std::list<ModelAPI_Validator*> aValidators;
501 std::list<std::list<std::string> > anArguments;
502 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
504 // Check the type of selected object
505 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
507 for (; aValidator != aValidators.end(); aValidator++) {
508 const ModelAPI_ResultValidator* aResValidator =
509 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
512 if (aResValidator->isValid(theObj)) {
521 // Check the acceptability of the object as attribute
522 aValidator = aValidators.begin();
523 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
524 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
525 const ModelAPI_RefAttrValidator* aAttrValidator =
526 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
527 if (aAttrValidator) {
528 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
533 const ModelAPI_ShapeValidator* aShapeValidator =
534 dynamic_cast<const ModelAPI_ShapeValidator*>(*aValidator);
535 if (aShapeValidator) {
536 DataPtr aData = myFeature->data();
537 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
538 if (!aShapeValidator->isValid(myFeature, aSelectAttr, theShape)) {