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>
37 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
43 myDescription = new ModuleBase_OperationDescription(theId);
46 ModuleBase_Operation::~ModuleBase_Operation()
52 QString ModuleBase_Operation::id() const
54 return getDescription()->operationId();
57 FeaturePtr ModuleBase_Operation::feature() const
62 bool ModuleBase_Operation::isValid() const
65 return true; // rename operation
66 //Get validators for the Id
67 SessionPtr aMgr = ModelAPI_Session::get();
68 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
69 return aFactory->validate(myFeature);
72 bool ModuleBase_Operation::isNestedOperationsEnabled() const
77 //void ModuleBase_Operation::storeCustomValue()
81 // qDebug() << "ModuleBase_Operation::storeCustom: " <<
82 // "trying to store value without opening a transaction.";
87 // ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
89 // aCustom->storeValue();
93 bool ModuleBase_Operation::canBeCommitted() const
98 //void ModuleBase_Operation::flushUpdated()
100 // Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
103 void ModuleBase_Operation::flushCreated()
105 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
108 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
110 if (myParentFeature) {
111 myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
113 std::shared_ptr<ModelAPI_Document> aDoc = document();
114 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
116 if (myFeature) { // TODO: generate an error if feature was not created
118 // Model update should call "execute" of a feature.
119 //myFeature->execute();
120 // Init default values
121 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
122 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
123 for (; anIt != aLast; anIt++) {
124 (*anIt)->storeValue(aFeature);
133 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
135 myFeature = theFeature;
139 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
141 FeaturePtr aFeature = feature();
143 if (aFeature == theObj)
145 std::list<ResultPtr> aResults = aFeature->results();
146 std::list<ResultPtr>::const_iterator aIt;
147 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
148 if (theObj == (*aIt))
156 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
158 return ModelAPI_Session::get()->moduleDocument();
162 void ModuleBase_Operation::start()
164 ModelAPI_Session::get()->startOperation();
173 void ModuleBase_Operation::postpone()
176 disconnect(myPropertyPanel, 0, this, 0);
180 void ModuleBase_Operation::resume()
183 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), SLOT(commit()));
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 bool ModuleBase_Operation::activateByPreselection()
236 if (!myPropertyPanel)
238 if (myPreSelection.empty())
240 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
241 if (aWidgets.empty())
244 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
245 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
246 QList<ModuleBase_WidgetValueFeature*>::const_iterator aPIt;
248 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
249 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
252 ModuleBase_WidgetValueFeature* aValue = (*aPIt);
253 if (!aWgt->setValue(aValue)) {
261 if (isSet && canBeCommitted()) {
262 // if all widgets are filled with selection
267 //activate next widget
269 myPropertyPanel->activateNextWidget(aFilledWgt);
274 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
275 //if ((myPreSelection.size() > 0) && aActiveWgt) {
276 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
277 // ModuleBase_WidgetValueFeature aValue;
278 // aValue.setObject(aPrs.object());
279 // if (aActiveWgt->setValue(&aValue)) {
280 // myPreSelection.removeOne(aPrs);
281 // myPropertyPanel->activateNextWidget();
283 // // If preselection is enough to make a valid feature - apply it immediately
288 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
289 ModuleBase_IViewer* theViewer)
293 QList<ModuleBase_ViewerPrs> aPreSelected;
294 // Check that the selected result are not results of operation feature
295 FeaturePtr aFeature = feature();
297 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
299 std::list<ResultPtr> aResults = aFeature->results();
300 QObjectPtrList aResList;
301 std::list<ResultPtr>::const_iterator aIt;
302 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
303 aResList.append(*aIt);
305 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
306 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
307 aPreSelected.append(aPrs);
310 aPreSelected = theSelection->getSelected();
312 // convert the selection values to the values, which are set to the operation widgets
314 Handle(V3d_View) aView = theViewer->activeView();
315 foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
316 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
317 aValue->setObject(aPrs.object());
320 if (getViewerPoint(aPrs, theViewer, aX, anY))
321 aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
322 myPreSelection.append(aValue);
326 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
328 // //activateByPreselection();
329 // //if (theWidget && myPropertyPanel) {
330 // // myPropertyPanel->activateNextWidget();
331 // //// //emit activateNextWidget(myActiveWidget);
335 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
337 // ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
340 // ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
341 // aValue->setObject(theFeature);
342 // aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
343 // bool isApplyed = aActiveWgt->setValue(aValue);
346 // myIsModified = (myIsModified || isApplyed);
350 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
351 ModuleBase_IViewer* theViewer,
352 double& theX, double& theY)
357 void ModuleBase_Operation::clearPreselection()
359 while (!myPreSelection.isEmpty()) {
360 delete myPreSelection.takeFirst();
364 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
366 myPropertyPanel = theProp;
367 //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
368 // SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
369 if (myPropertyPanel) {
370 connect(myPropertyPanel, SIGNAL(noMoreWidgets()), SLOT(commit()));
374 bool ModuleBase_Operation::isGranted(QString theId) const
376 return myNestedFeatures.contains(theId);