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>
34 #include <TopoDS_Vertex.hxx>
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();
95 void ModuleBase_Operation::startOperation()
101 void ModuleBase_Operation::stopOperation()
105 void ModuleBase_Operation::abortOperation()
109 void ModuleBase_Operation::commitOperation()
113 void ModuleBase_Operation::afterCommitOperation()
117 bool ModuleBase_Operation::canBeCommitted() const
122 void ModuleBase_Operation::flushUpdated()
124 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
127 void ModuleBase_Operation::flushCreated()
129 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
132 FeaturePtr ModuleBase_Operation::createFeature(
133 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
135 if (theCompositeFeature) {
136 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
138 boost::shared_ptr<ModelAPI_Document> aDoc = document();
139 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
141 if (myFeature) { // TODO: generate an error if feature was not created
143 // Model update should call "execute" of a feature.
144 //myFeature->execute();
145 // Init default values
146 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
147 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
148 for (; anIt != aLast; anIt++) {
149 (*anIt)->storeValue(aFeature);
158 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
160 myFeature = theFeature;
164 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
166 FeaturePtr aFeature = feature();
168 if (aFeature == theObj)
170 std::list<ResultPtr> aResults = aFeature->results();
171 std::list<ResultPtr>::const_iterator aIt;
172 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
173 if ((*aIt) == theObj)
181 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
183 return ModelAPI_Session::get()->moduleDocument();
187 void ModuleBase_Operation::start()
189 ModelAPI_Session::get()->startOperation();
195 void ModuleBase_Operation::resume()
198 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
199 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
203 void ModuleBase_Operation::abort()
208 disconnect(myPropertyPanel, 0, this, 0);
212 ModelAPI_Session::get()->abortOperation();
216 bool ModuleBase_Operation::commit()
218 if (canBeCommitted()) {
223 disconnect(myPropertyPanel, 0, this, 0);
226 // check whether there are modifications performed during the current operation
228 // in case if there are no modifications, do not increase the undo/redo stack
229 if (ModelAPI_Session::get()->isModified())
230 ModelAPI_Session::get()->finishOperation();
232 ModelAPI_Session::get()->abortOperation();
236 afterCommitOperation();
242 void ModuleBase_Operation::setRunning(bool theState)
249 bool ModuleBase_Operation::activateByPreselection()
251 if (!myPropertyPanel)
253 if (myPreSelection.empty())
255 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
256 if (aWidgets.empty())
259 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
260 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
261 QList<ModuleBase_WidgetValueFeature*>::const_iterator aPIt;
263 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
264 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
267 ModuleBase_WidgetValueFeature* aValue = (*aPIt);
268 if (!aWgt->setValue(aValue)) {
276 if (isSet && canBeCommitted()) {
277 // if all widgets are filled with selection
282 //activate next widget
284 myPropertyPanel->activateNextWidget(aFilledWgt);
289 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
290 //if ((myPreSelection.size() > 0) && aActiveWgt) {
291 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
292 // ModuleBase_WidgetValueFeature aValue;
293 // aValue.setObject(aPrs.object());
294 // if (aActiveWgt->setValue(&aValue)) {
295 // myPreSelection.removeOne(aPrs);
296 // myPropertyPanel->activateNextWidget();
298 // // If preselection is enough to make a valid feature - apply it immediately
303 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
304 ModuleBase_IViewer* theViewer)
308 QList<ModuleBase_ViewerPrs> aPreSelected;
309 // Check that the selected result are not results of operation feature
310 FeaturePtr aFeature = feature();
312 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
314 std::list<ResultPtr> aResults = aFeature->results();
315 QList<ObjectPtr> aResList;
316 std::list<ResultPtr>::const_iterator aIt;
317 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
318 aResList.append(*aIt);
320 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
321 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
322 aPreSelected.append(aPrs);
325 aPreSelected = theSelection->getSelected();
327 // convert the selection values to the values, which are set to the operation widgets
329 Handle(V3d_View) aView = theViewer->activeView();
330 foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
331 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
332 aValue->setObject(aPrs.object());
335 if (getViewerPoint(aPrs, theViewer, aX, anY))
336 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
337 myPreSelection.append(aValue);
341 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
343 //activateByPreselection();
344 //if (theWidget && myPropertyPanel) {
345 // myPropertyPanel->activateNextWidget();
346 //// //emit activateNextWidget(myActiveWidget);
350 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
352 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
355 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
356 aValue->setObject(theFeature);
357 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
358 bool isApplyed = aActiveWgt->setValue(aValue);
361 myIsModified = (myIsModified || isApplyed);
365 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
366 ModuleBase_IViewer* theViewer,
367 double& theX, double& theY)
372 void ModuleBase_Operation::clearPreselection()
374 while (!myPreSelection.isEmpty()) {
375 delete myPreSelection.takeFirst();
379 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
381 myPropertyPanel = theProp;
382 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
383 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));