]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
Salome HOME
Issie #204: Size of Fixed constraint depends on object size
[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_Definitions.h>
7 #include <ModuleBase_ISelection.h>
8 #include <ModuleBase_IWorkshop.h>
9 #include <ModuleBase_Tools.h>
10 #include <ModuleBase_WidgetValueFeature.h>
11 #include <Config_WidgetAPI.h>
12 #include <Events_Loop.h>
13 #include <Events_Message.h>
14 #include <GeomAPI_Interface.h>
15 #include <GeomAPI_Shape.h>
16 #include <ModelAPI_AttributeReference.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Events.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Result.h>
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_AttributeSelection.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <Config_WidgetAPI.h>
28 #include <Events_Error.h>
29
30 #include <GeomAPI_Shape.h>
31
32 #include <TopoDS_Shape.hxx>
33 #include <TopExp_Explorer.hxx>
34
35 #include <QWidget>
36 #include <QLayout>
37 #include <QLabel>
38 #include <QLineEdit>
39 #include <QToolButton>
40 #include <QString>
41 #include <QEvent>
42 #include <QDockWidget>
43
44 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Shape.hxx>
46
47 #include <boost/smart_ptr/shared_ptr.hpp>
48
49 #include <list>
50 #include <string>
51
52 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
53 static ShapeTypes MyShapeTypes;
54
55 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
56 {
57   if (MyShapeTypes.count() == 0) {
58     MyShapeTypes["face"] = TopAbs_FACE;
59     MyShapeTypes["faces"] = TopAbs_FACE;
60     MyShapeTypes["vertex"] = TopAbs_VERTEX;
61     MyShapeTypes["vertices"] = TopAbs_VERTEX;
62     MyShapeTypes["wire"] = TopAbs_WIRE;
63     MyShapeTypes["edge"] = TopAbs_EDGE;
64     MyShapeTypes["edges"] = TopAbs_EDGE;
65     MyShapeTypes["shell"] = TopAbs_SHELL;
66     MyShapeTypes["solid"] = TopAbs_SOLID;
67     MyShapeTypes["solids"] = TopAbs_SOLID;
68   }
69   QString aType = theType.toLower();
70   if (MyShapeTypes.contains(aType))
71     return MyShapeTypes[aType];
72   Events_Error::send("Shape type defined in XML is not implemented!");
73   return TopAbs_SHAPE;
74 }
75
76 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
77                                                      ModuleBase_IWorkshop* theWorkshop,
78                                                      const Config_WidgetAPI* theData,
79                                                      const std::string& theParentId)
80     : ModuleBase_ModelWidget(theParent, theData, theParentId),
81       myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
82 {
83   myContainer = new QWidget(theParent);
84   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
85   ModuleBase_Tools::adjustMargins(aLayout);
86
87   QString aLabelText = QString::fromStdString(theData->widgetLabel());
88   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
89   myLabel = new QLabel(aLabelText, myContainer);
90   if (!aLabelIcon.isEmpty())
91     myLabel->setPixmap(QPixmap(aLabelIcon));
92
93   aLayout->addWidget(myLabel);
94
95   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
96   myTextLine = new QLineEdit(myContainer);
97   myTextLine->setReadOnly(true);
98   myTextLine->setToolTip(aToolTip);
99   myTextLine->installEventFilter(this);
100
101   myBasePalet = myTextLine->palette();
102   myInactivePalet = myBasePalet;
103   myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
104   myTextLine->setPalette(myInactivePalet);
105
106   aLayout->addWidget(myTextLine, 1);
107
108   std::string aTypes = theData->getProperty("shape_types");
109   myShapeTypes = QString(aTypes.c_str()).split(' ');
110
111   myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); 
112 }
113
114 //********************************************************************
115 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
116 {
117   activateSelection(false);
118 }
119
120 //********************************************************************
121 bool ModuleBase_WidgetShapeSelector::storeValue() const
122 {
123   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
124   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
125     return false;
126
127   DataPtr aData = myFeature->data();
128   if (myUseSubShapes) {
129     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = 
130       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
131
132     ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
133     if (aBody) {
134       aSelect->setValue(aBody, myShape);
135       updateObject(myFeature);
136       return true;
137     }
138   } else {
139     boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
140       boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
141
142     ObjectPtr aObject = aRef->value();
143     if (!(aObject && aObject->isSame(mySelectedObject))) {
144       aRef->setValue(mySelectedObject);
145       updateObject(myFeature);
146       return true;
147     }
148   }
149   return false;
150 }
151
152 //********************************************************************
153 bool ModuleBase_WidgetShapeSelector::restoreValue()
154 {
155   DataPtr aData = myFeature->data();
156   bool isBlocked = this->blockSignals(true);
157   if (myUseSubShapes) {
158     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
159     if (aSelect) {
160       mySelectedObject = aSelect->context();
161       myShape = aSelect->value();
162     }
163   } else {
164     boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
165     mySelectedObject = aRef->value();
166   }
167   updateSelectionName();
168
169   this->blockSignals(isBlocked);
170   return true;
171 }
172
173 //********************************************************************
174 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
175 {
176   QList<QWidget*> aControls;
177   aControls.append(myLabel);
178   aControls.append(myTextLine);
179   return aControls;
180 }
181
182 //********************************************************************
183 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
184 {
185   QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
186   if (aObjects.size() > 0) {
187     ObjectPtr aObject = aObjects.first();
188     if ((!mySelectedObject) && (!aObject))
189       return;
190     // Check that object is not already selected
191     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
192       return;
193     // Check that the selected object is result (others can not be accepted)
194     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
195     if (!aRes)
196       return;
197
198     if (myFeature) {
199       // We can not select a result of our feature
200       const std::list<boost::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
201       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
202       for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
203         if ((*aIt) == aRes)
204           return;
205       }
206     }
207     // Check that object belongs to active document or PartSet
208     DocumentPtr aDoc = aRes->document();
209     SessionPtr aMgr = ModelAPI_Session::get();
210     if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
211       return;
212
213     // Check that the result has a shape
214     GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
215     if (!aShape)
216       return;
217
218     // Get sub-shapes from local selection
219     if (myUseSubShapes) {
220       NCollection_List<TopoDS_Shape> aShapeList;
221       std::list<ObjectPtr> aOwners;
222       myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
223       if (aShapeList.Extent() > 0) {
224         aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
225         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
226       }
227     }
228
229     // Check that the selection corresponds to selection type
230     if (myUseSubShapes) {
231       if (!isAccepted(aShape))
232         return;
233     } else {
234       if (!isAccepted(aObject))
235         return;
236     }
237     setObject(aObject, aShape);
238     //activateSelection(false);
239     emit focusOutWidget(this);
240   }
241 }
242
243 //********************************************************************
244 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
245 {
246   if (mySelectedObject == theObj)
247     return;
248   mySelectedObject = theObj;
249   myShape = theShape;
250   if (mySelectedObject) {
251     raisePanel();
252     if (!myUseSubShapes) {
253       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
254       ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
255     }
256   } 
257   updateSelectionName();
258   activateSelection(false);
259   emit valuesChanged();
260 }
261
262 //********************************************************************
263 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
264 {
265   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
266
267   // Check that the shape of necessary type
268   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
269   if (!aShapePtr)
270     return false;
271   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
272   if (aShape.IsNull())
273     return false;
274
275   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
276   if (aShapeType == TopAbs_COMPOUND) {
277     foreach (QString aType, myShapeTypes) {
278       TopExp_Explorer aEx(aShape, shapeType(aType));
279       if (aEx.More())
280         return true;
281     }
282   } else {
283     foreach (QString aType, myShapeTypes) {
284       if (shapeType(aType) == aShapeType)
285         return true;
286     }
287   }
288   return false;
289 }
290
291 //********************************************************************
292 bool ModuleBase_WidgetShapeSelector::isAccepted(boost::shared_ptr<GeomAPI_Shape> theShape) const
293 {
294   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
295   foreach (QString aType, myShapeTypes) {
296     if (aShape.ShapeType() == shapeType(aType))
297       return true;
298   }
299   return false;
300 }
301
302 //********************************************************************
303 void ModuleBase_WidgetShapeSelector::updateSelectionName()
304 {
305   if (mySelectedObject) {
306     std::string aName = mySelectedObject->data()->name();
307     myTextLine->setText(QString::fromStdString(aName));
308   } else {
309     if (myIsActive) {
310       QString aMsg = tr("Select a ");
311       int i = 0;
312       foreach (QString aType, myShapeTypes) {
313         if (i > 0)
314           aMsg += " or ";
315         aMsg += aType;
316         i++;
317       }
318       myTextLine->setText(aMsg);
319     } else
320       myTextLine->setText(tr("No object selected"));
321   }
322 }
323
324
325 //********************************************************************
326 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
327 {
328   myIsActive = toActivate;
329   if (myIsActive)
330     myTextLine->setPalette(myBasePalet);
331   else
332     myTextLine->setPalette(myInactivePalet);
333   updateSelectionName();
334
335   if (myIsActive) {
336     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
337     if (myUseSubShapes) {
338       QIntList aList;
339       foreach (QString aType, myShapeTypes)
340         aList.append(shapeType(aType));
341       myWorkshop->activateSubShapesSelection(aList);
342     }
343   } else {
344     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
345     if (myUseSubShapes) 
346       myWorkshop->deactivateSubShapesSelection();
347   }
348 }
349
350 //********************************************************************
351 void ModuleBase_WidgetShapeSelector::raisePanel() const
352 {
353   QWidget* aParent = myContainer->parentWidget();
354   QWidget* aLastPanel = 0;
355   while (!aParent->inherits("QDockWidget")) {
356     aLastPanel = aParent;
357     aParent = aParent->parentWidget();
358     if (!aParent)
359       return;
360   }
361   if (aParent->inherits("QDockWidget")) {
362     QDockWidget* aTabWgt = (QDockWidget*) aParent;
363     aTabWgt->raise();
364   }
365 }
366
367 //********************************************************************
368 bool ModuleBase_WidgetShapeSelector::focusTo()
369 {
370   activateSelection(true);
371   return true;
372 }
373
374 //********************************************************************
375 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
376 {
377   if (theObj == myTextLine) {
378     if (theEvent->type() == QEvent::FocusIn)
379       activateSelection(true);
380   }
381   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
382 }
383
384 //********************************************************************
385 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
386 {
387   if (theValue) {
388     ModuleBase_WidgetValueFeature* aFeatureValue =
389         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
390     if (aFeatureValue && aFeatureValue->object()) {
391       ObjectPtr aObject = aFeatureValue->object();
392       if (isAccepted(aObject)) {
393         setObject(aObject);
394         return true;
395       }
396     }
397   }
398   return false;
399 }
400