1 // File: ModuleBase_WidgetShapeSelector.h
2 // Created: 2 June 2014
3 // Author: Vitaly Smetannikov
5 #include <ModuleBase_WidgetShapeSelector.h>
6 #include <ModuleBase_Definitions.h>
7 #include <ModuleBase_ISelection.h>
8 #include <ModuleBase_IWorkshop.h>
9 #include <ModuleBase_IViewer.h>
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_FilterFactory.h>
12 #include <ModuleBase_Filter.h>
14 #include <Config_WidgetAPI.h>
15 #include <Events_Loop.h>
16 #include <Events_Message.h>
17 #include <GeomAPI_Interface.h>
18 #include <GeomAPI_Shape.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Feature.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_AttributeReference.h>
28 #include <ModelAPI_AttributeSelection.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_ResultBody.h>
32 #include <ModelAPI_AttributeRefAttr.h>
33 #include <ModelAPI_Validator.h>
34 #include <ModelAPI_ResultValidator.h>
35 #include <ModelAPI_RefAttrValidator.h>
37 #include <Config_WidgetAPI.h>
38 #include <Events_Error.h>
40 #include <GeomAPI_Shape.h>
42 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopExp_Explorer.hxx>
50 #include <QToolButton>
53 #include <QDockWidget>
54 #include <QApplication>
56 #include <TopExp_Explorer.hxx>
57 #include <TopoDS_Shape.hxx>
64 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
65 static ShapeTypes MyShapeTypes;
67 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
69 if (MyShapeTypes.count() == 0) {
70 MyShapeTypes["face"] = TopAbs_FACE;
71 MyShapeTypes["faces"] = TopAbs_FACE;
72 MyShapeTypes["vertex"] = TopAbs_VERTEX;
73 MyShapeTypes["vertices"] = TopAbs_VERTEX;
74 MyShapeTypes["wire"] = TopAbs_WIRE;
75 MyShapeTypes["edge"] = TopAbs_EDGE;
76 MyShapeTypes["edges"] = TopAbs_EDGE;
77 MyShapeTypes["shell"] = TopAbs_SHELL;
78 MyShapeTypes["solid"] = TopAbs_SOLID;
79 MyShapeTypes["solids"] = TopAbs_SOLID;
81 QString aType = theType.toLower();
82 if (MyShapeTypes.contains(aType))
83 return MyShapeTypes[aType];
84 Events_Error::send("Shape type defined in XML is not implemented!");
88 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
89 ModuleBase_IWorkshop* theWorkshop,
90 const Config_WidgetAPI* theData,
91 const std::string& theParentId)
92 : ModuleBase_ModelWidget(theParent, theData, theParentId),
93 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
95 myContainer = new QWidget(theParent);
96 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
97 ModuleBase_Tools::adjustMargins(aLayout);
99 QString aLabelText = QString::fromStdString(theData->widgetLabel());
100 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
101 myLabel = new QLabel(aLabelText, myContainer);
102 if (!aLabelIcon.isEmpty())
103 myLabel->setPixmap(QPixmap(aLabelIcon));
105 aLayout->addWidget(myLabel);
107 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
108 myTextLine = new QLineEdit(myContainer);
109 myTextLine->setReadOnly(true);
110 myTextLine->setToolTip(aToolTip);
111 myTextLine->installEventFilter(this);
113 aLayout->addWidget(myTextLine, 1);
115 std::string aTypes = theData->getProperty("shape_types");
116 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
118 std::string aObjTypes = theData->getProperty("object_types");
119 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
121 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
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 if (myUseSubShapes) {
139 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
141 AttributePtr aAttr = aData->attribute(attributeID());
143 // We have to check several attributes types
144 AttributeSelectionPtr aSelectAttr =
145 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
147 aSelectAttr->setValue(aBody, myShape);
148 updateObject(myFeature);
151 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
153 aRefAttr->setObject(mySelectedObject);
154 updateObject(myFeature);
160 AttributeReferencePtr aRef = aData->reference(attributeID());
162 ObjectPtr aObject = aRef->value();
163 if (!(aObject && aObject->isSame(mySelectedObject))) {
164 aRef->setValue(mySelectedObject);
165 updateObject(myFeature);
169 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
171 ObjectPtr aObject = aRefAttr->object();
172 if (!(aObject && aObject->isSame(mySelectedObject))) {
173 aRefAttr->setObject(mySelectedObject);
174 updateObject(myFeature);
183 //********************************************************************
184 void ModuleBase_WidgetShapeSelector::clearAttribute()
186 DataPtr aData = myFeature->data();
187 AttributeSelectionPtr aSelect = aData->selection(attributeID());
189 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
192 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
194 aRefAttr->setObject(ObjectPtr());
197 AttributeReferencePtr aRef = aData->reference(attributeID());
199 aRef->setObject(ObjectPtr());
203 //********************************************************************
204 bool ModuleBase_WidgetShapeSelector::restoreValue()
206 DataPtr aData = myFeature->data();
207 bool isBlocked = this->blockSignals(true);
208 if (myUseSubShapes) {
209 AttributeSelectionPtr aSelect = aData->selection(attributeID());
211 mySelectedObject = aSelect->context();
212 myShape = aSelect->value();
214 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
216 mySelectedObject = aRefAttr->object();
220 AttributeReferencePtr aRef = aData->reference(attributeID());
222 mySelectedObject = aRef->value();
224 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
226 mySelectedObject = aRefAttr->object();
229 updateSelectionName();
231 this->blockSignals(isBlocked);
235 //********************************************************************
236 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
238 QList<QWidget*> aControls;
239 aControls.append(myTextLine);
243 //********************************************************************
244 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
246 // In order to make reselection possible
247 // TODO: check with MPV clearAttribute();
249 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
250 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
251 if (aSelected.size() > 0) {
252 if (setSelection(aSelected.first()))
253 emit focusOutWidget(this);
257 //********************************************************************
258 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
260 ObjectPtr aObject = theValue.object();
261 if ((!mySelectedObject) && (!aObject))
264 // Check that the selected object is result (others can not be accepted)
265 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
270 // We can not select a result of our feature
271 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
272 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
273 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
278 // Check that object belongs to active document or PartSet
279 DocumentPtr aDoc = aRes->document();
280 SessionPtr aMgr = ModelAPI_Session::get();
281 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
284 // Check that the result has a shape
285 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
289 /// Check that object has acceptable type
290 if (!acceptObjectType(aObject))
293 // Get sub-shapes from local selection
294 if (myUseSubShapes) {
295 if (!theValue.shape().IsNull()) {
296 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
297 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
301 // Check that the selection corresponds to selection type
302 if (myUseSubShapes) {
303 if (!acceptSubShape(aShape))
306 if (!acceptObjectShape(aObject))
309 if (isValid(aObject, aShape)) {
310 setObject(aObject, aShape);
316 //********************************************************************
317 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
319 mySelectedObject = theObj;
321 if (mySelectedObject) {
324 updateSelectionName();
325 emit valuesChanged();
328 //********************************************************************
329 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
331 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
333 // Check that the shape of necessary type
334 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
337 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
341 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
342 if (aShapeType == TopAbs_COMPOUND) {
343 foreach (QString aType, myShapeTypes) {
344 TopExp_Explorer aEx(aShape, shapeType(aType));
349 foreach (QString aType, myShapeTypes) {
350 if (shapeType(aType) == aShapeType)
357 //********************************************************************
358 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
360 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
361 foreach (QString aType, myShapeTypes) {
362 if (aShape.ShapeType() == shapeType(aType))
368 //********************************************************************
369 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
371 // Definition of types is not obligatory. If types are not defined then
372 // it means that accepted any type
373 if (myObjectTypes.isEmpty())
376 foreach (QString aType, myObjectTypes) {
377 if (aType.toLower() == "construction") {
378 ResultConstructionPtr aConstr =
379 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
380 return (aConstr != NULL);
381 } // ToDo: Process other types of objects
383 // Object type is defined but not found
388 //********************************************************************
389 void ModuleBase_WidgetShapeSelector::updateSelectionName()
391 if (mySelectedObject) {
392 std::string aName = mySelectedObject->data()->name();
393 myTextLine->setText(QString::fromStdString(aName));
396 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()));
413 if (myUseSubShapes) {
416 foreach (QString aType, myShapeTypes)
417 aList.append(shapeType(aType));
418 myWorkshop->activateSubShapesSelection(aList);
419 if (!myObjectTypes.isEmpty()) {
420 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
421 aViewer->clearSelectionFilters();
422 aViewer->addSelectionFilter(myObjTypeFilter);
426 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
427 if (myUseSubShapes) {
428 if (!myObjTypeFilter.IsNull()) {
429 aViewer->removeSelectionFilter(myObjTypeFilter);
430 myObjTypeFilter.Nullify();
432 myWorkshop->deactivateSubShapesSelection();
436 /*ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
437 const SelectMgr_ListOfFilter& aFilters = aFactory->filters(parentID(), attributeID());
438 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
439 for (; aIt.More(); aIt.Next()) {
440 Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
442 aViewer->addSelectionFilter(aFilter);
444 aViewer->removeSelectionFilter(aFilter);
448 //********************************************************************
449 void ModuleBase_WidgetShapeSelector::raisePanel() const
451 QWidget* aParent = myContainer->parentWidget();
452 QWidget* aLastPanel = 0;
453 while (!aParent->inherits("QDockWidget")) {
454 aLastPanel = aParent;
455 aParent = aParent->parentWidget();
459 if (aParent->inherits("QDockWidget")) {
460 QDockWidget* aTabWgt = (QDockWidget*) aParent;
465 //********************************************************************
466 //bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
468 // if (theValue.object()) {
469 // ObjectPtr aObject = theValue.object();
470 // if (acceptObjectShape(aObject)) {
471 // setObject(aObject);
478 //********************************************************************
479 void ModuleBase_WidgetShapeSelector::activate()
481 activateSelection(true);
484 //********************************************************************
485 void ModuleBase_WidgetShapeSelector::deactivate()
487 activateSelection(false);
490 //********************************************************************
491 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
493 SessionPtr aMgr = ModelAPI_Session::get();
494 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
495 std::list<ModelAPI_Validator*> aValidators;
496 std::list<std::list<std::string> > anArguments;
497 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
499 // Check the type of selected object
500 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
502 for (; aValidator != aValidators.end(); aValidator++) {
503 const ModelAPI_ResultValidator* aResValidator =
504 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
507 if (aResValidator->isValid(theObj)) {
516 // Check the acceptability of the object as attribute
517 aValidator = aValidators.begin();
518 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
519 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
520 const ModelAPI_RefAttrValidator* aAttrValidator =
521 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
522 if (aAttrValidator) {
523 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {