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)
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);
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 if (mySelectedObject) {
369 std::string aName = mySelectedObject->data()->name();
370 myTextLine->setText(QString::fromStdString(aName));
373 myTextLine->setText("");
379 //********************************************************************
380 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
382 if (myIsActive == toActivate)
384 myIsActive = toActivate;
385 updateSelectionName();
386 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
389 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
391 foreach (QString aType, myShapeTypes) {
392 aList.append(shapeType(aType));
394 myWorkshop->activateSubShapesSelection(aList);
395 if (!myObjectTypes.isEmpty()) {
396 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
397 aViewer->clearSelectionFilters();
398 aViewer->addSelectionFilter(myObjTypeFilter);
401 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
402 if (!myObjTypeFilter.IsNull()) {
403 aViewer->removeSelectionFilter(myObjTypeFilter);
404 myObjTypeFilter.Nullify();
406 myWorkshop->deactivateSubShapesSelection();
408 // apply filters loaded from the XML definition of the widget
409 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
410 SelectMgr_ListOfFilter aFilters;
411 aFactory->filters(parentID(), attributeID(), aFilters);
412 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
413 for (; aIt.More(); aIt.Next()) {
414 Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
415 if (aSelFilter.IsNull())
418 //Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
419 //if (aFilter.IsNull())
422 aViewer->addSelectionFilter(aSelFilter);
424 aViewer->removeSelectionFilter(aSelFilter);
428 //********************************************************************
429 void ModuleBase_WidgetShapeSelector::raisePanel() const
431 QWidget* aParent = myContainer->parentWidget();
432 QWidget* aLastPanel = 0;
433 while (!aParent->inherits("QDockWidget")) {
434 aLastPanel = aParent;
435 aParent = aParent->parentWidget();
439 if (aParent->inherits("QDockWidget")) {
440 QDockWidget* aTabWgt = (QDockWidget*) aParent;
445 //********************************************************************
446 void ModuleBase_WidgetShapeSelector::activate()
448 activateSelection(true);
451 //********************************************************************
452 void ModuleBase_WidgetShapeSelector::deactivate()
454 activateSelection(false);
457 //********************************************************************
458 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
460 SessionPtr aMgr = ModelAPI_Session::get();
461 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
462 std::list<ModelAPI_Validator*> aValidators;
463 std::list<std::list<std::string> > anArguments;
464 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
466 // Check the type of selected object
467 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
469 for (; aValidator != aValidators.end(); aValidator++) {
470 const ModelAPI_ResultValidator* aResValidator =
471 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
474 if (aResValidator->isValid(theObj)) {
483 // Check the acceptability of the object as attribute
484 aValidator = aValidators.begin();
485 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
486 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
487 const ModelAPI_RefAttrValidator* aAttrValidator =
488 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
489 if (aAttrValidator) {
490 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {