1 // File: ModuleBase_WidgetShapeSelector.h
2 // Created: 2 June 2014
3 // Author: Vitaly Smetannikov
5 #include "ModuleBase_WidgetShapeSelector.h"
6 #include "ModuleBase_IWorkshop.h"
8 #include <Events_Loop.h>
9 #include <ModelAPI_Events.h>
10 #include <ModelAPI_Tools.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_Object.h>
14 #include <ModelAPI_Result.h>
15 #include <ModelAPI_AttributeReference.h>
16 #include <Config_WidgetAPI.h>
18 #include <GeomAPI_Shape.h>
20 #include <TopoDS_Shape.hxx>
21 #include <TopExp_Explorer.hxx>
27 #include <QToolButton>
30 #include <QDockWidget>
34 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
35 static ShapeTypes MyShapeTypes;
37 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
39 if (MyShapeTypes.count() == 0) {
40 MyShapeTypes["face"] = TopAbs_FACE;
41 MyShapeTypes["vertex"] = TopAbs_VERTEX;
42 MyShapeTypes["wire"] = TopAbs_WIRE;
43 MyShapeTypes["edge"] = TopAbs_EDGE;
44 MyShapeTypes["shell"] = TopAbs_SHELL;
45 MyShapeTypes["solid"] = TopAbs_SOLID;
47 if (MyShapeTypes.contains(theType))
48 return MyShapeTypes[theType];
49 throw std::invalid_argument("Shape type defined in XML is not implemented!");
52 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
53 ModuleBase_IWorkshop* theWorkshop,
54 const Config_WidgetAPI* theData,
55 const std::string& theParentId)
56 : ModuleBase_ModelWidget(theParent, theData, theParentId),
57 myWorkshop(theWorkshop), myIsActive(false)
59 myContainer = new QWidget(theParent);
60 QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
62 aLayout->setContentsMargins(0, 0, 0, 0);
63 QString aLabelText = QString::fromStdString(theData->widgetLabel());
64 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
65 myLabel = new QLabel(aLabelText, myContainer);
66 if (!aLabelIcon.isEmpty())
67 myLabel->setPixmap(QPixmap(aLabelIcon));
69 aLayout->addWidget(myLabel);
71 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
72 myTextLine = new QLineEdit(myContainer);
73 myTextLine->setReadOnly(true);
74 myTextLine->setToolTip(aToolTip);
75 processFocus(myTextLine);
76 myTextLine->installEventFilter(this);
78 myBasePalet = myTextLine->palette();
79 myInactivePalet = myBasePalet;
80 myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
81 myTextLine->setPalette(myInactivePalet);
83 aLayout->addWidget(myTextLine, 1);
85 std::string aTypes = theData->getProperty("shape_types");
86 myShapeTypes = QString(aTypes.c_str()).split(' ');
89 //********************************************************************
90 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
94 //********************************************************************
95 bool ModuleBase_WidgetShapeSelector::storeValue() const
97 FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
98 if (aSelectedFeature == myFeature) // In order to avoid selection of the same object
101 DataPtr aData = myFeature->data();
102 boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
103 ModelAPI_AttributeReference>(aData->attribute(attributeID()));
105 ObjectPtr aObject = aRef->value();
106 if (!(aObject && aObject->isSame(mySelectedObject))) {
107 aRef->setValue(mySelectedObject);
108 updateObject(myFeature);
113 //********************************************************************
114 bool ModuleBase_WidgetShapeSelector::restoreValue()
116 DataPtr aData = myFeature->data();
117 boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
119 bool isBlocked = this->blockSignals(true);
120 mySelectedObject = aRef->value();
121 updateSelectionName();
123 this->blockSignals(isBlocked);
127 //********************************************************************
128 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
130 QList<QWidget*> aControls;
131 aControls.append(myLabel);
132 aControls.append(myTextLine);
136 //********************************************************************
137 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
139 QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
140 if (aObjects.size() > 0) {
141 ObjectPtr aObject = aObjects.first();
142 if ((!mySelectedObject) && (!aObject))
144 if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
147 // Check that the selection corresponds to selection type
148 if (!isAccepted(aObject))
151 mySelectedObject = aObject;
152 if (mySelectedObject) {
153 updateSelectionName();
155 static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
156 ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
157 Events_Loop::loop()->flush(anEvent);
159 myTextLine->setText("");
161 activateSelection(false);
162 emit valuesChanged();
163 emit focusOutWidget(this);
167 //********************************************************************
168 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
170 ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
171 boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
174 TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
178 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
179 if (aShapeType == TopAbs_COMPOUND) {
180 foreach (QString aType, myShapeTypes) {
181 TopExp_Explorer aEx(aShape, shapeType(aType));
186 foreach (QString aType, myShapeTypes) {
187 if (shapeType(aType) == aShapeType)
194 //********************************************************************
195 void ModuleBase_WidgetShapeSelector::updateSelectionName()
197 if (mySelectedObject) {
198 std::string aName = mySelectedObject->data()->name();
199 myTextLine->setText(QString::fromStdString(aName));
202 myTextLine->setText(tr("Select an object"));
204 myTextLine->setText(tr("No object selected"));
209 //********************************************************************
210 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
212 myIsActive = toActivate;
214 myTextLine->setPalette(myBasePalet);
216 myTextLine->setPalette(myInactivePalet);
217 updateSelectionName();
220 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
222 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
225 //********************************************************************
226 void ModuleBase_WidgetShapeSelector::raisePanel() const
228 QWidget* aParent = myContainer->parentWidget();
229 QWidget* aLastPanel = 0;
230 while (!aParent->inherits("QDockWidget")) {
231 aLastPanel = aParent;
232 aParent = aParent->parentWidget();
236 if (aParent->inherits("QDockWidget")) {
237 QDockWidget* aTabWgt = (QDockWidget*) aParent;
242 //********************************************************************
243 bool ModuleBase_WidgetShapeSelector::focusTo()
245 activateSelection(true);
249 //********************************************************************
250 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
252 if (theObj == myTextLine) {
253 if (theEvent->type() == QEvent::FocusIn)
254 activateSelection(true);
256 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);