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>
58 #include <QFormLayout>
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Shape.hxx>
68 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
69 static ShapeTypes MyShapeTypes;
71 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
73 if (MyShapeTypes.count() == 0) {
74 MyShapeTypes["face"] = TopAbs_FACE;
75 MyShapeTypes["faces"] = TopAbs_FACE;
76 MyShapeTypes["vertex"] = TopAbs_VERTEX;
77 MyShapeTypes["vertices"] = TopAbs_VERTEX;
78 MyShapeTypes["wire"] = TopAbs_WIRE;
79 MyShapeTypes["edge"] = TopAbs_EDGE;
80 MyShapeTypes["edges"] = TopAbs_EDGE;
81 MyShapeTypes["shell"] = TopAbs_SHELL;
82 MyShapeTypes["solid"] = TopAbs_SOLID;
83 MyShapeTypes["solids"] = TopAbs_SOLID;
85 QString aType = theType.toLower();
86 if (MyShapeTypes.contains(aType))
87 return MyShapeTypes[aType];
88 Events_Error::send("Shape type defined in XML is not implemented!");
92 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
93 ModuleBase_IWorkshop* theWorkshop,
94 const Config_WidgetAPI* theData,
95 const std::string& theParentId)
96 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
97 myWorkshop(theWorkshop), myIsActive(false)
99 QFormLayout* aLayout = new QFormLayout(this);
100 ModuleBase_Tools::adjustMargins(aLayout);
102 QString aLabelText = QString::fromStdString(theData->widgetLabel());
103 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
104 myLabel = new QLabel(aLabelText, this);
105 if (!aLabelIcon.isEmpty())
106 myLabel->setPixmap(QPixmap(aLabelIcon));
109 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
110 myTextLine = new QLineEdit(this);
111 myTextLine->setReadOnly(true);
112 myTextLine->setToolTip(aToolTip);
113 myTextLine->installEventFilter(this);
115 aLayout->addRow(myLabel, myTextLine);
117 std::string aTypes = theData->getProperty("shape_types");
118 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
121 //********************************************************************
122 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
124 activateSelection(false);
127 //********************************************************************
128 bool ModuleBase_WidgetShapeSelector::storeValueCustom() 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 AttributeReferencePtr aRef = aData->reference(attributeID());
137 ObjectPtr aObject = aRef->value();
138 if (!(aObject && aObject->isSame(mySelectedObject))) {
139 aRef->setValue(mySelectedObject);
140 updateObject(myFeature);
144 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
146 ObjectPtr aObject = aRefAttr->object();
147 if (!(aObject && aObject->isSame(mySelectedObject))) {
148 aRefAttr->setObject(mySelectedObject);
149 updateObject(myFeature);
153 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
154 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
155 if (aSelectAttr && aBody && (myShape.get() != NULL)) {
156 aSelectAttr->setValue(aBody, myShape);
157 updateObject(myFeature);
165 //********************************************************************
166 void ModuleBase_WidgetShapeSelector::clearAttribute()
168 DataPtr aData = myFeature->data();
169 AttributeSelectionPtr aSelect = aData->selection(attributeID());
171 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
174 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
176 aRefAttr->setObject(ObjectPtr());
179 AttributeReferencePtr aRef = aData->reference(attributeID());
181 aRef->setObject(ObjectPtr());
185 //********************************************************************
186 bool ModuleBase_WidgetShapeSelector::restoreValue()
188 DataPtr aData = myFeature->data();
189 bool isBlocked = this->blockSignals(true);
191 AttributeSelectionPtr aSelect = aData->selection(attributeID());
193 mySelectedObject = aSelect->context();
194 myShape = aSelect->value();
196 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
198 mySelectedObject = aRefAttr->object();
200 AttributeReferencePtr aRef = aData->reference(attributeID());
202 mySelectedObject = aRef->value();
206 updateSelectionName();
208 this->blockSignals(isBlocked);
212 //********************************************************************
213 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
215 QList<QWidget*> aControls;
216 aControls.append(myTextLine);
220 //********************************************************************
221 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
223 // In order to make reselection possible
224 // TODO: check with MPV clearAttribute();
226 //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
227 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
228 if (aSelected.size() > 0) {
229 if (setSelection(aSelected.first()))
230 emit focusOutWidget(this);
234 //********************************************************************
235 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
237 ObjectPtr aObject = theValue.object();
238 if ((!mySelectedObject) && (!aObject))
241 // Check that the selected object is result (others can not be accepted)
242 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
247 // We can not select a result of our feature
248 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
249 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
250 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
255 // Check that object belongs to active document or PartSet
256 DocumentPtr aDoc = aRes->document();
257 SessionPtr aMgr = ModelAPI_Session::get();
258 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
261 // Check that the result has a shape
262 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
266 // Get sub-shapes from local selection
267 if (!theValue.shape().IsNull()) {
268 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
269 aShape->setImpl(new TopoDS_Shape(theValue.shape()));
272 // Check that the selection corresponds to selection type
273 if (!acceptSubShape(aShape))
275 // if (!acceptObjectShape(aObject))
278 // Check whether the value is valid for the viewer selection filters
279 Handle(SelectMgr_EntityOwner) anOwner = theValue.owner();
280 if (!anOwner.IsNull()) {
281 SelectMgr_ListOfFilter aFilters;
282 selectionFilters(myWorkshop, aFilters);
283 SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
284 for (; aIt.More(); aIt.Next()) {
285 const Handle(SelectMgr_Filter)& aFilter = aIt.Value();
286 if (!aFilter->IsOk(anOwner))
290 if (isValid(aObject, aShape)) {
291 setObject(aObject, aShape);
297 //********************************************************************
298 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
300 mySelectedObject = theObj;
302 if (mySelectedObject) {
305 updateSelectionName();
306 emit valuesChanged();
309 //********************************************************************
310 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
312 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
314 // // Check that the shape of necessary type
315 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
318 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
319 // if (aShape.IsNull())
322 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
323 // if (aShapeType == TopAbs_COMPOUND) {
324 // foreach (QString aType,
326 // TopExp_Explorer aEx(aShape, shapeType(aType));
331 // foreach (QString aType, myShapeTypes) {
332 // if (shapeType(aType) == aShapeType)
339 //********************************************************************
340 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
342 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
343 foreach (QString aType, myShapeTypes) {
344 if (aShape.ShapeType() == shapeType(aType))
350 //********************************************************************
351 void ModuleBase_WidgetShapeSelector::updateSelectionName()
353 DataPtr aData = myFeature->data();
354 bool isNameUpdated = false;
355 if (aData.get() != NULL) {
356 AttributeSelectionPtr aSelect = aData->selection(attributeID());
358 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
359 isNameUpdated = true;
362 if (!isNameUpdated) {
363 if (mySelectedObject) {
364 std::string aName = mySelectedObject->data()->name();
365 myTextLine->setText(QString::fromStdString(aName));
368 myTextLine->setText("");
375 //********************************************************************
376 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
378 if (myIsActive == toActivate)
380 myIsActive = toActivate;
381 updateSelectionName();
385 foreach (QString aType, myShapeTypes) {
386 aList.append(shapeType(aType));
388 myWorkshop->activateSubShapesSelection(aList);
390 myWorkshop->deactivateSubShapesSelection();
393 activateFilters(myWorkshop, myIsActive);
396 //********************************************************************
397 void ModuleBase_WidgetShapeSelector::raisePanel() const
399 QWidget* aParent = this->parentWidget();
400 QWidget* aLastPanel = 0;
401 while (!aParent->inherits("QDockWidget")) {
402 aLastPanel = aParent;
403 aParent = aParent->parentWidget();
407 if (aParent->inherits("QDockWidget")) {
408 QDockWidget* aTabWgt = (QDockWidget*) aParent;
413 //********************************************************************
414 void ModuleBase_WidgetShapeSelector::activateCustom()
416 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
417 activateSelection(true);
420 //********************************************************************
421 void ModuleBase_WidgetShapeSelector::deactivate()
423 activateSelection(false);
424 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
427 //********************************************************************
428 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
430 bool isValid = ModuleBase_WidgetValidated::isValid(theObj, theShape);
434 SessionPtr aMgr = ModelAPI_Session::get();
435 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
436 std::list<ModelAPI_Validator*> aValidators;
437 std::list<std::list<std::string> > anArguments;
438 aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
440 DataPtr aData = myFeature->data();
441 //AttributePtr aAttr = aData->attribute(attributeID());
442 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
444 // 1. saves the previous attribute values
445 bool isObject = aRefAttr->isObject();
446 ObjectPtr aPrevObject = aRefAttr->object();
447 AttributePtr aPrevAttr = aRefAttr->attr();
449 // 2. store the current values, disable the model's update
450 aData->blockSendAttributeUpdated(true);
451 ObjectPtr aPrevSelectedObject = mySelectedObject;
452 GeomShapePtr aPrevShape = myShape;
454 mySelectedObject = theObj;
458 // 3. check the acceptability of the current values
459 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
460 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
462 for (; aValidator != aValidators.end() && aValid; aValidator++, aArgs++) {
463 const ModelAPI_RefAttrValidator* aAttrValidator =
464 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
465 if (aAttrValidator) {
466 aValid = aAttrValidator->isValid(aRefAttr, *aArgs);
470 // 4. if the values are not valid, restore the previous values to the attribute
472 mySelectedObject = aPrevSelectedObject;
473 myShape = aPrevShape;
475 aRefAttr->setObject(aPrevObject);
477 aRefAttr->setAttr(aPrevAttr);
479 // 5. enable the model's update
480 aData->blockSendAttributeUpdated(false);
481 //updateObject(myFeature);
486 // Check the acceptability of the object as attribute
487 std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
488 std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
489 for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
490 const ModelAPI_RefAttrValidator* aAttrValidator =
491 dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
492 if (aAttrValidator) {
493 //if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
498 const ModelAPI_ShapeValidator* aShapeValidator =
499 dynamic_cast<const ModelAPI_ShapeValidator*>(*aValidator);
500 if (aShapeValidator) {
501 DataPtr aData = myFeature->data();
502 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
503 if (!aShapeValidator->isValid(myFeature, *aArgs, theObj, aSelectAttr, theShape)) {