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();
92 void ModuleBase_Operation::startOperation()
98 void ModuleBase_Operation::stopOperation()
102 void ModuleBase_Operation::abortOperation()
106 void ModuleBase_Operation::commitOperation()
110 void ModuleBase_Operation::afterCommitOperation()
114 bool ModuleBase_Operation::canBeCommitted() const
119 void ModuleBase_Operation::flushUpdated()
121 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
124 void ModuleBase_Operation::flushCreated()
126 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
129 FeaturePtr ModuleBase_Operation::createFeature(
130 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
132 if (theCompositeFeature) {
133 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
135 boost::shared_ptr<ModelAPI_Document> aDoc = document();
136 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
138 if (myFeature) { // TODO: generate an error if feature was not created
140 // Model update should call "execute" of a feature.
141 //myFeature->execute();
142 // Init default values
143 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
144 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
145 for (; anIt != aLast; anIt++) {
146 (*anIt)->storeValue(aFeature);
155 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
157 myFeature = theFeature;
161 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
163 FeaturePtr aFeature = feature();
165 if (aFeature == theObj)
167 std::list<ResultPtr> aResults = aFeature->results();
168 std::list<ResultPtr>::const_iterator aIt;
169 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
170 if ((*aIt) == theObj)
178 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
180 return ModelAPI_Session::get()->moduleDocument();
184 void ModuleBase_Operation::start()
186 ModelAPI_Session::get()->startOperation();
192 void ModuleBase_Operation::resume()
195 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
196 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
200 void ModuleBase_Operation::abort()
205 disconnect(myPropertyPanel, 0, this, 0);
209 ModelAPI_Session::get()->abortOperation();
213 bool ModuleBase_Operation::commit()
215 if (canBeCommitted()) {
220 disconnect(myPropertyPanel, 0, this, 0);
223 // check whether there are modifications performed during the current operation
225 // in case if there are no modifications, do not increase the undo/redo stack
226 if (ModelAPI_Session::get()->isModified())
227 ModelAPI_Session::get()->finishOperation();
229 ModelAPI_Session::get()->abortOperation();
233 afterCommitOperation();
239 void ModuleBase_Operation::setRunning(bool theState)
246 bool ModuleBase_Operation::activateByPreselection()
248 if (!myPropertyPanel)
250 if (myPreSelection.empty())
252 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
253 if (aWidgets.empty())
256 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
257 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
258 QList<ModuleBase_WidgetValueFeature*>::const_iterator aPIt;
260 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
261 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
264 ModuleBase_WidgetValueFeature* aValue = (*aPIt);
265 if (!aWgt->setValue(aValue)) {
273 if (isSet && canBeCommitted()) {
274 // if all widgets are filled with selection
279 //activate next widget
281 myPropertyPanel->activateNextWidget(aFilledWgt);
286 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
287 //if ((myPreSelection.size() > 0) && aActiveWgt) {
288 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
289 // ModuleBase_WidgetValueFeature aValue;
290 // aValue.setObject(aPrs.object());
291 // if (aActiveWgt->setValue(&aValue)) {
292 // myPreSelection.removeOne(aPrs);
293 // myPropertyPanel->activateNextWidget();
295 // // If preselection is enough to make a valid feature - apply it immediately
300 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
301 ModuleBase_IViewer* theViewer)
305 QList<ModuleBase_ViewerPrs> aPreSelected;
306 // Check that the selected result are not results of operation feature
307 FeaturePtr aFeature = feature();
309 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
311 std::list<ResultPtr> aResults = aFeature->results();
312 QList<ObjectPtr> aResList;
313 std::list<ResultPtr>::const_iterator aIt;
314 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
315 aResList.append(*aIt);
317 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
318 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
319 aPreSelected.append(aPrs);
322 aPreSelected = theSelection->getSelected();
324 // convert the selection values to the values, which are set to the operation widgets
326 Handle(V3d_View) aView = theViewer->activeView();
327 foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
328 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
329 aValue->setObject(aPrs.object());
332 if (getViewerPoint(aPrs, theViewer, aX, anY))
333 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
334 myPreSelection.append(aValue);
338 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
340 //activateByPreselection();
341 //if (theWidget && myPropertyPanel) {
342 // myPropertyPanel->activateNextWidget();
343 //// //emit activateNextWidget(myActiveWidget);
347 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
349 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
352 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
353 aValue->setObject(theFeature);
354 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
355 bool isApplyed = aActiveWgt->setValue(aValue);
358 myIsModified = (myIsModified || isApplyed);
362 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
363 ModuleBase_IViewer* theViewer,
364 double& theX, double& theY)
369 void ModuleBase_Operation::clearPreselection()
371 while (!myPreSelection.isEmpty()) {
372 delete myPreSelection.takeFirst();
376 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
378 myPropertyPanel = theProp;
379 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
380 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));