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>
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_ModelWidget(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);
120 std::string aObjTypes = theData->getProperty("object_types");
121 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
124 //********************************************************************
125 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
127 activateSelection(false);
130 //********************************************************************
131 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
133 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
134 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
137 DataPtr aData = myFeature->data();
138 AttributeReferencePtr aRef = aData->reference(attributeID());
140 ObjectPtr aObject = aRef->value();
141 if (!(aObject && aObject->isSame(mySelectedObject))) {
142 aRef->setValue(mySelectedObject);
143 updateObject(myFeature);
147 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
149 ObjectPtr aObject = aRefAttr->object();
150 if (!(aObject && aObject->isSame(mySelectedObject))) {
151 aRefAttr->setObject(mySelectedObject);
152 updateObject(myFeature);
156 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
157 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
158 if (aSelectAttr && aBody && (myShape.get() != NULL)) {
159 aSelectAttr->setValue(aBody, myShape);
160 updateObject(myFeature);
168 //********************************************************************
169 void ModuleBase_WidgetShapeSelector::clearAttribute()
171 DataPtr aData = myFeature->data();
172 AttributeSelectionPtr aSelect = aData->selection(attributeID());
174 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
177 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
179 aRefAttr->setObject(ObjectPtr());
182 AttributeReferencePtr aRef = aData->reference(attributeID());
184 aRef->setObject(ObjectPtr());
188 //********************************************************************
189 bool ModuleBase_WidgetShapeSelector::restoreValue()
191 DataPtr aData = myFeature->data();
192 bool isBlocked = this->blockSignals(true);
194 AttributeSelectionPtr aSelect = aData->selection(attributeID());
196 mySelectedObject = aSelect->context();
197 myShape = aSelect->value();
199 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
201 mySelectedObject = aRefAttr->object();
203 AttributeReferencePtr aRef = aData->reference(attributeID());
205 mySelectedObject = aRef->value();
209 updateSelectionName();
211 this->blockSignals(isBlocked);
215 //********************************************************************
216 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
218 QList<QWidget*> aControls;
219 aControls.append(myTextLine);
223 //********************************************************************
224 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
226 // In order to make reselection possible
227 // TODO: check with MPV clearAttribute();
229 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
230 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
231 if (aSelected.size() > 0) {
232 if (setSelection(aSelected.first()))
233 emit focusOutWidget(this);
237 //********************************************************************
238 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
240 ObjectPtr aObject = theValue.object();
241 if ((!mySelectedObject) && (!aObject))
244 // Check that the selected object is result (others can not be accepted)
245 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
250 // We can not select a result of our feature
251 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
252 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
253 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
258 // Check that object belongs to active document or PartSet
259 DocumentPtr aDoc = aRes->document();
260 SessionPtr aMgr = ModelAPI_Session::get();
261 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
264 // Check that the result has a shape
265 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
269 /// Check that object has acceptable type
270 if (!acceptObjectType(aObject))
273 // Get sub-shapes from local selection
274 if (!theValue.shape().IsNull()) {
275 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
276 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
279 // Check that the selection corresponds to selection type
280 if (!acceptSubShape(aShape))
282 // if (!acceptObjectShape(aObject))
285 // Check whether the value is valid for the viewer selection filters
286 Handle(SelectMgr_EntityOwner) anOwner = theValue.owner();
287 if (!anOwner.IsNull()) {
288 SelectMgr_ListOfFilter aFilters;
289 selectionFilters(aFilters);
290 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
291 for (; aIt.More(); aIt.Next()) {
292 const Handle(SelectMgr_Filter)& aFilter = aIt.Value();
293 if (!aFilter->IsOk(anOwner))
297 if (isValid(aObject, aShape)) {
298 setObject(aObject, aShape);
304 //********************************************************************
305 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
307 mySelectedObject = theObj;
309 if (mySelectedObject) {
312 updateSelectionName();
313 emit valuesChanged();
316 //********************************************************************
317 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
319 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
321 // // Check that the shape of necessary type
322 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
325 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
326 // if (aShape.IsNull())
329 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
330 // if (aShapeType == TopAbs_COMPOUND) {
331 // foreach (QString aType,
333 // TopExp_Explorer aEx(aShape, shapeType(aType));
338 // foreach (QString aType, myShapeTypes) {
339 // if (shapeType(aType) == aShapeType)
346 //********************************************************************
347 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
349 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
350 foreach (QString aType, myShapeTypes) {
351 if (aShape.ShapeType() == shapeType(aType))
357 //********************************************************************
358 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
360 // Definition of types is not obligatory. If types are not defined then
361 // it means that accepted any type
362 if (myObjectTypes.isEmpty())
365 foreach (QString aType, myObjectTypes) {
366 if (aType.toLower() == "construction") {
367 ResultConstructionPtr aConstr =
368 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
369 return (aConstr != NULL);
370 } // ToDo: Process other types of objects
372 // Object type is defined but not found
377 //********************************************************************
378 void ModuleBase_WidgetShapeSelector::updateSelectionName()
380 DataPtr aData = myFeature->data();
381 bool isNameUpdated = false;
382 if (aData.get() != NULL) {
383 AttributeSelectionPtr aSelect = aData->selection(attributeID());
385 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
386 isNameUpdated = true;
389 if (!isNameUpdated) {
390 if (mySelectedObject) {
391 std::string aName = mySelectedObject->data()->name();
392 myTextLine->setText(QString::fromStdString(aName));
395 myTextLine->setText("");
402 //********************************************************************
403 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
405 if (myIsActive == toActivate)
407 myIsActive = toActivate;
408 updateSelectionName();
409 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
412 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
414 foreach (QString aType, myShapeTypes) {
415 aList.append(shapeType(aType));
417 myWorkshop->activateSubShapesSelection(aList);
419 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
420 myWorkshop->deactivateSubShapesSelection();
423 // the initial code is moved here in order to clear filters, it is possible, that it is excessive and
424 // is performed outside
425 if (myIsActive && !myObjectTypes.isEmpty()) {
426 aViewer->clearSelectionFilters();
428 SelectMgr_ListOfFilter aFilters;
429 selectionFilters(aFilters);
430 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
431 for (; aIt.More(); aIt.Next()) {
432 const Handle(SelectMgr_Filter)& aSelFilter = aIt.Value();
434 aViewer->addSelectionFilter(aSelFilter);
436 aViewer->removeSelectionFilter(aSelFilter);
438 // the internal filter should be removed by the widget deactivation, it is done historically
439 if (!myIsActive && !myObjTypeFilter.IsNull()) {
440 myObjTypeFilter.Nullify();
444 //********************************************************************
445 void ModuleBase_WidgetShapeSelector::selectionFilters(SelectMgr_ListOfFilter& theFilters)
447 if (!myObjectTypes.isEmpty()) {
448 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
449 theFilters.Append(myObjTypeFilter);
451 // apply filters loaded from the XML definition of the widget
452 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
453 SelectMgr_ListOfFilter aFilters;
454 aFactory->filters(parentID(), attributeID(), aFilters);
455 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
456 for (; aIt.More(); aIt.Next()) {
457 Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
458 if (aSelFilter.IsNull())
461 theFilters.Append(aSelFilter);
465 //********************************************************************
466 void ModuleBase_WidgetShapeSelector::raisePanel() const
468 QWidget* aParent = this->parentWidget();
469 QWidget* aLastPanel = 0;
470 while (!aParent->inherits("QDockWidget")) {
471 aLastPanel = aParent;
472 aParent = aParent->parentWidget();
476 if (aParent->inherits("QDockWidget")) {
477 QDockWidget* aTabWgt = (QDockWidget*) aParent;
482 //********************************************************************
483 void ModuleBase_WidgetShapeSelector::activateCustom()
485 activateSelection(true);
488 //********************************************************************
489 void ModuleBase_WidgetShapeSelector::deactivate()
491 activateSelection(false);
494 //********************************************************************
495 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
497 SessionPtr aMgr = ModelAPI_Session::get();
498 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
499 std::list<ModelAPI_Validator*> aValidators;
500 std::list<std::list<std::string> > anArguments;
501 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
503 // Check the type of selected object
504 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
506 for (; aValidator != aValidators.end(); aValidator++) {
507 const ModelAPI_ResultValidator* aResValidator =
508 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
511 if (aResValidator->isValid(theObj)) {
520 // Check the acceptability of the object as attribute
521 aValidator = aValidators.begin();
522 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
523 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
524 const ModelAPI_RefAttrValidator* aAttrValidator =
525 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
526 if (aAttrValidator) {
527 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
532 const ModelAPI_ShapeValidator* aShapeValidator =
533 dynamic_cast<const ModelAPI_ShapeValidator*>(*aValidator);
534 if (aShapeValidator) {
535 DataPtr aData = myFeature->data();
536 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
537 if (!aShapeValidator->isValid(myFeature, aSelectAttr, theShape)) {