1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
\r
3 // File: ModuleBase_WidgetFeatureSelector.cpp
\r
4 // Created: 5 Sep 2016
\r
5 // Author: Natalia Ermolaeva
\r
7 #include <ModuleBase_WidgetFeatureSelector.h>
\r
9 #include <ModuleBase_Definitions.h>
\r
10 #include <ModuleBase_ISelection.h>
\r
11 #include <ModuleBase_IWorkshop.h>
\r
12 #include <ModuleBase_IViewer.h>
\r
13 #include <ModuleBase_Tools.h>
\r
14 #include <ModuleBase_FilterFactory.h>
\r
15 #include <ModuleBase_Filter.h>
\r
16 #include <ModuleBase_IModule.h>
\r
17 #include <ModuleBase_ViewerPrs.h>
\r
18 #include <ModuleBase_IconFactory.h>
\r
19 #include <ModuleBase_ResultPrs.h>
\r
21 #include <Config_WidgetAPI.h>
\r
22 #include <Events_Loop.h>
\r
23 #include <Events_Message.h>
\r
24 #include <GeomAPI_Interface.h>
\r
25 #include <GeomAPI_Shape.h>
\r
27 #include <ModelAPI_Data.h>
\r
28 #include <ModelAPI_Document.h>
\r
29 #include <ModelAPI_Events.h>
\r
30 #include <ModelAPI_Feature.h>
\r
31 #include <ModelAPI_Result.h>
\r
32 #include <ModelAPI_Session.h>
\r
34 #include <Config_WidgetAPI.h>
\r
36 #include <GeomAPI_Shape.h>
\r
41 #include <QLineEdit>
\r
42 #include <QToolButton>
\r
45 #include <QApplication>
\r
46 #include <QFormLayout>
\r
54 ModuleBase_WidgetFeatureSelector::ModuleBase_WidgetFeatureSelector(QWidget* theParent,
\r
55 ModuleBase_IWorkshop* theWorkshop,
\r
56 const Config_WidgetAPI* theData)
\r
57 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
\r
59 QFormLayout* aLayout = new QFormLayout(this);
\r
60 ModuleBase_Tools::adjustMargins(aLayout);
\r
62 QString aLabelText = QString::fromStdString(theData->widgetLabel());
\r
63 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
\r
64 myLabel = new QLabel(aLabelText, this);
\r
65 if (!aLabelIcon.isEmpty())
\r
66 myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
\r
69 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
\r
70 myTextLine = new QLineEdit(this);
\r
71 QString anObjName = QString::fromStdString(attributeID());
\r
72 myTextLine->setObjectName(anObjName);
\r
73 myTextLine->setReadOnly(true);
\r
74 myTextLine->setToolTip(aToolTip);
\r
75 myTextLine->installEventFilter(this);
\r
77 aLayout->addRow(myLabel, myTextLine);
\r
78 myLabel->setToolTip(aToolTip);
\r
81 //********************************************************************
\r
82 ModuleBase_WidgetFeatureSelector::~ModuleBase_WidgetFeatureSelector()
\r
86 //********************************************************************
87 bool ModuleBase_WidgetFeatureSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
89 ModuleBase_ISelection* aSelection = myWorkshop->selection();
90 ObjectPtr anObject = ModelAPI_Feature::feature(thePrs->object());
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);
99 //********************************************************************
100 void ModuleBase_WidgetFeatureSelector::deactivate()
102 ModuleBase_ModelWidget::deactivate();
103 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
104 activateFilters(false);
\r
105 myWorkshop->deactivateSubShapesSelection();
108 //********************************************************************
109 void ModuleBase_WidgetFeatureSelector::activateCustom()
111 connect(myWorkshop, SIGNAL(selectionChanged()), this,
112 SLOT(onSelectionChanged()), Qt::UniqueConnection);
114 activateFilters(true);
\r
116 QIntList aShapeTypes;
117 aShapeTypes.push_back(ModuleBase_ResultPrs::Sel_Result);
118 myWorkshop->activateSubShapesSelection(aShapeTypes);
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);
126 //********************************************************************
\r
127 bool ModuleBase_WidgetFeatureSelector::storeValueCustom()
\r
129 // the value is stored on the selection changed signal processing
\r
133 //********************************************************************
\r
134 bool ModuleBase_WidgetFeatureSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
\r
135 const bool theToValidate)
\r
137 if (theValues.empty()) {
\r
138 // In order to make reselection possible, set empty object and shape should be done
\r
139 setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
\r
140 ObjectPtr(), GeomShapePtr(), NULL)));
\r
143 // it removes the processed value from the parameters list
\r
144 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
\r
145 bool isDone = false;
\r
147 if (!theToValidate || isValidSelection(aValue))
\r
148 isDone = setSelectionCustom(aValue);
\r
153 //********************************************************************
\r
154 bool ModuleBase_WidgetFeatureSelector::restoreValueCustom()
\r
156 bool isBlocked = this->blockSignals(true);
\r
157 updateSelectionName();
\r
158 this->blockSignals(isBlocked);
\r
163 //********************************************************************
\r
164 QList<QWidget*> ModuleBase_WidgetFeatureSelector::getControls() const
\r
166 QList<QWidget*> aControls;
\r
167 aControls.append(myTextLine);
\r
171 void ModuleBase_WidgetFeatureSelector::updateFocus()
\r
173 emit focusOutWidget(this);
\r
176 //********************************************************************
\r
177 void ModuleBase_WidgetFeatureSelector::updateSelectionName()
\r
179 DataPtr aData = myFeature->data();
\r
180 if (!aData->isValid())
\r
183 ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
\r
184 if (anObject.get() != NULL) {
\r
185 std::string aName = anObject->data()->name();
\r
186 myTextLine->setText(QString::fromStdString(aName));
\r
190 //********************************************************************
\r
191 bool ModuleBase_WidgetFeatureSelector::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
\r
193 bool aValid = false;
\r
195 ObjectPtr anObject = thePrs->object();
\r
196 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
\r
197 aValid = aFeature.get();
\r
199 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
\r
200 aValid = aResult.get() && aResult->shape() == thePrs->shape();
\r
206 //********************************************************************
\r
207 void ModuleBase_WidgetFeatureSelector::onSelectionChanged()
209 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
\r
210 ModuleBase_ISelection::AllControls);
\r
212 bool isDone = setSelection(aSelected, true/*false*/);
213 updateOnSelectionChanged(isDone);
216 //********************************************************************
217 void ModuleBase_WidgetFeatureSelector::updateOnSelectionChanged(const bool theDone)
219 // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
220 // the same place repeatedly without mouse moved. In the case validation by filters is not
221 // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
222 // the 3rd click in the same point allow using this point.
223 emit valuesChanged();
224 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
225 // calls validators for the feature and, as a result, updates the Apply button state.
226 updateObject(myFeature);
228 // we need to forget about previous validation result as the current selection can influence on it
229 clearValidatedCash();