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"
18 #include "ModuleBase_Tools.h"
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
32 #include <GeomAPI_Pnt2d.h>
34 #include <Events_Loop.h>
38 // the define to check the activated object as a sub-feature by argument of
39 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
40 #define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
41 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
42 #include <ModelAPI_AttributeRefList.h>
45 //#define DEBUG_OPERATION_START
51 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
52 : ModuleBase_Operation(theId, theParent),
57 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
62 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
63 /*const bool theRestartTransaction*/)
65 bool isCurrentEditState = isEditOperation();
66 if (isCurrentEditState == isEditState)
70 // this case is obsolete as it was not approved for reentrant sketch operation
71 // it was implemented when isEditState did not exist and only edit operation can be set
72 if (theRestartTransaction) {
73 // finsh previous create operation
74 emit beforeCommitted();
75 SessionPtr aMgr = ModelAPI_Session::get();
76 ModelAPI_Session::get()->finishOperation();
78 // start new edit operation
80 QString anId = getDescription()->operationId();
82 anId = anId.append(EditSuffix());
84 ModelAPI_Session::get()->startOperation(anId.toStdString());
87 myIsEditing = isEditState;
89 propertyPanel()->setEditingMode(isEditOperation());
92 FeaturePtr ModuleBase_OperationFeature::feature() const
97 bool ModuleBase_OperationFeature::isValid() const
99 if (!myFeature || !myFeature->data()->isValid())
100 return true; // rename operation
101 if (myFeature->isAction())
104 std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
105 return anError.empty();
108 void ModuleBase_OperationFeature::startOperation()
110 FeaturePtr aFeature = feature();
111 if (!aFeature.get() || !isEditOperation())
114 if (aFeature.get() && isEditOperation())
115 aFeature->setStable(false);
117 myVisualizedObjects.clear();
118 // store hidden result features
119 std::list<ResultPtr> aResults = aFeature->results();
120 std::list<ResultPtr>::const_iterator aIt;
121 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
122 ObjectPtr anObject = *aIt;
123 if (anObject.get() && !anObject->isDisplayed()) {
124 myVisualizedObjects.insert(*aIt);
125 anObject->setDisplayed(true);
128 if (!aFeature->isDisplayed()) {
129 myVisualizedObjects.insert(aFeature);
130 aFeature->setDisplayed(true);
132 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
135 void ModuleBase_OperationFeature::stopOperation()
137 FeaturePtr aFeature = feature();
138 if (!aFeature.get() || !isEditOperation())
141 // store hidden result features
142 std::list<ResultPtr> aResults = aFeature->results();
143 std::list<ResultPtr>::const_iterator aIt;
144 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
145 ObjectPtr anObject = *aIt;
146 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
147 anObject->setDisplayed(false);
150 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
151 aFeature->setDisplayed(false);
153 if (myVisualizedObjects.size() > 0)
154 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
157 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
159 if (myParentFeature.get()) {
160 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
162 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
163 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
165 if (myFeature) { // TODO: generate an error if feature was not created
167 // Model update should call "execute" of a feature.
168 //myFeature->execute();
169 // Init default values
170 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
171 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
172 for (; anIt != aLast; anIt++) {
173 (*anIt)->storeValue(aFeature);
177 if (theFlushMessage) {
178 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
179 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
184 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
186 myFeature = theFeature;
190 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
192 FeaturePtr aFeature = feature();
194 if (aFeature == theObj)
196 std::list<ResultPtr> aResults = aFeature->results();
197 std::list<ResultPtr>::const_iterator aIt;
198 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
199 if (theObj == (*aIt))
202 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
203 if (aFeature->isMacro()) {
204 // macro feature may refers to sub-features, which also should be deactivated when the operation
205 // is active, e.g. rectangle'lines.
206 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
207 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
208 ModelAPI_AttributeRefList::typeId());
209 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
210 bool aFoundObject = false;
211 for (; anIt != aLast && !aFoundObject; anIt++) {
212 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
213 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
214 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
215 ObjectPtr anObject = aCurSelList->object(i);
216 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
217 if (aFeature.get()) {
218 aFoundObject = anObjectFeature == aFeature;
229 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
231 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
234 bool ModuleBase_OperationFeature::start()
236 #ifdef DEBUG_OPERATION_START
237 qDebug("ModuleBase_OperationFeature::start -- begin");
239 setIsModified(false);
240 QString anId = getDescription()->operationId();
242 anId = anId.append(EditSuffix());
244 ModelAPI_Session::get()->startOperation(anId.toStdString());
246 emit beforeStarted();
250 FeaturePtr aFeature = createFeature();
251 // if the feature is not created, there is no sense to start the operation
252 if (aFeature.get() == NULL) {
253 // it is necessary to abor the operation in the session and emit the aborted signal
254 // in order to update commands status in the workshop, to be exact the feature action
257 #ifdef DEBUG_OPERATION_START
258 qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
263 //Already called startOperation();
265 #ifdef DEBUG_OPERATION_START
266 qDebug("ModuleBase_OperationFeature::start -- end");
271 void ModuleBase_OperationFeature::abort()
273 #ifdef DEBUG_OPERATION_START
274 qDebug("ModuleBase_OperationFeature::abort -- begin");
277 emit beforeAborted();
279 // the viewer update should be blocked in order to avoid the features blinking before they are
281 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
282 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
283 Events_Loop::loop()->send(aMsg);
285 // the widgets of property panel should not process any events come from data mode
286 // after abort clicked. Some signal such as redisplay/create influence on content
287 // of the object browser and viewer context. Therefore it influence to the current
288 // selection and if the active widget listens it, the attribute value is errnoneous
290 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
292 aPropertyPanel->cleanContent();
295 myFeature->setStable(true);
300 SessionPtr aMgr = ModelAPI_Session::get();
301 aMgr->abortOperation();
303 // the viewer update should be unblocked in order to avoid the features blinking before they are
305 aMsg = std::shared_ptr<Events_Message>(
306 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
308 Events_Loop::loop()->send(aMsg);
311 #ifdef DEBUG_OPERATION_START
312 qDebug("ModuleBase_OperationFeature::abort -- end");
316 bool ModuleBase_OperationFeature::commit()
318 #ifdef DEBUG_OPERATION_START
319 qDebug("ModuleBase_OperationFeature::commit -- begin");
321 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
323 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
324 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
325 anActiveWidget->storeValue();
328 if (canBeCommitted()) {
329 emit beforeCommitted();
330 // the widgets of property panel should not process any events come from data mode
331 // after commit clicked. Some signal such as redisplay/create influence on content
332 // of the object browser and viewer context. Therefore it influence to the current
333 // selection and if the active widget listens it, the attribute value is errnoneous
335 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
337 aPropertyPanel->cleanContent();
339 myFeature->setStable(true);
341 SessionPtr aMgr = ModelAPI_Session::get();
342 /// Set current feature and remeber old current feature
345 aMgr->finishOperation();
351 afterCommitOperation();
352 #ifdef DEBUG_OPERATION_START
353 qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
357 #ifdef DEBUG_OPERATION_START
358 qDebug("ModuleBase_OperationFeature::commit -- end");
363 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
364 const std::string& theGreedAttributeId)
366 ModuleBase_ModelWidget* aWidget = 0;
367 if (myPreSelection.empty())
370 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
371 ModuleBase_ModelWidget* aFilledWgt = 0;
372 if (aPropertyPanel) {
373 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
374 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
375 ModuleBase_ModelWidget* aWgt = 0;
376 if (!aWidgets.empty()) {
377 // equal vertices should not be used here
378 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
380 if (!theGreedAttributeId.empty()) {
381 // set preselection to greed widget
382 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
384 if (aWgt->attributeID() == theGreedAttributeId) {
385 aPropertyPanel->setPreselectionWidget(aWgt);
386 aWgt->setSelection(myPreSelection, true);
387 aPropertyPanel->setPreselectionWidget(NULL);
395 // 1. apply the selection to controls
396 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
398 if (!aWgt->canAcceptFocus())
400 aPropertyPanel->setPreselectionWidget(aWgt);
401 if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true)) {
410 aPropertyPanel->setPreselectionWidget(NULL);
411 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
412 // it is better to perform it not in setSelection of each widget, but do it here,
413 // after the preselection is processed
414 ModuleBase_Tools::flushUpdated(myFeature);
422 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
424 myParentFeature = theParent;
427 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
429 return myParentFeature;
432 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
434 myPreviousCurrentFeature = theFeature;
437 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
439 return myPreviousCurrentFeature;
442 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
443 ModuleBase_IViewer* theViewer)
445 QList<ModuleBase_ViewerPrsPtr> aPreSelected;
446 // Check that the selected result are not results of operation feature
447 FeaturePtr aFeature = feature();
449 QList<ModuleBase_ViewerPrsPtr> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
451 std::list<ResultPtr> aResults = aFeature->results();
452 QObjectPtrList aResList;
453 std::list<ResultPtr>::const_iterator aIt;
454 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
455 aResList.append(*aIt);
457 foreach (ModuleBase_ViewerPrsPtr aPrs, aSelected) {
458 if ((!aResList.contains(aPrs->object())) && (aPrs->object() != aFeature))
459 aPreSelected.append(aPrs);
462 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
464 setPreselection(aPreSelected);
467 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
470 myPreSelection = theValues;
473 void ModuleBase_OperationFeature::clearPreselection()
475 myPreSelection.clear();
478 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
480 ModuleBase_Operation::setPropertyPanel(theProp);
482 theProp->setEditingMode(isEditOperation());
485 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
486 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
487 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
488 ModuleBase_ModelWidget* aWgt = (*aWIt);
489 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
493 // Do not activate widgets by default if the current operation is editing operation
494 // Because we don't know which widget is going to be edited.
495 if (!isEditOperation()) {
496 // 4. activate the first obligatory widget
497 theProp->activateNextWidget(NULL);
500 // set focus on Ok button in order to operation manager could process Enter press
502 theProp->setFocusOnOkButton();