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_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>
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>
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>
34 #include <Config_WidgetAPI.h>
36 #include <GeomAPI_Shape.h>
42 #include <QToolButton>
45 #include <QApplication>
46 #include <QFormLayout>
54 ModuleBase_WidgetFeatureSelector::ModuleBase_WidgetFeatureSelector(QWidget* theParent,
55 ModuleBase_IWorkshop* theWorkshop,
56 const Config_WidgetAPI* theData)
57 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
59 QFormLayout* aLayout = new QFormLayout(this);
60 ModuleBase_Tools::adjustMargins(aLayout);
62 QString aLabelText = QString::fromStdString(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));
69 QString aToolTip = QString::fromStdString(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);
77 aLayout->addRow(myLabel, myTextLine);
78 myLabel->setToolTip(aToolTip);
81 //********************************************************************
82 ModuleBase_WidgetFeatureSelector::~ModuleBase_WidgetFeatureSelector()
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);
105 myWorkshop->deactivateSubShapesSelection();
108 //********************************************************************
109 void ModuleBase_WidgetFeatureSelector::activateCustom()
111 connect(myWorkshop, SIGNAL(selectionChanged()), this,
112 SLOT(onSelectionChanged()), Qt::UniqueConnection);
114 activateFilters(true);
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 //********************************************************************
127 bool ModuleBase_WidgetFeatureSelector::storeValueCustom()
129 // the value is stored on the selection changed signal processing
133 //********************************************************************
134 bool ModuleBase_WidgetFeatureSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
135 const bool theToValidate)
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)));
143 // it removes the processed value from the parameters list
144 ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
147 if (!theToValidate || isValidSelection(aValue))
148 isDone = setSelectionCustom(aValue);
153 //********************************************************************
154 bool ModuleBase_WidgetFeatureSelector::restoreValueCustom()
156 bool isBlocked = this->blockSignals(true);
157 updateSelectionName();
158 this->blockSignals(isBlocked);
163 //********************************************************************
164 QList<QWidget*> ModuleBase_WidgetFeatureSelector::getControls() const
166 QList<QWidget*> aControls;
167 aControls.append(myTextLine);
171 void ModuleBase_WidgetFeatureSelector::updateFocus()
173 emit focusOutWidget(this);
176 //********************************************************************
177 void ModuleBase_WidgetFeatureSelector::updateSelectionName()
179 DataPtr aData = myFeature->data();
180 if (!aData->isValid())
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));
192 //********************************************************************
193 bool ModuleBase_WidgetFeatureSelector::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
197 ObjectPtr anObject = thePrs->object();
198 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
199 aValid = aFeature.get();
201 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
202 aValid = aResult.get() && aResult->shape() == thePrs->shape();
208 //********************************************************************
209 void ModuleBase_WidgetFeatureSelector::onSelectionChanged()
211 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
212 ModuleBase_ISelection::AllControls);
214 bool isDone = setSelection(aSelected, true/*false*/);
215 updateOnSelectionChanged(isDone);
218 //********************************************************************
219 void ModuleBase_WidgetFeatureSelector::updateOnSelectionChanged(const bool theDone)
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);
230 // we need to forget about previous validation result as the current selection can influence on it
231 clearValidatedCash();