X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_Operation.cpp;h=b3bf0b07572308f26b62a2fc2c177a4ae2eb24dc;hb=28c90c232ffe159b88edd156286a398bfa3bb73b;hp=0387105b784b57acc96c4ddb5ce475f42b48692f;hpb=31e7d42a5c5527a40094a3d1992258a989eb64f9;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_Operation.cpp b/src/ModuleBase/ModuleBase_Operation.cpp index 0387105b7..b3bf0b075 100644 --- a/src/ModuleBase/ModuleBase_Operation.cpp +++ b/src/ModuleBase/ModuleBase_Operation.cpp @@ -9,6 +9,10 @@ #include "ModuleBase_OperationDescription.h" #include "ModuleBase_ModelWidget.h" +#include "ModuleBase_WidgetValueFeature.h" +#include "ModuleBase_ViewerPrs.h" +#include "ModuleBase_IPropertyPanel.h" +#include "ModuleBase_ISelection.h" #include #include @@ -17,20 +21,33 @@ #include #include #include +#include +#include +#include + +#include #include +#include +#include + #ifdef _DEBUG #include #endif ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent) -: ModuleBase_IOperation(theId, theParent) + : QObject(theParent), + myIsEditing(false), + myIsModified(false), + myPropertyPanel(NULL) { + myDescription = new ModuleBase_OperationDescription(theId); } ModuleBase_Operation::~ModuleBase_Operation() { + delete myDescription; } QString ModuleBase_Operation::id() const @@ -43,6 +60,16 @@ FeaturePtr ModuleBase_Operation::feature() const return myFeature; } +bool ModuleBase_Operation::isValid() const +{ + if (!myFeature) + return true; // rename operation + //Get validators for the Id + SessionPtr aMgr = ModelAPI_Session::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + return aFactory->validate(myFeature); +} + bool ModuleBase_Operation::isNestedOperationsEnabled() const { return true; @@ -50,29 +77,23 @@ bool ModuleBase_Operation::isNestedOperationsEnabled() const void ModuleBase_Operation::storeCustomValue() { - if(!myFeature){ - #ifdef _DEBUG + if (!myFeature) { +#ifdef _DEBUG qDebug() << "ModuleBase_Operation::storeCustom: " << - "trying to store value without opening a transaction."; - #endif + "trying to store value without opening a transaction."; +#endif return; } ModuleBase_ModelWidget* aCustom = dynamic_cast(sender()); if (aCustom) - aCustom->storeValue(myFeature); -} - -void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget) -{ + aCustom->storeValue(); } void ModuleBase_Operation::startOperation() { if (!myIsEditing) createFeature(); - //emit callSlot(); - //commit(); } void ModuleBase_Operation::stopOperation() @@ -85,13 +106,17 @@ void ModuleBase_Operation::abortOperation() void ModuleBase_Operation::commitOperation() { - if (myFeature) myFeature->execute(); } void ModuleBase_Operation::afterCommitOperation() { } +bool ModuleBase_Operation::canBeCommitted() const +{ + return true; +} + void ModuleBase_Operation::flushUpdated() { Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); @@ -102,19 +127,25 @@ void ModuleBase_Operation::flushCreated() Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED)); } -FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage) +FeaturePtr ModuleBase_Operation::createFeature( + const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature) { - boost::shared_ptr aDoc = document(); - myFeature = aDoc->addFeature(getDescription()->operationId().toStdString()); - if (myFeature) { // TODO: generate an error if feature was not created + if (theCompositeFeature) { + myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString()); + } else { + boost::shared_ptr aDoc = document(); + myFeature = aDoc->addFeature(getDescription()->operationId().toStdString()); + } + if (myFeature) { // TODO: generate an error if feature was not created myIsModified = true; - myFeature->execute(); + // Model update should call "execute" of a feature. + //myFeature->execute(); // Init default values /*QList aWidgets = getDescription()->modelWidgets(); - QList::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end(); - for (; anIt != aLast; anIt++) { - (*anIt)->storeValue(aFeature); - }*/ + QList::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end(); + for (; anIt != aLast; anIt++) { + (*anIt)->storeValue(aFeature); + }*/ } if (theFlushMessage) @@ -125,11 +156,6 @@ FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage) void ModuleBase_Operation::setFeature(FeaturePtr theFeature) { myFeature = theFeature; -} - -void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature) -{ - setFeature(theFeature); myIsEditing = true; } @@ -137,14 +163,198 @@ bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const { FeaturePtr aFeature = feature(); if (aFeature) { - if (aFeature.get() == theObj.get()) + if (aFeature == theObj) return true; std::list aResults = aFeature->results(); std::list::const_iterator aIt; for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) { - if ((*aIt).get() == theObj.get()) + if ((*aIt) == theObj) return true; } } return false; -} \ No newline at end of file +} + + +boost::shared_ptr ModuleBase_Operation::document() const +{ + return ModelAPI_Session::get()->moduleDocument(); +} + + +void ModuleBase_Operation::start() +{ + ModelAPI_Session::get()->startOperation(); + + startOperation(); + emit started(); +} + +void ModuleBase_Operation::resume() +{ + if (myPropertyPanel) + connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), + this, SLOT(onWidgetActivated(ModuleBase_ModelWidget*))); + emit resumed(); +} + +void ModuleBase_Operation::abort() +{ + abortOperation(); + emit aborted(); + if (myPropertyPanel) + disconnect(myPropertyPanel, 0, this, 0); + + stopOperation(); + + ModelAPI_Session::get()->abortOperation(); + emit stopped(); +} + +bool ModuleBase_Operation::commit() +{ + if (canBeCommitted()) { + commitOperation(); + emit committed(); + + if (myPropertyPanel) + disconnect(myPropertyPanel, 0, this, 0); + + stopOperation(); + // check whether there are modifications performed during the current operation + // in the model + // in case if there are no modifications, do not increase the undo/redo stack + if (ModelAPI_Session::get()->isModified()) + ModelAPI_Session::get()->finishOperation(); + else + ModelAPI_Session::get()->abortOperation(); + + emit stopped(); + + afterCommitOperation(); + return true; + } + return false; +} + +void ModuleBase_Operation::setRunning(bool theState) +{ + if (!theState) { + abort(); + } +} + +bool ModuleBase_Operation::activateByPreselection() +{ + if (!myPropertyPanel) + return false; + if (myPreSelection.empty()) + return false; + const QList& aWidgets = myPropertyPanel->modelWidgets(); + if (aWidgets.empty()) + return false; + + ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0; + ModuleBase_ViewerPrs aPrs; + QList::const_iterator aWIt; + QList::const_iterator aPIt; + bool isSet = false; + for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin(); + (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd()); + ++aWIt, ++aPIt) { + aWgt = (*aWIt); + aPrs = (*aPIt); + ModuleBase_WidgetValueFeature aValue; + aValue.setObject(aPrs.object()); + // Check if the selection has a selected point + // for today it is impossible to do because + // the selected point demands convertation to Sketch plane 2d + if (!aWgt->setValue(&aValue)) { + isSet = false; + break; + } else { + isSet = true; + aFilledWgt = aWgt; + } + } + if (isSet && canBeCommitted()) { + // if all widgets are filled with selection + commit(); + return true; + } + else { + //activate next widget + if (aFilledWgt) { + myPropertyPanel->activateNextWidget(aFilledWgt); + return true; + } + } + + //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget(); + //if ((myPreSelection.size() > 0) && aActiveWgt) { + // const ModuleBase_ViewerPrs& aPrs = myPreSelection.first(); + // ModuleBase_WidgetValueFeature aValue; + // aValue.setObject(aPrs.object()); + // if (aActiveWgt->setValue(&aValue)) { + // myPreSelection.removeOne(aPrs); + // myPropertyPanel->activateNextWidget(); + // } + // // If preselection is enough to make a valid feature - apply it immediately + //} + return false; +} + +void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection, + ModuleBase_IViewer* /*theViewer*/) +{ + myPreSelection.clear(); + + // Check that the selected result are not results of operation feature + QList aSelected = theSelection->getSelected(); + FeaturePtr aFeature = feature(); + if (aFeature) { + std::list aResults = aFeature->results(); + QList aResList; + std::list::const_iterator aIt; + for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) + aResList.append(*aIt); + + foreach (ModuleBase_ViewerPrs aPrs, aSelected) { + if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature)) + myPreSelection.append(aPrs); + } + } else + myPreSelection = aSelected; +} + +void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget) +{ + //activateByPreselection(); + //if (theWidget && myPropertyPanel) { + // myPropertyPanel->activateNextWidget(); + //// //emit activateNextWidget(myActiveWidget); + //} +} + +bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY) +{ + ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget(); + if (!aActiveWgt) + return false; + ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature(); + aValue->setObject(theFeature); + aValue->setPoint(boost::shared_ptr(new GeomAPI_Pnt2d(theX, theY))); + bool isApplyed = aActiveWgt->setValue(aValue); + + delete aValue; + myIsModified = (myIsModified || isApplyed); + return isApplyed; +} + + +void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) +{ + myPropertyPanel = theProp; + connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this, + SLOT(onWidgetActivated(ModuleBase_ModelWidget*))); +}