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
68 return true; // rename operation
69 //Get validators for the Id
70 SessionPtr aMgr = ModelAPI_Session::get();
71 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
72 return aFactory->validate(myFeature);
75 //void ModuleBase_Operation::storeCustomValue()
79 // qDebug() << "ModuleBase_Operation::storeCustom: " <<
80 // "trying to store value without opening a transaction.";
85 // ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
87 // aCustom->storeValue();
91 bool ModuleBase_Operation::canBeCommitted() const
96 void ModuleBase_Operation::flushUpdated()
98 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
101 void ModuleBase_Operation::flushCreated()
103 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
106 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
108 if (myParentFeature) {
109 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
111 std::shared_ptr<ModelAPI_Document> aDoc = document();
112 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
114 if (myFeature) { // TODO: generate an error if feature was not created
116 // Model update should call "execute" of a feature.
117 //myFeature->execute();
118 // Init default values
119 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
120 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
121 for (; anIt != aLast; anIt++) {
122 (*anIt)->storeValue(aFeature);
131 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
133 myFeature = theFeature;
137 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
139 FeaturePtr aFeature = feature();
141 if (aFeature == theObj)
143 std::list<ResultPtr> aResults = aFeature->results();
144 std::list<ResultPtr>::const_iterator aIt;
145 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
146 if (theObj == (*aIt))
154 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
156 return ModelAPI_Session::get()->moduleDocument();
160 void ModuleBase_Operation::start()
162 ModelAPI_Session::get()->startOperation();
172 void ModuleBase_Operation::postpone()
175 disconnect(myPropertyPanel, 0, this, 0);
179 void ModuleBase_Operation::resume()
181 // connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
182 // this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
186 void ModuleBase_Operation::abort()
191 disconnect(myPropertyPanel, 0, this, 0);
195 ModelAPI_Session::get()->abortOperation();
199 bool ModuleBase_Operation::commit()
201 if (canBeCommitted()) {
203 disconnect(myPropertyPanel, 0, this, 0);
206 // check whether there are modifications performed during the current operation
208 // in case if there are no modifications, do not increase the undo/redo stack
209 if (ModelAPI_Session::get()->isModified())
210 ModelAPI_Session::get()->finishOperation();
212 ModelAPI_Session::get()->abortOperation();
218 afterCommitOperation();
224 void ModuleBase_Operation::setRunning(bool theState)
231 void ModuleBase_Operation::activateByPreselection()
233 if (!myPropertyPanel || myPreSelection.empty()) {
234 myPropertyPanel->activateNextWidget(NULL);
237 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
238 if (aWidgets.empty()) {
239 myPropertyPanel->activateNextWidget(NULL);
243 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
244 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
245 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
247 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
248 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
251 ModuleBase_ViewerPrs aValue = (*aPIt);
252 if (!aWgt->canSetValue())
256 if (!aWgt->setSelection(aValue)) {
265 myPropertyPanel->activateNextWidget(aFilledWgt);
267 emit activatedByPreselection();
271 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
272 ModuleBase_IViewer* theViewer)
276 QList<ModuleBase_ViewerPrs> aPreSelected;
277 // Check that the selected result are not results of operation feature
278 FeaturePtr aFeature = feature();
280 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
282 std::list<ResultPtr> aResults = aFeature->results();
283 QObjectPtrList aResList;
284 std::list<ResultPtr>::const_iterator aIt;
285 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
286 aResList.append(*aIt);
288 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
289 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
290 aPreSelected.append(aPrs);
293 aPreSelected = theSelection->getSelected();
295 // convert the selection values to the values, which are set to the operation widgets
297 //Handle(V3d_View) aView = theViewer->activeView();
298 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
299 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
300 // aValue->setObject(aPrs.object());
303 // if (getViewerPoint(aPrs, theViewer, aX, anY))
304 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
305 // myPreSelection.append(aValue);
307 myPreSelection = aPreSelected;
310 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
312 // //activateByPreselection();
313 // //if (theWidget && myPropertyPanel) {
314 // // myPropertyPanel->activateNextWidget();
315 // //// //emit activateNextWidget(myActiveWidget);
319 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
321 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
324 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
325 // aValue->setObject(theFeature);
326 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
327 // bool isApplyed = aActiveWgt->setValue(aValue);
330 // myIsModified = (myIsModified || isApplyed);
334 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
335 ModuleBase_IViewer* theViewer,
336 double& theX, double& theY)
341 void ModuleBase_Operation::clearPreselection()
343 myPreSelection.clear();
346 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
348 myPropertyPanel = theProp;
349 myPropertyPanel->setEditingMode(isEditOperation());
350 //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
351 // SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
353 // Do not activate widgets by default if the current operation is editing operation
354 // Because we don't know which widget is going to be edited.
355 if (!isEditOperation())
356 activateByPreselection();
359 bool ModuleBase_Operation::isGranted(QString theId) const
361 return myNestedFeatures.contains(theId);