X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_Operation.cpp;h=b3bf0b07572308f26b62a2fc2c177a4ae2eb24dc;hb=28c90c232ffe159b88edd156286a398bfa3bb73b;hp=4b9a4faa0e4f001bd10bc343e29ca8f86c4213cf;hpb=f581964034f8df715c46a07c5ecb762492d6b4cf;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_Operation.cpp b/src/ModuleBase/ModuleBase_Operation.cpp index 4b9a4faa0..b3bf0b075 100644 --- a/src/ModuleBase/ModuleBase_Operation.cpp +++ b/src/ModuleBase/ModuleBase_Operation.cpp @@ -9,27 +9,45 @@ #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 #include #include #include -#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 @@ -42,48 +60,40 @@ FeaturePtr ModuleBase_Operation::feature() const return myFeature; } -bool ModuleBase_Operation::isNestedOperationsEnabled() const +bool ModuleBase_Operation::isValid() const { - return true; + 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); } -void ModuleBase_Operation::storeReal(double theValue) +bool ModuleBase_Operation::isNestedOperationsEnabled() const { - if(!myFeature){ - #ifdef _DEBUG - qDebug() << "ModuleBase_Operation::storeReal: " << - "trying to store value without opening a transaction."; - #endif - return; - } - QString anId = sender()->objectName(); - boost::shared_ptr aData = myFeature->data(); - boost::shared_ptr aReal = aData->real(anId.toStdString()); - aReal->setValue(theValue); - Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED)); + return true; } 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); + aCustom->storeValue(); } void ModuleBase_Operation::startOperation() { - if (!myFeature) - setFeature(createFeature()); - //emit callSlot(); - //commit(); + if (!myIsEditing) + createFeature(); } void ModuleBase_Operation::stopOperation() @@ -96,37 +106,255 @@ 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_FEATURE_UPDATED)); + Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED)); } void ModuleBase_Operation::flushCreated() { - Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_CREATED)); + 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(); - FeaturePtr aFeature = aDoc->addFeature( - getDescription()->operationId().toStdString()); - if (aFeature) // TODO: generate an error if feature was not created - aFeature->execute(); + 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; + // 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); + }*/ + } if (theFlushMessage) flushCreated(); - return aFeature; + return myFeature; } void ModuleBase_Operation::setFeature(FeaturePtr theFeature) { myFeature = theFeature; + myIsEditing = true; +} + +bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const +{ + FeaturePtr aFeature = feature(); + if (aFeature) { + 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) == theObj) + return true; + } + } + return false; +} + + +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*))); }