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>
15 #include <ModuleBase_IModule.h>
17 #include <Config_WidgetAPI.h>
18 #include <Events_Loop.h>
19 #include <Events_Message.h>
20 #include <GeomAPI_Interface.h>
21 #include <GeomAPI_Shape.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>
39 #include <Config_WidgetAPI.h>
40 #include <Events_Error.h>
42 #include <GeomAPI_Shape.h>
44 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopExp_Explorer.hxx>
52 #include <QToolButton>
55 #include <QDockWidget>
56 #include <QApplication>
57 #include <QFormLayout>
59 #include <TopExp_Explorer.hxx>
60 #include <TopoDS_Shape.hxx>
67 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
68 ModuleBase_IWorkshop* theWorkshop,
69 const Config_WidgetAPI* theData,
70 const std::string& theParentId)
71 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
73 QFormLayout* aLayout = new QFormLayout(this);
74 ModuleBase_Tools::adjustMargins(aLayout);
76 QString aLabelText = QString::fromStdString(theData->widgetLabel());
77 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
78 myLabel = new QLabel(aLabelText, this);
79 if (!aLabelIcon.isEmpty())
80 myLabel->setPixmap(QPixmap(aLabelIcon));
83 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
84 myTextLine = new QLineEdit(this);
85 QString anObjName = QString::fromStdString(attributeID());
86 myTextLine->setObjectName(anObjName);
87 myTextLine->setReadOnly(true);
88 myTextLine->setToolTip(aToolTip);
89 myTextLine->installEventFilter(this);
91 aLayout->addRow(myLabel, myTextLine);
92 myLabel->setToolTip(aToolTip);
94 std::string aTypes = theData->getProperty("shape_types");
95 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
98 //********************************************************************
99 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
103 //********************************************************************
104 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
106 // the value is stored on the selection changed signal processing
110 //********************************************************************
111 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
112 GeomShapePtr theShape)
114 DataPtr aData = myFeature->data();
115 std::string aType = aData->attribute(attributeID())->attributeType();
116 if (aType == ModelAPI_AttributeReference::typeId()) {
117 AttributeReferencePtr aRef = aData->reference(attributeID());
118 ObjectPtr aObject = aRef->value();
119 if (!(aObject && aObject->isSame(theSelectedObject))) {
120 aRef->setValue(theSelectedObject);
122 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
123 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
125 AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
126 if (anAttribute.get())
127 aRefAttr->setAttr(anAttribute);
129 ObjectPtr aObject = aRefAttr->object();
130 if (!(aObject && aObject->isSame(theSelectedObject))) {
131 aRefAttr->setObject(theSelectedObject);
134 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
135 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
136 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
137 if (aSelectAttr.get() != NULL) {
138 aSelectAttr->setValue(aResult, theShape);
143 //********************************************************************
144 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
146 QList<ModuleBase_ViewerPrs> aSelected;
148 DataPtr aData = myFeature->data();
149 AttributePtr anAttribute = myFeature->attribute(attributeID());
151 ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
153 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
154 if (aShapePtr.get()) {
155 aShape = aShapePtr->impl<TopoDS_Shape>();
157 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
158 aSelected.append(aPrs);
163 //********************************************************************
164 void ModuleBase_WidgetShapeSelector::clearAttribute()
166 // In order to make reselection possible, set empty object and shape should be done
167 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
170 //********************************************************************
171 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
173 bool isBlocked = this->blockSignals(true);
174 updateSelectionName();
175 this->blockSignals(isBlocked);
180 //********************************************************************
181 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
183 QList<QWidget*> aControls;
184 aControls.append(myTextLine);
188 void ModuleBase_WidgetShapeSelector::updateFocus()
190 emit focusOutWidget(this);
193 //********************************************************************
194 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
196 QIntList aShapeTypes;
197 foreach(QString aType, myShapeTypes) {
198 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
203 //********************************************************************
204 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
207 DataPtr aData = myFeature->data();
208 if (!aData->isValid())
211 std::string aType = aData->attribute(attributeID())->attributeType();
212 if (aType == ModelAPI_AttributeReference::typeId()) {
213 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
214 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
215 aShape = myWorkshop->module()->findShape(aRefAttr);
216 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
217 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
218 aShape = aSelectAttr->value();
224 //********************************************************************
225 void ModuleBase_WidgetShapeSelector::updateSelectionName()
227 DataPtr aData = myFeature->data();
228 if (!aData->isValid())
231 bool isNameUpdated = false;
232 AttributeSelectionPtr aSelect = aData->selection(attributeID());
234 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
235 isNameUpdated = true;
237 if (!isNameUpdated) {
238 ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
239 if (anObject.get() != NULL) {
240 std::string aName = anObject->data()->name();
241 myTextLine->setText(QString::fromStdString(aName));
243 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
244 if (aRefAttr && aRefAttr->attr().get() != NULL) {
245 //myIsObject = aRefAttr->isObject();
246 std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
247 myTextLine->setText(QString::fromStdString(anAttrName));
250 myTextLine->setText(getDefaultValue().c_str());
256 //********************************************************************
257 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
259 ModuleBase_WidgetValidated::storeAttributeValue();
261 DataPtr aData = myFeature->data();
262 AttributePtr anAttribute = myFeature->attribute(attributeID());
264 myObject = ModuleBase_Tools::getObject(anAttribute);
265 myShape = getShape();
266 myRefAttribute = AttributePtr();
268 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
270 myIsObject = aRefAttr->isObject();
271 myRefAttribute = aRefAttr->attr();
275 //********************************************************************
276 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
278 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
280 DataPtr aData = myFeature->data();
281 AttributePtr anAttribute = myFeature->attribute(attributeID());
283 setObject(myObject, myShape);
284 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
287 aRefAttr->setAttr(myRefAttribute);