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>
71 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
72 ModuleBase_IWorkshop* theWorkshop,
73 const Config_WidgetAPI* theData,
74 const std::string& theParentId)
75 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
76 myWorkshop(theWorkshop)
78 QFormLayout* aLayout = new QFormLayout(this);
79 ModuleBase_Tools::adjustMargins(aLayout);
81 QString aLabelText = QString::fromStdString(theData->widgetLabel());
82 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
83 myLabel = new QLabel(aLabelText, this);
84 if (!aLabelIcon.isEmpty())
85 myLabel->setPixmap(QPixmap(aLabelIcon));
88 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
89 myTextLine = new QLineEdit(this);
90 myTextLine->setReadOnly(true);
91 myTextLine->setToolTip(aToolTip);
92 myTextLine->installEventFilter(this);
94 aLayout->addRow(myLabel, myTextLine);
96 std::string aTypes = theData->getProperty("shape_types");
97 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
99 myShapeValidator = new GeomValidators_ShapeType();
102 //********************************************************************
103 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
105 activateSelection(false);
106 activateFilters(myWorkshop, false);
108 delete myShapeValidator;
111 //********************************************************************
112 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
114 // the value is stored on the selection changed signal processing
118 //********************************************************************
119 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
120 GeomShapePtr theShape)
122 bool isChanged = false;
123 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
124 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
127 DataPtr aData = myFeature->data();
128 AttributeReferencePtr aRef = aData->reference(attributeID());
130 ObjectPtr aObject = aRef->value();
131 if (!(aObject && aObject->isSame(theSelectedObject))) {
132 aRef->setValue(theSelectedObject);
136 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
138 ObjectPtr aObject = aRefAttr->object();
139 if (!(aObject && aObject->isSame(theSelectedObject))) {
140 aRefAttr->setObject(theSelectedObject);
144 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
145 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
146 if (aSelectAttr.get() != NULL) {
147 aSelectAttr->setValue(aResult, theShape);
155 //********************************************************************
156 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
158 QList<ModuleBase_ViewerPrs> aSelected;
160 DataPtr aData = myFeature->data();
161 AttributePtr anAttribute = myFeature->attribute(attributeID());
163 ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
165 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
166 if (aShapePtr.get()) {
167 aShape = aShapePtr->impl<TopoDS_Shape>();
169 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
170 aSelected.append(aPrs);
175 //********************************************************************
176 void ModuleBase_WidgetShapeSelector::clearAttribute()
178 DataPtr aData = myFeature->data();
179 AttributeSelectionPtr aSelect = aData->selection(attributeID());
181 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
184 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
186 aRefAttr->setObject(ObjectPtr());
189 AttributeReferencePtr aRef = aData->reference(attributeID());
191 aRef->setObject(ObjectPtr());
195 //********************************************************************
196 bool ModuleBase_WidgetShapeSelector::restoreValue()
198 bool isBlocked = this->blockSignals(true);
199 updateSelectionName();
200 this->blockSignals(isBlocked);
205 //********************************************************************
206 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
208 QList<QWidget*> aControls;
209 aControls.append(myTextLine);
213 //********************************************************************
214 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
216 // In order to make reselection possible, set empty object and shape should be done
217 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
219 bool aHasObject = false;
220 QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(myWorkshop->selection());
221 if (!aSelectedPrs.empty()) {
222 ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
223 if (!aPrs.isEmpty() && isValidSelection(aPrs)) {
224 setSelectionCustom(aPrs);
228 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
229 // calls validators for the feature and, as a result, updates the Apply button state.
230 updateObject(myFeature);
231 // the widget loses the focus only if the selected object is set
233 emit focusOutWidget(this);
236 //********************************************************************
237 bool ModuleBase_WidgetShapeSelector::acceptSubShape(const TopoDS_Shape& theShape) const
239 foreach (QString aType, myShapeTypes) {
240 if (theShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
246 //********************************************************************
247 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
250 DataPtr aData = myFeature->data();
251 if (!aData->isValid())
254 AttributeSelectionPtr aSelect = aData->selection(attributeID());
256 aShape = aSelect->value();
261 //********************************************************************
262 void ModuleBase_WidgetShapeSelector::updateSelectionName()
264 DataPtr aData = myFeature->data();
265 if (!aData->isValid())
268 bool isNameUpdated = false;
269 AttributeSelectionPtr aSelect = aData->selection(attributeID());
271 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
272 isNameUpdated = true;
274 if (!isNameUpdated) {
275 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
276 if (anObject.get() != NULL) {
277 std::string aName = anObject->data()->name();
278 myTextLine->setText(QString::fromStdString(aName));
280 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
281 if (aRefAttr && aRefAttr->attr().get() != NULL) {
282 //myIsObject = aRefAttr->isObject();
283 AttributePtr anAttr = aRefAttr->attr();
284 if (anAttr.get() != NULL) {
285 std::stringstream aName;
286 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
287 myTextLine->setText(QString::fromStdString(aName.str()));
291 myTextLine->setText(getDefaultValue().c_str());
298 //********************************************************************
299 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
301 updateSelectionName();
305 foreach (QString aType, myShapeTypes) {
306 aList.append(ModuleBase_Tools::shapeType(aType));
308 myWorkshop->activateSubShapesSelection(aList);
310 myWorkshop->deactivateSubShapesSelection();
314 //********************************************************************
315 void ModuleBase_WidgetShapeSelector::raisePanel() const
317 QWidget* aParent = this->parentWidget();
318 QWidget* aLastPanel = 0;
319 while (!aParent->inherits("QDockWidget")) {
320 aLastPanel = aParent;
321 aParent = aParent->parentWidget();
325 if (aParent->inherits("QDockWidget")) {
326 QDockWidget* aTabWgt = (QDockWidget*) aParent;
331 //********************************************************************
332 void ModuleBase_WidgetShapeSelector::activateCustom()
334 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
335 activateSelection(true);
337 // Restore selection in the viewer by the attribute selection list
338 myWorkshop->setSelected(getAttributeSelection());
340 activateFilters(myWorkshop, true);
343 //********************************************************************
344 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
346 DataPtr aData = myFeature->data();
347 AttributePtr anAttribute = myFeature->attribute(attributeID());
349 myObject = GeomValidators_Tools::getObject(anAttribute);
350 myShape = getShape();
351 myRefAttribute = AttributePtr();
353 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
355 myIsObject = aRefAttr->isObject();
356 myRefAttribute = aRefAttr->attr();
360 //********************************************************************
361 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
363 DataPtr aData = myFeature->data();
364 AttributePtr anAttribute = myFeature->attribute(attributeID());
366 setObject(myObject, myShape);
367 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
370 aRefAttr->setAttr(myRefAttribute);
374 //********************************************************************
375 void ModuleBase_WidgetShapeSelector::customValidators(
376 std::list<ModelAPI_Validator*>& theValidators,
377 std::list<std::list<std::string> >& theArguments) const
380 theValidators.push_back(myShapeValidator);
382 std::list<std::string> anArguments;
383 foreach(QString aType, myShapeTypes) {
384 anArguments.push_back(aType.toStdString().c_str());
386 theArguments.push_back(anArguments);
389 //********************************************************************
390 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
394 // It should be checked by corresponded validator
395 ObjectPtr aObject = thePrs.object();
396 ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
398 if ((!aCurrentObject) && (!aObject))
401 // It should be checked by corresponded validator
402 // Check that the selected object is result (others can not be accepted)
403 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
407 // We can not select a result of our feature
408 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
409 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
410 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
416 // It should be checked by corresponded validator
418 // Check that object belongs to active document or PartSet
419 DocumentPtr aDoc = aRes->document();
420 SessionPtr aMgr = ModelAPI_Session::get();
421 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
424 // It should be checked by corresponded validator
425 // Check that the result has a shape
426 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
430 // Get sub-shapes from local selection
431 if (!thePrs.shape().IsNull()) {
432 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
433 aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
435 // Check that the selection corresponds to selection type
436 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
437 if (!acceptSubShape(aTopoShape))
440 setObject(aObject, aShape);
444 //********************************************************************
445 void ModuleBase_WidgetShapeSelector::deactivate()
447 activateSelection(false);
448 activateFilters(myWorkshop, false);
449 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));