]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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 <Config_WidgetAPI.h>
22
23 #include <GeomAPI_Shape.h>
24
25 #include <TopoDS_Shape.hxx>
26 #include <TopExp_Explorer.hxx>
27
28 #include <QWidget>
29 #include <QLayout>
30 #include <QLabel>
31 #include <QLineEdit>
32 #include <QToolButton>
33 #include <QString>
34 #include <QEvent>
35 #include <QDockWidget>
36
37 #include <stdexcept>
38
39 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
40 static ShapeTypes MyShapeTypes;
41
42 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
43 {
44   if (MyShapeTypes.count() == 0) {
45     MyShapeTypes["face"] = TopAbs_FACE;
46     MyShapeTypes["vertex"] = TopAbs_VERTEX;
47     MyShapeTypes["wire"] = TopAbs_WIRE;
48     MyShapeTypes["edge"] = TopAbs_EDGE;
49     MyShapeTypes["shell"] = TopAbs_SHELL;
50     MyShapeTypes["solid"] = TopAbs_SOLID;
51   }
52   QString aType = theType.toLower();
53   if (MyShapeTypes.contains(aType))
54     return MyShapeTypes[aType];
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 }
104
105 //********************************************************************
106 bool ModuleBase_WidgetShapeSelector::storeValue() const
107 {
108   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
109   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
110     return false;
111
112   DataPtr aData = myFeature->data();
113   boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
114       ModelAPI_AttributeReference>(aData->attribute(attributeID()));
115
116   ObjectPtr aObject = aRef->value();
117   if (!(aObject && aObject->isSame(mySelectedObject))) {
118     aRef->setValue(mySelectedObject);
119     updateObject(myFeature);
120   }
121   return true;
122 }
123
124 //********************************************************************
125 bool ModuleBase_WidgetShapeSelector::restoreValue()
126 {
127   DataPtr aData = myFeature->data();
128   boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
129
130   bool isBlocked = this->blockSignals(true);
131   mySelectedObject = aRef->value();
132   updateSelectionName();
133
134   this->blockSignals(isBlocked);
135   return true;
136 }
137
138 //********************************************************************
139 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
140 {
141   QList<QWidget*> aControls;
142   aControls.append(myLabel);
143   aControls.append(myTextLine);
144   return aControls;
145 }
146
147 //********************************************************************
148 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
149 {
150   QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
151   if (aObjects.size() > 0) {
152     ObjectPtr aObject = aObjects.first();
153     if ((!mySelectedObject) && (!aObject))
154       return;
155     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
156       return;
157
158     // Check that the selection corresponds to selection type
159     if (!isAccepted(aObject))
160       return;
161
162     setObject(aObject);
163     emit focusOutWidget(this);
164   }
165 }
166
167 //********************************************************************
168 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj)
169 {
170   if (mySelectedObject == theObj)
171     return;
172   mySelectedObject = theObj;
173   if (mySelectedObject) {
174     raisePanel();
175     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
176     ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
177     Events_Loop::loop()->flush(anEvent);
178   } 
179   updateSelectionName();
180   activateSelection(false);
181   emit valuesChanged();
182 }
183
184 //********************************************************************
185 bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const
186 {
187   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
188   if (myFeature) {
189     // We can not select a result of our feature
190     const std::list<boost::shared_ptr<ModelAPI_Result>>& aRes = myFeature->results();
191     std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
192     for (aIt = aRes.cbegin(); aIt != aRes.cend(); ++aIt) {
193       if ((*aIt) == aResult)
194         return false;
195     }
196   }
197   // Check that object belongs to active document or PartSet
198   DocumentPtr aDoc = aResult->document();
199   SessionPtr aMgr = ModelAPI_Session::get();
200   if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
201     return false;
202
203   // Check that the shape of necessary type
204   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
205   if (!aShapePtr)
206     return false;
207   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
208   if (aShape.IsNull())
209     return false;
210
211   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
212   if (aShapeType == TopAbs_COMPOUND) {
213     foreach (QString aType, myShapeTypes) {
214       TopExp_Explorer aEx(aShape, shapeType(aType));
215       if (aEx.More())
216         return true;
217     }
218   } else {
219     foreach (QString aType, myShapeTypes) {
220       if (shapeType(aType) == aShapeType)
221         return true;
222     }
223   }
224   return false;
225 }
226
227 //********************************************************************
228 void ModuleBase_WidgetShapeSelector::updateSelectionName()
229 {
230   if (mySelectedObject) {
231     std::string aName = mySelectedObject->data()->name();
232     myTextLine->setText(QString::fromStdString(aName));
233   } else {
234     if (myIsActive) {
235       QString aMsg = tr("Select a ");
236       int i = 0;
237       foreach (QString aType, myShapeTypes) {
238         if (i > 0)
239           aMsg += " or ";
240         aMsg += aType;
241         i++;
242       }
243       myTextLine->setText(aMsg);
244     } else
245       myTextLine->setText(tr("No object selected"));
246   }
247 }
248
249
250 //********************************************************************
251 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
252 {
253   myIsActive = toActivate;
254   if (myIsActive)
255     myTextLine->setPalette(myBasePalet);
256   else
257     myTextLine->setPalette(myInactivePalet);
258   updateSelectionName();
259
260   if (myIsActive) {
261     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
262     if (myUseSubShapes) {
263       QIntList aList;
264       foreach (QString aType, myShapeTypes)
265         aList.append(shapeType(aType));
266       myWorkshop->activateSubShapesSelection(aList);
267     }
268   } else {
269     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
270     if (myUseSubShapes) 
271       myWorkshop->deactivateSubShapesSelection();
272   }
273 }
274
275 //********************************************************************
276 void ModuleBase_WidgetShapeSelector::raisePanel() const
277 {
278   QWidget* aParent = myContainer->parentWidget();
279   QWidget* aLastPanel = 0;
280   while (!aParent->inherits("QDockWidget")) {
281     aLastPanel = aParent;
282     aParent = aParent->parentWidget();
283     if (!aParent)
284       return;
285   }
286   if (aParent->inherits("QDockWidget")) {
287     QDockWidget* aTabWgt = (QDockWidget*) aParent;
288     aTabWgt->raise();
289   }
290 }
291
292 //********************************************************************
293 bool ModuleBase_WidgetShapeSelector::focusTo()
294 {
295   activateSelection(true);
296   return true;
297 }
298
299 //********************************************************************
300 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
301 {
302   if (theObj == myTextLine) {
303     if (theEvent->type() == QEvent::FocusIn)
304       activateSelection(true);
305   }
306   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
307 }
308
309 //********************************************************************
310 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
311 {
312   if (theValue) {
313     ModuleBase_WidgetValueFeature* aFeatureValue =
314         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
315     if (aFeatureValue && aFeatureValue->object()) {
316       ObjectPtr aObject = aFeatureValue->object();
317       if (isAccepted(aObject)) {
318         setObject(aObject);
319         return true;
320       }
321     }
322   }
323   return false;
324 }
325