2 * ModuleBase_Operation.cpp
4 * Created on: Apr 2, 2014
8 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_OperationDescription.h"
11 #include "ModuleBase_ModelWidget.h"
12 #include "ModuleBase_ViewerPrs.h"
13 #include "ModuleBase_IPropertyPanel.h"
14 #include "ModuleBase_ISelection.h"
15 #include "ModuleBase_IViewer.h"
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_Object.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_Session.h>
28 #include <GeomAPI_Pnt2d.h>
30 #include <Events_Loop.h>
38 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
44 myDescription = new ModuleBase_OperationDescription(theId);
47 ModuleBase_Operation::~ModuleBase_Operation()
53 QString ModuleBase_Operation::id() const
55 return getDescription()->operationId();
58 FeaturePtr ModuleBase_Operation::feature() const
63 bool ModuleBase_Operation::isValid() const
66 return true; // rename operation
67 //Get validators for the Id
68 SessionPtr aMgr = ModelAPI_Session::get();
69 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
70 return aFactory->validate(myFeature);
73 bool ModuleBase_Operation::isNestedOperationsEnabled() const
78 //void ModuleBase_Operation::storeCustomValue()
82 // qDebug() << "ModuleBase_Operation::storeCustom: " <<
83 // "trying to store value without opening a transaction.";
88 // ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
90 // aCustom->storeValue();
94 bool ModuleBase_Operation::canBeCommitted() const
99 void ModuleBase_Operation::flushUpdated()
101 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
104 void ModuleBase_Operation::flushCreated()
106 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
109 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
111 if (myParentFeature) {
112 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
114 std::shared_ptr<ModelAPI_Document> aDoc = document();
115 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
117 if (myFeature) { // TODO: generate an error if feature was not created
119 // Model update should call "execute" of a feature.
120 //myFeature->execute();
121 // Init default values
122 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
123 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
124 for (; anIt != aLast; anIt++) {
125 (*anIt)->storeValue(aFeature);
134 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
136 myFeature = theFeature;
140 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
142 FeaturePtr aFeature = feature();
144 if (aFeature == theObj)
146 std::list<ResultPtr> aResults = aFeature->results();
147 std::list<ResultPtr>::const_iterator aIt;
148 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
149 if (theObj == (*aIt))
157 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
159 return ModelAPI_Session::get()->moduleDocument();
163 void ModuleBase_Operation::start()
165 ModelAPI_Session::get()->startOperation();
175 void ModuleBase_Operation::postpone()
178 disconnect(myPropertyPanel, 0, this, 0);
182 void ModuleBase_Operation::resume()
184 // connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
185 // this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
189 void ModuleBase_Operation::abort()
194 disconnect(myPropertyPanel, 0, this, 0);
198 ModelAPI_Session::get()->abortOperation();
202 bool ModuleBase_Operation::commit()
204 if (canBeCommitted()) {
206 disconnect(myPropertyPanel, 0, this, 0);
209 // check whether there are modifications performed during the current operation
211 // in case if there are no modifications, do not increase the undo/redo stack
212 if (ModelAPI_Session::get()->isModified())
213 ModelAPI_Session::get()->finishOperation();
215 ModelAPI_Session::get()->abortOperation();
221 afterCommitOperation();
227 void ModuleBase_Operation::setRunning(bool theState)
234 void ModuleBase_Operation::activateByPreselection()
236 if (!myPropertyPanel || myPreSelection.empty()) {
237 myPropertyPanel->activateNextWidget(NULL);
240 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
241 if (aWidgets.empty()) {
242 myPropertyPanel->activateNextWidget(NULL);
246 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
247 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
248 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
250 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
251 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
254 ModuleBase_ViewerPrs aValue = (*aPIt);
255 if (!aWgt->canSetValue())
259 if (!aWgt->setSelection(aValue)) {
268 myPropertyPanel->activateNextWidget(aFilledWgt);
270 emit activatedByPreselection();
274 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
275 ModuleBase_IViewer* theViewer)
279 QList<ModuleBase_ViewerPrs> aPreSelected;
280 // Check that the selected result are not results of operation feature
281 FeaturePtr aFeature = feature();
283 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
285 std::list<ResultPtr> aResults = aFeature->results();
286 QObjectPtrList aResList;
287 std::list<ResultPtr>::const_iterator aIt;
288 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
289 aResList.append(*aIt);
291 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
292 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
293 aPreSelected.append(aPrs);
296 aPreSelected = theSelection->getSelected();
298 // convert the selection values to the values, which are set to the operation widgets
300 //Handle(V3d_View) aView = theViewer->activeView();
301 //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
302 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
303 // aValue->setObject(aPrs.object());
306 // if (getViewerPoint(aPrs, theViewer, aX, anY))
307 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
308 // myPreSelection.append(aValue);
310 myPreSelection = aPreSelected;
313 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
315 // //activateByPreselection();
316 // //if (theWidget && myPropertyPanel) {
317 // // myPropertyPanel->activateNextWidget();
318 // //// //emit activateNextWidget(myActiveWidget);
322 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
324 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
327 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
328 // aValue->setObject(theFeature);
329 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
330 // bool isApplyed = aActiveWgt->setValue(aValue);
333 // myIsModified = (myIsModified || isApplyed);
337 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
338 ModuleBase_IViewer* theViewer,
339 double& theX, double& theY)
344 void ModuleBase_Operation::clearPreselection()
346 myPreSelection.clear();
349 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
351 myPropertyPanel = theProp;
352 myPropertyPanel->setEditingMode(isEditOperation());
353 //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
354 // SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
356 // Do not activate widgets by default if the current operation is editing operation
357 // Because we don't know which widget is going to be edited.
358 if (!isEditOperation())
359 activateByPreselection();
362 bool ModuleBase_Operation::isGranted(QString theId) const
364 return myNestedFeatures.contains(theId);