1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_OperationFeature.cpp
6 * Created on: Apr 2, 2014
10 #include "ModuleBase_OperationFeature.h"
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_ModelWidget.h"
14 #include "ModuleBase_ViewerPrs.h"
15 #include "ModuleBase_IPropertyPanel.h"
16 #include "ModuleBase_ISelection.h"
17 #include "ModuleBase_IViewer.h"
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
31 #include <GeomAPI_Pnt2d.h>
33 #include <Events_Loop.h>
37 // the define to check the activated object as a sub-feature by argument of
38 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
39 //#define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
40 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
41 #include <ModelAPI_AttributeRefList.h>
48 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
49 : ModuleBase_Operation(theId, theParent),
54 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
59 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
60 /*const bool theRestartTransaction*/)
62 bool isCurrentEditState = isEditOperation();
63 if (isCurrentEditState == isEditState)
67 // this case is obsolete as it was not approved for reentrant sketch operation
68 // it was implemented when isEditState did not exist and only edit operation can be set
69 if (theRestartTransaction) {
70 // finsh previous create operation
71 emit beforeCommitted();
72 SessionPtr aMgr = ModelAPI_Session::get();
73 ModelAPI_Session::get()->finishOperation();
75 // start new edit operation
77 QString anId = getDescription()->operationId();
79 anId = anId.append(EditSuffix());
81 ModelAPI_Session::get()->startOperation(anId.toStdString());
84 myIsEditing = isEditState;
86 propertyPanel()->setEditingMode(isEditOperation());
89 FeaturePtr ModuleBase_OperationFeature::feature() const
94 bool ModuleBase_OperationFeature::isValid() const
96 if (!myFeature || !myFeature->data()->isValid())
97 return true; // rename operation
98 if (myFeature->isAction())
101 std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
102 return anError.empty();
105 void ModuleBase_OperationFeature::startOperation()
107 FeaturePtr aFeature = feature();
108 if (!aFeature.get() || !isEditOperation())
111 if (aFeature.get() && isEditOperation())
112 aFeature->setStable(false);
114 myVisualizedObjects.clear();
115 // store hidden result features
116 std::list<ResultPtr> aResults = aFeature->results();
117 std::list<ResultPtr>::const_iterator aIt;
118 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
119 ObjectPtr anObject = *aIt;
120 if (anObject.get() && !anObject->isDisplayed()) {
121 myVisualizedObjects.insert(*aIt);
122 anObject->setDisplayed(true);
125 if (!aFeature->isDisplayed()) {
126 myVisualizedObjects.insert(aFeature);
127 aFeature->setDisplayed(true);
129 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
132 void ModuleBase_OperationFeature::stopOperation()
134 FeaturePtr aFeature = feature();
135 if (!aFeature.get() || !isEditOperation())
138 // store hidden result features
139 std::list<ResultPtr> aResults = aFeature->results();
140 std::list<ResultPtr>::const_iterator aIt;
141 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
142 ObjectPtr anObject = *aIt;
143 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
144 anObject->setDisplayed(false);
147 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
148 aFeature->setDisplayed(false);
150 if (myVisualizedObjects.size() > 0)
151 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
154 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
156 if (myParentFeature.get()) {
157 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
159 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
160 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
162 if (myFeature) { // TODO: generate an error if feature was not created
164 // Model update should call "execute" of a feature.
165 //myFeature->execute();
166 // Init default values
167 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
168 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
169 for (; anIt != aLast; anIt++) {
170 (*anIt)->storeValue(aFeature);
175 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
179 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
181 myFeature = theFeature;
185 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
187 FeaturePtr aFeature = feature();
189 if (aFeature == theObj)
191 std::list<ResultPtr> aResults = aFeature->results();
192 std::list<ResultPtr>::const_iterator aIt;
193 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
194 if (theObj == (*aIt))
197 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
198 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
199 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
200 ModelAPI_AttributeRefList::typeId());
201 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
202 bool aFoundObject = false;
203 for (; anIt != aLast && !aFoundObject; anIt++) {
204 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
205 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
206 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
207 ObjectPtr anObject = aCurSelList->object(i);
208 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
209 if (aFeature.get()) {
210 aFoundObject = anObjectFeature == aFeature;
220 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
222 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
225 bool ModuleBase_OperationFeature::start()
227 setIsModified(false);
228 QString anId = getDescription()->operationId();
230 anId = anId.append(EditSuffix());
232 ModelAPI_Session::get()->startOperation(anId.toStdString());
234 emit beforeStarted();
238 FeaturePtr aFeature = createFeature();
239 // if the feature is not created, there is no sense to start the operation
240 if (aFeature.get() == NULL) {
241 // it is necessary to abor the operation in the session and emit the aborted signal
242 // in order to update commands status in the workshop, to be exact the feature action
248 //Already called startOperation();
253 void ModuleBase_OperationFeature::abort()
255 emit beforeAborted();
257 // the viewer update should be blocked in order to avoid the features blinking before they are
259 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
260 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
261 Events_Loop::loop()->send(aMsg);
263 // the widgets of property panel should not process any events come from data mode
264 // after abort clicked. Some signal such as redisplay/create influence on content
265 // of the object browser and viewer context. Therefore it influence to the current
266 // selection and if the active widget listens it, the attribute value is errnoneous
268 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
270 aPropertyPanel->cleanContent();
273 myFeature->setStable(true);
278 SessionPtr aMgr = ModelAPI_Session::get();
279 aMgr->abortOperation();
281 // the viewer update should be unblocked in order to avoid the features blinking before they are
283 aMsg = std::shared_ptr<Events_Message>(
284 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
286 Events_Loop::loop()->send(aMsg);
291 bool ModuleBase_OperationFeature::commit()
293 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
295 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
296 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
297 anActiveWidget->storeValue();
300 if (canBeCommitted()) {
301 emit beforeCommitted();
302 // the widgets of property panel should not process any events come from data mode
303 // after commit clicked. Some signal such as redisplay/create influence on content
304 // of the object browser and viewer context. Therefore it influence to the current
305 // selection and if the active widget listens it, the attribute value is errnoneous
307 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
309 aPropertyPanel->cleanContent();
311 myFeature->setStable(true);
313 SessionPtr aMgr = ModelAPI_Session::get();
314 /// Set current feature and remeber old current feature
317 aMgr->finishOperation();
323 afterCommitOperation();
329 void ModuleBase_OperationFeature::activateByPreselection()
331 if (myPreSelection.empty())
334 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
336 ModuleBase_ModelWidget* aFilledWgt = 0;
337 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
338 if (aPropertyPanel) {
339 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
340 if (!aWidgets.empty()) {
341 ModuleBase_ModelWidget* aWgt = 0;
342 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
344 // 1. apply the selection to controls
345 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
347 if (!aWgt->canSetValue())
349 aPropertyPanel->setPreselectionWidget(aWgt);
350 if (!aWgt->setSelection(myPreSelection, true)) {
358 aPropertyPanel->setPreselectionWidget(NULL);
359 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
360 // it is better to perform it not in setSelection of each widget, but do it here,
361 // after the preselection is processed
362 ModuleBase_ModelWidget::updateObject(myFeature);
364 // 3. a signal should be emitted before the next widget activation
365 // because, the activation of the next widget will give a focus to the widget. As a result
366 // the value of the widget is initialized. And commit may happens until the value is entered.
368 emit activatedByPreselection();
370 // 4. activate the next obligatory widget
371 aPropertyPanel->activateNextWidget(aFilledWgt);
377 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
379 myParentFeature = theParent;
382 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
384 return myParentFeature;
387 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
389 myPreviousCurrentFeature = theFeature;
392 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
394 return myPreviousCurrentFeature;
397 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
398 ModuleBase_IViewer* theViewer)
402 QList<ModuleBase_ViewerPrs> aPreSelected;
403 // Check that the selected result are not results of operation feature
404 FeaturePtr aFeature = feature();
406 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
408 std::list<ResultPtr> aResults = aFeature->results();
409 QObjectPtrList aResList;
410 std::list<ResultPtr>::const_iterator aIt;
411 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
412 aResList.append(*aIt);
414 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
415 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
416 aPreSelected.append(aPrs);
419 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
421 myPreSelection = aPreSelected;
424 void ModuleBase_OperationFeature::clearPreselection()
426 myPreSelection.clear();
429 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
431 ModuleBase_Operation::setPropertyPanel(theProp);
433 theProp->setEditingMode(isEditOperation());
436 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
437 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
438 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
439 ModuleBase_ModelWidget* aWgt = (*aWIt);
440 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
444 // Do not activate widgets by default if the current operation is editing operation
445 // Because we don't know which widget is going to be edited.
446 if (!isEditOperation()) {
447 // 4. activate the first obligatory widget
448 theProp->activateNextWidget(NULL);
451 // set focus on Ok button in order to operation manager could process Enter press
453 theProp->setFocusOnOkButton();