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 <GeomValidators_ShapeType.h>
18 #include <Config_WidgetAPI.h>
19 #include <Events_Loop.h>
20 #include <Events_Message.h>
21 #include <GeomAPI_Interface.h>
22 #include <GeomAPI_Shape.h>
23 #include <GeomValidators_Tools.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_AttributeReference.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_AttributeValidator.h>
40 #include <ModelAPI_ShapeValidator.h>
42 #include <Config_WidgetAPI.h>
43 #include <Events_Error.h>
45 #include <GeomAPI_Shape.h>
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopExp_Explorer.hxx>
55 #include <QToolButton>
58 #include <QDockWidget>
59 #include <QApplication>
60 #include <QFormLayout>
62 #include <TopExp_Explorer.hxx>
63 #include <TopoDS_Shape.hxx>
70 //#define DEBUG_SHAPE_VALIDATION_PREVIOUS
72 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
73 ModuleBase_IWorkshop* theWorkshop,
74 const Config_WidgetAPI* theData,
75 const std::string& theParentId)
76 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
77 myWorkshop(theWorkshop)
79 QFormLayout* aLayout = new QFormLayout(this);
80 ModuleBase_Tools::adjustMargins(aLayout);
82 QString aLabelText = QString::fromStdString(theData->widgetLabel());
83 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
84 myLabel = new QLabel(aLabelText, this);
85 if (!aLabelIcon.isEmpty())
86 myLabel->setPixmap(QPixmap(aLabelIcon));
89 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
90 myTextLine = new QLineEdit(this);
91 myTextLine->setReadOnly(true);
92 myTextLine->setToolTip(aToolTip);
93 myTextLine->installEventFilter(this);
95 aLayout->addRow(myLabel, myTextLine);
97 std::string aTypes = theData->getProperty("shape_types");
98 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
100 myShapeValidator = new GeomValidators_ShapeType();
103 //********************************************************************
104 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
106 delete myShapeValidator;
109 //********************************************************************
110 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
112 // the value is stored on the selection changed signal processing
116 //********************************************************************
117 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
118 GeomShapePtr theShape)
120 bool isChanged = false;
121 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
122 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
125 DataPtr aData = myFeature->data();
126 AttributeReferencePtr aRef = aData->reference(attributeID());
128 ObjectPtr aObject = aRef->value();
129 if (!(aObject && aObject->isSame(theSelectedObject))) {
130 aRef->setValue(theSelectedObject);
134 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
136 ObjectPtr aObject = aRefAttr->object();
137 if (!(aObject && aObject->isSame(theSelectedObject))) {
138 aRefAttr->setObject(theSelectedObject);
142 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
143 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
144 if (aSelectAttr.get() != NULL) {
145 aSelectAttr->setValue(aResult, theShape);
153 //********************************************************************
154 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
156 QList<ModuleBase_ViewerPrs> aSelected;
158 DataPtr aData = myFeature->data();
159 AttributePtr anAttribute = myFeature->attribute(attributeID());
161 ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
163 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
164 if (aShapePtr.get()) {
165 aShape = aShapePtr->impl<TopoDS_Shape>();
167 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
168 aSelected.append(aPrs);
173 //********************************************************************
174 void ModuleBase_WidgetShapeSelector::clearAttribute()
176 DataPtr aData = myFeature->data();
177 AttributeSelectionPtr aSelect = aData->selection(attributeID());
179 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
182 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
184 aRefAttr->setObject(ObjectPtr());
187 AttributeReferencePtr aRef = aData->reference(attributeID());
189 aRef->setObject(ObjectPtr());
193 //********************************************************************
194 bool ModuleBase_WidgetShapeSelector::restoreValue()
196 bool isBlocked = this->blockSignals(true);
197 updateSelectionName();
198 this->blockSignals(isBlocked);
203 //********************************************************************
204 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
206 QList<QWidget*> aControls;
207 aControls.append(myTextLine);
211 //********************************************************************
212 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
214 // In order to make reselection possible, set empty object and shape should be done
215 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
217 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
218 bool aHasObject = setSelection(aSelected);
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);
223 // the widget loses the focus only if the selected object is set
225 emit focusOutWidget(this);
228 //********************************************************************
229 bool ModuleBase_WidgetShapeSelector::acceptSubShape(const TopoDS_Shape& theShape) const
231 foreach (QString aType, myShapeTypes) {
232 if (theShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
238 //********************************************************************
239 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
242 DataPtr aData = myFeature->data();
243 if (!aData->isValid())
246 AttributeSelectionPtr aSelect = aData->selection(attributeID());
248 aShape = aSelect->value();
253 //********************************************************************
254 void ModuleBase_WidgetShapeSelector::updateSelectionName()
256 DataPtr aData = myFeature->data();
257 if (!aData->isValid())
260 bool isNameUpdated = false;
261 AttributeSelectionPtr aSelect = aData->selection(attributeID());
263 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
264 isNameUpdated = true;
266 if (!isNameUpdated) {
267 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
268 if (anObject.get() != NULL) {
269 std::string aName = anObject->data()->name();
270 myTextLine->setText(QString::fromStdString(aName));
272 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
273 if (aRefAttr && aRefAttr->attr().get() != NULL) {
274 //myIsObject = aRefAttr->isObject();
275 AttributePtr anAttr = aRefAttr->attr();
276 if (anAttr.get() != NULL) {
277 std::stringstream aName;
278 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
279 myTextLine->setText(QString::fromStdString(aName.str()));
283 myTextLine->setText(getDefaultValue().c_str());
290 //********************************************************************
291 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
293 updateSelectionName();
297 foreach (QString aType, myShapeTypes) {
298 aList.append(ModuleBase_Tools::shapeType(aType));
300 myWorkshop->activateSubShapesSelection(aList);
302 myWorkshop->deactivateSubShapesSelection();
306 //********************************************************************
307 void ModuleBase_WidgetShapeSelector::raisePanel() const
309 QWidget* aParent = this->parentWidget();
310 QWidget* aLastPanel = 0;
311 while (!aParent->inherits("QDockWidget")) {
312 aLastPanel = aParent;
313 aParent = aParent->parentWidget();
317 if (aParent->inherits("QDockWidget")) {
318 QDockWidget* aTabWgt = (QDockWidget*) aParent;
323 //********************************************************************
324 void ModuleBase_WidgetShapeSelector::activateCustom()
326 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
327 activateSelection(true);
329 // Restore selection in the viewer by the attribute selection list
330 myWorkshop->setSelected(getAttributeSelection());
332 activateFilters(myWorkshop, true);
335 //********************************************************************
336 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
338 DataPtr aData = myFeature->data();
339 AttributePtr anAttribute = myFeature->attribute(attributeID());
341 myObject = GeomValidators_Tools::getObject(anAttribute);
342 myShape = getShape();
343 myRefAttribute = AttributePtr();
345 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
347 myIsObject = aRefAttr->isObject();
348 myRefAttribute = aRefAttr->attr();
352 //********************************************************************
353 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
355 DataPtr aData = myFeature->data();
356 AttributePtr anAttribute = myFeature->attribute(attributeID());
358 setObject(myObject, myShape);
359 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
362 aRefAttr->setAttr(myRefAttribute);
366 //********************************************************************
367 void ModuleBase_WidgetShapeSelector::customValidators(
368 std::list<ModelAPI_Validator*>& theValidators,
369 std::list<std::list<std::string> >& theArguments) const
372 theValidators.push_back(myShapeValidator);
374 std::list<std::string> anArguments;
375 foreach(QString aType, myShapeTypes) {
376 anArguments.push_back(aType.toStdString().c_str());
378 theArguments.push_back(anArguments);
381 //********************************************************************
382 bool ModuleBase_WidgetShapeSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
384 #ifdef DEBUG_SHAPE_VALIDATION_PREVIOUS
388 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
390 // if there is no selected shape, the method returns true
394 // Check that the selection corresponds to selection type
395 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
396 aValid = acceptSubShape(aTopoShape);
401 //********************************************************************
402 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
406 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
408 // It should be checked by corresponded validator
409 //ObjectPtr aObject = thePrs.object();
410 //ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
412 if ((!aCurrentObject) && (!aObject))
415 // It should be checked by corresponded validator
416 // Check that the selected object is result (others can not be accepted)
417 //ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
421 // We can not select a result of our feature
422 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
423 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
424 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
430 // It should be checked by corresponded validator
432 // Check that object belongs to active document or PartSet
433 DocumentPtr aDoc = aRes->document();
434 SessionPtr aMgr = ModelAPI_Session::get();
435 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
438 #ifdef DEBUG_SHAPE_VALIDATION_PREVIOUS
439 // It should be checked by corresponded validator
440 // Check that the result has a shape
441 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
445 // Get sub-shapes from local selection
446 if (!thePrs.shape().IsNull()) {
447 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
448 aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
451 // Check that the selection corresponds to selection type
452 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
453 if (!acceptSubShape(aTopoShape))
456 // the difference is that the next method returns an empty shape if the result has the same shape
457 // to be checked for all cases and uncommented
458 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
461 setObject(aResult, aShape);
465 //********************************************************************
466 void ModuleBase_WidgetShapeSelector::deactivate()
468 activateSelection(false);
469 activateFilters(myWorkshop, false);
470 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));