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::storeValue() 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 if (isValid(aObject, aShape)) {
287 setObject(aObject, aShape);
293 //********************************************************************
294 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
296 mySelectedObject = theObj;
298 if (mySelectedObject) {
301 updateSelectionName();
302 emit valuesChanged();
305 //********************************************************************
306 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
308 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
310 // // Check that the shape of necessary type
311 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
314 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
315 // if (aShape.IsNull())
318 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
319 // if (aShapeType == TopAbs_COMPOUND) {
320 // foreach (QString aType,
322 // TopExp_Explorer aEx(aShape, shapeType(aType));
327 // foreach (QString aType, myShapeTypes) {
328 // if (shapeType(aType) == aShapeType)
335 //********************************************************************
336 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
338 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
339 foreach (QString aType, myShapeTypes) {
340 if (aShape.ShapeType() == shapeType(aType))
346 //********************************************************************
347 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
349 // Definition of types is not obligatory. If types are not defined then
350 // it means that accepted any type
351 if (myObjectTypes.isEmpty())
354 foreach (QString aType, myObjectTypes) {
355 if (aType.toLower() == "construction") {
356 ResultConstructionPtr aConstr =
357 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
358 return (aConstr != NULL);
359 } // ToDo: Process other types of objects
361 // Object type is defined but not found
366 //********************************************************************
367 void ModuleBase_WidgetShapeSelector::updateSelectionName()
369 DataPtr aData = myFeature->data();
370 bool isNameUpdated = false;
371 if (aData.get() != NULL) {
372 AttributeSelectionPtr aSelect = aData->selection(attributeID());
374 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
375 isNameUpdated = true;
378 if (!isNameUpdated) {
379 if (mySelectedObject) {
380 std::string aName = mySelectedObject->data()->name();
381 myTextLine->setText(QString::fromStdString(aName));
384 myTextLine->setText("");
391 //********************************************************************
392 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
394 if (myIsActive == toActivate)
396 myIsActive = toActivate;
397 updateSelectionName();
398 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
401 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
403 foreach (QString aType, myShapeTypes) {
404 aList.append(shapeType(aType));
406 myWorkshop->activateSubShapesSelection(aList);
407 if (!myObjectTypes.isEmpty()) {
408 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
409 aViewer->clearSelectionFilters();
410 aViewer->addSelectionFilter(myObjTypeFilter);
413 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
414 if (!myObjTypeFilter.IsNull()) {
415 aViewer->removeSelectionFilter(myObjTypeFilter);
416 myObjTypeFilter.Nullify();
418 myWorkshop->deactivateSubShapesSelection();
420 // apply filters loaded from the XML definition of the widget
421 ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
422 SelectMgr_ListOfFilter aFilters;
423 aFactory->filters(parentID(), attributeID(), aFilters);
424 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
425 for (; aIt.More(); aIt.Next()) {
426 Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
427 if (aSelFilter.IsNull())
430 //Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
431 //if (aFilter.IsNull())
434 aViewer->addSelectionFilter(aSelFilter);
436 aViewer->removeSelectionFilter(aSelFilter);
440 //********************************************************************
441 void ModuleBase_WidgetShapeSelector::raisePanel() const
443 QWidget* aParent = myContainer->parentWidget();
444 QWidget* aLastPanel = 0;
445 while (!aParent->inherits("QDockWidget")) {
446 aLastPanel = aParent;
447 aParent = aParent->parentWidget();
451 if (aParent->inherits("QDockWidget")) {
452 QDockWidget* aTabWgt = (QDockWidget*) aParent;
457 //********************************************************************
458 void ModuleBase_WidgetShapeSelector::activateCustom()
460 activateSelection(true);
463 //********************************************************************
464 void ModuleBase_WidgetShapeSelector::deactivate()
466 activateSelection(false);
469 //********************************************************************
470 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
472 SessionPtr aMgr = ModelAPI_Session::get();
473 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
474 std::list<ModelAPI_Validator*> aValidators;
475 std::list<std::list<std::string> > anArguments;
476 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
478 // Check the type of selected object
479 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
481 for (; aValidator != aValidators.end(); aValidator++) {
482 const ModelAPI_ResultValidator* aResValidator =
483 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
486 if (aResValidator->isValid(theObj)) {
495 // Check the acceptability of the object as attribute
496 aValidator = aValidators.begin();
497 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
498 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
499 const ModelAPI_RefAttrValidator* aAttrValidator =
500 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
501 if (aAttrValidator) {
502 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
507 const ModelAPI_ShapeValidator* aShapeValidator =
508 dynamic_cast<const ModelAPI_ShapeValidator*>(*aValidator);
509 if (aShapeValidator) {
510 DataPtr aData = myFeature->data();
511 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
512 if (!aShapeValidator->isValid(myFeature, aSelectAttr, theShape)) {