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_Tools.h>
10 #include <ModuleBase_WidgetValueFeature.h>
11 #include <Config_WidgetAPI.h>
12 #include <Events_Loop.h>
13 #include <Events_Message.h>
14 #include <GeomAPI_Interface.h>
15 #include <GeomAPI_Shape.h>
16 #include <ModelAPI_AttributeReference.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26 #include <Config_WidgetAPI.h>
27 #include <Events_Error.h>
29 #include <GeomAPI_Shape.h>
31 #include <TopoDS_Shape.hxx>
32 #include <TopExp_Explorer.hxx>
38 #include <QToolButton>
41 #include <QDockWidget>
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS_Shape.hxx>
46 #include <boost/smart_ptr/shared_ptr.hpp>
52 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
53 static ShapeTypes MyShapeTypes;
55 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
57 if (MyShapeTypes.count() == 0) {
58 MyShapeTypes["face"] = TopAbs_FACE;
59 MyShapeTypes["faces"] = TopAbs_FACE;
60 MyShapeTypes["vertex"] = TopAbs_VERTEX;
61 MyShapeTypes["vertices"] = TopAbs_VERTEX;
62 MyShapeTypes["wire"] = TopAbs_WIRE;
63 MyShapeTypes["edge"] = TopAbs_EDGE;
64 MyShapeTypes["edges"] = TopAbs_EDGE;
65 MyShapeTypes["shell"] = TopAbs_SHELL;
66 MyShapeTypes["solid"] = TopAbs_SOLID;
67 MyShapeTypes["solids"] = TopAbs_SOLID;
69 QString aType = theType.toLower();
70 if (MyShapeTypes.contains(aType))
71 return MyShapeTypes[aType];
72 Events_Error::send("Shape type defined in XML is not implemented!");
76 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
77 ModuleBase_IWorkshop* theWorkshop,
78 const Config_WidgetAPI* theData,
79 const std::string& theParentId)
80 : ModuleBase_ModelWidget(theParent, theData, theParentId),
81 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
83 myContainer = new QWidget(theParent);
84 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
85 ModuleBase_Tools::adjustMargins(aLayout);
87 QString aLabelText = QString::fromStdString(theData->widgetLabel());
88 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
89 myLabel = new QLabel(aLabelText, myContainer);
90 if (!aLabelIcon.isEmpty())
91 myLabel->setPixmap(QPixmap(aLabelIcon));
93 aLayout->addWidget(myLabel);
95 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
96 myTextLine = new QLineEdit(myContainer);
97 myTextLine->setReadOnly(true);
98 myTextLine->setToolTip(aToolTip);
99 myTextLine->installEventFilter(this);
101 myBasePalet = myTextLine->palette();
102 myInactivePalet = myBasePalet;
103 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
104 myTextLine->setPalette(myInactivePalet);
106 aLayout->addWidget(myTextLine, 1);
108 std::string aTypes = theData->getProperty("shape_types");
109 myShapeTypes = QString(aTypes.c_str()).split(' ');
111 myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false);
114 //********************************************************************
115 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
117 activateSelection(false);
120 //********************************************************************
121 bool ModuleBase_WidgetShapeSelector::storeValue() const
123 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
124 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
127 DataPtr aData = myFeature->data();
128 if (myUseSubShapes) {
129 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect =
130 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
132 ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
134 aSelect->setValue(aBody, myShape);
135 updateObject(myFeature);
138 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
139 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
141 ObjectPtr aObject = aRef->value();
142 if (!(aObject && aObject->isSame(mySelectedObject))) {
143 aRef->setValue(mySelectedObject);
144 updateObject(myFeature);
150 //********************************************************************
151 bool ModuleBase_WidgetShapeSelector::restoreValue()
153 DataPtr aData = myFeature->data();
154 bool isBlocked = this->blockSignals(true);
155 if (myUseSubShapes) {
156 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
158 mySelectedObject = aSelect->context();
159 myShape = aSelect->value();
162 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
163 mySelectedObject = aRef->value();
165 updateSelectionName();
167 this->blockSignals(isBlocked);
171 //********************************************************************
172 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
174 QList<QWidget*> aControls;
175 aControls.append(myLabel);
176 aControls.append(myTextLine);
180 //********************************************************************
181 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
183 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
184 if (aObjects.size() > 0) {
185 ObjectPtr aObject = aObjects.first();
186 if ((!mySelectedObject) && (!aObject))
188 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
191 // Get sub-shapes from local selection
192 boost::shared_ptr<GeomAPI_Shape> aShape;
193 if (myUseSubShapes) {
194 NCollection_List<TopoDS_Shape> aShapeList;
195 myWorkshop->selection()->selectedShapes(aShapeList);
196 if (aShapeList.Extent() > 0) {
197 aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
198 aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
202 // Check that the selection corresponds to selection type
203 if (myUseSubShapes) {
204 if (!isAccepted(aShape))
207 if (!isAccepted(aObject))
210 setObject(aObject, aShape);
211 emit focusOutWidget(this);
215 //********************************************************************
216 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
218 if (mySelectedObject == theObj)
220 mySelectedObject = theObj;
222 if (mySelectedObject) {
224 if (!myUseSubShapes) {
225 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
226 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
227 Events_Loop::loop()->flush(anEvent);
230 updateSelectionName();
231 activateSelection(false);
232 emit valuesChanged();
235 //********************************************************************
236 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
238 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
240 // We can not select a result of our feature
241 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
242 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
243 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
244 if ((*aIt) == aResult)
248 // Check that object belongs to active document or PartSet
249 DocumentPtr aDoc = aResult->document();
250 SessionPtr aMgr = ModelAPI_Session::get();
251 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
254 // Check that the shape of necessary type
255 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
258 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
262 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
263 if (aShapeType == TopAbs_COMPOUND) {
264 foreach (QString aType, myShapeTypes) {
265 TopExp_Explorer aEx(aShape, shapeType(aType));
270 foreach (QString aType, myShapeTypes) {
271 if (shapeType(aType) == aShapeType)
278 //********************************************************************
279 bool ModuleBase_WidgetShapeSelector::isAccepted(boost::shared_ptr<GeomAPI_Shape> theShape) const
281 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
282 foreach (QString aType, myShapeTypes) {
283 if (aShape.ShapeType() == shapeType(aType))
289 //********************************************************************
290 void ModuleBase_WidgetShapeSelector::updateSelectionName()
292 if (mySelectedObject) {
293 std::string aName = mySelectedObject->data()->name();
294 myTextLine->setText(QString::fromStdString(aName));
297 QString aMsg = tr("Select a ");
299 foreach (QString aType, myShapeTypes) {
305 myTextLine->setText(aMsg);
307 myTextLine->setText(tr("No object selected"));
312 //********************************************************************
313 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
315 myIsActive = toActivate;
317 myTextLine->setPalette(myBasePalet);
319 myTextLine->setPalette(myInactivePalet);
320 updateSelectionName();
323 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
324 if (myUseSubShapes) {
326 foreach (QString aType, myShapeTypes)
327 aList.append(shapeType(aType));
328 myWorkshop->activateSubShapesSelection(aList);
331 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
333 myWorkshop->deactivateSubShapesSelection();
337 //********************************************************************
338 void ModuleBase_WidgetShapeSelector::raisePanel() const
340 QWidget* aParent = myContainer->parentWidget();
341 QWidget* aLastPanel = 0;
342 while (!aParent->inherits("QDockWidget")) {
343 aLastPanel = aParent;
344 aParent = aParent->parentWidget();
348 if (aParent->inherits("QDockWidget")) {
349 QDockWidget* aTabWgt = (QDockWidget*) aParent;
354 //********************************************************************
355 bool ModuleBase_WidgetShapeSelector::focusTo()
357 activateSelection(true);
361 //********************************************************************
362 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
364 if (theObj == myTextLine) {
365 if (theEvent->type() == QEvent::FocusIn)
366 activateSelection(true);
368 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
371 //********************************************************************
372 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
375 ModuleBase_WidgetValueFeature* aFeatureValue =
376 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
377 if (aFeatureValue && aFeatureValue->object()) {
378 ObjectPtr aObject = aFeatureValue->object();
379 if (isAccepted(aObject)) {