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 <Config_WidgetAPI.h>
23 #include <GeomAPI_Shape.h>
25 #include <TopoDS_Shape.hxx>
26 #include <TopExp_Explorer.hxx>
32 #include <QToolButton>
35 #include <QDockWidget>
39 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
40 static ShapeTypes MyShapeTypes;
42 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
44 if (MyShapeTypes.count() == 0) {
45 MyShapeTypes["face"] = TopAbs_FACE;
46 MyShapeTypes["vertex"] = TopAbs_VERTEX;
47 MyShapeTypes["wire"] = TopAbs_WIRE;
48 MyShapeTypes["edge"] = TopAbs_EDGE;
49 MyShapeTypes["shell"] = TopAbs_SHELL;
50 MyShapeTypes["solid"] = TopAbs_SOLID;
52 QString aType = theType.toLower();
53 if (MyShapeTypes.contains(aType))
54 return MyShapeTypes[aType];
55 throw std::invalid_argument("Shape type defined in XML is not implemented!");
58 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
59 ModuleBase_IWorkshop* theWorkshop,
60 const Config_WidgetAPI* theData,
61 const std::string& theParentId)
62 : ModuleBase_ModelWidget(theParent, theData, theParentId),
63 myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
65 myContainer = new QWidget(theParent);
66 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
67 ModuleBase_Tools::adjustMargins(aLayout);
69 QString aLabelText = QString::fromStdString(theData->widgetLabel());
70 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
71 myLabel = new QLabel(aLabelText, myContainer);
72 if (!aLabelIcon.isEmpty())
73 myLabel->setPixmap(QPixmap(aLabelIcon));
75 aLayout->addWidget(myLabel);
77 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
78 myTextLine = new QLineEdit(myContainer);
79 myTextLine->setReadOnly(true);
80 myTextLine->setToolTip(aToolTip);
81 myTextLine->installEventFilter(this);
83 myBasePalet = myTextLine->palette();
84 myInactivePalet = myBasePalet;
85 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
86 myTextLine->setPalette(myInactivePalet);
88 aLayout->addWidget(myTextLine, 1);
90 std::string aTypes = theData->getProperty("shape_types");
91 myShapeTypes = QString(aTypes.c_str()).split(' ');
93 std::string aUseSubShapes = theData->getProperty("use_subshapes");
94 if (aUseSubShapes.length() > 0) {
95 QString aVal(aUseSubShapes.c_str());
96 myUseSubShapes = (aVal.toUpper() == "TRUE");
100 //********************************************************************
101 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
105 //********************************************************************
106 bool ModuleBase_WidgetShapeSelector::storeValue() const
108 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
109 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
112 DataPtr aData = myFeature->data();
113 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
114 ModelAPI_AttributeReference>(aData->attribute(attributeID()));
116 ObjectPtr aObject = aRef->value();
117 if (!(aObject && aObject->isSame(mySelectedObject))) {
118 aRef->setValue(mySelectedObject);
119 updateObject(myFeature);
124 //********************************************************************
125 bool ModuleBase_WidgetShapeSelector::restoreValue()
127 DataPtr aData = myFeature->data();
128 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
130 bool isBlocked = this->blockSignals(true);
131 mySelectedObject = aRef->value();
132 updateSelectionName();
134 this->blockSignals(isBlocked);
138 //********************************************************************
139 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
141 QList<QWidget*> aControls;
142 aControls.append(myLabel);
143 aControls.append(myTextLine);
147 //********************************************************************
148 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
150 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
151 if (aObjects.size() > 0) {
152 ObjectPtr aObject = aObjects.first();
153 if ((!mySelectedObject) && (!aObject))
155 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
158 // Check that the selection corresponds to selection type
159 if (!isAccepted(aObject))
163 emit focusOutWidget(this);
167 //********************************************************************
168 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj)
170 if (mySelectedObject == theObj)
172 mySelectedObject = theObj;
173 if (mySelectedObject) {
175 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
176 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
177 Events_Loop::loop()->flush(anEvent);
179 updateSelectionName();
180 activateSelection(false);
181 emit valuesChanged();
184 //********************************************************************
185 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
187 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
189 // We can not select a result of our feature
190 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
191 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
192 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
193 if ((*aIt) == aResult)
197 // Check that object belongs to active document or PartSet
198 DocumentPtr aDoc = aResult->document();
199 SessionPtr aMgr = ModelAPI_Session::get();
200 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
203 // Check that the shape of necessary type
204 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
207 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
211 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
212 if (aShapeType == TopAbs_COMPOUND) {
213 foreach (QString aType, myShapeTypes) {
214 TopExp_Explorer aEx(aShape, shapeType(aType));
219 foreach (QString aType, myShapeTypes) {
220 if (shapeType(aType) == aShapeType)
227 //********************************************************************
228 void ModuleBase_WidgetShapeSelector::updateSelectionName()
230 if (mySelectedObject) {
231 std::string aName = mySelectedObject->data()->name();
232 myTextLine->setText(QString::fromStdString(aName));
235 QString aMsg = tr("Select a ");
237 foreach (QString aType, myShapeTypes) {
243 myTextLine->setText(aMsg);
245 myTextLine->setText(tr("No object selected"));
250 //********************************************************************
251 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
253 myIsActive = toActivate;
255 myTextLine->setPalette(myBasePalet);
257 myTextLine->setPalette(myInactivePalet);
258 updateSelectionName();
261 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
262 if (myUseSubShapes) {
264 foreach (QString aType, myShapeTypes)
265 aList.append(shapeType(aType));
266 myWorkshop->activateSubShapesSelection(aList);
269 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
271 myWorkshop->deactivateSubShapesSelection();
275 //********************************************************************
276 void ModuleBase_WidgetShapeSelector::raisePanel() const
278 QWidget* aParent = myContainer->parentWidget();
279 QWidget* aLastPanel = 0;
280 while (!aParent->inherits("QDockWidget")) {
281 aLastPanel = aParent;
282 aParent = aParent->parentWidget();
286 if (aParent->inherits("QDockWidget")) {
287 QDockWidget* aTabWgt = (QDockWidget*) aParent;
292 //********************************************************************
293 bool ModuleBase_WidgetShapeSelector::focusTo()
295 activateSelection(true);
299 //********************************************************************
300 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
302 if (theObj == myTextLine) {
303 if (theEvent->type() == QEvent::FocusIn)
304 activateSelection(true);
306 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
309 //********************************************************************
310 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
313 ModuleBase_WidgetValueFeature* aFeatureValue =
314 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
315 if (aFeatureValue && aFeatureValue->object()) {
316 ObjectPtr aObject = aFeatureValue->object();
317 if (isAccepted(aObject)) {