]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetSelector.cpp
Salome HOME
Sources formated according to the codeing standards
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetSelector.cpp
1 // File:        ModuleBase_WidgetSelector.h
2 // Created:     2 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include "ModuleBase_WidgetSelector.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_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 ModuleBase_WidgetSelector::ModuleBase_WidgetSelector(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),
58       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() const
103 {
104   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
105   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
106     return false;
107
108   DataPtr aData = myFeature->data();
109   boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
110       ModelAPI_AttributeReference>(aData->attribute(attributeID()));
111
112   ObjectPtr aObject = aRef->value();
113   if (!(aObject && aObject->isSame(mySelectedObject))) {
114     aRef->setValue(mySelectedObject);
115     updateObject(myFeature);
116   }
117   return true;
118 }
119
120 //********************************************************************
121 bool ModuleBase_WidgetSelector::restoreValue()
122 {
123   DataPtr aData = myFeature->data();
124   boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
125
126   bool isBlocked = this->blockSignals(true);
127   mySelectedObject = aRef->value();
128   updateSelectionName();
129
130   this->blockSignals(isBlocked);
131   return true;
132 }
133
134 //********************************************************************
135 QList<QWidget*> ModuleBase_WidgetSelector::getControls() const
136 {
137   QList<QWidget*> aControls;
138   aControls.append(myLabel);
139   aControls.append(myTextLine);
140   aControls.append(myActivateBtn);
141   return aControls;
142 }
143
144 //********************************************************************
145 void ModuleBase_WidgetSelector::onSelectionChanged()
146 {
147   QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
148   if (aObjects.size() > 0) {
149     ObjectPtr aObject = aObjects.first();
150     if ((!mySelectedObject) && (!aObject))
151       return;
152     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
153       return;
154
155     // Check that the selection corresponds to selection type
156     if (!isAccepted(aObject))
157       return;
158
159     mySelectedObject = aObject;
160     if (mySelectedObject) {
161       updateSelectionName();
162       myActivateBtn->setChecked(false);
163       raisePanel();
164     } else {
165       myTextLine->setText("");
166     }
167     emit valuesChanged();
168   }
169 }
170
171 //********************************************************************
172 bool ModuleBase_WidgetSelector::isAccepted(const ObjectPtr theResult) const
173 {
174   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
175   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
176   if (!aShapePtr)
177     return false;
178   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
179   if (aShape.IsNull())
180     return false;
181
182   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
183   if (aShapeType == TopAbs_COMPOUND) {
184     foreach (QString aType, myShapeTypes)
185     {
186       TopExp_Explorer aEx(aShape, shapeType(aType));
187       if (aEx.More())
188         return true;
189     }
190   } else {
191     foreach (QString aType, myShapeTypes)
192     {
193       if (shapeType(aType) == aShapeType)
194         return true;
195     }
196   }
197   return false;
198 }
199
200 //********************************************************************
201 void ModuleBase_WidgetSelector::updateSelectionName()
202 {
203   if (mySelectedObject) {
204     std::string aName = mySelectedObject->data()->name();
205
206     myTextLine->setText(QString::fromStdString(aName));
207   } else
208     myTextLine->setText("");
209 }
210
211 //********************************************************************
212 bool ModuleBase_WidgetSelector::eventFilter(QObject* theObj, QEvent* theEvent)
213 {
214   if (theObj == myTextLine) {
215     if (theEvent->type() == QEvent::Polish) {
216       myActivateBtn->setChecked(myActivateOnStart);
217       onSelectionChanged();
218     }
219   }
220   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
221 }
222
223 //********************************************************************
224 void ModuleBase_WidgetSelector::enableOthersControls(bool toEnable) const
225 {
226   QWidget* aParent = myContainer->parentWidget();
227   QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
228   foreach(QWidget* aWgt, aChldList)
229   {
230     if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine)
231         && (aWgt != myContainer))
232       aWgt->setEnabled(toEnable);
233   }
234 }
235
236 //********************************************************************
237 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
238 {
239   enableOthersControls(!toActivate);
240   myTextLine->setEnabled(toActivate);
241
242   if (toActivate)
243     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
244   else
245     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
246
247   myActivateBtn->setDown(toActivate);
248 }
249
250 //********************************************************************
251 void ModuleBase_WidgetSelector::raisePanel() const
252 {
253   QWidget* aParent = myContainer->parentWidget();
254   QWidget* aLastPanel = 0;
255   while (!aParent->inherits("QDockWidget")) {
256     aLastPanel = aParent;
257     aParent = aParent->parentWidget();
258     if (!aParent)
259       return;
260   }
261   if (aParent->inherits("QDockWidget")) {
262     QDockWidget* aTabWgt = (QDockWidget*) aParent;
263     aTabWgt->raise();
264   }
265 }