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