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