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>
41 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
42 : ModuleBase_Operation(theId, theParent),
47 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
52 void ModuleBase_OperationFeature::setEditOperation(const bool theRestartTransaction)
54 if (isEditOperation())
57 if (theRestartTransaction) {
58 // finsh previous create operation
59 emit beforeCommitted();
60 SessionPtr aMgr = ModelAPI_Session::get();
61 ModelAPI_Session::get()->finishOperation();
63 // start new edit operation
65 QString anId = getDescription()->operationId();
67 anId = anId.append(EditSuffix());
69 ModelAPI_Session::get()->startOperation(anId.toStdString());
75 propertyPanel()->setEditingMode(isEditOperation());
78 FeaturePtr ModuleBase_OperationFeature::feature() const
83 bool ModuleBase_OperationFeature::isValid() const
85 if (!myFeature || !myFeature->data()->isValid())
86 return true; // rename operation
87 if (myFeature->isAction())
90 std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
91 return anError.empty();
94 void ModuleBase_OperationFeature::startOperation()
96 FeaturePtr aFeature = feature();
97 if (!aFeature.get() || !isEditOperation())
100 if (aFeature.get() && isEditOperation())
101 aFeature->setStable(false);
103 myVisualizedObjects.clear();
104 // store hidden result features
105 std::list<ResultPtr> aResults = aFeature->results();
106 std::list<ResultPtr>::const_iterator aIt;
107 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
108 ObjectPtr anObject = *aIt;
109 if (anObject.get() && !anObject->isDisplayed()) {
110 myVisualizedObjects.insert(*aIt);
111 anObject->setDisplayed(true);
114 if (!aFeature->isDisplayed()) {
115 myVisualizedObjects.insert(aFeature);
116 aFeature->setDisplayed(true);
118 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
121 void ModuleBase_OperationFeature::stopOperation()
123 FeaturePtr aFeature = feature();
124 if (!aFeature.get() || !isEditOperation())
127 // store hidden result features
128 std::list<ResultPtr> aResults = aFeature->results();
129 std::list<ResultPtr>::const_iterator aIt;
130 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
131 ObjectPtr anObject = *aIt;
132 if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
133 anObject->setDisplayed(false);
136 if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
137 aFeature->setDisplayed(false);
139 if (myVisualizedObjects.size() > 0)
140 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
143 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
145 if (myParentFeature.get()) {
146 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
148 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
149 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
151 if (myFeature) { // TODO: generate an error if feature was not created
153 // Model update should call "execute" of a feature.
154 //myFeature->execute();
155 // Init default values
156 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
157 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
158 for (; anIt != aLast; anIt++) {
159 (*anIt)->storeValue(aFeature);
164 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
168 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
170 myFeature = theFeature;
174 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
176 FeaturePtr aFeature = feature();
178 if (aFeature == theObj)
180 std::list<ResultPtr> aResults = aFeature->results();
181 std::list<ResultPtr>::const_iterator aIt;
182 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
183 if (theObj == (*aIt))
190 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
192 return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
195 bool ModuleBase_OperationFeature::start()
197 setIsModified(false);
198 QString anId = getDescription()->operationId();
200 anId = anId.append(EditSuffix());
202 ModelAPI_Session::get()->startOperation(anId.toStdString());
204 emit beforeStarted();
208 FeaturePtr aFeature = createFeature();
209 // if the feature is not created, there is no sense to start the operation
210 if (aFeature.get() == NULL) {
211 // it is necessary to abor the operation in the session and emit the aborted signal
212 // in order to update commands status in the workshop, to be exact the feature action
218 //Already called startOperation();
223 void ModuleBase_OperationFeature::abort()
225 emit beforeAborted();
227 // the viewer update should be blocked in order to avoid the features blinking before they are
229 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
230 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
231 Events_Loop::loop()->send(aMsg);
233 // the widgets of property panel should not process any events come from data mode
234 // after abort clicked. Some signal such as redisplay/create influence on content
235 // of the object browser and viewer context. Therefore it influence to the current
236 // selection and if the active widget listens it, the attribute value is errnoneous
238 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
240 aPropertyPanel->cleanContent();
243 myFeature->setStable(true);
248 SessionPtr aMgr = ModelAPI_Session::get();
249 aMgr->abortOperation();
251 // the viewer update should be unblocked in order to avoid the features blinking before they are
253 aMsg = std::shared_ptr<Events_Message>(
254 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
256 Events_Loop::loop()->send(aMsg);
261 bool ModuleBase_OperationFeature::commit()
263 ModuleBase_IPropertyPanel* aPanel = propertyPanel();
265 ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
266 if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
267 anActiveWidget->storeValue();
270 if (canBeCommitted()) {
271 emit beforeCommitted();
272 // the widgets of property panel should not process any events come from data mode
273 // after commit clicked. Some signal such as redisplay/create influence on content
274 // of the object browser and viewer context. Therefore it influence to the current
275 // selection and if the active widget listens it, the attribute value is errnoneous
277 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
279 aPropertyPanel->cleanContent();
281 myFeature->setStable(true);
283 SessionPtr aMgr = ModelAPI_Session::get();
284 /// Set current feature and remeber old current feature
287 aMgr->finishOperation();
293 afterCommitOperation();
299 void ModuleBase_OperationFeature::activateByPreselection()
301 if (myPreSelection.empty())
304 ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
306 ModuleBase_ModelWidget* aFilledWgt = 0;
307 ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
308 if (aPropertyPanel) {
309 const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
310 if (!aWidgets.empty()) {
311 ModuleBase_ModelWidget* aWgt = 0;
312 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
314 // 1. apply the selection to controls
315 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
317 if (!aWgt->canSetValue())
319 aPropertyPanel->setPreselectionWidget(aWgt);
320 if (!aWgt->setSelection(myPreSelection, true)) {
328 aPropertyPanel->setPreselectionWidget(NULL);
329 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
330 // it is better to perform it not in setSelection of each widget, but do it here,
331 // after the preselection is processed
332 ModuleBase_ModelWidget::updateObject(myFeature);
334 // 3. a signal should be emitted before the next widget activation
335 // because, the activation of the next widget will give a focus to the widget. As a result
336 // the value of the widget is initialized. And commit may happens until the value is entered.
338 emit activatedByPreselection();
340 // 4. activate the next obligatory widget
341 aPropertyPanel->activateNextWidget(aFilledWgt);
347 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
349 myParentFeature = theParent;
352 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
354 return myParentFeature;
357 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
359 myPreviousCurrentFeature = theFeature;
362 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
364 return myPreviousCurrentFeature;
367 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
368 ModuleBase_IViewer* theViewer)
372 QList<ModuleBase_ViewerPrs> aPreSelected;
373 // Check that the selected result are not results of operation feature
374 FeaturePtr aFeature = feature();
376 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
378 std::list<ResultPtr> aResults = aFeature->results();
379 QObjectPtrList aResList;
380 std::list<ResultPtr>::const_iterator aIt;
381 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
382 aResList.append(*aIt);
384 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
385 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
386 aPreSelected.append(aPrs);
389 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
391 myPreSelection = aPreSelected;
394 void ModuleBase_OperationFeature::clearPreselection()
396 myPreSelection.clear();
399 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
401 ModuleBase_Operation::setPropertyPanel(theProp);
403 theProp->setEditingMode(isEditOperation());
406 const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
407 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
408 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
409 ModuleBase_ModelWidget* aWgt = (*aWIt);
410 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
414 // Do not activate widgets by default if the current operation is editing operation
415 // Because we don't know which widget is going to be edited.
416 if (!isEditOperation()) {
417 // 4. activate the first obligatory widget
418 theProp->activateNextWidget(NULL);
421 // set focus on Ok button in order to operation manager could process Enter press
423 theProp->setFocusOnOkButton();