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>
12 #include <Config_WidgetAPI.h>
13 #include <Events_Loop.h>
14 #include <Events_Message.h>
15 #include <GeomAPI_Interface.h>
16 #include <GeomAPI_Shape.h>
18 #include <ModelAPI_AttributeReference.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_Validator.h>
32 #include <ModelAPI_ResultValidator.h>
33 #include <ModelAPI_RefAttrValidator.h>
35 #include <Config_WidgetAPI.h>
36 #include <Events_Error.h>
38 #include <GeomAPI_Shape.h>
40 #include <TopoDS_Shape.hxx>
41 #include <TopExp_Explorer.hxx>
47 #include <QToolButton>
50 #include <QDockWidget>
51 #include <QApplication>
53 #include <TopExp_Explorer.hxx>
54 #include <TopoDS_Shape.hxx>
61 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
62 static ShapeTypes MyShapeTypes;
64 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
66 if (MyShapeTypes.count() == 0) {
67 MyShapeTypes["face"] = TopAbs_FACE;
68 MyShapeTypes["faces"] = TopAbs_FACE;
69 MyShapeTypes["vertex"] = TopAbs_VERTEX;
70 MyShapeTypes["vertices"] = TopAbs_VERTEX;
71 MyShapeTypes["wire"] = TopAbs_WIRE;
72 MyShapeTypes["edge"] = TopAbs_EDGE;
73 MyShapeTypes["edges"] = TopAbs_EDGE;
74 MyShapeTypes["shell"] = TopAbs_SHELL;
75 MyShapeTypes["solid"] = TopAbs_SOLID;
76 MyShapeTypes["solids"] = TopAbs_SOLID;
78 QString aType = theType.toLower();
79 if (MyShapeTypes.contains(aType))
80 return MyShapeTypes[aType];
81 Events_Error::send("Shape type defined in XML is not implemented!");
85 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
86 ModuleBase_IWorkshop* theWorkshop,
87 const Config_WidgetAPI* theData,
88 const std::string& theParentId)
89 : ModuleBase_ModelWidget(theParent, theData, theParentId),
90 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
92 myContainer = new QWidget(theParent);
93 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
94 ModuleBase_Tools::adjustMargins(aLayout);
96 QString aLabelText = QString::fromStdString(theData->widgetLabel());
97 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
98 myLabel = new QLabel(aLabelText, myContainer);
99 if (!aLabelIcon.isEmpty())
100 myLabel->setPixmap(QPixmap(aLabelIcon));
102 aLayout->addWidget(myLabel);
104 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
105 myTextLine = new QLineEdit(myContainer);
106 myTextLine->setReadOnly(true);
107 myTextLine->setToolTip(aToolTip);
108 myTextLine->installEventFilter(this);
110 aLayout->addWidget(myTextLine, 1);
112 std::string aTypes = theData->getProperty("shape_types");
113 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
115 std::string aObjTypes = theData->getProperty("object_types");
116 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
118 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
121 //********************************************************************
122 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
124 activateSelection(false);
127 //********************************************************************
128 bool ModuleBase_WidgetShapeSelector::storeValue() const
130 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
131 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
134 DataPtr aData = myFeature->data();
135 if (myUseSubShapes) {
136 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
138 AttributePtr aAttr = aData->attribute(attributeID());
140 // We have to check several attributes types
141 AttributeSelectionPtr aSelectAttr =
142 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
144 aSelectAttr->setValue(aBody, myShape);
145 updateObject(myFeature);
148 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
150 aRefAttr->setObject(mySelectedObject);
151 updateObject(myFeature);
157 AttributeReferencePtr aRef = aData->reference(attributeID());
159 ObjectPtr aObject = aRef->value();
160 if (!(aObject && aObject->isSame(mySelectedObject))) {
161 aRef->setValue(mySelectedObject);
162 updateObject(myFeature);
166 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
168 ObjectPtr aObject = aRefAttr->object();
169 if (!(aObject && aObject->isSame(mySelectedObject))) {
170 aRefAttr->setObject(mySelectedObject);
171 updateObject(myFeature);
180 //********************************************************************
181 void ModuleBase_WidgetShapeSelector::clearAttribute()
183 DataPtr aData = myFeature->data();
184 AttributeSelectionPtr aSelect = aData->selection(attributeID());
186 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
189 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
191 aRefAttr->setObject(ObjectPtr());
194 AttributeReferencePtr aRef = aData->reference(attributeID());
196 aRef->setObject(ObjectPtr());
200 //********************************************************************
201 bool ModuleBase_WidgetShapeSelector::restoreValue()
203 DataPtr aData = myFeature->data();
204 bool isBlocked = this->blockSignals(true);
205 if (myUseSubShapes) {
206 AttributeSelectionPtr aSelect = aData->selection(attributeID());
208 mySelectedObject = aSelect->context();
209 myShape = aSelect->value();
211 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
213 mySelectedObject = aRefAttr->object();
217 AttributeReferencePtr aRef = aData->reference(attributeID());
219 mySelectedObject = aRef->value();
221 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
223 mySelectedObject = aRefAttr->object();
226 updateSelectionName();
228 this->blockSignals(isBlocked);
232 //********************************************************************
233 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
235 QList<QWidget*> aControls;
236 aControls.append(myTextLine);
240 //********************************************************************
241 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
243 // In order to make reselection possible
244 // TODO: check with MPV clearAttribute();
246 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
247 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
248 if (aSelected.size() > 0)
249 setSelection(aSelected.first());
252 //********************************************************************
253 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
255 ObjectPtr aObject = theValue.object();
256 if ((!mySelectedObject) && (!aObject))
259 // Check that the selected object is result (others can not be accepted)
260 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
265 // We can not select a result of our feature
266 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
267 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
268 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
273 // Check that object belongs to active document or PartSet
274 DocumentPtr aDoc = aRes->document();
275 SessionPtr aMgr = ModelAPI_Session::get();
276 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
279 // Check that the result has a shape
280 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
284 /// Check that object has acceptable type
285 if (!acceptObjectType(aObject))
288 // Get sub-shapes from local selection
289 if (myUseSubShapes) {
290 if (!theValue.shape().IsNull()) {
291 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
292 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
296 // Check that the selection corresponds to selection type
297 if (myUseSubShapes) {
298 if (!acceptSubShape(aShape))
301 if (!acceptObjectShape(aObject))
304 if (isValid(aObject, aShape)) {
305 setObject(aObject, aShape);
306 emit focusOutWidget(this);
311 //********************************************************************
312 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
314 mySelectedObject = theObj;
316 if (mySelectedObject) {
319 updateSelectionName();
320 emit valuesChanged();
323 //********************************************************************
324 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
326 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
328 // Check that the shape of necessary type
329 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
332 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
336 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
337 if (aShapeType == TopAbs_COMPOUND) {
338 foreach (QString aType, myShapeTypes) {
339 TopExp_Explorer aEx(aShape, shapeType(aType));
344 foreach (QString aType, myShapeTypes) {
345 if (shapeType(aType) == aShapeType)
352 //********************************************************************
353 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
355 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
356 foreach (QString aType, myShapeTypes) {
357 if (aShape.ShapeType() == shapeType(aType))
363 //********************************************************************
364 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
366 // Definition of types is not obligatory. If types are not defined then
367 // it means that accepted any type
368 if (myObjectTypes.isEmpty())
371 foreach (QString aType, myObjectTypes) {
372 if (aType.toLower() == "construction") {
373 ResultConstructionPtr aConstr =
374 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
375 return (aConstr != NULL);
376 } // ToDo: Process other types of objects
378 // Object type is defined but not found
383 //********************************************************************
384 void ModuleBase_WidgetShapeSelector::updateSelectionName()
386 if (mySelectedObject) {
387 std::string aName = mySelectedObject->data()->name();
388 myTextLine->setText(QString::fromStdString(aName));
391 myTextLine->setText("");
397 //********************************************************************
398 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
400 if (myIsActive == toActivate)
402 myIsActive = toActivate;
403 updateSelectionName();
406 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
407 if (myUseSubShapes) {
410 foreach (QString aType, myShapeTypes)
411 aList.append(shapeType(aType));
412 myWorkshop->activateSubShapesSelection(aList);
413 if (!myObjectTypes.isEmpty()) {
414 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
415 myWorkshop->viewer()->clearSelectionFilters();
416 myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
420 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
421 if (myUseSubShapes) {
422 if (!myObjTypeFilter.IsNull()) {
423 myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
424 myObjTypeFilter.Nullify();
426 myWorkshop->deactivateSubShapesSelection();
431 //********************************************************************
432 void ModuleBase_WidgetShapeSelector::raisePanel() const
434 QWidget* aParent = myContainer->parentWidget();
435 QWidget* aLastPanel = 0;
436 while (!aParent->inherits("QDockWidget")) {
437 aLastPanel = aParent;
438 aParent = aParent->parentWidget();
442 if (aParent->inherits("QDockWidget")) {
443 QDockWidget* aTabWgt = (QDockWidget*) aParent;
448 //********************************************************************
449 //bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
451 // if (theValue.object()) {
452 // ObjectPtr aObject = theValue.object();
453 // if (acceptObjectShape(aObject)) {
454 // setObject(aObject);
461 //********************************************************************
462 void ModuleBase_WidgetShapeSelector::activate()
464 activateSelection(true);
467 //********************************************************************
468 void ModuleBase_WidgetShapeSelector::deactivate()
470 activateSelection(false);
473 //********************************************************************
474 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
476 SessionPtr aMgr = ModelAPI_Session::get();
477 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
478 std::list<ModelAPI_Validator*> aValidators;
479 std::list<std::list<std::string> > anArguments;
480 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
482 // Check the type of selected object
483 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
485 for (; aValidator != aValidators.end(); aValidator++) {
486 const ModelAPI_ResultValidator* aResValidator =
487 dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
490 if (aResValidator->isValid(theObj)) {
499 // Check the acceptability of the object as attribute
500 aValidator = aValidators.begin();
501 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
502 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
503 const ModelAPI_RefAttrValidator* aAttrValidator =
504 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
505 if (aAttrValidator) {
506 if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {