Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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   myBasePalet = myTextLine->palette();
78   myInactivePalet = myBasePalet;
79   myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
80
81   aLayout->addWidget(myTextLine);
82
83   myActivateBtn = new QToolButton(myContainer);
84   myActivateBtn->setIcon(QIcon(":icons/hand_point.png"));
85   myActivateBtn->setCheckable(true);
86   myActivateBtn->setToolTip(tr("Activate/Deactivate selection"));
87   connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool)));
88
89   aLayout->addWidget(myActivateBtn);
90
91   QString aActivateTxt = QString::fromStdString(theData->getProperty("activate"));
92   if (!aActivateTxt.isNull()) {
93     myActivateOnStart = (aActivateTxt == "true");
94   }
95
96   std::string aTypes = theData->getProperty("shape_types");
97   myShapeTypes = QString(aTypes.c_str()).split(',');
98 }
99
100 //********************************************************************
101 ModuleBase_WidgetSelector::~ModuleBase_WidgetSelector()
102 {
103 }
104
105 //********************************************************************
106 bool ModuleBase_WidgetSelector::storeValue() const
107 {
108   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
109   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
110     return false;
111
112   DataPtr aData = myFeature->data();
113   boost::shared_ptr<ModelAPI_AttributeReference> aRef = boost::dynamic_pointer_cast<
114       ModelAPI_AttributeReference>(aData->attribute(attributeID()));
115
116   ObjectPtr aObject = aRef->value();
117   if (!(aObject && aObject->isSame(mySelectedObject))) {
118     aRef->setValue(mySelectedObject);
119     updateObject(myFeature);
120   }
121   return true;
122 }
123
124 //********************************************************************
125 bool ModuleBase_WidgetSelector::restoreValue()
126 {
127   DataPtr aData = myFeature->data();
128   boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
129
130   bool isBlocked = this->blockSignals(true);
131   mySelectedObject = aRef->value();
132   updateSelectionName();
133
134   this->blockSignals(isBlocked);
135   return true;
136 }
137
138 //********************************************************************
139 QList<QWidget*> ModuleBase_WidgetSelector::getControls() const
140 {
141   QList<QWidget*> aControls;
142   aControls.append(myLabel);
143   aControls.append(myTextLine);
144   aControls.append(myActivateBtn);
145   return aControls;
146 }
147
148 //********************************************************************
149 void ModuleBase_WidgetSelector::onSelectionChanged()
150 {
151   QList<ObjectPtr> aObjects = myWorkshop->selectedObjects();
152   if (aObjects.size() > 0) {
153     ObjectPtr aObject = aObjects.first();
154     if ((!mySelectedObject) && (!aObject))
155       return;
156     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
157       return;
158
159     // Check that the selection corresponds to selection type
160     if (!isAccepted(aObject))
161       return;
162
163     mySelectedObject = aObject;
164     if (mySelectedObject) {
165       updateSelectionName();
166       myActivateBtn->setChecked(false);
167       raisePanel();
168     } else {
169       myTextLine->setText("");
170     }
171     emit valuesChanged();
172     emit focusOutWidget(this);
173   }
174 }
175
176 //********************************************************************
177 bool ModuleBase_WidgetSelector::isAccepted(const ObjectPtr theResult) const
178 {
179   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
180   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
181   if (!aShapePtr)
182     return false;
183   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
184   if (aShape.IsNull())
185     return false;
186
187   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
188   if (aShapeType == TopAbs_COMPOUND) {
189     foreach (QString aType, myShapeTypes) {
190       TopExp_Explorer aEx(aShape, shapeType(aType));
191       if (aEx.More())
192         return true;
193     }
194   } else {
195     foreach (QString aType, myShapeTypes) {
196       if (shapeType(aType) == aShapeType)
197         return true;
198     }
199   }
200   return false;
201 }
202
203 //********************************************************************
204 void ModuleBase_WidgetSelector::updateSelectionName()
205 {
206   if (mySelectedObject) {
207     std::string aName = mySelectedObject->data()->name();
208
209     myTextLine->setText(QString::fromStdString(aName));
210   } else
211     myTextLine->setText("");
212 }
213
214 //********************************************************************
215 bool ModuleBase_WidgetSelector::eventFilter(QObject* theObj, QEvent* theEvent)
216 {
217   if (theObj == myTextLine) {
218     if (theEvent->type() == QEvent::Polish) {
219       myActivateBtn->setChecked(myActivateOnStart);
220       onSelectionChanged();
221     }
222   }
223   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
224 }
225
226 //********************************************************************
227 void ModuleBase_WidgetSelector::enableOthersControls(bool toEnable) const
228 {
229   QWidget* aParent = myContainer->parentWidget();
230   QList<QWidget*> aChldList = aParent->findChildren<QWidget*>();
231   foreach(QWidget* aWgt, aChldList)
232   {
233     if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine)
234         && (aWgt != myContainer))
235       aWgt->setEnabled(toEnable);
236   }
237 }
238
239 //********************************************************************
240 void ModuleBase_WidgetSelector::activateSelection(bool toActivate)
241 {
242   enableOthersControls(!toActivate);
243   //myTextLine->setEnabled(toActivate);
244   if (toActivate)
245     myTextLine->setPalette(myBasePalet);
246   else
247     myTextLine->setPalette(myInactivePalet);
248
249   if (toActivate)
250     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
251   else
252     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
253
254   myActivateBtn->setDown(toActivate);
255 }
256
257 //********************************************************************
258 void ModuleBase_WidgetSelector::raisePanel() const
259 {
260   QWidget* aParent = myContainer->parentWidget();
261   QWidget* aLastPanel = 0;
262   while (!aParent->inherits("QDockWidget")) {
263     aLastPanel = aParent;
264     aParent = aParent->parentWidget();
265     if (!aParent)
266       return;
267   }
268   if (aParent->inherits("QDockWidget")) {
269     QDockWidget* aTabWgt = (QDockWidget*) aParent;
270     aTabWgt->raise();
271   }
272 }
273
274 //********************************************************************
275 bool ModuleBase_WidgetSelector::focusTo()
276 {
277   myActivateBtn->setChecked(true);
278   return true;
279 }