Salome HOME
Issue #144 make sources compilable: processFocus is not required anymore
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // File:        ModuleBase_WidgetShapeSelector.h
2 // Created:     2 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include "ModuleBase_WidgetShapeSelector.h"
6 #include "ModuleBase_IWorkshop.h"
7
8 #include <Events_Loop.h>
9 #include <ModelAPI_Events.h>
10 #include <ModelAPI_Tools.h>
11
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>
17
18 #include <GeomAPI_Shape.h>
19
20 #include <TopoDS_Shape.hxx>
21 #include <TopExp_Explorer.hxx>
22
23 #include <QWidget>
24 #include <QLayout>
25 #include <QLabel>
26 #include <QLineEdit>
27 #include <QToolButton>
28 #include <QString>
29 #include <QEvent>
30 #include <QDockWidget>
31
32 #include <stdexcept>
33
34 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
35 static ShapeTypes MyShapeTypes;
36
37 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
38 {
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;
46   }
47   if (MyShapeTypes.contains(theType))
48     return MyShapeTypes[theType];
49   throw std::invalid_argument("Shape type defined in XML is not implemented!");
50 }
51
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)
58 {
59   myContainer = new QWidget(theParent);
60   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
61
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));
68
69   aLayout->addWidget(myLabel);
70
71   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
72   myTextLine = new QLineEdit(myContainer);
73   myTextLine->setReadOnly(true);
74   myTextLine->setToolTip(aToolTip);
75   myTextLine->installEventFilter(this);
76
77   myBasePalet = myTextLine->palette();
78   myInactivePalet = myBasePalet;
79   myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
80   myTextLine->setPalette(myInactivePalet);
81
82   aLayout->addWidget(myTextLine, 1);
83
84   std::string aTypes = theData->getProperty("shape_types");
85   myShapeTypes = QString(aTypes.c_str()).split(' ');
86 }
87
88 //********************************************************************
89 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
90 {
91 }
92
93 //********************************************************************
94 bool ModuleBase_WidgetShapeSelector::storeValue() const
95 {
96   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
97   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
98     return false;
99
100   DataPtr aData = myFeature->data();
101   boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
102       ModelAPI_AttributeReference>(aData->attribute(attributeID()));
103
104   ObjectPtr aObject = aRef->value();
105   if (!(aObject && aObject->isSame(mySelectedObject))) {
106     aRef->setValue(mySelectedObject);
107     updateObject(myFeature);
108   }
109   return true;
110 }
111
112 //********************************************************************
113 bool ModuleBase_WidgetShapeSelector::restoreValue()
114 {
115   DataPtr aData = myFeature->data();
116   boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
117
118   bool isBlocked = this->blockSignals(true);
119   mySelectedObject = aRef->value();
120   updateSelectionName();
121
122   this->blockSignals(isBlocked);
123   return true;
124 }
125
126 //********************************************************************
127 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
128 {
129   QList<QWidget*> aControls;
130   aControls.append(myLabel);
131   aControls.append(myTextLine);
132   return aControls;
133 }
134
135 //********************************************************************
136 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
137 {
138   QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
139   if (aObjects.size() > 0) {
140     ObjectPtr aObject = aObjects.first();
141     if ((!mySelectedObject) && (!aObject))
142       return;
143     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
144       return;
145
146     // Check that the selection corresponds to selection type
147     if (!isAccepted(aObject))
148       return;
149
150     mySelectedObject = aObject;
151     if (mySelectedObject) {
152       updateSelectionName();
153       raisePanel();
154       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
155       ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
156       Events_Loop::loop()->flush(anEvent);
157     } else {
158       myTextLine->setText("");
159     }
160     activateSelection(false);
161     emit valuesChanged();
162     emit focusOutWidget(this);
163   }
164 }
165
166 //********************************************************************
167 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
168 {
169   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
170   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
171   if (!aShapePtr)
172     return false;
173   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
174   if (aShape.IsNull())
175     return false;
176
177   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
178   if (aShapeType == TopAbs_COMPOUND) {
179     foreach (QString aType, myShapeTypes) {
180       TopExp_Explorer aEx(aShape, shapeType(aType));
181       if (aEx.More())
182         return true;
183     }
184   } else {
185     foreach (QString aType, myShapeTypes) {
186       if (shapeType(aType) == aShapeType)
187         return true;
188     }
189   }
190   return false;
191 }
192
193 //********************************************************************
194 void ModuleBase_WidgetShapeSelector::updateSelectionName()
195 {
196   if (mySelectedObject) {
197     std::string aName = mySelectedObject->data()->name();
198     myTextLine->setText(QString::fromStdString(aName));
199   } else {
200     if (myIsActive)
201       myTextLine->setText(tr("Select an object"));
202     else
203       myTextLine->setText(tr("No object selected"));
204   }
205 }
206
207
208 //********************************************************************
209 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
210 {
211   myIsActive = toActivate;
212   if (myIsActive)
213     myTextLine->setPalette(myBasePalet);
214   else
215     myTextLine->setPalette(myInactivePalet);
216   updateSelectionName();
217
218   if (myIsActive)
219     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
220   else
221     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
222 }
223
224 //********************************************************************
225 void ModuleBase_WidgetShapeSelector::raisePanel() const
226 {
227   QWidget* aParent = myContainer->parentWidget();
228   QWidget* aLastPanel = 0;
229   while (!aParent->inherits("QDockWidget")) {
230     aLastPanel = aParent;
231     aParent = aParent->parentWidget();
232     if (!aParent)
233       return;
234   }
235   if (aParent->inherits("QDockWidget")) {
236     QDockWidget* aTabWgt = (QDockWidget*) aParent;
237     aTabWgt->raise();
238   }
239 }
240
241 //********************************************************************
242 bool ModuleBase_WidgetShapeSelector::focusTo()
243 {
244   activateSelection(true);
245   return true;
246 }
247
248 //********************************************************************
249 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
250 {
251   if (theObj == myTextLine) {
252     if (theEvent->type() == QEvent::FocusIn)
253       activateSelection(true);
254   }
255   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
256 }