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);
91 std::string aTypes = theData->getProperty("shape_types");
92 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
95 //********************************************************************
96 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
100 //********************************************************************
101 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
103 // the value is stored on the selection changed signal processing
107 //********************************************************************
108 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
109 GeomShapePtr theShape)
111 DataPtr aData = myFeature->data();
112 AttributeReferencePtr aRef = aData->reference(attributeID());
114 ObjectPtr aObject = aRef->value();
115 if (!(aObject && aObject->isSame(theSelectedObject))) {
116 aRef->setValue(theSelectedObject);
119 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
121 ObjectPtr aObject = aRefAttr->object();
122 if (!(aObject && aObject->isSame(theSelectedObject))) {
123 aRefAttr->setObject(theSelectedObject);
126 AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
127 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
128 if (aSelectAttr.get() != NULL) {
129 aSelectAttr->setValue(aResult, theShape);
135 //********************************************************************
136 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
138 QList<ModuleBase_ViewerPrs> aSelected;
140 DataPtr aData = myFeature->data();
141 AttributePtr anAttribute = myFeature->attribute(attributeID());
143 ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
145 std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
146 if (aShapePtr.get()) {
147 aShape = aShapePtr->impl<TopoDS_Shape>();
149 ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
150 aSelected.append(aPrs);
155 //********************************************************************
156 void ModuleBase_WidgetShapeSelector::clearAttribute()
158 // In order to make reselection possible, set empty object and shape should be done
159 setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
162 //********************************************************************
163 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
165 bool isBlocked = this->blockSignals(true);
166 updateSelectionName();
167 this->blockSignals(isBlocked);
172 //********************************************************************
173 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
175 QList<QWidget*> aControls;
176 aControls.append(myTextLine);
180 void ModuleBase_WidgetShapeSelector::updateFocus()
182 emit focusOutWidget(this);
185 //********************************************************************
186 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
188 QIntList aShapeTypes;
189 foreach(QString aType, myShapeTypes) {
190 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
195 //********************************************************************
196 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
199 DataPtr aData = myFeature->data();
200 if (!aData->isValid())
203 AttributeSelectionPtr aSelect = aData->selection(attributeID());
205 aShape = aSelect->value();
210 //********************************************************************
211 void ModuleBase_WidgetShapeSelector::updateSelectionName()
213 DataPtr aData = myFeature->data();
214 if (!aData->isValid())
217 bool isNameUpdated = false;
218 AttributeSelectionPtr aSelect = aData->selection(attributeID());
220 myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
221 isNameUpdated = true;
223 if (!isNameUpdated) {
224 ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
225 if (anObject.get() != NULL) {
226 std::string aName = anObject->data()->name();
227 myTextLine->setText(QString::fromStdString(aName));
229 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
230 if (aRefAttr && aRefAttr->attr().get() != NULL) {
231 //myIsObject = aRefAttr->isObject();
232 AttributePtr anAttr = aRefAttr->attr();
233 if (anAttr.get() != NULL) {
234 std::stringstream aName;
235 aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
236 myTextLine->setText(QString::fromStdString(aName.str()));
240 myTextLine->setText(getDefaultValue().c_str());
246 //********************************************************************
247 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
249 ModuleBase_WidgetValidated::storeAttributeValue();
251 DataPtr aData = myFeature->data();
252 AttributePtr anAttribute = myFeature->attribute(attributeID());
254 myObject = GeomValidators_Tools::getObject(anAttribute);
255 myShape = getShape();
256 myRefAttribute = AttributePtr();
258 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
260 myIsObject = aRefAttr->isObject();
261 myRefAttribute = aRefAttr->attr();
265 //********************************************************************
266 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
268 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
270 DataPtr aData = myFeature->data();
271 AttributePtr anAttribute = myFeature->attribute(attributeID());
273 setObject(myObject, myShape);
274 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
277 aRefAttr->setAttr(myRefAttribute);