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 // check whether there are modifications performed during the current operation
226 // in case if there are no modifications, do not increase the undo/redo stack
227 if (ModelAPI_Session::get()->isModified())
228 ModelAPI_Session::get()->finishOperation();
230 ModelAPI_Session::get()->abortOperation();
234 afterCommitOperation();
240 void ModuleBase_Operation::setRunning(bool theState)
247 bool ModuleBase_Operation::activateByPreselection()
249 if (!myPropertyPanel)
251 if (myPreSelection.empty())
253 const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
254 if (aWidgets.empty())
257 ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
258 ModuleBase_ViewerPrs aPrs;
259 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
260 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
262 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
263 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
267 ModuleBase_WidgetValueFeature aValue;
268 aValue.setObject(aPrs.object());
269 // Check if the selection has a selected point
270 // for today it is impossible to do because
271 // the selected point demands convertation to Sketch plane 2d
272 if (!aWgt->setValue(&aValue)) {
280 if (isSet && canBeCommitted()) {
281 // if all widgets are filled with selection
286 //activate next widget
288 myPropertyPanel->activateNextWidget(aFilledWgt);
293 //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
294 //if ((myPreSelection.size() > 0) && aActiveWgt) {
295 // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
296 // ModuleBase_WidgetValueFeature aValue;
297 // aValue.setObject(aPrs.object());
298 // if (aActiveWgt->setValue(&aValue)) {
299 // myPreSelection.removeOne(aPrs);
300 // myPropertyPanel->activateNextWidget();
302 // // If preselection is enough to make a valid feature - apply it immediately
307 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
308 ModuleBase_IViewer* /*theViewer*/)
310 myPreSelection.clear();
312 // Check that the selected result are not results of operation feature
313 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
314 FeaturePtr aFeature = feature();
316 std::list<ResultPtr> aResults = aFeature->results();
317 QList<ObjectPtr> aResList;
318 std::list<ResultPtr>::const_iterator aIt;
319 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
320 aResList.append(*aIt);
322 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
323 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
324 myPreSelection.append(aPrs);
327 myPreSelection = aSelected;
330 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
332 //activateByPreselection();
333 //if (theWidget && myPropertyPanel) {
334 // myPropertyPanel->activateNextWidget();
335 //// //emit activateNextWidget(myActiveWidget);
339 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
341 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
344 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
345 aValue->setObject(theFeature);
346 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
347 bool isApplyed = aActiveWgt->setValue(aValue);
350 myIsModified = (myIsModified || isApplyed);
355 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
357 myPropertyPanel = theProp;
358 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
359 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));