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_Object.h>
18 #include <ModelAPI_Result.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <Config_WidgetAPI.h>
22 #include <GeomAPI_Shape.h>
24 #include <TopoDS_Shape.hxx>
25 #include <TopExp_Explorer.hxx>
31 #include <QToolButton>
34 #include <QDockWidget>
38 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
39 static ShapeTypes MyShapeTypes;
41 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
43 if (MyShapeTypes.count() == 0) {
44 MyShapeTypes["face"] = TopAbs_FACE;
45 MyShapeTypes["vertex"] = TopAbs_VERTEX;
46 MyShapeTypes["wire"] = TopAbs_WIRE;
47 MyShapeTypes["edge"] = TopAbs_EDGE;
48 MyShapeTypes["shell"] = TopAbs_SHELL;
49 MyShapeTypes["solid"] = TopAbs_SOLID;
51 if (MyShapeTypes.contains(theType))
52 return MyShapeTypes[theType];
53 throw std::invalid_argument("Shape type defined in XML is not implemented!");
56 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
57 ModuleBase_IWorkshop* theWorkshop,
58 const Config_WidgetAPI* theData,
59 const std::string& theParentId)
60 : ModuleBase_ModelWidget(theParent, theData, theParentId),
61 myWorkshop(theWorkshop), myIsActive(false)
63 myContainer = new QWidget(theParent);
64 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
65 ModuleBase_Tools::adjustMargins(aLayout);
67 QString aLabelText = QString::fromStdString(theData->widgetLabel());
68 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
69 myLabel = new QLabel(aLabelText, myContainer);
70 if (!aLabelIcon.isEmpty())
71 myLabel->setPixmap(QPixmap(aLabelIcon));
73 aLayout->addWidget(myLabel);
75 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
76 myTextLine = new QLineEdit(myContainer);
77 myTextLine->setReadOnly(true);
78 myTextLine->setToolTip(aToolTip);
79 myTextLine->installEventFilter(this);
81 myBasePalet = myTextLine->palette();
82 myInactivePalet = myBasePalet;
83 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
84 myTextLine->setPalette(myInactivePalet);
86 aLayout->addWidget(myTextLine, 1);
88 std::string aTypes = theData->getProperty("shape_types");
89 myShapeTypes = QString(aTypes.c_str()).split(' ');
92 //********************************************************************
93 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
97 //********************************************************************
98 bool ModuleBase_WidgetShapeSelector::storeValue() const
100 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
101 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
104 DataPtr aData = myFeature->data();
105 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
106 ModelAPI_AttributeReference>(aData->attribute(attributeID()));
108 ObjectPtr aObject = aRef->value();
109 if (!(aObject && aObject->isSame(mySelectedObject))) {
110 aRef->setValue(mySelectedObject);
111 updateObject(myFeature);
116 //********************************************************************
117 bool ModuleBase_WidgetShapeSelector::restoreValue()
119 DataPtr aData = myFeature->data();
120 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
122 bool isBlocked = this->blockSignals(true);
123 mySelectedObject = aRef->value();
124 updateSelectionName();
126 this->blockSignals(isBlocked);
130 //********************************************************************
131 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
133 QList<QWidget*> aControls;
134 aControls.append(myLabel);
135 aControls.append(myTextLine);
139 //********************************************************************
140 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
142 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
143 if (aObjects.size() > 0) {
144 ObjectPtr aObject = aObjects.first();
145 if ((!mySelectedObject) && (!aObject))
147 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
150 // Check that the selection corresponds to selection type
151 if (!isAccepted(aObject))
155 emit focusOutWidget(this);
159 //********************************************************************
160 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj)
162 if (mySelectedObject == theObj)
164 mySelectedObject = theObj;
165 if (mySelectedObject) {
167 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
168 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
169 Events_Loop::loop()->flush(anEvent);
171 updateSelectionName();
172 activateSelection(false);
173 emit valuesChanged();
176 //********************************************************************
177 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
179 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
181 // We can not select a result of our feature
182 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
183 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
184 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
185 if ((*aIt) == aResult)
189 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
192 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
196 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
197 if (aShapeType == TopAbs_COMPOUND) {
198 foreach (QString aType, myShapeTypes) {
199 TopExp_Explorer aEx(aShape, shapeType(aType));
204 foreach (QString aType, myShapeTypes) {
205 if (shapeType(aType) == aShapeType)
212 //********************************************************************
213 void ModuleBase_WidgetShapeSelector::updateSelectionName()
215 if (mySelectedObject) {
216 std::string aName = mySelectedObject->data()->name();
217 myTextLine->setText(QString::fromStdString(aName));
220 QString aMsg = tr("Select a ");
222 foreach (QString aType, myShapeTypes) {
228 myTextLine->setText(aMsg);
230 myTextLine->setText(tr("No object selected"));
235 //********************************************************************
236 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
238 myIsActive = toActivate;
240 myTextLine->setPalette(myBasePalet);
242 myTextLine->setPalette(myInactivePalet);
243 updateSelectionName();
246 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
248 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
251 //********************************************************************
252 void ModuleBase_WidgetShapeSelector::raisePanel() const
254 QWidget* aParent = myContainer->parentWidget();
255 QWidget* aLastPanel = 0;
256 while (!aParent->inherits("QDockWidget")) {
257 aLastPanel = aParent;
258 aParent = aParent->parentWidget();
262 if (aParent->inherits("QDockWidget")) {
263 QDockWidget* aTabWgt = (QDockWidget*) aParent;
268 //********************************************************************
269 bool ModuleBase_WidgetShapeSelector::focusTo()
271 activateSelection(true);
275 //********************************************************************
276 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
278 if (theObj == myTextLine) {
279 if (theEvent->type() == QEvent::FocusIn)
280 activateSelection(true);
282 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
285 //********************************************************************
286 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
289 ModuleBase_WidgetValueFeature* aFeatureValue =
290 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
291 if (aFeatureValue && aFeatureValue->object()) {
292 ObjectPtr aObject = aFeatureValue->object();
293 if (isAccepted(aObject)) {