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