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