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"
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>
33 #include <TopoDS_Vertex.hxx>
39 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
45 myDescription = new ModuleBase_OperationDescription(theId);
48 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();
93 void ModuleBase_Operation::startOperation()
99 void ModuleBase_Operation::stopOperation()
103 void ModuleBase_Operation::abortOperation()
107 void ModuleBase_Operation::commitOperation()
111 void ModuleBase_Operation::afterCommitOperation()
115 bool ModuleBase_Operation::canBeCommitted() const
120 void ModuleBase_Operation::flushUpdated()
122 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
125 void ModuleBase_Operation::flushCreated()
127 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
130 FeaturePtr ModuleBase_Operation::createFeature(
131 const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
133 if (theCompositeFeature) {
134 myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
136 boost::shared_ptr<ModelAPI_Document> aDoc = document();
137 myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
139 if (myFeature) { // TODO: generate an error if feature was not created
141 // Model update should call "execute" of a feature.
142 //myFeature->execute();
143 // Init default values
144 /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
145 QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
146 for (; anIt != aLast; anIt++) {
147 (*anIt)->storeValue(aFeature);
156 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
158 myFeature = theFeature;
162 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
164 FeaturePtr aFeature = feature();
166 if (aFeature == theObj)
168 std::list<ResultPtr> aResults = aFeature->results();
169 std::list<ResultPtr>::const_iterator aIt;
170 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
171 if ((*aIt) == theObj)
179 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
181 return ModelAPI_Session::get()->moduleDocument();
185 void ModuleBase_Operation::start()
187 ModelAPI_Session::get()->startOperation();
193 void ModuleBase_Operation::resume()
196 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
197 this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
201 void ModuleBase_Operation::abort()
206 disconnect(myPropertyPanel, 0, this, 0);
210 ModelAPI_Session::get()->abortOperation();
214 bool ModuleBase_Operation::commit()
216 if (canBeCommitted()) {
221 disconnect(myPropertyPanel, 0, this, 0);
224 ModelAPI_Session::get()->finishOperation();
228 afterCommitOperation();
234 void ModuleBase_Operation::setRunning(bool theState)
241 bool ModuleBase_Operation::activateByPreselection()
243 if (!myPropertyPanel)
245 if (myPreSelection.empty())
247 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
248 if (aWidgets.empty())
251 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
252 ModuleBase_ViewerPrs aPrs;
253 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
254 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
256 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
257 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
261 ModuleBase_WidgetValueFeature aValue;
262 aValue.setObject(aPrs.object());
263 // Check if the selection has a selected point
264 // for today it is impossible to do because
265 // the selected point demands convertation to Sketch plane 2d
266 if (!aWgt->setValue(&aValue)) {
274 if (isSet && canBeCommitted()) {
275 // if all widgets are filled with selection
280 //activate next widget
282 myPropertyPanel->activateNextWidget(aFilledWgt);
287 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
288 //if ((myPreSelection.size() > 0) && aActiveWgt) {
289 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
290 // ModuleBase_WidgetValueFeature aValue;
291 // aValue.setObject(aPrs.object());
292 // if (aActiveWgt->setValue(&aValue)) {
293 // myPreSelection.removeOne(aPrs);
294 // myPropertyPanel->activateNextWidget();
296 // // If preselection is enough to make a valid feature - apply it immediately
301 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
302 ModuleBase_IViewer* /*theViewer*/)
304 myPreSelection.clear();
306 // Check that the selected result are not results of operation feature
307 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
308 FeaturePtr aFeature = feature();
310 std::list<ResultPtr> aResults = aFeature->results();
311 QList<ObjectPtr> aResList;
312 std::list<ResultPtr>::const_iterator aIt;
313 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
314 aResList.append(*aIt);
316 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
317 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
318 myPreSelection.append(aPrs);
321 myPreSelection = aSelected;
324 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
326 //activateByPreselection();
327 //if (theWidget && myPropertyPanel) {
328 // myPropertyPanel->activateNextWidget();
329 //// //emit activateNextWidget(myActiveWidget);
333 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
335 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
338 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
339 aValue->setObject(theFeature);
340 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
341 bool isApplyed = aActiveWgt->setValue(aValue);
344 myIsModified = (myIsModified || isApplyed);
349 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
351 myPropertyPanel = theProp;
352 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
353 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));