1 // File: ModuleBase_WidgetShapeSelector.h
2 // Created: 2 June 2014
3 // Author: Vitaly Smetannikov
5 #include <ModuleBase_WidgetShapeSelector.h>
6 #include <ModuleBase_Definitions.h>
7 #include <ModuleBase_ISelection.h>
8 #include <ModuleBase_IWorkshop.h>
9 #include <ModuleBase_IViewer.h>
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_WidgetValueFeature.h>
13 #include <Config_WidgetAPI.h>
14 #include <Events_Loop.h>
15 #include <Events_Message.h>
16 #include <GeomAPI_Interface.h>
17 #include <GeomAPI_Shape.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Feature.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_AttributeReference.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_AttributeRefAttr.h>
33 #include <Config_WidgetAPI.h>
34 #include <Events_Error.h>
36 #include <GeomAPI_Shape.h>
38 #include <TopoDS_Shape.hxx>
39 #include <TopExp_Explorer.hxx>
45 #include <QToolButton>
48 #include <QDockWidget>
49 #include <QApplication>
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Shape.hxx>
59 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
60 static ShapeTypes MyShapeTypes;
62 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
64 if (MyShapeTypes.count() == 0) {
65 MyShapeTypes["face"] = TopAbs_FACE;
66 MyShapeTypes["faces"] = TopAbs_FACE;
67 MyShapeTypes["vertex"] = TopAbs_VERTEX;
68 MyShapeTypes["vertices"] = TopAbs_VERTEX;
69 MyShapeTypes["wire"] = TopAbs_WIRE;
70 MyShapeTypes["edge"] = TopAbs_EDGE;
71 MyShapeTypes["edges"] = TopAbs_EDGE;
72 MyShapeTypes["shell"] = TopAbs_SHELL;
73 MyShapeTypes["solid"] = TopAbs_SOLID;
74 MyShapeTypes["solids"] = TopAbs_SOLID;
76 QString aType = theType.toLower();
77 if (MyShapeTypes.contains(aType))
78 return MyShapeTypes[aType];
79 Events_Error::send("Shape type defined in XML is not implemented!");
83 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
84 ModuleBase_IWorkshop* theWorkshop,
85 const Config_WidgetAPI* theData,
86 const std::string& theParentId)
87 : ModuleBase_ModelWidget(theParent, theData, theParentId),
88 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
90 myContainer = new QWidget(theParent);
91 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
92 ModuleBase_Tools::adjustMargins(aLayout);
94 QString aLabelText = QString::fromStdString(theData->widgetLabel());
95 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
96 myLabel = new QLabel(aLabelText, myContainer);
97 if (!aLabelIcon.isEmpty())
98 myLabel->setPixmap(QPixmap(aLabelIcon));
100 aLayout->addWidget(myLabel);
102 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
103 myTextLine = new QLineEdit(myContainer);
104 myTextLine->setReadOnly(true);
105 myTextLine->setToolTip(aToolTip);
106 myTextLine->installEventFilter(this);
108 aLayout->addWidget(myTextLine, 1);
110 std::string aTypes = theData->getProperty("shape_types");
111 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
113 std::string aObjTypes = theData->getProperty("object_types");
114 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
116 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
119 //********************************************************************
120 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
122 activateSelection(false);
125 //********************************************************************
126 bool ModuleBase_WidgetShapeSelector::storeValue() const
128 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
129 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
132 DataPtr aData = myFeature->data();
133 if (myUseSubShapes) {
134 ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
136 AttributePtr aAttr = aData->attribute(attributeID());
138 AttributeSelectionPtr aSelectAttr =
139 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
141 aSelectAttr->setValue(aBody, myShape);
142 updateObject(myFeature);
146 AttributeReferencePtr aRef = aData->reference(attributeID());
148 ObjectPtr aObject = aRef->value();
149 if (!(aObject && aObject->isSame(mySelectedObject))) {
150 aRef->setValue(mySelectedObject);
151 updateObject(myFeature);
155 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
157 ObjectPtr aObject = aRefAttr->object();
158 if (!(aObject && aObject->isSame(mySelectedObject))) {
159 aRefAttr->setObject(mySelectedObject);
160 updateObject(myFeature);
169 //********************************************************************
170 bool ModuleBase_WidgetShapeSelector::restoreValue()
172 DataPtr aData = myFeature->data();
173 bool isBlocked = this->blockSignals(true);
174 if (myUseSubShapes) {
175 AttributeSelectionPtr aSelect = aData->selection(attributeID());
177 mySelectedObject = aSelect->context();
178 myShape = aSelect->value();
181 AttributeReferencePtr aRef = aData->reference(attributeID());
183 mySelectedObject = aRef->value();
185 AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
187 mySelectedObject = aRefAttr->object();
190 updateSelectionName();
192 this->blockSignals(isBlocked);
196 //********************************************************************
197 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
199 QList<QWidget*> aControls;
200 aControls.append(myTextLine);
204 //********************************************************************
205 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
207 QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
208 if (aObjects.size() > 0) {
209 ObjectPtr aObject = aObjects.first();
210 if ((!mySelectedObject) && (!aObject))
213 // Check that the selected object is result (others can not be accepted)
214 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
219 // We can not select a result of our feature
220 const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
221 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
222 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
227 // Check that object belongs to active document or PartSet
228 DocumentPtr aDoc = aRes->document();
229 SessionPtr aMgr = ModelAPI_Session::get();
230 if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
233 // Check that the result has a shape
234 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
238 /// Check that object has acceptable type
239 if (!acceptObjectType(aObject))
242 // Get sub-shapes from local selection
243 if (myUseSubShapes) {
244 NCollection_List<TopoDS_Shape> aShapeList;
245 std::list<ObjectPtr> aOwners;
246 myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
247 if (aShapeList.Extent() > 0) {
248 aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
249 aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
253 // Check that the selection corresponds to selection type
254 if (myUseSubShapes) {
255 if (!acceptSubShape(aShape))
258 if (!acceptObjectShape(aObject))
261 setObject(aObject, aShape);
262 emit focusOutWidget(this);
266 //********************************************************************
267 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
269 mySelectedObject = theObj;
271 if (mySelectedObject) {
274 updateSelectionName();
275 emit valuesChanged();
278 //********************************************************************
279 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
281 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
283 // Check that the shape of necessary type
284 std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
287 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
291 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
292 if (aShapeType == TopAbs_COMPOUND) {
293 foreach (QString aType, myShapeTypes) {
294 TopExp_Explorer aEx(aShape, shapeType(aType));
299 foreach (QString aType, myShapeTypes) {
300 if (shapeType(aType) == aShapeType)
307 //********************************************************************
308 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
310 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
311 foreach (QString aType, myShapeTypes) {
312 if (aShape.ShapeType() == shapeType(aType))
318 //********************************************************************
319 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
321 // Definition of types is not obligatory. If types are not defined then
322 // it means that accepted any type
323 if (myObjectTypes.isEmpty())
326 foreach (QString aType, myObjectTypes) {
327 if (aType.toLower() == "construction") {
328 ResultConstructionPtr aConstr =
329 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
330 return (aConstr != NULL);
331 } // ToDo: Process other types of objects
333 // Object type is defined but not found
338 //********************************************************************
339 void ModuleBase_WidgetShapeSelector::updateSelectionName()
341 if (mySelectedObject) {
342 std::string aName = mySelectedObject->data()->name();
343 myTextLine->setText(QString::fromStdString(aName));
346 myTextLine->setText("");
352 //********************************************************************
353 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
355 if (myIsActive == toActivate)
357 myIsActive = toActivate;
358 updateSelectionName();
361 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
362 if (myUseSubShapes) {
365 foreach (QString aType, myShapeTypes)
366 aList.append(shapeType(aType));
367 myWorkshop->activateSubShapesSelection(aList);
368 if (!myObjectTypes.isEmpty()) {
369 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
370 myWorkshop->viewer()->clearSelectionFilters();
371 myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
375 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
376 if (myUseSubShapes) {
377 if (!myObjTypeFilter.IsNull()) {
378 myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
379 myObjTypeFilter.Nullify();
381 myWorkshop->deactivateSubShapesSelection();
386 //********************************************************************
387 void ModuleBase_WidgetShapeSelector::raisePanel() const
389 QWidget* aParent = myContainer->parentWidget();
390 QWidget* aLastPanel = 0;
391 while (!aParent->inherits("QDockWidget")) {
392 aLastPanel = aParent;
393 aParent = aParent->parentWidget();
397 if (aParent->inherits("QDockWidget")) {
398 QDockWidget* aTabWgt = (QDockWidget*) aParent;
403 //********************************************************************
404 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
407 ModuleBase_WidgetValueFeature* aFeatureValue =
408 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
409 if (aFeatureValue && aFeatureValue->object()) {
410 ObjectPtr aObject = aFeatureValue->object();
411 if (acceptObjectShape(aObject)) {
420 //********************************************************************
421 void ModuleBase_WidgetShapeSelector::activate()
423 activateSelection(true);
426 //********************************************************************
427 void ModuleBase_WidgetShapeSelector::deactivate()
429 activateSelection(false);