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