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 ModuleBase_Tools::translate(myFeature->getKind(), anError);
106 return anError.empty();
109 void ModuleBase_OperationFeature::startOperation()
111 FeaturePtr aFeature = feature();
112 if (!aFeature.get() || !isEditOperation())
115 if (aFeature.get() && isEditOperation())
116 aFeature->setStable(false);
118 myVisualizedObjects.clear();
119 // store hidden result features
120 std::list<ResultPtr> aResults = aFeature->results();
121 std::list<ResultPtr>::const_iterator aIt;
122 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
123 ObjectPtr anObject = *aIt;
124 if (anObject.get() && !anObject->isDisplayed()) {
125 myVisualizedObjects.insert(*aIt);
126 anObject->setDisplayed(true);
129 if (!aFeature->isDisplayed()) {
130 myVisualizedObjects.insert(aFeature);
131 aFeature->setDisplayed(true);
133 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
136 void ModuleBase_OperationFeature::stopOperation()
138 FeaturePtr aFeature = feature();
139 if (!aFeature.get() || !isEditOperation())
142 // store hidden result features
143 std::list<ResultPtr> aResults = aFeature->results();
144 std::list<ResultPtr>::const_iterator aIt;
145 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
146 ObjectPtr anObject = *aIt;
147 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
148 anObject->setDisplayed(false);
151 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
152 aFeature->setDisplayed(false);
154 if (myVisualizedObjects.size() > 0)
155 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
158 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
160 if (myParentFeature.get()) {
161 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
163 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
164 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
166 if (myFeature) { // TODO: generate an error if feature was not created
168 // Model update should call "execute" of a feature.
169 //myFeature->execute();
170 // Init default values
171 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
172 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
173 for (; anIt != aLast; anIt++) {
174 (*anIt)->storeValue(aFeature);
178 if (theFlushMessage) {
179 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
180 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
185 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
187 myFeature = theFeature;
191 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
193 FeaturePtr aFeature = feature();
195 if (aFeature == theObj)
197 std::list<ResultPtr> aResults = aFeature->results();
198 std::list<ResultPtr>::const_iterator aIt;
199 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
200 if (theObj == (*aIt))
203 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
204 if (aFeature->isMacro()) {
205 // macro feature may refers to sub-features, which also should be deactivated when the operation
206 // is active, e.g. rectangle'lines.
207 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
208 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
209 ModelAPI_AttributeRefList::typeId());
210 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
211 bool aFoundObject = false;
212 for (; anIt != aLast && !aFoundObject; anIt++) {
213 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
214 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
215 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
216 ObjectPtr anObject = aCurSelList->object(i);
217 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
218 if (aFeature.get()) {
219 aFoundObject = anObjectFeature == aFeature;
230 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
232 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
235 bool ModuleBase_OperationFeature::start()
237 #ifdef DEBUG_OPERATION_START
238 qDebug("ModuleBase_OperationFeature::start -- begin");
240 setIsModified(false);
241 QString anId = getDescription()->operationId();
243 anId = anId.append(EditSuffix());
245 ModelAPI_Session::get()->startOperation(anId.toStdString());
247 emit beforeStarted();
251 FeaturePtr aFeature = createFeature();
252 // if the feature is not created, there is no sense to start the operation
253 if (aFeature.get() == NULL) {
254 // it is necessary to abor the operation in the session and emit the aborted signal
255 // in order to update commands status in the workshop, to be exact the feature action
258 #ifdef DEBUG_OPERATION_START
259 qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
264 //Already called startOperation();
266 #ifdef DEBUG_OPERATION_START
267 qDebug("ModuleBase_OperationFeature::start -- end");
272 void ModuleBase_OperationFeature::abort()
274 #ifdef DEBUG_OPERATION_START
275 qDebug("ModuleBase_OperationFeature::abort -- begin");
278 emit beforeAborted();
280 // the viewer update should be blocked in order to avoid the features blinking before they are
282 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
283 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
284 Events_Loop::loop()->send(aMsg);
286 // the widgets of property panel should not process any events come from data mode
287 // after abort clicked. Some signal such as redisplay/create influence on content
288 // of the object browser and viewer context. Therefore it influence to the current
289 // selection and if the active widget listens it, the attribute value is errnoneous
291 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
293 aPropertyPanel->cleanContent();
296 myFeature->setStable(true);
301 SessionPtr aMgr = ModelAPI_Session::get();
302 aMgr->abortOperation();
304 // the viewer update should be unblocked in order to avoid the features blinking before they are
306 aMsg = std::shared_ptr<Events_Message>(
307 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
309 Events_Loop::loop()->send(aMsg);
312 #ifdef DEBUG_OPERATION_START
313 qDebug("ModuleBase_OperationFeature::abort -- end");
317 bool ModuleBase_OperationFeature::commit()
319 #ifdef DEBUG_OPERATION_START
320 qDebug("ModuleBase_OperationFeature::commit -- begin");
322 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
324 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
325 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
326 anActiveWidget->storeValue();
329 if (canBeCommitted()) {
330 emit beforeCommitted();
331 // the widgets of property panel should not process any events come from data mode
332 // after commit clicked. Some signal such as redisplay/create influence on content
333 // of the object browser and viewer context. Therefore it influence to the current
334 // selection and if the active widget listens it, the attribute value is errnoneous
336 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
338 aPropertyPanel->cleanContent();
340 myFeature->setStable(true);
342 SessionPtr aMgr = ModelAPI_Session::get();
343 /// Set current feature and remeber old current feature
346 aMgr->finishOperation();
352 afterCommitOperation();
353 #ifdef DEBUG_OPERATION_START
354 qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
358 #ifdef DEBUG_OPERATION_START
359 qDebug("ModuleBase_OperationFeature::commit -- end");
364 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
365 const std::string& theGreedAttributeId)
367 ModuleBase_ModelWidget* aWidget = 0;
368 if (myPreSelection.empty())
371 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
372 ModuleBase_ModelWidget* aFilledWgt = 0;
373 if (aPropertyPanel) {
374 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
375 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
376 ModuleBase_ModelWidget* aWgt = 0;
377 if (!aWidgets.empty()) {
378 // equal vertices should not be used here
379 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
381 if (!theGreedAttributeId.empty()) {
382 // set preselection to greed widget
383 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
385 if (aWgt->attributeID() == theGreedAttributeId) {
386 aPropertyPanel->setPreselectionWidget(aWgt);
387 if (aWgt->setSelection(myPreSelection, true)) {
388 aPropertyPanel->setPreselectionWidget(NULL);
392 else { // do not process invalid for greed widget selection
400 // 1. apply the selection to controls
401 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
403 if (!aWgt->canAcceptFocus())
405 aPropertyPanel->setPreselectionWidget(aWgt);
406 if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true)) {
415 aPropertyPanel->setPreselectionWidget(NULL);
416 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
417 // it is better to perform it not in setSelection of each widget, but do it here,
418 // after the preselection is processed
419 ModuleBase_Tools::flushUpdated(myFeature);
427 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
429 myParentFeature = theParent;
432 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
434 return myParentFeature;
437 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
439 myPreviousCurrentFeature = theFeature;
442 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
444 return myPreviousCurrentFeature;
447 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
448 ModuleBase_IViewer* theViewer)
450 QList<ModuleBase_ViewerPrsPtr> aPreSelected;
451 // Check that the selected result are not results of operation feature
452 FeaturePtr aFeature = feature();
454 QList<ModuleBase_ViewerPrsPtr> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
456 std::list<ResultPtr> aResults = aFeature->results();
457 QObjectPtrList aResList;
458 std::list<ResultPtr>::const_iterator aIt;
459 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
460 aResList.append(*aIt);
462 foreach (ModuleBase_ViewerPrsPtr aPrs, aSelected) {
463 if ((!aResList.contains(aPrs->object())) && (aPrs->object() != aFeature))
464 aPreSelected.append(aPrs);
467 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
469 setPreselection(aPreSelected);
472 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
475 myPreSelection = theValues;
478 void ModuleBase_OperationFeature::clearPreselection()
480 myPreSelection.clear();
483 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
485 ModuleBase_Operation::setPropertyPanel(theProp);
487 theProp->setEditingMode(isEditOperation());
490 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
491 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
492 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
493 ModuleBase_ModelWidget* aWgt = (*aWIt);
494 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
498 // Do not activate widgets by default if the current operation is editing operation
499 // Because we don't know which widget is going to be edited.
500 if (!isEditOperation()) {
501 // 4. activate the first obligatory widget
502 theProp->activateNextWidget(NULL);
505 // set focus on Ok button in order to operation manager could process Enter press
507 theProp->setFocusOnOkButton();