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_AttributeValidator.h>
37 #include <ModelAPI_ShapeValidator.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>
57 #include <QFormLayout>
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_WidgetValidated(theParent, theData, theParentId),
96 myWorkshop(theWorkshop), myIsActive(false)
98 QFormLayout* aLayout = new QFormLayout(this);
99 ModuleBase_Tools::adjustMargins(aLayout);
101 QString aLabelText = QString::fromStdString(theData->widgetLabel());
102 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
103 myLabel = new QLabel(aLabelText, this);
104 if (!aLabelIcon.isEmpty())
105 myLabel->setPixmap(QPixmap(aLabelIcon));
108 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
109 myTextLine = new QLineEdit(this);
110 myTextLine->setReadOnly(true);
111 myTextLine->setToolTip(aToolTip);
112 myTextLine->installEventFilter(this);
114 aLayout->addRow(myLabel, myTextLine);
116 std::string aTypes = theData->getProperty("shape_types");
117 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
120 //********************************************************************
121 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
123 activateSelection(false);
126 //********************************************************************
127 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
129 // the value is stored on the selection changed signal processing
133 //********************************************************************
134 bool ModuleBase_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject,
135 GeomShapePtr theShape) const
137 bool isChanged = false;
138 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
139 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
142 DataPtr aData = myFeature->data();
143 AttributeReferencePtr aRef = aData->reference(attributeID());
145 ObjectPtr aObject = aRef->value();
146 if (!(aObject && aObject->isSame(theSelectedObject))) {
147 aRef->setValue(theSelectedObject);
151 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
153 ObjectPtr aObject = aRefAttr->object();
154 if (!(aObject && aObject->isSame(theSelectedObject))) {
155 aRefAttr->setObject(theSelectedObject);
159 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
160 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
161 if (aSelectAttr && aBody && (theShape.get() != NULL)) {
162 aSelectAttr->setValue(aBody, theShape);
170 //********************************************************************
171 void ModuleBase_WidgetShapeSelector::clearAttribute()
173 DataPtr aData = myFeature->data();
174 AttributeSelectionPtr aSelect = aData->selection(attributeID());
176 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
179 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
181 aRefAttr->setObject(ObjectPtr());
184 AttributeReferencePtr aRef = aData->reference(attributeID());
186 aRef->setObject(ObjectPtr());
190 //********************************************************************
191 bool ModuleBase_WidgetShapeSelector::restoreValue()
193 bool isBlocked = this->blockSignals(true);
194 updateSelectionName();
195 this->blockSignals(isBlocked);
200 //********************************************************************
201 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
203 QList<QWidget*> aControls;
204 aControls.append(myTextLine);
208 //********************************************************************
209 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
211 // In order to make reselection possible
212 // TODO: check with MPV clearAttribute();
214 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
215 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
216 if (aSelected.size() > 0) {
217 Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
218 if (isValid(anOwner)) {
219 setSelection(anOwner);
220 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
221 // calls validators for the feature and, as a result, updates the Apply button state.
222 updateObject(myFeature);
226 //updateSelectionName();
227 //emit valuesChanged();
228 emit focusOutWidget(this);
233 //********************************************************************
234 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
236 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
238 // // Check that the shape of necessary type
239 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
242 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
243 // if (aShape.IsNull())
246 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
247 // if (aShapeType == TopAbs_COMPOUND) {
248 // foreach (QString aType,
250 // TopExp_Explorer aEx(aShape, shapeType(aType));
255 // foreach (QString aType, myShapeTypes) {
256 // if (shapeType(aType) == aShapeType)
263 //********************************************************************
264 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
266 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
267 foreach (QString aType, myShapeTypes) {
268 if (aShape.ShapeType() == shapeType(aType))
274 ObjectPtr ModuleBase_WidgetShapeSelector::getObject(const AttributePtr& theAttribute)
277 std::string anAttrType = theAttribute->attributeType();
278 if (anAttrType == ModelAPI_AttributeRefAttr::type()) {
279 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
280 if (anAttr != NULL && anAttr->isObject())
281 anObject = anAttr->object();
283 if (anAttrType == ModelAPI_AttributeSelection::type()) {
284 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
285 if (anAttr != NULL && anAttr->isInitialized())
286 anObject = anAttr->context();
288 if (anAttrType == ModelAPI_AttributeReference::type()) {
289 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
290 if (anAttr.get() != NULL && anAttr->isInitialized())
291 anObject = anAttr->value();
297 //********************************************************************
298 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
301 DataPtr aData = myFeature->data();
302 if (aData.get() == NULL)
305 AttributeSelectionPtr aSelect = aData->selection(attributeID());
307 aShape = aSelect->value();
312 //********************************************************************
313 void ModuleBase_WidgetShapeSelector::updateSelectionName()
315 DataPtr aData = myFeature->data();
316 if (aData.get() == NULL)
319 bool isNameUpdated = false;
320 AttributeSelectionPtr aSelect = aData->selection(attributeID());
322 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
323 isNameUpdated = true;
325 if (!isNameUpdated) {
326 ObjectPtr anObject = getObject(myFeature->attribute(attributeID()));
327 if (anObject.get() != NULL) {
328 std::string aName = anObject->data()->name();
329 myTextLine->setText(QString::fromStdString(aName));
331 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
332 if (aRefAttr && aRefAttr->attr().get() != NULL) {
333 //myIsObject = aRefAttr->isObject();
334 AttributePtr anAttr = aRefAttr->attr();
335 if (anAttr.get() != NULL) {
336 std::stringstream aName;
337 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
338 myTextLine->setText(QString::fromStdString(aName.str()));
341 else if (myIsActive) {
342 myTextLine->setText("");
349 //********************************************************************
350 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
352 if (myIsActive == toActivate)
354 myIsActive = toActivate;
355 updateSelectionName();
359 foreach (QString aType, myShapeTypes) {
360 aList.append(shapeType(aType));
362 myWorkshop->activateSubShapesSelection(aList);
364 myWorkshop->deactivateSubShapesSelection();
367 activateFilters(myWorkshop, myIsActive);
370 //********************************************************************
371 void ModuleBase_WidgetShapeSelector::raisePanel() const
373 QWidget* aParent = this->parentWidget();
374 QWidget* aLastPanel = 0;
375 while (!aParent->inherits("QDockWidget")) {
376 aLastPanel = aParent;
377 aParent = aParent->parentWidget();
381 if (aParent->inherits("QDockWidget")) {
382 QDockWidget* aTabWgt = (QDockWidget*) aParent;
387 //********************************************************************
388 void ModuleBase_WidgetShapeSelector::activateCustom()
390 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
391 activateSelection(true);
394 //********************************************************************
395 void ModuleBase_WidgetShapeSelector::backupAttributeValue(const bool isBackup)
397 DataPtr aData = myFeature->data();
398 AttributePtr anAttribute = myFeature->attribute(attributeID());
401 myObject = getObject(anAttribute);
402 myShape = getShape();
403 myRefAttribute = NULL;
405 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
407 myIsObject = aRefAttr->isObject();
408 myRefAttribute = aRefAttr->attr();
412 storeAttributeValues(myObject, myShape);
413 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
416 aRefAttr->setAttr(myRefAttribute);
421 //********************************************************************
422 bool ModuleBase_WidgetShapeSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
426 ModuleBase_ViewerPrs aPrs;
427 myWorkshop->selection()->fillPresentation(aPrs, theOwner);
428 ObjectPtr aObject = aPrs.object();
429 ObjectPtr aCurrentObject = getObject(myFeature->attribute(attributeID()));
430 if ((!aCurrentObject) && (!aObject))
433 // Check that the selected object is result (others can not be accepted)
434 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
439 // We can not select a result of our feature
440 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
441 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
442 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
447 // Check that object belongs to active document or PartSet
448 DocumentPtr aDoc = aRes->document();
449 SessionPtr aMgr = ModelAPI_Session::get();
450 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
453 // Check that the result has a shape
454 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
458 // Get sub-shapes from local selection
459 if (!aPrs.shape().IsNull()) {
460 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
461 aShape->setImpl(new TopoDS_Shape(aPrs.shape()));
463 // Check that the selection corresponds to selection type
464 if (!acceptSubShape(aShape))
467 storeAttributeValues(aObject, aShape);
471 //********************************************************************
472 void ModuleBase_WidgetShapeSelector::deactivate()
474 activateSelection(false);
475 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));