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 if (MyShapeTypes.contains(theType))
53 return MyShapeTypes[theType];
54 throw std::invalid_argument("Shape type defined in XML is not implemented!");
57 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
58 ModuleBase_IWorkshop* theWorkshop,
59 const Config_WidgetAPI* theData,
60 const std::string& theParentId)
61 : ModuleBase_ModelWidget(theParent, theData, theParentId),
62 myWorkshop(theWorkshop), myIsActive(false)
64 myContainer = new QWidget(theParent);
65 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
66 ModuleBase_Tools::adjustMargins(aLayout);
68 QString aLabelText = QString::fromStdString(theData->widgetLabel());
69 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
70 myLabel = new QLabel(aLabelText, myContainer);
71 if (!aLabelIcon.isEmpty())
72 myLabel->setPixmap(QPixmap(aLabelIcon));
74 aLayout->addWidget(myLabel);
76 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
77 myTextLine = new QLineEdit(myContainer);
78 myTextLine->setReadOnly(true);
79 myTextLine->setToolTip(aToolTip);
80 myTextLine->installEventFilter(this);
82 myBasePalet = myTextLine->palette();
83 myInactivePalet = myBasePalet;
84 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
85 myTextLine->setPalette(myInactivePalet);
87 aLayout->addWidget(myTextLine, 1);
89 std::string aTypes = theData->getProperty("shape_types");
90 myShapeTypes = QString(aTypes.c_str()).split(' ');
93 //********************************************************************
94 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
98 //********************************************************************
99 bool ModuleBase_WidgetShapeSelector::storeValue() const
101 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
102 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
105 DataPtr aData = myFeature->data();
106 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
107 ModelAPI_AttributeReference>(aData->attribute(attributeID()));
109 ObjectPtr aObject = aRef->value();
110 if (!(aObject && aObject->isSame(mySelectedObject))) {
111 aRef->setValue(mySelectedObject);
112 updateObject(myFeature);
117 //********************************************************************
118 bool ModuleBase_WidgetShapeSelector::restoreValue()
120 DataPtr aData = myFeature->data();
121 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
123 bool isBlocked = this->blockSignals(true);
124 mySelectedObject = aRef->value();
125 updateSelectionName();
127 this->blockSignals(isBlocked);
131 //********************************************************************
132 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
134 QList<QWidget*> aControls;
135 aControls.append(myLabel);
136 aControls.append(myTextLine);
140 //********************************************************************
141 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
143 QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
144 if (aObjects.size() > 0) {
145 ObjectPtr aObject = aObjects.first();
146 if ((!mySelectedObject) && (!aObject))
148 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
151 // Check that the selection corresponds to selection type
152 if (!isAccepted(aObject))
156 emit focusOutWidget(this);
160 //********************************************************************
161 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj)
163 if (mySelectedObject == theObj)
165 mySelectedObject = theObj;
166 if (mySelectedObject) {
168 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
169 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
170 Events_Loop::loop()->flush(anEvent);
172 updateSelectionName();
173 activateSelection(false);
174 emit valuesChanged();
177 //********************************************************************
178 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
180 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
182 // We can not select a result of our feature
183 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
184 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
185 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
186 if ((*aIt) == aResult)
190 // Check that object belongs to active document or PartSet
191 DocumentPtr aDoc = aResult->document();
192 SessionPtr aMgr = ModelAPI_Session::get();
193 if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
196 // Check that the shape of necessary type
197 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
200 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
204 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
205 if (aShapeType == TopAbs_COMPOUND) {
206 foreach (QString aType, myShapeTypes) {
207 TopExp_Explorer aEx(aShape, shapeType(aType));
212 foreach (QString aType, myShapeTypes) {
213 if (shapeType(aType) == aShapeType)
220 //********************************************************************
221 void ModuleBase_WidgetShapeSelector::updateSelectionName()
223 if (mySelectedObject) {
224 std::string aName = mySelectedObject->data()->name();
225 myTextLine->setText(QString::fromStdString(aName));
228 QString aMsg = tr("Select a ");
230 foreach (QString aType, myShapeTypes) {
236 myTextLine->setText(aMsg);
238 myTextLine->setText(tr("No object selected"));
243 //********************************************************************
244 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
246 myIsActive = toActivate;
248 myTextLine->setPalette(myBasePalet);
250 myTextLine->setPalette(myInactivePalet);
251 updateSelectionName();
254 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
256 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
259 //********************************************************************
260 void ModuleBase_WidgetShapeSelector::raisePanel() const
262 QWidget* aParent = myContainer->parentWidget();
263 QWidget* aLastPanel = 0;
264 while (!aParent->inherits("QDockWidget")) {
265 aLastPanel = aParent;
266 aParent = aParent->parentWidget();
270 if (aParent->inherits("QDockWidget")) {
271 QDockWidget* aTabWgt = (QDockWidget*) aParent;
276 //********************************************************************
277 bool ModuleBase_WidgetShapeSelector::focusTo()
279 activateSelection(true);
283 //********************************************************************
284 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
286 if (theObj == myTextLine) {
287 if (theEvent->type() == QEvent::FocusIn)
288 activateSelection(true);
290 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
293 //********************************************************************
294 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
297 ModuleBase_WidgetValueFeature* aFeatureValue =
298 dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
299 if (aFeatureValue && aFeatureValue->object()) {
300 ObjectPtr aObject = aFeatureValue->object();
301 if (isAccepted(aObject)) {