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), myIsActive(false)
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);
104 //********************************************************************
105 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
107 // the value is stored on the selection changed signal processing
111 //********************************************************************
112 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
113 GeomShapePtr theShape)
115 bool isChanged = false;
116 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
117 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
120 DataPtr aData = myFeature->data();
121 AttributeReferencePtr aRef = aData->reference(attributeID());
123 ObjectPtr aObject = aRef->value();
124 if (!(aObject && aObject->isSame(theSelectedObject))) {
125 aRef->setValue(theSelectedObject);
129 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
131 ObjectPtr aObject = aRefAttr->object();
132 if (!(aObject && aObject->isSame(theSelectedObject))) {
133 aRefAttr->setObject(theSelectedObject);
137 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
138 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
139 if (aSelectAttr.get() != NULL) {
140 aSelectAttr->setValue(aResult, theShape);
148 //********************************************************************
149 void ModuleBase_WidgetShapeSelector::clearAttribute()
151 DataPtr aData = myFeature->data();
152 AttributeSelectionPtr aSelect = aData->selection(attributeID());
154 aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
157 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
159 aRefAttr->setObject(ObjectPtr());
162 AttributeReferencePtr aRef = aData->reference(attributeID());
164 aRef->setObject(ObjectPtr());
168 //********************************************************************
169 bool ModuleBase_WidgetShapeSelector::restoreValue()
171 bool isBlocked = this->blockSignals(true);
172 updateSelectionName();
173 this->blockSignals(isBlocked);
178 //********************************************************************
179 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
181 QList<QWidget*> aControls;
182 aControls.append(myTextLine);
186 //********************************************************************
187 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
189 // In order to make reselection possible
190 // TODO: check with MPV clearAttribute();
192 QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(myWorkshop->selection());
193 if (aSelectedPrs.empty())
195 ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
196 if (aPrs.isEmpty() || !isValidSelection(aPrs))
199 if (!aPrs.isEmpty() && isValidSelection(aPrs)) {
200 setSelectionCustom(aPrs);
201 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
202 // calls validators for the feature and, as a result, updates the Apply button state.
203 updateObject(myFeature);
207 //updateSelectionName();
208 //emit valuesChanged();
209 emit focusOutWidget(this);
214 //********************************************************************
215 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
217 // ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
219 // // Check that the shape of necessary type
220 // std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
223 // TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
224 // if (aShape.IsNull())
227 // TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
228 // if (aShapeType == TopAbs_COMPOUND) {
229 // foreach (QString aType,
231 // TopExp_Explorer aEx(aShape, shapeType(aType));
236 // foreach (QString aType, myShapeTypes) {
237 // if (shapeType(aType) == aShapeType)
244 //********************************************************************
245 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
247 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
248 foreach (QString aType, myShapeTypes) {
249 if (aShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
255 //********************************************************************
256 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
259 DataPtr aData = myFeature->data();
260 if (aData.get() == NULL)
263 AttributeSelectionPtr aSelect = aData->selection(attributeID());
265 aShape = aSelect->value();
270 //********************************************************************
271 void ModuleBase_WidgetShapeSelector::updateSelectionName()
273 DataPtr aData = myFeature->data();
274 if (aData.get() == NULL)
277 bool isNameUpdated = false;
278 AttributeSelectionPtr aSelect = aData->selection(attributeID());
280 myTextLine->setText(QString::fromStdString(aSelect->namingName()));
281 isNameUpdated = true;
283 if (!isNameUpdated) {
284 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
285 if (anObject.get() != NULL) {
286 std::string aName = anObject->data()->name();
287 myTextLine->setText(QString::fromStdString(aName));
289 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
290 if (aRefAttr && aRefAttr->attr().get() != NULL) {
291 //myIsObject = aRefAttr->isObject();
292 AttributePtr anAttr = aRefAttr->attr();
293 if (anAttr.get() != NULL) {
294 std::stringstream aName;
295 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
296 myTextLine->setText(QString::fromStdString(aName.str()));
299 else if (myIsActive) {
300 myTextLine->setText("");
307 //********************************************************************
308 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
310 if (myIsActive == toActivate)
312 myIsActive = toActivate;
313 updateSelectionName();
317 foreach (QString aType, myShapeTypes) {
318 aList.append(ModuleBase_Tools::shapeType(aType));
320 myWorkshop->activateSubShapesSelection(aList);
322 myWorkshop->deactivateSubShapesSelection();
325 activateFilters(myWorkshop, myIsActive);
328 //********************************************************************
329 void ModuleBase_WidgetShapeSelector::raisePanel() const
331 QWidget* aParent = this->parentWidget();
332 QWidget* aLastPanel = 0;
333 while (!aParent->inherits("QDockWidget")) {
334 aLastPanel = aParent;
335 aParent = aParent->parentWidget();
339 if (aParent->inherits("QDockWidget")) {
340 QDockWidget* aTabWgt = (QDockWidget*) aParent;
345 //********************************************************************
346 void ModuleBase_WidgetShapeSelector::activateCustom()
348 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
349 activateSelection(true);
352 //********************************************************************
353 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
355 DataPtr aData = myFeature->data();
356 AttributePtr anAttribute = myFeature->attribute(attributeID());
358 myObject = GeomValidators_Tools::getObject(anAttribute);
359 myShape = getShape();
360 myRefAttribute = AttributePtr();
362 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
364 myIsObject = aRefAttr->isObject();
365 myRefAttribute = aRefAttr->attr();
369 //********************************************************************
370 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
372 DataPtr aData = myFeature->data();
373 AttributePtr anAttribute = myFeature->attribute(attributeID());
375 setObject(myObject, myShape);
376 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
379 aRefAttr->setAttr(myRefAttribute);
383 //********************************************************************
384 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
388 // It should be checked by corresponded validator
389 ObjectPtr aObject = thePrs.object();
390 ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
392 if ((!aCurrentObject) && (!aObject))
395 // It should be checked by corresponded validator
396 // Check that the selected object is result (others can not be accepted)
397 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
401 // We can not select a result of our feature
402 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
403 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
404 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
410 // It should be checked by corresponded validator
412 // Check that object belongs to active document or PartSet
413 DocumentPtr aDoc = aRes->document();
414 SessionPtr aMgr = ModelAPI_Session::get();
415 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
418 // It should be checked by corresponded validator
419 // Check that the result has a shape
420 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
424 // Get sub-shapes from local selection
425 if (!thePrs.shape().IsNull()) {
426 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
427 aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
429 // Check that the selection corresponds to selection type
430 if (!acceptSubShape(aShape))
433 setObject(aObject, aShape);
437 //********************************************************************
438 void ModuleBase_WidgetShapeSelector::deactivate()
440 activateSelection(false);
441 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));