1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetShapeSelector.cpp
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_AttributeValidator.h>
38 #include <Config_WidgetAPI.h>
39 #include <Events_Error.h>
41 #include <GeomAPI_Shape.h>
43 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopExp_Explorer.hxx>
51 #include <QToolButton>
54 #include <QDockWidget>
55 #include <QApplication>
56 #include <QFormLayout>
58 #include <TopExp_Explorer.hxx>
59 #include <TopoDS_Shape.hxx>
66 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
67 ModuleBase_IWorkshop* theWorkshop,
68 const Config_WidgetAPI* theData,
69 const std::string& theParentId)
70 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
72 QFormLayout* aLayout = new QFormLayout(this);
73 ModuleBase_Tools::adjustMargins(aLayout);
75 QString aLabelText = QString::fromStdString(theData->widgetLabel());
76 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
77 myLabel = new QLabel(aLabelText, this);
78 if (!aLabelIcon.isEmpty())
79 myLabel->setPixmap(QPixmap(aLabelIcon));
82 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
83 myTextLine = new QLineEdit(this);
84 QString anObjName = QString::fromStdString(attributeID());
85 myTextLine->setObjectName(anObjName);
86 myTextLine->setReadOnly(true);
87 myTextLine->setToolTip(aToolTip);
88 myTextLine->installEventFilter(this);
90 aLayout->addRow(myLabel, myTextLine);
91 myLabel->setToolTip(aToolTip);
93 std::string aTypes = theData->getProperty("shape_types");
94 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
97 //********************************************************************
98 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
102 //********************************************************************
103 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
105 // the value is stored on the selection changed signal processing
109 //********************************************************************
110 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
111 GeomShapePtr theShape)
113 DataPtr aData = myFeature->data();
114 std::string aType = aData->attribute(attributeID())->attributeType();
115 if (aType == ModelAPI_AttributeReference::typeId()) {
116 AttributeReferencePtr aRef = aData->reference(attributeID());
117 ObjectPtr aObject = aRef->value();
118 if (!(aObject && aObject->isSame(theSelectedObject))) {
119 aRef->setValue(theSelectedObject);
121 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
122 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
123 ObjectPtr aObject = aRefAttr->object();
124 if (!(aObject && aObject->isSame(theSelectedObject))) {
125 aRefAttr->setObject(theSelectedObject);
127 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
128 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
129 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
130 if (aSelectAttr.get() != NULL) {
131 aSelectAttr->setValue(aResult, theShape);
136 //********************************************************************
137 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
139 QList<ModuleBase_ViewerPrs> aSelected;
141 DataPtr aData = myFeature->data();
142 AttributePtr anAttribute = myFeature->attribute(attributeID());
144 ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
146 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
147 if (aShapePtr.get()) {
148 aShape = aShapePtr->impl<TopoDS_Shape>();
150 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
151 aSelected.append(aPrs);
156 //********************************************************************
157 void ModuleBase_WidgetShapeSelector::clearAttribute()
159 // In order to make reselection possible, set empty object and shape should be done
160 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
163 //********************************************************************
164 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
166 bool isBlocked = this->blockSignals(true);
167 updateSelectionName();
168 this->blockSignals(isBlocked);
173 //********************************************************************
174 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
176 QList<QWidget*> aControls;
177 aControls.append(myTextLine);
181 void ModuleBase_WidgetShapeSelector::updateFocus()
183 emit focusOutWidget(this);
186 //********************************************************************
187 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
189 QIntList aShapeTypes;
190 foreach(QString aType, myShapeTypes) {
191 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
196 //********************************************************************
197 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
200 DataPtr aData = myFeature->data();
201 if (!aData->isValid())
204 AttributeSelectionPtr aSelect = aData->selection(attributeID());
206 aShape = aSelect->value();
211 //********************************************************************
212 void ModuleBase_WidgetShapeSelector::updateSelectionName()
214 DataPtr aData = myFeature->data();
215 if (!aData->isValid())
218 bool isNameUpdated = false;
219 AttributeSelectionPtr aSelect = aData->selection(attributeID());
221 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
222 isNameUpdated = true;
224 if (!isNameUpdated) {
225 ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
226 if (anObject.get() != NULL) {
227 std::string aName = anObject->data()->name();
228 myTextLine->setText(QString::fromStdString(aName));
230 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
231 if (aRefAttr && aRefAttr->attr().get() != NULL) {
232 //myIsObject = aRefAttr->isObject();
233 std::string anAttrName = generateName(aRefAttr->attr());
234 myTextLine->setText(QString::fromStdString(anAttrName));
237 myTextLine->setText(getDefaultValue().c_str());
243 //********************************************************************
244 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
246 ModuleBase_WidgetValidated::storeAttributeValue();
248 DataPtr aData = myFeature->data();
249 AttributePtr anAttribute = myFeature->attribute(attributeID());
251 myObject = ModuleBase_Tools::getObject(anAttribute);
252 myShape = getShape();
253 myRefAttribute = AttributePtr();
255 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
257 myIsObject = aRefAttr->isObject();
258 myRefAttribute = aRefAttr->attr();
262 //********************************************************************
263 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
265 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
267 DataPtr aData = myFeature->data();
268 AttributePtr anAttribute = myFeature->attribute(attributeID());
270 setObject(myObject, myShape);
271 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
274 aRefAttr->setAttr(myRefAttribute);
278 //********************************************************************
279 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
283 getGeomSelection(thePrs, anObject, aShape);
285 setObject(anObject, aShape);