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