1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_Operation.cpp
6 * Created on: Apr 2, 2014
10 #include "ModuleBase_Operation.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>
30 #include <GeomAPI_Pnt2d.h>
32 #include <Events_Loop.h>
40 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
46 myDescription = new ModuleBase_OperationDescription(theId);
49 ModuleBase_Operation::~ModuleBase_Operation()
55 QString ModuleBase_Operation::id() const
57 return getDescription()->operationId();
60 FeaturePtr ModuleBase_Operation::feature() const
65 bool ModuleBase_Operation::isValid() const
67 if (!myFeature || !myFeature->data()->isValid())
68 return true; // rename operation
69 if (myFeature->isAction())
71 //Get validators for the Id
72 SessionPtr aMgr = ModelAPI_Session::get();
73 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74 bool aValid = aFactory->validate(myFeature);
76 // the feature exec state should be checked in order to do not apply features, which result can not
77 // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
78 bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
79 || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
81 return aValid && isDone;
85 bool ModuleBase_Operation::canBeCommitted() const
90 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
92 if (myParentFeature.get()) {
93 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
95 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
96 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
98 if (myFeature) { // TODO: generate an error if feature was not created
100 // Model update should call "execute" of a feature.
101 //myFeature->execute();
102 // Init default values
103 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
104 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
105 for (; anIt != aLast; anIt++) {
106 (*anIt)->storeValue(aFeature);
111 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
115 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
117 myFeature = theFeature;
121 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
123 FeaturePtr aFeature = feature();
125 if (aFeature == theObj)
127 std::list<ResultPtr> aResults = aFeature->results();
128 std::list<ResultPtr>::const_iterator aIt;
129 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
130 if (theObj == (*aIt))
137 void ModuleBase_Operation::start()
139 myIsModified = false;
140 QString anId = getDescription()->operationId();
142 anId = anId.append(EditSuffix());
144 ModelAPI_Session::get()->startOperation(anId.toStdString());
147 FeaturePtr aFeature = createFeature();
148 // if the feature is not created, there is no sense to start the operation
149 if (aFeature.get() == NULL) {
150 // it is necessary to abor the operation in the session and emit the aborted signal
151 // in order to update commands status in the workshop, to be exact the feature action
157 /// Set current feature and remeber old current feature
159 SessionPtr aMgr = ModelAPI_Session::get();
160 DocumentPtr aDoc = aMgr->activeDocument();
161 myCurrentFeature = aDoc->currentFeature(true);
162 aDoc->setCurrentFeature(feature(), false);
170 void ModuleBase_Operation::postpone()
176 void ModuleBase_Operation::resume()
182 void ModuleBase_Operation::abort()
184 // the viewer update should be blocked in order to avoid the features blinking before they are
186 std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
187 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
188 Events_Loop::loop()->send(aMsg);
190 SessionPtr aMgr = ModelAPI_Session::get();
192 DocumentPtr aDoc = aMgr->activeDocument();
193 bool aIsOp = aMgr->isOperation();
195 aMgr->startOperation();
196 aDoc->setCurrentFeature(myCurrentFeature, true);
198 aMgr->finishOperation();
199 myCurrentFeature = FeaturePtr();
204 // is is necessary to deactivate current widgets before the model operation is aborted
205 // because abort removes the feature and activated filters should not check it
206 propertyPanel()->cleanContent();
208 aMgr->abortOperation();
210 // the viewer update should be unblocked in order to avoid the features blinking before they are
212 aMsg = std::shared_ptr<Events_Message>(
213 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
215 Events_Loop::loop()->send(aMsg);
220 bool ModuleBase_Operation::commit()
222 if (canBeCommitted()) {
223 SessionPtr aMgr = ModelAPI_Session::get();
224 /// Set current feature and remeber old current feature
226 DocumentPtr aDoc = aMgr->activeDocument();
227 bool aIsOp = aMgr->isOperation();
229 aMgr->startOperation();
230 aDoc->setCurrentFeature(myCurrentFeature, true);
232 aMgr->finishOperation();
233 myCurrentFeature = FeaturePtr();
236 aMgr->finishOperation();
242 afterCommitOperation();
248 void ModuleBase_Operation::setRunning(bool theState)
250 emit triggered(theState);
253 void ModuleBase_Operation::onValuesChanged()
258 void ModuleBase_Operation::commitOperation()
260 if(!myPropertyPanel) {
265 void ModuleBase_Operation::activateByPreselection()
267 if (myPreSelection.empty())
270 ModuleBase_ModelWidget* aFilledWgt = 0;
271 if (myPropertyPanel) {
272 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
273 if (!aWidgets.empty()) {
274 ModuleBase_ModelWidget* aWgt = 0;
275 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
277 // 1. apply the selection to controls
278 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
280 if (!aWgt->canSetValue())
282 myPropertyPanel->setPreselectionWidget(aWgt);
283 if (!aWgt->setSelection(myPreSelection, true)) {
291 myPropertyPanel->setPreselectionWidget(NULL);
292 // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
293 // it is better to perform it not in setSelection of each widget, but do it here,
294 // after the preselection is processed
295 ModuleBase_ModelWidget::updateObject(myFeature);
297 // 3. a signal should be emitted before the next widget activation
298 // because, the activation of the next widget will give a focus to the widget. As a result
299 // the value of the widget is initialized. And commit may happens until the value is entered.
301 emit activatedByPreselection();
303 // 4. activate the next obligatory widget
304 myPropertyPanel->activateNextWidget(aFilledWgt);
310 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
312 myParentFeature = theParent;
315 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
317 return myParentFeature;
320 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
321 ModuleBase_IViewer* theViewer)
325 QList<ModuleBase_ViewerPrs> aPreSelected;
326 // Check that the selected result are not results of operation feature
327 FeaturePtr aFeature = feature();
329 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
331 std::list<ResultPtr> aResults = aFeature->results();
332 QObjectPtrList aResList;
333 std::list<ResultPtr>::const_iterator aIt;
334 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
335 aResList.append(*aIt);
337 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
338 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
339 aPreSelected.append(aPrs);
342 aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
344 // convert the selection values to the values, which are set to the operation widgets
346 //Handle(V3d_View) aView = theViewer->activeView();
347 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
348 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
349 // aValue->setObject(aPrs.object());
352 // if (getViewerPoint(aPrs, theViewer, aX, anY))
353 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
354 // myPreSelection.append(aValue);
356 myPreSelection = aPreSelected;
360 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
361 ModuleBase_IViewer* theViewer,
362 double& theX, double& theY)
367 void ModuleBase_Operation::clearPreselection()
369 myPreSelection.clear();
372 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
374 myPropertyPanel = theProp;
375 myPropertyPanel->setEditingMode(isEditOperation());
377 if (myPropertyPanel) {
378 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
379 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
380 for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
381 ModuleBase_ModelWidget* aWgt = (*aWIt);
382 connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
386 // Do not activate widgets by default if the current operation is editing operation
387 // Because we don't know which widget is going to be edited.
388 if (!isEditOperation()) {
389 // 4. activate the first obligatory widget
390 myPropertyPanel->activateNextWidget(NULL);
394 bool ModuleBase_Operation::isGranted(QString theId) const
396 return myNestedFeatures.contains(theId);