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>
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>
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 myTextLine->setReadOnly(true);
86 myTextLine->setToolTip(aToolTip);
87 myTextLine->installEventFilter(this);
89 aLayout->addRow(myLabel, myTextLine);
90 myLabel->setToolTip(aToolTip);
92 std::string aTypes = theData->getProperty("shape_types");
93 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
96 //********************************************************************
97 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
101 //********************************************************************
102 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
104 // the value is stored on the selection changed signal processing
108 //********************************************************************
109 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
110 GeomShapePtr theShape)
112 DataPtr aData = myFeature->data();
113 AttributeReferencePtr aRef = aData->reference(attributeID());
115 ObjectPtr aObject = aRef->value();
116 if (!(aObject && aObject->isSame(theSelectedObject))) {
117 aRef->setValue(theSelectedObject);
120 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
122 ObjectPtr aObject = aRefAttr->object();
123 if (!(aObject && aObject->isSame(theSelectedObject))) {
124 aRefAttr->setObject(theSelectedObject);
127 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
128 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
129 if (aSelectAttr.get() != NULL) {
130 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 = GeomValidators_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 = GeomValidators_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 AttributePtr anAttr = aRefAttr->attr();
234 if (anAttr.get() != NULL) {
235 std::stringstream aName;
236 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
237 myTextLine->setText(QString::fromStdString(aName.str()));
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 = GeomValidators_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);