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 <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 emit focusOutWidget(this);
188 //********************************************************************
189 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
191 QIntList aShapeTypes;
192 foreach(QString aType, myShapeTypes) {
193 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
198 //********************************************************************
199 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
202 DataPtr aData = myFeature->data();
203 if (!aData->isValid())
206 AttributeSelectionPtr aSelect = aData->selection(attributeID());
208 aShape = aSelect->value();
213 //********************************************************************
214 void ModuleBase_WidgetShapeSelector::updateSelectionName()
216 DataPtr aData = myFeature->data();
217 if (!aData->isValid())
220 bool isNameUpdated = false;
221 AttributeSelectionPtr aSelect = aData->selection(attributeID());
223 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
224 isNameUpdated = true;
226 if (!isNameUpdated) {
227 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
228 if (anObject.get() != NULL) {
229 std::string aName = anObject->data()->name();
230 myTextLine->setText(QString::fromStdString(aName));
232 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
233 if (aRefAttr && aRefAttr->attr().get() != NULL) {
234 //myIsObject = aRefAttr->isObject();
235 AttributePtr anAttr = aRefAttr->attr();
236 if (anAttr.get() != NULL) {
237 std::stringstream aName;
238 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
239 myTextLine->setText(QString::fromStdString(aName.str()));
243 myTextLine->setText(getDefaultValue().c_str());
249 //********************************************************************
250 void ModuleBase_WidgetShapeSelector::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 DataPtr aData = myFeature->data();
270 AttributePtr anAttribute = myFeature->attribute(attributeID());
272 setObject(myObject, myShape);
273 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
276 aRefAttr->setAttr(myRefAttribute);