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;
121 ModelAPI_Tools::allResults(aFeature, aResults);
122 std::list<ResultPtr>::const_iterator aIt;
123 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
124 ObjectPtr anObject = *aIt;
125 if (anObject.get() && !anObject->isDisplayed()) {
126 myVisualizedObjects.insert(*aIt);
127 anObject->setDisplayed(true);
130 if (!aFeature->isDisplayed()) {
131 myVisualizedObjects.insert(aFeature);
132 aFeature->setDisplayed(true);
134 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
137 void ModuleBase_OperationFeature::stopOperation()
139 FeaturePtr aFeature = feature();
140 if (!aFeature.get() || !isEditOperation())
143 // store hidden result features
144 std::list<ResultPtr> aResults;
145 ModelAPI_Tools::allResults(aFeature, aResults);
146 std::list<ResultPtr>::const_iterator aIt;
147 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
148 ObjectPtr anObject = *aIt;
149 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
150 anObject->setDisplayed(false);
153 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
154 aFeature->setDisplayed(false);
156 if (myVisualizedObjects.size() > 0)
157 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
160 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
162 if (myParentFeature.get()) {
163 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
165 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
166 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
168 if (myFeature) { // TODO: generate an error if feature was not created
170 // Model update should call "execute" of a feature.
171 //myFeature->execute();
172 // Init default values
173 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
174 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
175 for (; anIt != aLast; anIt++) {
176 (*anIt)->storeValue(aFeature);
180 if (theFlushMessage) {
181 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
182 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
187 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
189 myFeature = theFeature;
193 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
195 FeaturePtr aFeature = feature();
197 if (aFeature == theObj)
199 std::list<ResultPtr> aResults;
200 ModelAPI_Tools::allResults(aFeature, aResults);
201 std::list<ResultPtr>::const_iterator aIt;
202 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
203 ResultPtr aResult = *aIt;
204 if (theObj == aResult)
207 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
208 if (aFeature->isMacro()) {
209 // macro feature may refers to sub-features, which also should be deactivated when the operation
210 // is active, e.g. rectangle'lines.
211 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
212 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
213 ModelAPI_AttributeRefList::typeId());
214 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
215 bool aFoundObject = false;
216 for (; anIt != aLast && !aFoundObject; anIt++) {
217 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
218 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
219 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
220 ObjectPtr anObject = aCurSelList->object(i);
221 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
222 if (aFeature.get()) {
223 aFoundObject = anObjectFeature == aFeature;
234 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
236 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
239 bool ModuleBase_OperationFeature::start()
241 #ifdef DEBUG_OPERATION_START
242 qDebug("ModuleBase_OperationFeature::start -- begin");
244 setIsModified(false);
245 QString anId = getDescription()->operationId();
247 anId = anId.append(EditSuffix());
249 ModelAPI_Session::get()->startOperation(anId.toStdString());
251 emit beforeStarted();
255 FeaturePtr aFeature = createFeature();
256 // if the feature is not created, there is no sense to start the operation
257 if (aFeature.get() == NULL) {
258 // it is necessary to abor the operation in the session and emit the aborted signal
259 // in order to update commands status in the workshop, to be exact the feature action
262 #ifdef DEBUG_OPERATION_START
263 qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
268 //Already called startOperation();
270 #ifdef DEBUG_OPERATION_START
271 qDebug("ModuleBase_OperationFeature::start -- end");
276 void ModuleBase_OperationFeature::abort()
278 #ifdef DEBUG_OPERATION_START
279 qDebug("ModuleBase_OperationFeature::abort -- begin");
282 emit beforeAborted();
284 // the viewer update should be blocked in order to avoid the features blinking before they are
286 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
287 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
288 Events_Loop::loop()->send(aMsg);
290 // the widgets of property panel should not process any events come from data mode
291 // after abort clicked. Some signal such as redisplay/create influence on content
292 // of the object browser and viewer context. Therefore it influence to the current
293 // selection and if the active widget listens it, the attribute value is errnoneous
295 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
297 aPropertyPanel->cleanContent();
300 myFeature->setStable(true);
305 SessionPtr aMgr = ModelAPI_Session::get();
306 aMgr->abortOperation();
308 // the viewer update should be unblocked in order to avoid the features blinking before they are
310 aMsg = std::shared_ptr<Events_Message>(
311 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
313 Events_Loop::loop()->send(aMsg);
316 #ifdef DEBUG_OPERATION_START
317 qDebug("ModuleBase_OperationFeature::abort -- end");
321 bool ModuleBase_OperationFeature::commit()
323 #ifdef DEBUG_OPERATION_START
324 qDebug("ModuleBase_OperationFeature::commit -- begin");
326 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
328 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
329 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
330 anActiveWidget->storeValue();
333 if (canBeCommitted()) {
334 emit beforeCommitted();
335 // the widgets of property panel should not process any events come from data mode
336 // after commit clicked. Some signal such as redisplay/create influence on content
337 // of the object browser and viewer context. Therefore it influence to the current
338 // selection and if the active widget listens it, the attribute value is errnoneous
340 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
342 aPropertyPanel->cleanContent();
344 myFeature->setStable(true);
346 SessionPtr aMgr = ModelAPI_Session::get();
347 /// Set current feature and remeber old current feature
350 aMgr->finishOperation();
356 afterCommitOperation();
357 #ifdef DEBUG_OPERATION_START
358 qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
362 #ifdef DEBUG_OPERATION_START
363 qDebug("ModuleBase_OperationFeature::commit -- end");
368 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
369 const std::string& theGreedAttributeId)
371 ModuleBase_ModelWidget* aWidget = 0;
372 if (myPreSelection.empty())
375 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
376 ModuleBase_ModelWidget* aFilledWgt = 0;
377 if (aPropertyPanel) {
378 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
379 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
380 ModuleBase_ModelWidget* aWgt = 0;
381 if (!aWidgets.empty()) {
382 // equal vertices should not be used here
383 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
385 if (!theGreedAttributeId.empty()) {
386 // set preselection to greed widget
387 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
389 if (aWgt->attributeID() == theGreedAttributeId) {
390 aPropertyPanel->setPreselectionWidget(aWgt);
391 if (aWgt->setSelection(myPreSelection, true)) {
392 aPropertyPanel->setPreselectionWidget(NULL);
396 else { // do not process invalid for greed widget selection
404 // 1. apply the selection to controls
405 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
407 if (!aWgt->canAcceptFocus())
409 aPropertyPanel->setPreselectionWidget(aWgt);
410 if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true)) {
419 aPropertyPanel->setPreselectionWidget(NULL);
420 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
421 // it is better to perform it not in setSelection of each widget, but do it here,
422 // after the preselection is processed
423 ModuleBase_Tools::flushUpdated(myFeature);
431 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
433 myParentFeature = theParent;
436 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
438 return myParentFeature;
441 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
443 myPreviousCurrentFeature = theFeature;
446 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
448 return myPreviousCurrentFeature;
451 void ModuleBase_OperationFeature::initSelection(const QList<ModuleBase_ViewerPrsPtr>& thePreSelected)
453 QObjectPtrList aCurrentFeatureResults;
455 // Check that the selected result are not results of operation feature
456 FeaturePtr aFeature = feature();
458 std::list<ResultPtr> aResults;
459 ModelAPI_Tools::allResults(aFeature, aResults);
460 std::list<ResultPtr>::const_iterator aIt;
461 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
462 aCurrentFeatureResults.append(*aIt);
465 if (aCurrentFeatureResults.empty()) /// filtering of selection is not necessary
466 setPreselection(thePreSelected);
467 else { // create preselection list without results of current feature
468 QList<ModuleBase_ViewerPrsPtr> aPreSelected;
469 foreach (ModuleBase_ViewerPrsPtr aPrs, thePreSelected) {
470 if ((!aCurrentFeatureResults.contains(aPrs->object())) && (aPrs->object() != aFeature))
471 aPreSelected.append(aPrs);
473 setPreselection(aPreSelected);
477 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
480 myPreSelection = theValues;
483 void ModuleBase_OperationFeature::clearPreselection()
485 myPreSelection.clear();
488 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
490 ModuleBase_Operation::setPropertyPanel(theProp);
492 theProp->setEditingMode(isEditOperation());
495 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
496 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
497 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
498 ModuleBase_ModelWidget* aWgt = (*aWIt);
499 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
500 connect(aWgt, SIGNAL(valueStateChanged(int)), this, SLOT(onValueStateChanged(int)));
504 // Do not activate widgets by default if the current operation is editing operation
505 // Because we don't know which widget is going to be edited.
506 if (!isEditOperation()) {
507 // 4. activate the first obligatory widget
508 theProp->activateNextWidget(NULL);
511 // set focus on Ok button in order to operation manager could process Enter press
513 theProp->setFocusOnOkButton();