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;
252 ModuleBase_ViewerPrs aPrs;
253 QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
254 QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
255 for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
256 (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
260 ModuleBase_WidgetValueFeature aValue;
261 aValue.setObject(aPrs.object());
262 // Check if the selection has a selected point
263 // for today it is impossible to do because
264 // the selected point demands convertation to Sketch plane 2d
265 if (!aWgt->setValue(&aValue))
268 if (canBeCommitted()) {
269 // if all widgets are filled with selection
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*/)
291 myPreSelection.clear();
293 // Check that the selected result are not results of operation feature
294 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
295 FeaturePtr aFeature = feature();
297 std::list<ResultPtr> aResults = aFeature->results();
298 QList<ObjectPtr> aResList;
299 std::list<ResultPtr>::const_iterator aIt;
300 for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
301 aResList.append(*aIt);
303 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
304 if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
305 myPreSelection.append(aPrs);
308 myPreSelection = aSelected;
311 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
313 //activateByPreselection();
314 //if (theWidget && myPropertyPanel) {
315 // myPropertyPanel->activateNextWidget();
316 //// //emit activateNextWidget(myActiveWidget);
320 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
322 ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
325 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
326 aValue->setObject(theFeature);
327 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
328 bool isApplyed = aActiveWgt->setValue(aValue);
331 myIsModified = (myIsModified || isApplyed);
336 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
338 myPropertyPanel = theProp;
339 connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
340 SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));