Salome HOME
4dcbecf714aac75d12b8fc0ab17c9c7228ec3bcf
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeatureSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetFeatureSelector.cpp
4 // Created:     5 Sep 2016
5 // Author:      Natalia Ermolaeva
6
7 #include <ModuleBase_WidgetFeatureSelector.h>
8
9 #include <ModuleBase_Definitions.h>
10 #include <ModuleBase_ISelection.h>
11 #include <ModuleBase_IWorkshop.h>
12 #include <ModuleBase_IViewer.h>
13 #include <ModuleBase_Tools.h>
14 #include <ModuleBase_FilterFactory.h>
15 #include <ModuleBase_Filter.h>
16 #include <ModuleBase_IModule.h>
17 #include <ModuleBase_ViewerPrs.h>
18 #include <ModuleBase_IconFactory.h>
19 #include <ModuleBase_ResultPrs.h>
20
21 #include <Config_WidgetAPI.h>
22 #include <Events_Loop.h>
23 #include <Events_Message.h>
24 #include <GeomAPI_Interface.h>
25 #include <GeomAPI_Shape.h>
26
27 #include <ModelAPI_Data.h>
28 #include <ModelAPI_Document.h>
29 #include <ModelAPI_Events.h>
30 #include <ModelAPI_Feature.h>
31 #include <ModelAPI_Result.h>
32 #include <ModelAPI_Session.h>
33
34 #include <Config_WidgetAPI.h>
35
36 #include <GeomAPI_Shape.h>
37
38 #include <QWidget>
39 #include <QLayout>
40 #include <QLabel>
41 #include <QLineEdit>
42 #include <QToolButton>
43 #include <QString>
44 #include <QEvent>
45 #include <QApplication>
46 #include <QFormLayout>
47
48 #include <memory>
49
50 #include <list>
51 #include <string>
52
53
54 ModuleBase_WidgetFeatureSelector::ModuleBase_WidgetFeatureSelector(QWidget* theParent,
55                                                      ModuleBase_IWorkshop* theWorkshop,
56                                                      const Config_WidgetAPI* theData)
57 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
58 {
59   QFormLayout* aLayout = new QFormLayout(this);
60   ModuleBase_Tools::adjustMargins(aLayout);
61
62   QString aLabelText = translate(theData->widgetLabel());
63   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
64   myLabel = new QLabel(aLabelText, this);
65   if (!aLabelIcon.isEmpty())
66     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
67
68
69   QString aToolTip = translate(theData->widgetTooltip());
70   myTextLine = new QLineEdit(this);
71   QString anObjName = QString::fromStdString(attributeID());
72   myTextLine->setObjectName(anObjName);
73   myTextLine->setReadOnly(true);
74   myTextLine->setToolTip(aToolTip);
75   myTextLine->installEventFilter(this);
76
77   aLayout->addRow(myLabel, myTextLine);
78   myLabel->setToolTip(aToolTip);
79 }
80
81 //********************************************************************
82 ModuleBase_WidgetFeatureSelector::~ModuleBase_WidgetFeatureSelector()
83 {
84 }
85
86 //********************************************************************
87 bool ModuleBase_WidgetFeatureSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
88 {
89   ModuleBase_ISelection* aSelection = myWorkshop->selection();
90   ObjectPtr anObject = ModelAPI_Feature::feature(thePrs->object());
91   GeomShapePtr aShape;
92
93   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
94   // the last flag is to be depending on hasObject is called before. To be corrected later
95   return ModuleBase_Tools::setObject(attribute(), aFeature, aShape,
96                                      myWorkshop, myIsInValidate, true);
97 }
98
99 //********************************************************************
100 void ModuleBase_WidgetFeatureSelector::deactivate()
101 {
102   ModuleBase_ModelWidget::deactivate();
103   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
104   activateFilters(false);
105   myWorkshop->deactivateSubShapesSelection();
106 }
107
108 //********************************************************************
109 void ModuleBase_WidgetFeatureSelector::activateCustom()
110 {
111   connect(myWorkshop, SIGNAL(selectionChanged()), this,
112           SLOT(onSelectionChanged()), Qt::UniqueConnection);
113
114   activateFilters(true);
115
116   QIntList aShapeTypes;
117   aShapeTypes.push_back(ModuleBase_ResultPrs::Sel_Result);
118   myWorkshop->activateSubShapesSelection(aShapeTypes);
119
120   // Restore selection in the viewer by the attribute selection list
121   // it should be postponed to have current widget as active to validate restored selection
122   //static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
123   //ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
124 }
125
126 //********************************************************************
127 bool ModuleBase_WidgetFeatureSelector::storeValueCustom()
128 {
129   // the value is stored on the selection changed signal processing
130   return true;
131 }
132
133 //********************************************************************
134 bool ModuleBase_WidgetFeatureSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
135                                                   const bool theToValidate)
136 {
137   if (theValues.empty()) {
138     // In order to make reselection possible, set empty object and shape should be done
139     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
140                                                   ObjectPtr(), GeomShapePtr(), NULL)));
141     return false;
142   }
143   // it removes the processed value from the parameters list
144   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
145   bool isDone = false;
146
147   if (!theToValidate || isValidSelection(aValue))
148     isDone = setSelectionCustom(aValue);
149
150   return isDone;
151 }
152
153 //********************************************************************
154 bool ModuleBase_WidgetFeatureSelector::restoreValueCustom()
155 {
156   bool isBlocked = this->blockSignals(true);
157   updateSelectionName();
158   this->blockSignals(isBlocked);
159
160   return true;
161 }
162
163 //********************************************************************
164 QList<QWidget*> ModuleBase_WidgetFeatureSelector::getControls() const
165 {
166   QList<QWidget*> aControls;
167   aControls.append(myTextLine);
168   return aControls;
169 }
170
171 void ModuleBase_WidgetFeatureSelector::updateFocus()
172 {
173   emit focusOutWidget(this);
174 }
175
176 //********************************************************************
177 void ModuleBase_WidgetFeatureSelector::updateSelectionName()
178 {
179   DataPtr aData = myFeature->data();
180   if (!aData->isValid())
181     return;
182
183   ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
184   if (anObject.get() != NULL) {
185     std::string aName = anObject->data()->name();
186     myTextLine->setText(QString::fromStdString(aName));
187   } else {
188     myTextLine->clear();
189   }
190 }
191
192 //********************************************************************
193 bool ModuleBase_WidgetFeatureSelector::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
194 {
195   bool aValid = false;
196
197   ObjectPtr anObject = thePrs->object();
198   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
199   aValid = aFeature.get();
200   if (!aValid) {
201     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
202     aValid = aResult.get() && aResult->shape() == thePrs->shape();
203   }
204
205   return aValid;
206 }
207
208 //********************************************************************
209 void ModuleBase_WidgetFeatureSelector::onSelectionChanged()
210 {
211   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
212                                                               ModuleBase_ISelection::AllControls);
213
214   bool isDone = setSelection(aSelected, true/*false*/);
215   updateOnSelectionChanged(isDone);
216 }
217
218 //********************************************************************
219 void ModuleBase_WidgetFeatureSelector::updateOnSelectionChanged(const bool theDone)
220 {
221   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
222   // the same place repeatedly without mouse moved. In the case validation by filters is not
223   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
224   // the 3rd click in the same point allow using this point.
225   emit valuesChanged();
226   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
227   // calls validators for the feature and, as a result, updates the Apply button state.
228   updateObject(myFeature);
229
230   // we need to forget about previous validation result as the current selection can influence on it
231   clearValidatedCash();
232
233   if (theDone)
234     updateFocus();
235 }
236
237