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_WidgetValueFeature.h"
13 #include "ModuleBase_ViewerPrs.h"
14 #include "ModuleBase_IPropertyPanel.h"
15 #include "ModuleBase_ISelection.h"
16 #include "ModuleBase_IViewer.h"
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_Document.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
29 #include <GeomAPI_Pnt2d.h>
31 #include <Events_Loop.h>
33 #include <QApplication>
39 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
45 myDescription = new ModuleBase_OperationDescription(theId);
48 ModuleBase_Operation::~ModuleBase_Operation()
54 QString ModuleBase_Operation::id() const
56 return getDescription()->operationId();
59 FeaturePtr ModuleBase_Operation::feature() const
64 bool ModuleBase_Operation::isValid() const
67 return true; // rename operation
68 //Get validators for the Id
69 SessionPtr aMgr = ModelAPI_Session::get();
70 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
71 return aFactory->validate(myFeature);
74 bool ModuleBase_Operation::isNestedOperationsEnabled() const
79 //void ModuleBase_Operation::storeCustomValue()
83 // qDebug() << "ModuleBase_Operation::storeCustom: " <<
84 // "trying to store value without opening a transaction.";
89 // ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
91 // aCustom->storeValue();
95 bool ModuleBase_Operation::canBeCommitted() const
100 void ModuleBase_Operation::flushUpdated()
102 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
105 void ModuleBase_Operation::flushCreated()
107 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
110 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
112 if (myParentFeature) {
113 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
115 std::shared_ptr<ModelAPI_Document> aDoc = document();
116 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
118 if (myFeature) { // TODO: generate an error if feature was not created
120 // Model update should call "execute" of a feature.
121 //myFeature->execute();
122 // Init default values
123 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
124 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
125 for (; anIt != aLast; anIt++) {
126 (*anIt)->storeValue(aFeature);
135 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
137 myFeature = theFeature;
141 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
143 FeaturePtr aFeature = feature();
145 if (aFeature == theObj)
147 std::list<ResultPtr> aResults = aFeature->results();
148 std::list<ResultPtr>::const_iterator aIt;
149 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
150 if (theObj == (*aIt))
158 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
160 return ModelAPI_Session::get()->moduleDocument();
164 void ModuleBase_Operation::start()
166 ModelAPI_Session::get()->startOperation();
175 void ModuleBase_Operation::postpone()
178 disconnect(myPropertyPanel, 0, this, 0);
182 void ModuleBase_Operation::resume()
185 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), SLOT(commit()));
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 bool ModuleBase_Operation::activateByPreselection()
238 if (!myPropertyPanel)
240 if (myPreSelection.empty())
242 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
243 if (aWidgets.empty())
246 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
247 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
248 QList<ModuleBase_WidgetValueFeature*>::const_iterator aPIt;
250 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
251 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
254 ModuleBase_WidgetValueFeature* aValue = (*aPIt);
255 if (!aWgt->setValue(aValue)) {
263 if (isSet && canBeCommitted()) {
264 // if all widgets are filled with selection
265 bool aIsDone = commit();
266 QApplication::processEvents();
270 //activate next widget
272 myPropertyPanel->activateNextWidget(aFilledWgt);
279 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
280 ModuleBase_IViewer* theViewer)
284 QList<ModuleBase_ViewerPrs> aPreSelected;
285 // Check that the selected result are not results of operation feature
286 FeaturePtr aFeature = feature();
288 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
290 std::list<ResultPtr> aResults = aFeature->results();
291 QObjectPtrList aResList;
292 std::list<ResultPtr>::const_iterator aIt;
293 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
294 aResList.append(*aIt);
296 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
297 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
298 aPreSelected.append(aPrs);
301 aPreSelected = theSelection->getSelected();
303 // convert the selection values to the values, which are set to the operation widgets
305 Handle(V3d_View) aView = theViewer->activeView();
306 foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
307 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
308 aValue->setObject(aPrs.object());
311 if (getViewerPoint(aPrs, theViewer, aX, anY))
312 aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
313 myPreSelection.append(aValue);
317 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
319 // //activateByPreselection();
320 // //if (theWidget && myPropertyPanel) {
321 // // myPropertyPanel->activateNextWidget();
322 // //// //emit activateNextWidget(myActiveWidget);
326 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
328 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
331 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
332 // aValue->setObject(theFeature);
333 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
334 // bool isApplyed = aActiveWgt->setValue(aValue);
337 // myIsModified = (myIsModified || isApplyed);
341 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
342 ModuleBase_IViewer* theViewer,
343 double& theX, double& theY)
348 void ModuleBase_Operation::clearPreselection()
350 while (!myPreSelection.isEmpty()) {
351 delete myPreSelection.takeFirst();
355 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
357 myPropertyPanel = theProp;
358 myPropertyPanel->setEditingMode(isEditOperation());
359 //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
360 // SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
361 if (myPropertyPanel) {
362 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), SLOT(commit()));
366 bool ModuleBase_Operation::isGranted(QString theId) const
368 return myNestedFeatures.contains(theId);