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,
210 // which also should be deactivated when the operation
211 // is active, e.g. rectangle'lines.
212 FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
213 std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
214 ModelAPI_AttributeRefList::typeId());
215 std::list<AttributePtr>::const_iterator
216 anIt = anAttributes.begin(), aLast = anAttributes.end();
217 bool aFoundObject = false;
218 for (; anIt != aLast && !aFoundObject; anIt++) {
219 std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
220 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
221 for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
222 ObjectPtr anObject = aCurSelList->object(i);
223 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
224 if (aFeature.get()) {
225 aFoundObject = anObjectFeature == aFeature;
236 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
238 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
241 bool ModuleBase_OperationFeature::start()
243 #ifdef DEBUG_OPERATION_START
244 qDebug("ModuleBase_OperationFeature::start -- begin");
246 setIsModified(false);
247 QString anId = getDescription()->operationId();
249 anId = anId.append(EditSuffix());
251 ModelAPI_Session::get()->startOperation(anId.toStdString());
253 emit beforeStarted();
257 FeaturePtr aFeature = createFeature();
258 // if the feature is not created, there is no sense to start the operation
259 if (aFeature.get() == NULL) {
260 // it is necessary to abor the operation in the session and emit the aborted signal
261 // in order to update commands status in the workshop, to be exact the feature action
264 #ifdef DEBUG_OPERATION_START
265 qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
270 //Already called startOperation();
272 #ifdef DEBUG_OPERATION_START
273 qDebug("ModuleBase_OperationFeature::start -- end");
278 void ModuleBase_OperationFeature::abort()
280 #ifdef DEBUG_OPERATION_START
281 qDebug("ModuleBase_OperationFeature::abort -- begin");
284 emit beforeAborted();
286 // the viewer update should be blocked in order to avoid the features blinking before they are
288 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
289 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
290 Events_Loop::loop()->send(aMsg);
292 // the widgets of property panel should not process any events come from data mode
293 // after abort clicked. Some signal such as redisplay/create influence on content
294 // of the object browser and viewer context. Therefore it influence to the current
295 // selection and if the active widget listens it, the attribute value is errnoneous
297 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
299 aPropertyPanel->cleanContent();
302 myFeature->setStable(true);
307 SessionPtr aMgr = ModelAPI_Session::get();
308 aMgr->abortOperation();
310 // the viewer update should be unblocked in order to avoid the features blinking before they are
312 aMsg = std::shared_ptr<Events_Message>(
313 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
315 Events_Loop::loop()->send(aMsg);
318 #ifdef DEBUG_OPERATION_START
319 qDebug("ModuleBase_OperationFeature::abort -- end");
323 bool ModuleBase_OperationFeature::commit()
325 #ifdef DEBUG_OPERATION_START
326 qDebug("ModuleBase_OperationFeature::commit -- begin");
328 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
330 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
331 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
332 anActiveWidget->storeValue();
335 if (canBeCommitted()) {
336 emit beforeCommitted();
337 // the widgets of property panel should not process any events come from data mode
338 // after commit clicked. Some signal such as redisplay/create influence on content
339 // of the object browser and viewer context. Therefore it influence to the current
340 // selection and if the active widget listens it, the attribute value is errnoneous
342 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
344 aPropertyPanel->cleanContent();
346 myFeature->setStable(true);
348 SessionPtr aMgr = ModelAPI_Session::get();
349 /// Set current feature and remeber old current feature
352 aMgr->finishOperation();
358 afterCommitOperation();
359 #ifdef DEBUG_OPERATION_START
360 qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
364 #ifdef DEBUG_OPERATION_START
365 qDebug("ModuleBase_OperationFeature::commit -- end");
370 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
371 const std::string& theGreedAttributeId)
373 ModuleBase_ModelWidget* aWidget = 0;
374 if (myPreSelection.empty())
377 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
378 ModuleBase_ModelWidget* aFilledWgt = 0;
379 if (aPropertyPanel) {
380 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
381 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
382 ModuleBase_ModelWidget* aWgt = 0;
383 if (!aWidgets.empty()) {
384 // equal vertices should not be used here
385 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
387 if (!theGreedAttributeId.empty()) {
388 // set preselection to greed widget
389 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
391 if (aWgt->attributeID() == theGreedAttributeId) {
392 aPropertyPanel->setPreselectionWidget(aWgt);
393 if (aWgt->setSelection(myPreSelection, true)) {
394 aPropertyPanel->setPreselectionWidget(NULL);
398 else { // do not process invalid for greed widget selection
406 // 1. apply the selection to controls
407 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
409 if (!aWgt->canAcceptFocus())
411 aPropertyPanel->setPreselectionWidget(aWgt);
412 if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true)) {
421 aPropertyPanel->setPreselectionWidget(NULL);
422 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
423 // it is better to perform it not in setSelection of each widget, but do it here,
424 // after the preselection is processed
425 ModuleBase_Tools::flushUpdated(myFeature);
433 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
435 myParentFeature = theParent;
438 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
440 return myParentFeature;
443 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
445 myPreviousCurrentFeature = theFeature;
448 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
450 return myPreviousCurrentFeature;
453 void ModuleBase_OperationFeature::initSelection(
454 const QList<ModuleBase_ViewerPrsPtr>& thePreSelected)
456 QObjectPtrList aCurrentFeatureResults;
458 // Check that the selected result are not results of operation feature
459 FeaturePtr aFeature = feature();
461 std::list<ResultPtr> aResults;
462 ModelAPI_Tools::allResults(aFeature, aResults);
463 std::list<ResultPtr>::const_iterator aIt;
464 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
465 aCurrentFeatureResults.append(*aIt);
468 if (aCurrentFeatureResults.empty()) /// filtering of selection is not necessary
469 setPreselection(thePreSelected);
470 else { // create preselection list without results of current feature
471 QList<ModuleBase_ViewerPrsPtr> aPreSelected;
472 foreach (ModuleBase_ViewerPrsPtr aPrs, thePreSelected) {
473 if ((!aCurrentFeatureResults.contains(aPrs->object())) && (aPrs->object() != aFeature))
474 aPreSelected.append(aPrs);
476 setPreselection(aPreSelected);
480 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
483 myPreSelection = theValues;
486 void ModuleBase_OperationFeature::clearPreselection()
488 myPreSelection.clear();
491 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
493 ModuleBase_Operation::setPropertyPanel(theProp);
495 theProp->setEditingMode(isEditOperation());
498 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
499 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
500 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
501 ModuleBase_ModelWidget* aWgt = (*aWIt);
502 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
503 connect(aWgt, SIGNAL(valueStateChanged(int)), this, SLOT(onValueStateChanged(int)));
507 // Do not activate widgets by default if the current operation is editing operation
508 // Because we don't know which widget is going to be edited.
509 if (!isEditOperation()) {
510 // 4. activate the first obligatory widget
511 theProp->activateNextWidget(NULL);
514 // set focus on Ok button in order to operation manager could process Enter press
516 theProp->setFocusOnOkButton();