1 // File: ModuleBase_WidgetShapeSelector.h
2 // Created: 2 June 2014
3 // Author: Vitaly Smetannikov
5 #include "ModuleBase_WidgetShapeSelector.h"
6 #include <ModuleBase_IWorkshop.h>
7 #include <ModuleBase_ISelection.h>
8 #include "ModuleBase_WidgetValue.h"
9 #include <ModuleBase_Tools.h>
10 #include "ModuleBase_WidgetValueFeature.h"
12 #include <Events_Loop.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_Tools.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Object.h>
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_AttributeReference.h>
21 #include <ModelAPI_AttributeSelection.h>
22 #include <Config_WidgetAPI.h>
24 #include <GeomAPI_Shape.h>
26 #include <TopoDS_Shape.hxx>
27 #include <TopExp_Explorer.hxx>
33 #include <QToolButton>
36 #include <QDockWidget>
40 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
41 static ShapeTypes MyShapeTypes;
43 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
45 if (MyShapeTypes.count() == 0) {
46 MyShapeTypes["face"] = TopAbs_FACE;
47 MyShapeTypes["vertex"] = TopAbs_VERTEX;
48 MyShapeTypes["wire"] = TopAbs_WIRE;
49 MyShapeTypes["edge"] = TopAbs_EDGE;
50 MyShapeTypes["shell"] = TopAbs_SHELL;
51 MyShapeTypes["solid"] = TopAbs_SOLID;
53 QString aType = theType.toLower();
54 if (MyShapeTypes.contains(aType))
55 return MyShapeTypes[aType];
56 throw std::invalid_argument("Shape type defined in XML is not implemented!");
59 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
60 ModuleBase_IWorkshop* theWorkshop,
61 const Config_WidgetAPI* theData,
62 const std::string& theParentId)
63 : ModuleBase_ModelWidget(theParent, theData, theParentId),
64 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
66 myContainer = new QWidget(theParent);
67 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
68 ModuleBase_Tools::adjustMargins(aLayout);
70 QString aLabelText = QString::fromStdString(theData->widgetLabel());
71 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
72 myLabel = new QLabel(aLabelText, myContainer);
73 if (!aLabelIcon.isEmpty())
74 myLabel->setPixmap(QPixmap(aLabelIcon));
76 aLayout->addWidget(myLabel);
78 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
79 myTextLine = new QLineEdit(myContainer);
80 myTextLine->setReadOnly(true);
81 myTextLine->setToolTip(aToolTip);
82 myTextLine->installEventFilter(this);
84 myBasePalet = myTextLine->palette();
85 myInactivePalet = myBasePalet;
86 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
87 myTextLine->setPalette(myInactivePalet);
89 aLayout->addWidget(myTextLine, 1);
91 std::string aTypes = theData->getProperty("shape_types");
92 myShapeTypes = QString(aTypes.c_str()).split(' ');
94 std::string aUseSubShapes = theData->getProperty("use_subshapes");
95 if (aUseSubShapes.length() > 0) {
96 QString aVal(aUseSubShapes.c_str());
97 myUseSubShapes = (aVal.toUpper() == "TRUE");
101 //********************************************************************
102 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
104 activateSelection(false);
107 //********************************************************************
108 bool ModuleBase_WidgetShapeSelector::storeValue() const
110 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
111 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
114 DataPtr aData = myFeature->data();
115 if (myUseSubShapes) {
116 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect =
117 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
119 ResultBodyPtr aBody = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(mySelectedObject);
121 aSelect->setValue(aBody, myShape);
123 boost::shared_ptr<ModelAPI_AttributeReference> aRef =
124 boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
126 ObjectPtr aObject = aRef->value();
127 if (!(aObject && aObject->isSame(mySelectedObject))) {
128 aRef->setValue(mySelectedObject);
129 updateObject(myFeature);
135 //********************************************************************
136 bool ModuleBase_WidgetShapeSelector::restoreValue()
138 DataPtr aData = myFeature->data();
139 bool isBlocked = this->blockSignals(true);
140 if (myUseSubShapes) {
141 boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
143 mySelectedObject = aSelect->context();
144 myShape = aSelect->value();
147 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
148 mySelectedObject = aRef->value();
150 updateSelectionName();
152 this->blockSignals(isBlocked);
156 //********************************************************************
157 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
159 QList<QWidget*> aControls;
160 aControls.append(myLabel);
161 aControls.append(myTextLine);
165 //********************************************************************
166 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
168 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
169 if (aObjects.size() > 0) {
170 ObjectPtr aObject = aObjects.first();
171 if ((!mySelectedObject) && (!aObject))
173 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
176 // Get sub-shapes from local selection
177 boost::shared_ptr<GeomAPI_Shape> aShape;
178 if (myUseSubShapes) {
179 NCollection_List<TopoDS_Shape> aShapeList;
180 myWorkshop->selection()->selectedShapes(aShapeList);
181 if (aShapeList.Extent() > 0) {
182 aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
183 aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
187 // Check that the selection corresponds to selection type
188 if (myUseSubShapes) {
189 if (!isAccepted(aShape))
192 if (!isAccepted(aObject))
195 setObject(aObject, aShape);
196 emit focusOutWidget(this);
200 //********************************************************************
201 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
203 if (mySelectedObject == theObj)
205 mySelectedObject = theObj;
207 if (mySelectedObject) {
209 if (!myUseSubShapes) {
210 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
211 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
212 Events_Loop::loop()->flush(anEvent);
215 updateSelectionName();
216 activateSelection(false);
217 emit valuesChanged();
220 //********************************************************************
221 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
223 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
225 // We can not select a result of our feature
226 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
227 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
228 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
229 if ((*aIt) == aResult)
233 // Check that object belongs to active document or PartSet
234 DocumentPtr aDoc = aResult->document();
235 SessionPtr aMgr = ModelAPI_Session::get();
236 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
239 // Check that the shape of necessary type
240 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
243 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
247 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
248 if (aShapeType == TopAbs_COMPOUND) {
249 foreach (QString aType, myShapeTypes) {
250 TopExp_Explorer aEx(aShape, shapeType(aType));
255 foreach (QString aType, myShapeTypes) {
256 if (shapeType(aType) == aShapeType)
263 //********************************************************************
264 bool ModuleBase_WidgetShapeSelector::isAccepted(boost::shared_ptr<GeomAPI_Shape> theShape) const
266 TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
267 foreach (QString aType, myShapeTypes) {
268 if (aShape.ShapeType() == shapeType(aType))
274 //********************************************************************
275 void ModuleBase_WidgetShapeSelector::updateSelectionName()
277 if (mySelectedObject) {
278 std::string aName = mySelectedObject->data()->name();
279 myTextLine->setText(QString::fromStdString(aName));
282 QString aMsg = tr("Select a ");
284 foreach (QString aType, myShapeTypes) {
290 myTextLine->setText(aMsg);
292 myTextLine->setText(tr("No object selected"));
297 //********************************************************************
298 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
300 myIsActive = toActivate;
302 myTextLine->setPalette(myBasePalet);
304 myTextLine->setPalette(myInactivePalet);
305 updateSelectionName();
308 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
309 if (myUseSubShapes) {
311 foreach (QString aType, myShapeTypes)
312 aList.append(shapeType(aType));
313 myWorkshop->activateSubShapesSelection(aList);
316 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
318 myWorkshop->deactivateSubShapesSelection();
322 //********************************************************************
323 void ModuleBase_WidgetShapeSelector::raisePanel() const
325 QWidget* aParent = myContainer->parentWidget();
326 QWidget* aLastPanel = 0;
327 while (!aParent->inherits("QDockWidget")) {
328 aLastPanel = aParent;
329 aParent = aParent->parentWidget();
333 if (aParent->inherits("QDockWidget")) {
334 QDockWidget* aTabWgt = (QDockWidget*) aParent;
339 //********************************************************************
340 bool ModuleBase_WidgetShapeSelector::focusTo()
342 activateSelection(true);
346 //********************************************************************
347 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
349 if (theObj == myTextLine) {
350 if (theEvent->type() == QEvent::FocusIn)
351 activateSelection(true);
353 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
356 //********************************************************************
357 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
360 ModuleBase_WidgetValueFeature* aFeatureValue =
361 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
362 if (aFeatureValue && aFeatureValue->object()) {
363 ObjectPtr aObject = aFeatureValue->object();
364 if (isAccepted(aObject)) {