1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_WidgetFeatureSelector.cpp
5 // Author: Natalia Ermolaeva
7 #include <ModuleBase_WidgetFeatureSelector.h>
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>
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>
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>
33 #include <Config_WidgetAPI.h>
35 #include <GeomAPI_Shape.h>
41 #include <QToolButton>
44 #include <QApplication>
45 #include <QFormLayout>
53 ModuleBase_WidgetFeatureSelector::ModuleBase_WidgetFeatureSelector(QWidget* theParent,
54 ModuleBase_IWorkshop* theWorkshop,
55 const Config_WidgetAPI* theData)
56 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
58 QFormLayout* aLayout = new QFormLayout(this);
59 ModuleBase_Tools::adjustMargins(aLayout);
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));
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);
76 aLayout->addRow(myLabel, myTextLine);
77 myLabel->setToolTip(aToolTip);
80 //********************************************************************
81 ModuleBase_WidgetFeatureSelector::~ModuleBase_WidgetFeatureSelector()
85 //********************************************************************
86 bool ModuleBase_WidgetFeatureSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
88 ModuleBase_ISelection* aSelection = myWorkshop->selection();
89 ObjectPtr anObject = ModelAPI_Feature::feature(thePrs->object());
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);
98 //********************************************************************
99 void ModuleBase_WidgetFeatureSelector::deactivate()
101 ModuleBase_ModelWidget::deactivate();
102 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
103 activateFilters(false);
104 myWorkshop->deactivateSubShapesSelection();
107 //********************************************************************
108 void ModuleBase_WidgetFeatureSelector::activateCustom()
110 connect(myWorkshop, SIGNAL(selectionChanged()), this,
111 SLOT(onSelectionChanged()), Qt::UniqueConnection);
113 activateFilters(true);
115 QIntList aShapeTypes;
116 aShapeTypes.push_back(ModuleBase_ResultPrs::Sel_Result);
117 myWorkshop->activateSubShapesSelection(aShapeTypes);
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);
125 //********************************************************************
126 bool ModuleBase_WidgetFeatureSelector::storeValueCustom()
128 // the value is stored on the selection changed signal processing
132 //********************************************************************
133 bool ModuleBase_WidgetFeatureSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
134 const bool theToValidate)
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)));
142 // it removes the processed value from the parameters list
143 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
146 if (!theToValidate || isValidSelection(aValue))
147 isDone = setSelectionCustom(aValue);
152 //********************************************************************
153 bool ModuleBase_WidgetFeatureSelector::restoreValueCustom()
155 bool isBlocked = this->blockSignals(true);
156 updateSelectionName();
157 this->blockSignals(isBlocked);
162 //********************************************************************
163 QList<QWidget*> ModuleBase_WidgetFeatureSelector::getControls() const
165 QList<QWidget*> aControls;
166 aControls.append(myTextLine);
170 void ModuleBase_WidgetFeatureSelector::updateFocus()
172 emit focusOutWidget(this);
175 //********************************************************************
176 void ModuleBase_WidgetFeatureSelector::updateSelectionName()
178 DataPtr aData = myFeature->data();
179 if (!aData->isValid())
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));
191 //********************************************************************
192 bool ModuleBase_WidgetFeatureSelector::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
196 ObjectPtr anObject = thePrs->object();
197 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
198 aValid = aFeature.get();
200 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
201 aValid = aResult.get() && aResult->shape() == thePrs->shape();
207 //********************************************************************
208 void ModuleBase_WidgetFeatureSelector::onSelectionChanged()
210 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
211 ModuleBase_ISelection::AllControls);
213 bool isDone = setSelection(aSelected, true/*false*/);
214 updateOnSelectionChanged(isDone);
217 //********************************************************************
218 void ModuleBase_WidgetFeatureSelector::updateOnSelectionChanged(const bool theDone)
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);
229 // we need to forget about previous validation result as the current selection can influence on it
230 clearValidatedCash();