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>
21 #include <GeomValidators_Tools.h>
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_Validator.h>
37 #include <ModelAPI_AttributeValidator.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>
69 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
70 ModuleBase_IWorkshop* theWorkshop,
71 const Config_WidgetAPI* theData,
72 const std::string& theParentId)
73 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
74 myWorkshop(theWorkshop)
76 QFormLayout* aLayout = new QFormLayout(this);
77 ModuleBase_Tools::adjustMargins(aLayout);
79 QString aLabelText = QString::fromStdString(theData->widgetLabel());
80 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
81 myLabel = new QLabel(aLabelText, this);
82 if (!aLabelIcon.isEmpty())
83 myLabel->setPixmap(QPixmap(aLabelIcon));
86 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
87 myTextLine = new QLineEdit(this);
88 myTextLine->setReadOnly(true);
89 myTextLine->setToolTip(aToolTip);
90 myTextLine->installEventFilter(this);
92 aLayout->addRow(myLabel, myTextLine);
94 std::string aTypes = theData->getProperty("shape_types");
95 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
98 //********************************************************************
99 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
101 activateSelection(false);
102 activateFilters(myWorkshop, false);
105 //********************************************************************
106 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
108 // the value is stored on the selection changed signal processing
112 //********************************************************************
113 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
114 GeomShapePtr theShape)
116 bool isChanged = false;
117 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
118 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
121 DataPtr aData = myFeature->data();
122 AttributeReferencePtr aRef = aData->reference(attributeID());
124 ObjectPtr aObject = aRef->value();
125 if (!(aObject && aObject->isSame(theSelectedObject))) {
126 aRef->setValue(theSelectedObject);
130 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
132 ObjectPtr aObject = aRefAttr->object();
133 if (!(aObject && aObject->isSame(theSelectedObject))) {
134 aRefAttr->setObject(theSelectedObject);
138 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
139 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
140 if (aSelectAttr.get() != NULL) {
141 aSelectAttr->setValue(aResult, theShape);
149 //********************************************************************
150 void ModuleBase_WidgetShapeSelector::clearAttribute()
152 DataPtr aData = myFeature->data();
153 AttributeSelectionPtr aSelect = aData->selection(attributeID());
155 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
158 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
160 aRefAttr->setObject(ObjectPtr());
163 AttributeReferencePtr aRef = aData->reference(attributeID());
165 aRef->setObject(ObjectPtr());
169 //********************************************************************
170 bool ModuleBase_WidgetShapeSelector::restoreValue()
172 bool isBlocked = this->blockSignals(true);
173 updateSelectionName();
174 this->blockSignals(isBlocked);
179 //********************************************************************
180 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
182 QList<QWidget*> aControls;
183 aControls.append(myTextLine);
187 //********************************************************************
188 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
190 // In order to make reselection possible, set empty object and shape should be done
191 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
193 bool aHasObject = false;
194 QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(myWorkshop->selection());
195 if (!aSelectedPrs.empty()) {
196 ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
197 if (!aPrs.isEmpty() && isValidSelection(aPrs)) {
198 setSelectionCustom(aPrs);
202 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
203 // calls validators for the feature and, as a result, updates the Apply button state.
204 updateObject(myFeature);
205 // the widget loses the focus only if the selected object is set
207 emit focusOutWidget(this);
210 //********************************************************************
211 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
213 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
214 foreach (QString aType, myShapeTypes) {
215 if (aShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
221 //********************************************************************
222 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
225 DataPtr aData = myFeature->data();
226 if (aData.get() == NULL)
229 AttributeSelectionPtr aSelect = aData->selection(attributeID());
231 aShape = aSelect->value();
236 //********************************************************************
237 void ModuleBase_WidgetShapeSelector::updateSelectionName()
239 DataPtr aData = myFeature->data();
240 if (aData.get() == NULL)
243 bool isNameUpdated = false;
244 AttributeSelectionPtr aSelect = aData->selection(attributeID());
246 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
247 isNameUpdated = true;
249 if (!isNameUpdated) {
250 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
251 if (anObject.get() != NULL) {
252 std::string aName = anObject->data()->name();
253 myTextLine->setText(QString::fromStdString(aName));
255 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
256 if (aRefAttr && aRefAttr->attr().get() != NULL) {
257 //myIsObject = aRefAttr->isObject();
258 AttributePtr anAttr = aRefAttr->attr();
259 if (anAttr.get() != NULL) {
260 std::stringstream aName;
261 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
262 myTextLine->setText(QString::fromStdString(aName.str()));
266 myTextLine->setText("");
273 //********************************************************************
274 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
276 updateSelectionName();
280 foreach (QString aType, myShapeTypes) {
281 aList.append(ModuleBase_Tools::shapeType(aType));
283 myWorkshop->activateSubShapesSelection(aList);
285 myWorkshop->deactivateSubShapesSelection();
289 //********************************************************************
290 void ModuleBase_WidgetShapeSelector::raisePanel() const
292 QWidget* aParent = this->parentWidget();
293 QWidget* aLastPanel = 0;
294 while (!aParent->inherits("QDockWidget")) {
295 aLastPanel = aParent;
296 aParent = aParent->parentWidget();
300 if (aParent->inherits("QDockWidget")) {
301 QDockWidget* aTabWgt = (QDockWidget*) aParent;
306 //********************************************************************
307 void ModuleBase_WidgetShapeSelector::activateCustom()
309 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
310 activateSelection(true);
311 // Restore selection in the viewer by the attribute selection list
312 QList<ModuleBase_ViewerPrs> aSelected;
314 DataPtr aData = myFeature->data();
315 AttributePtr anAttribute = myFeature->attribute(attributeID());
317 ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
319 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
320 if (aShapePtr.get()) {
321 aShape = aShapePtr->impl<TopoDS_Shape>();
323 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
324 aSelected.append(aPrs);
326 myWorkshop->setSelected(aSelected);
328 activateFilters(myWorkshop, true);
331 //********************************************************************
332 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
334 DataPtr aData = myFeature->data();
335 AttributePtr anAttribute = myFeature->attribute(attributeID());
337 myObject = GeomValidators_Tools::getObject(anAttribute);
338 myShape = getShape();
339 myRefAttribute = AttributePtr();
341 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
343 myIsObject = aRefAttr->isObject();
344 myRefAttribute = aRefAttr->attr();
348 //********************************************************************
349 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
351 DataPtr aData = myFeature->data();
352 AttributePtr anAttribute = myFeature->attribute(attributeID());
354 setObject(myObject, myShape);
355 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
358 aRefAttr->setAttr(myRefAttribute);
362 //********************************************************************
363 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
367 // It should be checked by corresponded validator
368 ObjectPtr aObject = thePrs.object();
369 ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
371 if ((!aCurrentObject) && (!aObject))
374 // It should be checked by corresponded validator
375 // Check that the selected object is result (others can not be accepted)
376 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
380 // We can not select a result of our feature
381 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
382 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
383 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
389 // It should be checked by corresponded validator
391 // Check that object belongs to active document or PartSet
392 DocumentPtr aDoc = aRes->document();
393 SessionPtr aMgr = ModelAPI_Session::get();
394 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
397 // It should be checked by corresponded validator
398 // Check that the result has a shape
399 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
403 // Get sub-shapes from local selection
404 if (!thePrs.shape().IsNull()) {
405 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
406 aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
408 // Check that the selection corresponds to selection type
409 if (!acceptSubShape(aShape))
412 setObject(aObject, aShape);
416 //********************************************************************
417 void ModuleBase_WidgetShapeSelector::deactivate()
419 activateSelection(false);
420 activateFilters(myWorkshop, false);
421 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));