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_Tools.h>
9 #include <Events_Loop.h>
10 #include <ModelAPI_Events.h>
11 #include <ModelAPI_Tools.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_Result.h>
16 #include <ModelAPI_AttributeReference.h>
17 #include <Config_WidgetAPI.h>
19 #include <GeomAPI_Shape.h>
21 #include <TopoDS_Shape.hxx>
22 #include <TopExp_Explorer.hxx>
28 #include <QToolButton>
31 #include <QDockWidget>
35 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
36 static ShapeTypes MyShapeTypes;
38 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
40 if (MyShapeTypes.count() == 0) {
41 MyShapeTypes["face"] = TopAbs_FACE;
42 MyShapeTypes["vertex"] = TopAbs_VERTEX;
43 MyShapeTypes["wire"] = TopAbs_WIRE;
44 MyShapeTypes["edge"] = TopAbs_EDGE;
45 MyShapeTypes["shell"] = TopAbs_SHELL;
46 MyShapeTypes["solid"] = TopAbs_SOLID;
48 if (MyShapeTypes.contains(theType))
49 return MyShapeTypes[theType];
50 throw std::invalid_argument("Shape type defined in XML is not implemented!");
53 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
54 ModuleBase_IWorkshop* theWorkshop,
55 const Config_WidgetAPI* theData,
56 const std::string& theParentId)
57 : ModuleBase_ModelWidget(theParent, theData, theParentId),
58 myWorkshop(theWorkshop), myIsActive(false)
60 myContainer = new QWidget(theParent);
61 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
62 ModuleBase_Tools::adjustMargins(aLayout);
64 QString aLabelText = QString::fromStdString(theData->widgetLabel());
65 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
66 myLabel = new QLabel(aLabelText, myContainer);
67 if (!aLabelIcon.isEmpty())
68 myLabel->setPixmap(QPixmap(aLabelIcon));
70 aLayout->addWidget(myLabel);
72 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
73 myTextLine = new QLineEdit(myContainer);
74 myTextLine->setReadOnly(true);
75 myTextLine->setToolTip(aToolTip);
77 myBasePalet = myTextLine->palette();
78 myInactivePalet = myBasePalet;
79 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
80 myTextLine->setPalette(myInactivePalet);
82 aLayout->addWidget(myTextLine, 1);
84 std::string aTypes = theData->getProperty("shape_types");
85 myShapeTypes = QString(aTypes.c_str()).split(' ');
88 //********************************************************************
89 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
93 //********************************************************************
94 bool ModuleBase_WidgetShapeSelector::storeValue() const
96 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
97 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
100 DataPtr aData = myFeature->data();
101 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
102 ModelAPI_AttributeReference>(aData->attribute(attributeID()));
104 ObjectPtr aObject = aRef->value();
105 if (!(aObject && aObject->isSame(mySelectedObject))) {
106 aRef->setValue(mySelectedObject);
107 updateObject(myFeature);
112 //********************************************************************
113 bool ModuleBase_WidgetShapeSelector::restoreValue()
115 DataPtr aData = myFeature->data();
116 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
118 bool isBlocked = this->blockSignals(true);
119 mySelectedObject = aRef->value();
120 updateSelectionName();
122 this->blockSignals(isBlocked);
126 //********************************************************************
127 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
129 QList<QWidget*> aControls;
130 aControls.append(myLabel);
131 aControls.append(myTextLine);
135 //********************************************************************
136 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
138 QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
139 if (aObjects.size() > 0) {
140 ObjectPtr aObject = aObjects.first();
141 if ((!mySelectedObject) && (!aObject))
143 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
146 // Check that the selection corresponds to selection type
147 if (!isAccepted(aObject))
150 mySelectedObject = aObject;
151 if (mySelectedObject) {
152 updateSelectionName();
154 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
155 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
156 Events_Loop::loop()->flush(anEvent);
158 myTextLine->setText("");
160 activateSelection(false);
161 emit valuesChanged();
162 emit focusOutWidget(this);
166 //********************************************************************
167 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
169 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
171 // We can not select a result of our feature
172 const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
173 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
174 for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
175 if ((*aIt) == aResult)
179 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
182 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
186 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
187 if (aShapeType == TopAbs_COMPOUND) {
188 foreach (QString aType, myShapeTypes) {
189 TopExp_Explorer aEx(aShape, shapeType(aType));
194 foreach (QString aType, myShapeTypes) {
195 if (shapeType(aType) == aShapeType)
202 //********************************************************************
203 void ModuleBase_WidgetShapeSelector::updateSelectionName()
205 if (mySelectedObject) {
206 std::string aName = mySelectedObject->data()->name();
207 myTextLine->setText(QString::fromStdString(aName));
210 QString aMsg = tr("Select a ");
212 foreach (QString aType, myShapeTypes) {
218 myTextLine->setText(aMsg);
220 myTextLine->setText(tr("No object selected"));
225 //********************************************************************
226 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
228 myIsActive = toActivate;
230 myTextLine->setPalette(myBasePalet);
232 myTextLine->setPalette(myInactivePalet);
233 updateSelectionName();
236 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
238 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
240 // if (myWorkshop->selectedObjects().size() > 0)
241 // onSelectionChanged();
244 //********************************************************************
245 void ModuleBase_WidgetShapeSelector::raisePanel() const
247 QWidget* aParent = myContainer->parentWidget();
248 QWidget* aLastPanel = 0;
249 while (!aParent->inherits("QDockWidget")) {
250 aLastPanel = aParent;
251 aParent = aParent->parentWidget();
255 if (aParent->inherits("QDockWidget")) {
256 QDockWidget* aTabWgt = (QDockWidget*) aParent;
261 //********************************************************************
262 bool ModuleBase_WidgetShapeSelector::focusTo()
264 activateSelection(true);
268 //********************************************************************
269 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
271 if (theObj == myTextLine) {
272 if (theEvent->type() == QEvent::FocusIn)
273 activateSelection(true);
275 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);