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 <GeomValidators_ShapeType.h>
18 #include <Config_WidgetAPI.h>
19 #include <Events_Loop.h>
20 #include <Events_Message.h>
21 #include <GeomAPI_Interface.h>
22 #include <GeomAPI_Shape.h>
23 #include <GeomValidators_Tools.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_AttributeReference.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_AttributeValidator.h>
40 #include <ModelAPI_ShapeValidator.h>
42 #include <Config_WidgetAPI.h>
43 #include <Events_Error.h>
45 #include <GeomAPI_Shape.h>
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopExp_Explorer.hxx>
55 #include <QToolButton>
58 #include <QDockWidget>
59 #include <QApplication>
60 #include <QFormLayout>
62 #include <TopExp_Explorer.hxx>
63 #include <TopoDS_Shape.hxx>
70 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
71 ModuleBase_IWorkshop* theWorkshop,
72 const Config_WidgetAPI* theData,
73 const std::string& theParentId)
74 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
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()
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 AttributeReferencePtr aRef = aData->reference(attributeID());
117 ObjectPtr aObject = aRef->value();
118 if (!(aObject && aObject->isSame(theSelectedObject))) {
119 aRef->setValue(theSelectedObject);
122 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
124 ObjectPtr aObject = aRefAttr->object();
125 if (!(aObject && aObject->isSame(theSelectedObject))) {
126 aRefAttr->setObject(theSelectedObject);
129 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
130 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
131 if (aSelectAttr.get() != NULL) {
132 aSelectAttr->setValue(aResult, theShape);
138 //********************************************************************
139 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
141 QList<ModuleBase_ViewerPrs> aSelected;
143 DataPtr aData = myFeature->data();
144 AttributePtr anAttribute = myFeature->attribute(attributeID());
146 ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
148 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
149 if (aShapePtr.get()) {
150 aShape = aShapePtr->impl<TopoDS_Shape>();
152 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
153 aSelected.append(aPrs);
158 //********************************************************************
159 void ModuleBase_WidgetShapeSelector::clearAttribute()
161 // In order to make reselection possible, set empty object and shape should be done
162 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
165 //********************************************************************
166 bool ModuleBase_WidgetShapeSelector::restoreValue()
168 bool isBlocked = this->blockSignals(true);
169 updateSelectionName();
170 this->blockSignals(isBlocked);
175 //********************************************************************
176 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
178 QList<QWidget*> aControls;
179 aControls.append(myTextLine);
183 void ModuleBase_WidgetShapeSelector::updateFocus()
185 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
187 emit focusOutWidget(this);
190 //********************************************************************
191 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
193 QIntList aShapeTypes;
194 foreach(QString aType, myShapeTypes) {
195 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
200 //********************************************************************
201 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
204 DataPtr aData = myFeature->data();
205 if (!aData->isValid())
208 AttributeSelectionPtr aSelect = aData->selection(attributeID());
210 aShape = aSelect->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 = GeomValidators_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 AttributePtr anAttr = aRefAttr->attr();
238 if (anAttr.get() != NULL) {
239 std::stringstream aName;
240 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
241 myTextLine->setText(QString::fromStdString(aName.str()));
245 myTextLine->setText(getDefaultValue().c_str());
251 //********************************************************************
252 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
254 DataPtr aData = myFeature->data();
255 AttributePtr anAttribute = myFeature->attribute(attributeID());
257 myObject = GeomValidators_Tools::getObject(anAttribute);
258 myShape = getShape();
259 myRefAttribute = AttributePtr();
261 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
263 myIsObject = aRefAttr->isObject();
264 myRefAttribute = aRefAttr->attr();
268 //********************************************************************
269 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool 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);