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 myBasePalet = myTextLine->palette();
105 myInactivePalet = myBasePalet;
106 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
107 myTextLine->setPalette(myInactivePalet);
109 aLayout->addWidget(myTextLine, 1);
111 std::string aTypes = theData->getProperty("shape_types");
112 myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
114 std::string aObjTypes = theData->getProperty("object_types");
115 myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
117 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
120 //********************************************************************
121 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
123 activateSelection(false);
126 //********************************************************************
127 bool ModuleBase_WidgetShapeSelector::storeValue() const
129 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
130 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
133 DataPtr aData = myFeature->data();
134 if (myUseSubShapes) {
135 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect =
136 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
138 ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
140 aSelect->setValue(aBody, myShape);
141 updateObject(myFeature);
145 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
146 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
148 ObjectPtr aObject = aRef->value();
149 if (!(aObject && aObject->isSame(mySelectedObject))) {
150 aRef->setValue(mySelectedObject);
151 updateObject(myFeature);
158 //********************************************************************
159 bool ModuleBase_WidgetShapeSelector::restoreValue()
161 DataPtr aData = myFeature->data();
162 bool isBlocked = this->blockSignals(true);
163 if (myUseSubShapes) {
164 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
166 mySelectedObject = aSelect->context();
167 myShape = aSelect->value();
170 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
171 mySelectedObject = aRef->value();
173 updateSelectionName();
175 this->blockSignals(isBlocked);
179 //********************************************************************
180 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
182 QList<QWidget*> aControls;
183 aControls.append(myLabel);
184 aControls.append(myTextLine);
188 //********************************************************************
189 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
191 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
192 if (aObjects.size() > 0) {
193 ObjectPtr aObject = aObjects.first();
194 if ((!mySelectedObject) && (!aObject))
197 // Check that the selected object is result (others can not be accepted)
198 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
203 // We can not select a result of our feature
204 const std::list<boost::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
205 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
206 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
211 // Check that object belongs to active document or PartSet
212 DocumentPtr aDoc = aRes->document();
213 SessionPtr aMgr = ModelAPI_Session::get();
214 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
217 // Check that the result has a shape
218 GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
222 /// Check that object has acceptable type
223 if (!acceptObjectType(aObject))
226 // Get sub-shapes from local selection
227 if (myUseSubShapes) {
228 NCollection_List<TopoDS_Shape> aShapeList;
229 std::list<ObjectPtr> aOwners;
230 myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
231 if (aShapeList.Extent() > 0) {
232 aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
233 aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
237 // Check that the selection corresponds to selection type
238 if (myUseSubShapes) {
239 if (!acceptSubShape(aShape))
242 if (!acceptObjectShape(aObject))
245 setObject(aObject, aShape);
246 //activateSelection(false);
247 emit focusOutWidget(this);
251 //********************************************************************
252 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
254 mySelectedObject = theObj;
256 if (mySelectedObject) {
258 if (!myUseSubShapes) {
259 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
260 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
263 updateSelectionName();
264 activateSelection(false);
265 emit valuesChanged();
268 //********************************************************************
269 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
271 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
273 // Check that the shape of necessary type
274 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
277 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
281 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
282 if (aShapeType == TopAbs_COMPOUND) {
283 foreach (QString aType, myShapeTypes) {
284 TopExp_Explorer aEx(aShape, shapeType(aType));
289 foreach (QString aType, myShapeTypes) {
290 if (shapeType(aType) == aShapeType)
297 //********************************************************************
298 bool ModuleBase_WidgetShapeSelector::acceptSubShape(boost::shared_ptr<GeomAPI_Shape> theShape) const
300 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
301 foreach (QString aType, myShapeTypes) {
302 if (aShape.ShapeType() == shapeType(aType))
308 //********************************************************************
309 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
311 // Definition of types is not obligatory. If types are not defined then
312 // it means that accepted any type
313 if (myObjectTypes.isEmpty())
316 foreach (QString aType, myObjectTypes) {
317 if (aType.toLower() == "construction") {
318 ResultConstructionPtr aConstr =
319 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
320 return (aConstr != NULL);
321 } // ToDo: Process other types of objects
323 // Object type is defined but not found
328 //********************************************************************
329 void ModuleBase_WidgetShapeSelector::updateSelectionName()
331 if (mySelectedObject) {
332 std::string aName = mySelectedObject->data()->name();
333 myTextLine->setText(QString::fromStdString(aName));
336 QString aMsg = tr("Select a ");
338 foreach (QString aType, myShapeTypes) {
344 myTextLine->setText(aMsg);
346 myTextLine->setText(tr("No object selected"));
351 //********************************************************************
352 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
354 myIsActive = toActivate;
356 myTextLine->setPalette(myBasePalet);
358 myTextLine->setPalette(myInactivePalet);
359 updateSelectionName();
362 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
363 if (myUseSubShapes) {
366 foreach (QString aType, myShapeTypes)
367 aList.append(shapeType(aType));
368 myWorkshop->activateSubShapesSelection(aList);
369 if (!myObjectTypes.isEmpty()) {
370 myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
371 myWorkshop->viewer()->clearSelectionFilters();
372 myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
376 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
377 if (myUseSubShapes) {
378 if (!myObjTypeFilter.IsNull()) {
379 myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
380 myObjTypeFilter.Nullify();
382 myWorkshop->deactivateSubShapesSelection();
387 //********************************************************************
388 void ModuleBase_WidgetShapeSelector::raisePanel() const
390 QWidget* aParent = myContainer->parentWidget();
391 QWidget* aLastPanel = 0;
392 while (!aParent->inherits("QDockWidget")) {
393 aLastPanel = aParent;
394 aParent = aParent->parentWidget();
398 if (aParent->inherits("QDockWidget")) {
399 QDockWidget* aTabWgt = (QDockWidget*) aParent;
404 //********************************************************************
405 bool ModuleBase_WidgetShapeSelector::focusTo()
407 activateSelection(true);
411 //********************************************************************
412 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
414 if (theObj == myTextLine) {
415 if (theEvent->type() == QEvent::FocusIn)
416 activateSelection(true);
418 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
421 //********************************************************************
422 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
425 ModuleBase_WidgetValueFeature* aFeatureValue =
426 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
427 if (aFeatureValue && aFeatureValue->object()) {
428 ObjectPtr aObject = aFeatureValue->object();
429 if (acceptObjectShape(aObject)) {