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::storeValue() 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 if (isValid(aObject, aShape)) {
286 setObject(aObject, aShape);
292 //********************************************************************
293 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
295 mySelectedObject = theObj;
297 if (mySelectedObject) {
300 updateSelectionName();
301 emit valuesChanged();
304 //********************************************************************
305 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
307 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
309 // // Check that the shape of necessary type
310 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
313 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
314 // if (aShape.IsNull())
317 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
318 // if (aShapeType == TopAbs_COMPOUND) {
319 // foreach (QString aType,
321 // TopExp_Explorer aEx(aShape, shapeType(aType));
326 // foreach (QString aType, myShapeTypes) {
327 // if (shapeType(aType) == aShapeType)
334 //********************************************************************
335 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
337 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
338 foreach (QString aType, myShapeTypes) {
339 if (aShape.ShapeType() == shapeType(aType))
345 //********************************************************************
346 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
348 // Definition of types is not obligatory. If types are not defined then
349 // it means that accepted any type
350 if (myObjectTypes.isEmpty())
353 foreach (QString aType, myObjectTypes) {
354 if (aType.toLower() == "construction") {
355 ResultConstructionPtr aConstr =
356 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
357 return (aConstr != NULL);
358 } // ToDo: Process other types of objects
360 // Object type is defined but not found
365 //********************************************************************
366 void ModuleBase_WidgetShapeSelector::updateSelectionName()
368 DataPtr aData = myFeature->data();
369 bool isNameUpdated = false;
370 if (aData.get() != NULL) {
371 AttributeSelectionPtr aSelect = aData->selection(attributeID());
373 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
374 isNameUpdated = true;
377 if (!isNameUpdated) {
378 if (mySelectedObject) {
379 std::string aName = mySelectedObject->data()->name();
380 myTextLine->setText(QString::fromStdString(aName));
383 myTextLine->setText("");
390 //********************************************************************
391 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
393 if (myIsActive == toActivate)
395 myIsActive = toActivate;
396 updateSelectionName();
397 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
400 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
402 foreach (QString aType, myShapeTypes) {
403 aList.append(shapeType(aType));
405 myWorkshop->activateSubShapesSelection(aList);
406 if (!myObjectTypes.isEmpty()) {
407 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
408 aViewer->clearSelectionFilters();
409 aViewer->addSelectionFilter(myObjTypeFilter);
412 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
413 if (!myObjTypeFilter.IsNull()) {
414 aViewer->removeSelectionFilter(myObjTypeFilter);
415 myObjTypeFilter.Nullify();
417 myWorkshop->deactivateSubShapesSelection();
419 // apply filters loaded from the XML definition of the widget
420 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
421 SelectMgr_ListOfFilter aFilters;
422 aFactory->filters(parentID(), attributeID(), aFilters);
423 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
424 for (; aIt.More(); aIt.Next()) {
425 Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
426 if (aSelFilter.IsNull())
429 //Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
430 //if (aFilter.IsNull())
433 aViewer->addSelectionFilter(aSelFilter);
435 aViewer->removeSelectionFilter(aSelFilter);
439 //********************************************************************
440 void ModuleBase_WidgetShapeSelector::raisePanel() const
442 QWidget* aParent = this->parentWidget();
443 QWidget* aLastPanel = 0;
444 while (!aParent->inherits("QDockWidget")) {
445 aLastPanel = aParent;
446 aParent = aParent->parentWidget();
450 if (aParent->inherits("QDockWidget")) {
451 QDockWidget* aTabWgt = (QDockWidget*) aParent;
456 //********************************************************************
457 void ModuleBase_WidgetShapeSelector::activateCustom()
459 activateSelection(true);
462 //********************************************************************
463 void ModuleBase_WidgetShapeSelector::deactivate()
465 activateSelection(false);
468 //********************************************************************
469 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
471 SessionPtr aMgr = ModelAPI_Session::get();
472 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
473 std::list<ModelAPI_Validator*> aValidators;
474 std::list<std::list<std::string> > anArguments;
475 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
477 // Check the type of selected object
478 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
480 for (; aValidator != aValidators.end(); aValidator++) {
481 const ModelAPI_ResultValidator* aResValidator =
482 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
485 if (aResValidator->isValid(theObj)) {
494 // Check the acceptability of the object as attribute
495 aValidator = aValidators.begin();
496 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
497 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
498 const ModelAPI_RefAttrValidator* aAttrValidator =
499 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
500 if (aAttrValidator) {
501 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
506 const ModelAPI_ShapeValidator* aShapeValidator =
507 dynamic_cast<const ModelAPI_ShapeValidator*>(*aValidator);
508 if (aShapeValidator) {
509 DataPtr aData = myFeature->data();
510 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
511 if (!aShapeValidator->isValid(myFeature, aSelectAttr, theShape)) {