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