Salome HOME
84cfee206ca66ae7fa68db6fa094c59f7f6988fc
[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 <Config_WidgetAPI.h>
27 #include <Events_Error.h>
28
29 #include <GeomAPI_Shape.h>
30
31 #include <TopoDS_Shape.hxx>
32 #include <TopExp_Explorer.hxx>
33
34 #include <QWidget>
35 #include <QLayout>
36 #include <QLabel>
37 #include <QLineEdit>
38 #include <QToolButton>
39 #include <QString>
40 #include <QEvent>
41 #include <QDockWidget>
42
43 #include <TopExp_Explorer.hxx>
44 #include <TopoDS_Shape.hxx>
45
46 #include <boost/smart_ptr/shared_ptr.hpp>
47
48 #include <list>
49 #include <stdexcept>
50 #include <xstring>
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   } else {
136     boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
137       boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
138
139     ObjectPtr aObject = aRef->value();
140     if (!(aObject && aObject->isSame(mySelectedObject))) {
141       aRef->setValue(mySelectedObject);
142       updateObject(myFeature);
143     }
144   }
145   return true;
146 }
147
148 //********************************************************************
149 bool ModuleBase_WidgetShapeSelector::restoreValue()
150 {
151   DataPtr aData = myFeature->data();
152   bool isBlocked = this->blockSignals(true);
153   if (myUseSubShapes) {
154     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
155     if (aSelect) {
156       mySelectedObject = aSelect->context();
157       myShape = aSelect->value();
158     }
159   } else {
160     boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
161     mySelectedObject = aRef->value();
162   }
163   updateSelectionName();
164
165   this->blockSignals(isBlocked);
166   return true;
167 }
168
169 //********************************************************************
170 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
171 {
172   QList<QWidget*> aControls;
173   aControls.append(myLabel);
174   aControls.append(myTextLine);
175   return aControls;
176 }
177
178 //********************************************************************
179 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
180 {
181   QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
182   if (aObjects.size() > 0) {
183     ObjectPtr aObject = aObjects.first();
184     if ((!mySelectedObject) && (!aObject))
185       return;
186     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
187       return;
188
189     // Get sub-shapes from local selection
190     boost::shared_ptr<GeomAPI_Shape> aShape;
191     if (myUseSubShapes) {
192       NCollection_List<TopoDS_Shape> aShapeList;
193       myWorkshop->selection()->selectedShapes(aShapeList);
194       if (aShapeList.Extent() > 0) {
195         aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
196         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
197       }
198     }
199
200     // Check that the selection corresponds to selection type
201     if (myUseSubShapes) {
202       if (!isAccepted(aShape))
203         return;
204     } else {
205       if (!isAccepted(aObject))
206         return;
207     }
208     setObject(aObject, aShape);
209     emit focusOutWidget(this);
210   }
211 }
212
213 //********************************************************************
214 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
215 {
216   if (mySelectedObject == theObj)
217     return;
218   mySelectedObject = theObj;
219   myShape = theShape;
220   if (mySelectedObject) {
221     raisePanel();
222     if (!myUseSubShapes) {
223       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
224       ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
225       Events_Loop::loop()->flush(anEvent);
226     }
227   } 
228   updateSelectionName();
229   activateSelection(false);
230   emit valuesChanged();
231 }
232
233 //********************************************************************
234 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
235 {
236   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
237   if (myFeature) {
238     // We can not select a result of our feature
239     const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
240     std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
241     for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
242       if ((*aIt) == aResult)
243         return false;
244     }
245   }
246   // Check that object belongs to active document or PartSet
247   DocumentPtr aDoc = aResult->document();
248   SessionPtr aMgr = ModelAPI_Session::get();
249   if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
250     return false;
251
252   // Check that the shape of necessary type
253   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
254   if (!aShapePtr)
255     return false;
256   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
257   if (aShape.IsNull())
258     return false;
259
260   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
261   if (aShapeType == TopAbs_COMPOUND) {
262     foreach (QString aType, myShapeTypes) {
263       TopExp_Explorer aEx(aShape, shapeType(aType));
264       if (aEx.More())
265         return true;
266     }
267   } else {
268     foreach (QString aType, myShapeTypes) {
269       if (shapeType(aType) == aShapeType)
270         return true;
271     }
272   }
273   return false;
274 }
275
276 //********************************************************************
277 bool ModuleBase_WidgetShapeSelector::isAccepted(boost::shared_ptr<GeomAPI_Shape> theShape) const
278 {
279   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
280   foreach (QString aType, myShapeTypes) {
281     if (aShape.ShapeType() == shapeType(aType))
282       return true;
283   }
284   return false;
285 }
286
287 //********************************************************************
288 void ModuleBase_WidgetShapeSelector::updateSelectionName()
289 {
290   if (mySelectedObject) {
291     std::string aName = mySelectedObject->data()->name();
292     myTextLine->setText(QString::fromStdString(aName));
293   } else {
294     if (myIsActive) {
295       QString aMsg = tr("Select a ");
296       int i = 0;
297       foreach (QString aType, myShapeTypes) {
298         if (i > 0)
299           aMsg += " or ";
300         aMsg += aType;
301         i++;
302       }
303       myTextLine->setText(aMsg);
304     } else
305       myTextLine->setText(tr("No object selected"));
306   }
307 }
308
309
310 //********************************************************************
311 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
312 {
313   myIsActive = toActivate;
314   if (myIsActive)
315     myTextLine->setPalette(myBasePalet);
316   else
317     myTextLine->setPalette(myInactivePalet);
318   updateSelectionName();
319
320   if (myIsActive) {
321     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
322     if (myUseSubShapes) {
323       QIntList aList;
324       foreach (QString aType, myShapeTypes)
325         aList.append(shapeType(aType));
326       myWorkshop->activateSubShapesSelection(aList);
327     }
328   } else {
329     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
330     if (myUseSubShapes) 
331       myWorkshop->deactivateSubShapesSelection();
332   }
333 }
334
335 //********************************************************************
336 void ModuleBase_WidgetShapeSelector::raisePanel() const
337 {
338   QWidget* aParent = myContainer->parentWidget();
339   QWidget* aLastPanel = 0;
340   while (!aParent->inherits("QDockWidget")) {
341     aLastPanel = aParent;
342     aParent = aParent->parentWidget();
343     if (!aParent)
344       return;
345   }
346   if (aParent->inherits("QDockWidget")) {
347     QDockWidget* aTabWgt = (QDockWidget*) aParent;
348     aTabWgt->raise();
349   }
350 }
351
352 //********************************************************************
353 bool ModuleBase_WidgetShapeSelector::focusTo()
354 {
355   activateSelection(true);
356   return true;
357 }
358
359 //********************************************************************
360 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
361 {
362   if (theObj == myTextLine) {
363     if (theEvent->type() == QEvent::FocusIn)
364       activateSelection(true);
365   }
366   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
367 }
368
369 //********************************************************************
370 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
371 {
372   if (theValue) {
373     ModuleBase_WidgetValueFeature* aFeatureValue =
374         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
375     if (aFeatureValue && aFeatureValue->object()) {
376       ObjectPtr aObject = aFeatureValue->object();
377       if (isAccepted(aObject)) {
378         setObject(aObject);
379         return true;
380       }
381     }
382   }
383   return false;
384 }
385