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)
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(' ');
94 //********************************************************************
95 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
99 //********************************************************************
100 bool ModuleBase_WidgetShapeSelector::storeValue() const
102 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
103 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
106 DataPtr aData = myFeature->data();
107 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
108 ModelAPI_AttributeReference>(aData->attribute(attributeID()));
110 ObjectPtr aObject = aRef->value();
111 if (!(aObject && aObject->isSame(mySelectedObject))) {
112 aRef->setValue(mySelectedObject);
113 updateObject(myFeature);
118 //********************************************************************
119 bool ModuleBase_WidgetShapeSelector::restoreValue()
121 DataPtr aData = myFeature->data();
122 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
124 bool isBlocked = this->blockSignals(true);
125 mySelectedObject = aRef->value();
126 updateSelectionName();
128 this->blockSignals(isBlocked);
132 //********************************************************************
133 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
135 QList<QWidget*> aControls;
136 aControls.append(myLabel);
137 aControls.append(myTextLine);
141 //********************************************************************
142 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
144 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
145 if (aObjects.size() > 0) {
146 ObjectPtr aObject = aObjects.first();
147 if ((!mySelectedObject) && (!aObject))
149 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
152 // Check that the selection corresponds to selection type
153 if (!isAccepted(aObject))
157 emit focusOutWidget(this);
161 //********************************************************************
162 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj)
164 if (mySelectedObject == theObj)
166 mySelectedObject = theObj;
167 if (mySelectedObject) {
169 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
170 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
171 Events_Loop::loop()->flush(anEvent);
173 updateSelectionName();
174 activateSelection(false);
175 emit valuesChanged();
178 //********************************************************************
179 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
181 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
183 // We can not select a result of our feature
184 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
185 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
186 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
187 if ((*aIt) == aResult)
191 // Check that object belongs to active document or PartSet
192 DocumentPtr aDoc = aResult->document();
193 SessionPtr aMgr = ModelAPI_Session::get();
194 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
197 // Check that the shape of necessary type
198 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
201 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
205 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
206 if (aShapeType == TopAbs_COMPOUND) {
207 foreach (QString aType, myShapeTypes) {
208 TopExp_Explorer aEx(aShape, shapeType(aType));
213 foreach (QString aType, myShapeTypes) {
214 if (shapeType(aType) == aShapeType)
221 //********************************************************************
222 void ModuleBase_WidgetShapeSelector::updateSelectionName()
224 if (mySelectedObject) {
225 std::string aName = mySelectedObject->data()->name();
226 myTextLine->setText(QString::fromStdString(aName));
229 QString aMsg = tr("Select a ");
231 foreach (QString aType, myShapeTypes) {
237 myTextLine->setText(aMsg);
239 myTextLine->setText(tr("No object selected"));
244 //********************************************************************
245 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
247 myIsActive = toActivate;
249 myTextLine->setPalette(myBasePalet);
251 myTextLine->setPalette(myInactivePalet);
252 updateSelectionName();
255 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
257 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
260 //********************************************************************
261 void ModuleBase_WidgetShapeSelector::raisePanel() const
263 QWidget* aParent = myContainer->parentWidget();
264 QWidget* aLastPanel = 0;
265 while (!aParent->inherits("QDockWidget")) {
266 aLastPanel = aParent;
267 aParent = aParent->parentWidget();
271 if (aParent->inherits("QDockWidget")) {
272 QDockWidget* aTabWgt = (QDockWidget*) aParent;
277 //********************************************************************
278 bool ModuleBase_WidgetShapeSelector::focusTo()
280 activateSelection(true);
284 //********************************************************************
285 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
287 if (theObj == myTextLine) {
288 if (theEvent->type() == QEvent::FocusIn)
289 activateSelection(true);
291 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
294 //********************************************************************
295 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
298 ModuleBase_WidgetValueFeature* aFeatureValue =
299 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
300 if (aFeatureValue && aFeatureValue->object()) {
301 ObjectPtr aObject = aFeatureValue->object();
302 if (isAccepted(aObject)) {