Salome HOME
36f6af51fff4cbbfc62fa526fe4b0eb211bf0d9e
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // File:        ModuleBase_WidgetSelector.h
2 // Created:     2 June 2014
3 // Author:      Vitaly Smetannikov
4
5
6 #include "ModuleBase_WidgetSelector.h"
7 #include "ModuleBase_IWorkshop.h"
8 #include "ModuleBase_Tools.h"
9
10 #include <Events_Loop.h>
11 #include <Model_Events.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
34 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
35 static ShapeTypes MyShapeTypes;
36
37 TopAbs_ShapeEnum ModuleBase_WidgetSelector::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
53
54
55 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(QWidget* theParent, 
56                                                      ModuleBase_IWorkshop* theWorkshop, 
57                                                      const Config_WidgetAPI* theData)
58 : ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop), myActivateOnStart(false)
59 {
60   myContainer = new QWidget(theParent);
61   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
62
63   aLayout->setContentsMargins(0, 0, 0, 0);
64   QString aLabelText = QString::fromStdString(theData->widgetLabel());
65   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
66   myLabel = new QLabel(aLabelText, myContainer);
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   myTextLine->installEventFilter(this);
76
77   aLayout->addWidget(myTextLine);
78
79   myActivateBtn = new QToolButton(myContainer);
80   myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
81   myActivateBtn->setCheckable(true);
82   myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
83   connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
84
85   aLayout->addWidget(myActivateBtn);
86
87   QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
88   if (!aActivateTxt.isNull()) {
89     myActivateOnStart = (aActivateTxt == "true");
90   }
91
92   std::string aTypes = theData->getProperty("shape_types");
93   myShapeTypes = QString(aTypes.c_str()).split(',');
94 }
95
96 //********************************************************************
97 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
98 {
99 }
100
101 //********************************************************************
102 bool ModuleBase_WidgetSelector::storeValue(FeaturePtr theFeature) const
103 {
104   DataPtr aData = theFeature->data();
105   boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
106     boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
107
108   FeaturePtr aFeature = aRef->value();
109   if (!(aFeature && aFeature->isSame(mySelectedObject))) {
110     aRef->setValue(mySelectedObject);
111     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
112   }
113   return true;
114 }
115
116 //********************************************************************
117 bool ModuleBase_WidgetSelector::restoreValue(FeaturePtr theFeature)
118 {
119   DataPtr aData = theFeature->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_WidgetSelector::getControls() const
132 {
133   QList<QWidget*> aControls;
134   aControls.append(myLabel);
135   aControls.append(myTextLine);
136   aControls.append(myActivateBtn);
137   return aControls;
138 }
139
140 //********************************************************************
141 void ModuleBase_WidgetSelector::onSelectionChanged()
142 {
143   QList<ObjectPtr> aObjects = myWorkshop->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)) return;
153
154     mySelectedObject = aObject;
155     if (mySelectedObject) {
156       updateSelectionName();
157       activateSelection(false);
158       raisePanel();
159     } else {
160       myTextLine->setText("");
161     }
162     emit valuesChanged();
163   }
164 }
165
166 //********************************************************************
167 bool ModuleBase_WidgetSelector::isAccepted(const ObjectPtr theResult) const
168 {
169   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
170   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModuleBase_Tools::shape(aResult);
171   if (!aShapePtr) return false;
172   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
173   if (aShape.IsNull()) return false;
174
175   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
176   if (aShapeType == TopAbs_COMPOUND) {
177     foreach (QString aType, myShapeTypes) {
178       TopExp_Explorer aEx(aShape, shapeType(aType));
179       if (aEx.More())
180         return true;
181     }
182   } else {
183     foreach (QString aType, myShapeTypes) {
184       if (shapeType(aType) == aShapeType)
185         return true;
186     }
187   }
188   return false;
189 }
190
191 //********************************************************************
192 void ModuleBase_WidgetSelector::updateSelectionName()
193 {
194   if (mySelectedObject) {
195     std::string aName = mySelectedObject->data()->name();
196  
197     myTextLine->setText(QString::fromStdString(aName));
198   } else 
199     myTextLine->setText("");
200 }
201
202 //********************************************************************
203 bool ModuleBase_WidgetSelector::eventFilter(QObject* theObj, QEvent* theEvent)
204 {
205   if (theObj == myTextLine) {
206     if (theEvent->type() == QEvent::Polish) {
207       activateSelection(myActivateOnStart);
208       onSelectionChanged();
209     }
210   }
211   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
212 }
213
214 //********************************************************************
215 void ModuleBase_WidgetSelector::enableOthersControls(bool toEnable) const
216 {
217   QWidget* aParent = myContainer->parentWidget();
218   QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
219   foreach(QWidget* aWgt, aChldList) {
220     if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) && (aWgt != myContainer))
221       aWgt->setEnabled(toEnable);
222   }
223 }
224
225 //********************************************************************
226 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
227 {
228   enableOthersControls(!toActivate);
229   myTextLine->setEnabled(toActivate);
230
231   if (toActivate)
232     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
233   else
234     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
235
236   myActivateBtn->setDown(toActivate);
237 }
238
239 //********************************************************************
240 void ModuleBase_WidgetSelector::raisePanel() const
241 {
242   QWidget* aParent = myContainer->parentWidget();
243   QWidget* aLastPanel = 0;
244   while (!aParent->inherits("QDockWidget")) {
245     aLastPanel = aParent;
246     aParent = aParent->parentWidget();
247     if (!aParent) return;
248   }
249   if (aParent->inherits("QDockWidget")) {
250     QDockWidget* aTabWgt = (QDockWidget*) aParent;
251     aTabWgt->raise();
252   }
253 }