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 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
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());
124 AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
125 if (anAttribute.get())
126 aRefAttr->setAttr(anAttribute);
128 ObjectPtr aObject = aRefAttr->object();
129 if (!(aObject && aObject->isSame(theSelectedObject))) {
130 aRefAttr->setObject(theSelectedObject);
133 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
134 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
135 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
136 if (aSelectAttr.get() != NULL) {
137 aSelectAttr->setValue(aResult, theShape);
142 //********************************************************************
143 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
145 QList<ModuleBase_ViewerPrs> aSelected;
147 DataPtr aData = myFeature->data();
148 AttributePtr anAttribute = myFeature->attribute(attributeID());
150 ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
151 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
152 ModuleBase_ViewerPrs aPrs(anObject, aShapePtr, NULL);
153 aSelected.append(aPrs);
158 //********************************************************************
159 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
161 bool isBlocked = this->blockSignals(true);
162 updateSelectionName();
163 this->blockSignals(isBlocked);
168 //********************************************************************
169 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
171 QList<QWidget*> aControls;
172 aControls.append(myTextLine);
176 void ModuleBase_WidgetShapeSelector::updateFocus()
178 emit focusOutWidget(this);
181 //********************************************************************
182 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
184 QIntList aShapeTypes;
185 foreach(QString aType, myShapeTypes) {
186 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
191 //********************************************************************
192 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
195 DataPtr aData = myFeature->data();
196 if (!aData->isValid())
199 std::string aType = aData->attribute(attributeID())->attributeType();
200 if (aType == ModelAPI_AttributeReference::typeId()) {
201 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
202 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
203 if (aRefAttr.get() && !aRefAttr->isObject()) {
204 AttributePtr anAttribute = aRefAttr->attr();
205 aShape = myWorkshop->module()->findShape(anAttribute);
207 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
208 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
209 aShape = aSelectAttr->value();
215 //********************************************************************
216 void ModuleBase_WidgetShapeSelector::updateSelectionName()
218 DataPtr aData = myFeature->data();
219 if (!aData->isValid())
222 bool isNameUpdated = false;
223 AttributeSelectionPtr aSelect = aData->selection(attributeID());
225 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
226 isNameUpdated = true;
228 if (!isNameUpdated) {
229 ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
230 if (anObject.get() != NULL) {
231 std::string aName = anObject->data()->name();
232 myTextLine->setText(QString::fromStdString(aName));
234 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
235 if (aRefAttr && aRefAttr->attr().get() != NULL) {
236 //myIsObject = aRefAttr->isObject();
237 std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
238 myTextLine->setText(QString::fromStdString(anAttrName));
241 myTextLine->setText(getDefaultValue().c_str());
247 //********************************************************************
248 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
250 ModuleBase_WidgetValidated::storeAttributeValue();
252 DataPtr aData = myFeature->data();
253 AttributePtr anAttribute = myFeature->attribute(attributeID());
255 myObject = ModuleBase_Tools::getObject(anAttribute);
256 myShape = getShape();
257 myRefAttribute = AttributePtr();
259 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
261 myIsObject = aRefAttr->isObject();
262 myRefAttribute = aRefAttr->attr();
266 //********************************************************************
267 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
269 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
271 DataPtr aData = myFeature->data();
272 AttributePtr anAttribute = myFeature->attribute(attributeID());
274 setObject(myObject, myShape);
275 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
278 aRefAttr->setAttr(myRefAttribute);