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 bool ModuleBase_Operation::isNestedOperationsEnabled() const
80 //void ModuleBase_Operation::storeCustomValue()
84 // qDebug() << "ModuleBase_Operation::storeCustom: " <<
85 // "trying to store value without opening a transaction.";
90 // ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
92 // aCustom->storeValue();
96 bool ModuleBase_Operation::canBeCommitted() const
101 void ModuleBase_Operation::flushUpdated()
103 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
106 void ModuleBase_Operation::flushCreated()
108 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
111 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
113 if (myParentFeature) {
114 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
116 std::shared_ptr<ModelAPI_Document> aDoc = document();
117 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
119 if (myFeature) { // TODO: generate an error if feature was not created
121 // Model update should call "execute" of a feature.
122 //myFeature->execute();
123 // Init default values
124 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
125 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
126 for (; anIt != aLast; anIt++) {
127 (*anIt)->storeValue(aFeature);
136 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
138 myFeature = theFeature;
142 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
144 FeaturePtr aFeature = feature();
146 if (aFeature == theObj)
148 std::list<ResultPtr> aResults = aFeature->results();
149 std::list<ResultPtr>::const_iterator aIt;
150 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
151 if (theObj == (*aIt))
159 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
161 return ModelAPI_Session::get()->moduleDocument();
165 void ModuleBase_Operation::start()
167 ModelAPI_Session::get()->startOperation();
177 void ModuleBase_Operation::postpone()
180 disconnect(myPropertyPanel, 0, this, 0);
184 void ModuleBase_Operation::resume()
186 // connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
187 // this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
191 void ModuleBase_Operation::abort()
196 disconnect(myPropertyPanel, 0, this, 0);
200 ModelAPI_Session::get()->abortOperation();
204 bool ModuleBase_Operation::commit()
206 if (canBeCommitted()) {
208 disconnect(myPropertyPanel, 0, this, 0);
211 // check whether there are modifications performed during the current operation
213 // in case if there are no modifications, do not increase the undo/redo stack
214 if (ModelAPI_Session::get()->isModified())
215 ModelAPI_Session::get()->finishOperation();
217 ModelAPI_Session::get()->abortOperation();
223 afterCommitOperation();
229 void ModuleBase_Operation::setRunning(bool theState)
236 void ModuleBase_Operation::activateByPreselection()
238 if (!myPropertyPanel || myPreSelection.empty()) {
239 myPropertyPanel->activateNextWidget(NULL);
242 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
243 if (aWidgets.empty()) {
244 myPropertyPanel->activateNextWidget(NULL);
248 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
249 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
250 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
252 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
253 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
256 ModuleBase_ViewerPrs aValue = (*aPIt);
257 if (!aWgt->canSetValue())
261 if (!aWgt->setSelection(aValue)) {
270 myPropertyPanel->activateNextWidget(aFilledWgt);
272 emit activatedByPreselection();
276 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
277 ModuleBase_IViewer* theViewer)
281 QList<ModuleBase_ViewerPrs> aPreSelected;
282 // Check that the selected result are not results of operation feature
283 FeaturePtr aFeature = feature();
285 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
287 std::list<ResultPtr> aResults = aFeature->results();
288 QObjectPtrList aResList;
289 std::list<ResultPtr>::const_iterator aIt;
290 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
291 aResList.append(*aIt);
293 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
294 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
295 aPreSelected.append(aPrs);
298 aPreSelected = theSelection->getSelected();
300 // convert the selection values to the values, which are set to the operation widgets
302 //Handle(V3d_View) aView = theViewer->activeView();
303 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
304 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
305 // aValue->setObject(aPrs.object());
308 // if (getViewerPoint(aPrs, theViewer, aX, anY))
309 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
310 // myPreSelection.append(aValue);
312 myPreSelection = aPreSelected;
315 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
317 // //activateByPreselection();
318 // //if (theWidget && myPropertyPanel) {
319 // // myPropertyPanel->activateNextWidget();
320 // //// //emit activateNextWidget(myActiveWidget);
324 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
326 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
329 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
330 // aValue->setObject(theFeature);
331 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
332 // bool isApplyed = aActiveWgt->setValue(aValue);
335 // myIsModified = (myIsModified || isApplyed);
339 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
340 ModuleBase_IViewer* theViewer,
341 double& theX, double& theY)
346 void ModuleBase_Operation::clearPreselection()
348 myPreSelection.clear();
351 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
353 myPropertyPanel = theProp;
354 myPropertyPanel->setEditingMode(isEditOperation());
355 //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
356 // SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
358 // Do not activate widgets by default if the current operation is editing operation
359 // Because we don't know which widget is going to be edited.
360 if (!isEditOperation())
361 activateByPreselection();
364 bool ModuleBase_Operation::isGranted(QString theId) const
366 return myNestedFeatures.contains(theId);