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