Salome HOME
e70e5dc08b011513a8ce6d8b549ea9cce6909de7
[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     // Check that the selection corresponds to selection type
176     if (!isAccepted(aObject))
177       return;
178
179     setObject(aObject);
180     emit focusOutWidget(this);
181   }
182 }
183
184 //********************************************************************
185 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj)
186 {
187   if (mySelectedObject == theObj)
188     return;
189   mySelectedObject = theObj;
190   if (mySelectedObject) {
191     raisePanel();
192     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
193     ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
194     Events_Loop::loop()->flush(anEvent);
195   } 
196   updateSelectionName();
197   activateSelection(false);
198   emit valuesChanged();
199 }
200
201 //********************************************************************
202 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
203 {
204   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
205   if (myFeature) {
206     // We can not select a result of our feature
207     const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
208     std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
209     for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
210       if ((*aIt) == aResult)
211         return false;
212     }
213   }
214   // Check that object belongs to active document or PartSet
215   DocumentPtr aDoc = aResult->document();
216   SessionPtr aMgr = ModelAPI_Session::get();
217   if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
218     return false;
219
220   // Check that the shape of necessary type
221   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
222   if (!aShapePtr)
223     return false;
224   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
225   if (aShape.IsNull())
226     return false;
227
228   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
229   if (aShapeType == TopAbs_COMPOUND) {
230     foreach (QString aType, myShapeTypes) {
231       TopExp_Explorer aEx(aShape, shapeType(aType));
232       if (aEx.More())
233         return true;
234     }
235   } else {
236     foreach (QString aType, myShapeTypes) {
237       if (shapeType(aType) == aShapeType)
238         return true;
239     }
240   }
241   return false;
242 }
243
244 //********************************************************************
245 void ModuleBase_WidgetShapeSelector::updateSelectionName()
246 {
247   if (mySelectedObject) {
248     std::string aName = mySelectedObject->data()->name();
249     myTextLine->setText(QString::fromStdString(aName));
250   } else {
251     if (myIsActive) {
252       QString aMsg = tr("Select a ");
253       int i = 0;
254       foreach (QString aType, myShapeTypes) {
255         if (i > 0)
256           aMsg += " or ";
257         aMsg += aType;
258         i++;
259       }
260       myTextLine->setText(aMsg);
261     } else
262       myTextLine->setText(tr("No object selected"));
263   }
264 }
265
266
267 //********************************************************************
268 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
269 {
270   myIsActive = toActivate;
271   if (myIsActive)
272     myTextLine->setPalette(myBasePalet);
273   else
274     myTextLine->setPalette(myInactivePalet);
275   updateSelectionName();
276
277   if (myIsActive) {
278     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
279     if (myUseSubShapes) {
280       QIntList aList;
281       foreach (QString aType, myShapeTypes)
282         aList.append(shapeType(aType));
283       myWorkshop->activateSubShapesSelection(aList);
284     }
285   } else {
286     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
287     if (myUseSubShapes) 
288       myWorkshop->deactivateSubShapesSelection();
289   }
290 }
291
292 //********************************************************************
293 void ModuleBase_WidgetShapeSelector::raisePanel() const
294 {
295   QWidget* aParent = myContainer->parentWidget();
296   QWidget* aLastPanel = 0;
297   while (!aParent->inherits("QDockWidget")) {
298     aLastPanel = aParent;
299     aParent = aParent->parentWidget();
300     if (!aParent)
301       return;
302   }
303   if (aParent->inherits("QDockWidget")) {
304     QDockWidget* aTabWgt = (QDockWidget*) aParent;
305     aTabWgt->raise();
306   }
307 }
308
309 //********************************************************************
310 bool ModuleBase_WidgetShapeSelector::focusTo()
311 {
312   activateSelection(true);
313   return true;
314 }
315
316 //********************************************************************
317 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
318 {
319   if (theObj == myTextLine) {
320     if (theEvent->type() == QEvent::FocusIn)
321       activateSelection(true);
322   }
323   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
324 }
325
326 //********************************************************************
327 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
328 {
329   if (theValue) {
330     ModuleBase_WidgetValueFeature* aFeatureValue =
331         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
332     if (aFeatureValue && aFeatureValue->object()) {
333       ObjectPtr aObject = aFeatureValue->object();
334       if (isAccepted(aObject)) {
335         setObject(aObject);
336         return true;
337       }
338     }
339   }
340   return false;
341 }
342