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>
18 #include <ModelAPI_AttributeReference.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Tools.h>
29 #include <ModelAPI_ResultBody.h>
30 #include <Config_WidgetAPI.h>
31 #include <Events_Error.h>
33 #include <GeomAPI_Shape.h>
35 #include <TopoDS_Shape.hxx>
36 #include <TopExp_Explorer.hxx>
42 #include <QToolButton>
45 #include <QDockWidget>
47 #include <TopExp_Explorer.hxx>
48 #include <TopoDS_Shape.hxx>
50 #include <boost/smart_ptr/shared_ptr.hpp>
55 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
56 static ShapeTypes MyShapeTypes;
58 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
60 if (MyShapeTypes.count() == 0) {
61 MyShapeTypes["face"] = TopAbs_FACE;
62 MyShapeTypes["faces"] = TopAbs_FACE;
63 MyShapeTypes["vertex"] = TopAbs_VERTEX;
64 MyShapeTypes["vertices"] = TopAbs_VERTEX;
65 MyShapeTypes["wire"] = TopAbs_WIRE;
66 MyShapeTypes["edge"] = TopAbs_EDGE;
67 MyShapeTypes["edges"] = TopAbs_EDGE;
68 MyShapeTypes["shell"] = TopAbs_SHELL;
69 MyShapeTypes["solid"] = TopAbs_SOLID;
70 MyShapeTypes["solids"] = TopAbs_SOLID;
72 QString aType = theType.toLower();
73 if (MyShapeTypes.contains(aType))
74 return MyShapeTypes[aType];
75 Events_Error::send("Shape type defined in XML is not implemented!");
79 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
80 ModuleBase_IWorkshop* theWorkshop,
81 const Config_WidgetAPI* theData,
82 const std::string& theParentId)
83 : ModuleBase_ModelWidget(theParent, theData, theParentId),
84 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
86 myContainer = new QWidget(theParent);
87 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
88 ModuleBase_Tools::adjustMargins(aLayout);
90 QString aLabelText = QString::fromStdString(theData->widgetLabel());
91 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
92 myLabel = new QLabel(aLabelText, myContainer);
93 if (!aLabelIcon.isEmpty())
94 myLabel->setPixmap(QPixmap(aLabelIcon));
96 aLayout->addWidget(myLabel);
98 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
99 myTextLine = new QLineEdit(myContainer);
100 myTextLine->setReadOnly(true);
101 myTextLine->setToolTip(aToolTip);
102 myTextLine->installEventFilter(this);
104 aLayout->addWidget(myTextLine, 1);
106 std::string aTypes = theData->getProperty("shape_types");
107 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
109 std::string aObjTypes = theData->getProperty("object_types");
110 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
112 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
115 //********************************************************************
116 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
118 activateSelection(false);
121 //********************************************************************
122 bool ModuleBase_WidgetShapeSelector::storeValue() const
124 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
125 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
128 DataPtr aData = myFeature->data();
129 if (myUseSubShapes) {
130 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect =
131 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
133 ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
135 aSelect->setValue(aBody, myShape);
136 updateObject(myFeature);
140 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
141 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
143 ObjectPtr aObject = aRef->value();
144 if (!(aObject && aObject->isSame(mySelectedObject))) {
145 aRef->setValue(mySelectedObject);
146 updateObject(myFeature);
153 //********************************************************************
154 bool ModuleBase_WidgetShapeSelector::restoreValue()
156 DataPtr aData = myFeature->data();
157 bool isBlocked = this->blockSignals(true);
158 if (myUseSubShapes) {
159 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
161 mySelectedObject = aSelect->context();
162 myShape = aSelect->value();
165 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
166 mySelectedObject = aRef->value();
168 updateSelectionName();
170 this->blockSignals(isBlocked);
174 //********************************************************************
175 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
177 QList<QWidget*> aControls;
178 aControls.append(myTextLine);
182 //********************************************************************
183 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
185 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
186 if (aObjects.size() > 0) {
187 ObjectPtr aObject = aObjects.first();
188 if ((!mySelectedObject) && (!aObject))
191 // Check that the selected object is result (others can not be accepted)
192 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
197 // We can not select a result of our feature
198 const std::list<boost::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
199 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
200 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
205 // Check that object belongs to active document or PartSet
206 DocumentPtr aDoc = aRes->document();
207 SessionPtr aMgr = ModelAPI_Session::get();
208 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
211 // Check that the result has a shape
212 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
216 /// Check that object has acceptable type
217 if (!acceptObjectType(aObject))
220 // Get sub-shapes from local selection
221 if (myUseSubShapes) {
222 NCollection_List<TopoDS_Shape> aShapeList;
223 std::list<ObjectPtr> aOwners;
224 myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
225 if (aShapeList.Extent() > 0) {
226 aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
227 aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
231 // Check that the selection corresponds to selection type
232 if (myUseSubShapes) {
233 if (!acceptSubShape(aShape))
236 if (!acceptObjectShape(aObject))
239 setObject(aObject, aShape);
240 //activateSelection(false);
241 emit focusOutWidget(this);
245 //********************************************************************
246 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
248 mySelectedObject = theObj;
250 if (mySelectedObject) {
252 if (!myUseSubShapes) {
253 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
254 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
257 updateSelectionName();
258 activateSelection(false);
259 emit valuesChanged();
262 //********************************************************************
263 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
265 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
267 // Check that the shape of necessary type
268 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
271 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
275 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
276 if (aShapeType == TopAbs_COMPOUND) {
277 foreach (QString aType, myShapeTypes) {
278 TopExp_Explorer aEx(aShape, shapeType(aType));
283 foreach (QString aType, myShapeTypes) {
284 if (shapeType(aType) == aShapeType)
291 //********************************************************************
292 bool ModuleBase_WidgetShapeSelector::acceptSubShape(boost::shared_ptr<GeomAPI_Shape> theShape) const
294 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
295 foreach (QString aType, myShapeTypes) {
296 if (aShape.ShapeType() == shapeType(aType))
302 //********************************************************************
303 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
305 // Definition of types is not obligatory. If types are not defined then
306 // it means that accepted any type
307 if (myObjectTypes.isEmpty())
310 foreach (QString aType, myObjectTypes) {
311 if (aType.toLower() == "construction") {
312 ResultConstructionPtr aConstr =
313 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
314 return (aConstr != NULL);
315 } // ToDo: Process other types of objects
317 // Object type is defined but not found
322 //********************************************************************
323 void ModuleBase_WidgetShapeSelector::updateSelectionName()
325 if (mySelectedObject) {
326 std::string aName = mySelectedObject->data()->name();
327 myTextLine->setText(QString::fromStdString(aName));
330 myTextLine->setText("");
336 //********************************************************************
337 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
339 myIsActive = toActivate;
340 updateSelectionName();
343 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
344 if (myUseSubShapes) {
347 foreach (QString aType, myShapeTypes)
348 aList.append(shapeType(aType));
349 myWorkshop->activateSubShapesSelection(aList);
350 if (!myObjectTypes.isEmpty()) {
351 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
352 myWorkshop->viewer()->clearSelectionFilters();
353 myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
357 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
358 if (myUseSubShapes) {
359 if (!myObjTypeFilter.IsNull()) {
360 myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
361 myObjTypeFilter.Nullify();
363 myWorkshop->deactivateSubShapesSelection();
368 //********************************************************************
369 void ModuleBase_WidgetShapeSelector::raisePanel() const
371 QWidget* aParent = myContainer->parentWidget();
372 QWidget* aLastPanel = 0;
373 while (!aParent->inherits("QDockWidget")) {
374 aLastPanel = aParent;
375 aParent = aParent->parentWidget();
379 if (aParent->inherits("QDockWidget")) {
380 QDockWidget* aTabWgt = (QDockWidget*) aParent;
385 //********************************************************************
386 bool ModuleBase_WidgetShapeSelector::focusTo()
388 activateSelection(true);
389 return ModuleBase_ModelWidget::focusTo();
392 //********************************************************************
393 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
395 if (theObj == myTextLine) {
396 if (theEvent->type() == QEvent::FocusIn)
397 activateSelection(true);
399 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
402 //********************************************************************
403 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
406 ModuleBase_WidgetValueFeature* aFeatureValue =
407 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
408 if (aFeatureValue && aFeatureValue->object()) {
409 ObjectPtr aObject = aFeatureValue->object();
410 if (acceptObjectShape(aObject)) {