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