X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetShapeSelector.cpp;h=2c970d1740de74d2ffb4fbe1bb5f4813b1d5354c;hb=074b1d850036614d4a08999c4bc4e8630db63d2b;hp=76c2da2062fa2955e807410e1198d5dd5470c3c8;hpb=35f0e7a2748c1ae77e4dae1a156d10ddadc10afe;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp index 76c2da206..2c970d174 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + // File: ModuleBase_WidgetShapeSelector.h // Created: 2 June 2014 // Author: Vitaly Smetannikov @@ -8,13 +10,15 @@ #include #include #include -#include +#include +#include #include #include #include #include #include + #include #include #include @@ -27,11 +31,18 @@ #include #include #include +#include +#include +#include +#include +#include + #include #include #include +#include #include #include @@ -43,11 +54,13 @@ #include #include #include +#include +#include #include #include -#include +#include #include #include @@ -80,36 +93,29 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData, const std::string& theParentId) - : ModuleBase_ModelWidget(theParent, theData, theParentId), - myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false) + : ModuleBase_WidgetValidated(theParent, theData, theParentId), + myWorkshop(theWorkshop), myIsActive(false) { - myContainer = new QWidget(theParent); - QHBoxLayout* aLayout = new QHBoxLayout(myContainer); + QFormLayout* aLayout = new QFormLayout(this); ModuleBase_Tools::adjustMargins(aLayout); QString aLabelText = QString::fromStdString(theData->widgetLabel()); QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); - myLabel = new QLabel(aLabelText, myContainer); + myLabel = new QLabel(aLabelText, this); if (!aLabelIcon.isEmpty()) myLabel->setPixmap(QPixmap(aLabelIcon)); - aLayout->addWidget(myLabel); QString aToolTip = QString::fromStdString(theData->widgetTooltip()); - myTextLine = new QLineEdit(myContainer); + myTextLine = new QLineEdit(this); myTextLine->setReadOnly(true); myTextLine->setToolTip(aToolTip); myTextLine->installEventFilter(this); - aLayout->addWidget(myTextLine, 1); + aLayout->addRow(myLabel, myTextLine); std::string aTypes = theData->getProperty("shape_types"); myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts); - - std::string aObjTypes = theData->getProperty("object_types"); - myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts); - - myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); } //******************************************************************** @@ -119,55 +125,76 @@ ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector() } //******************************************************************** -bool ModuleBase_WidgetShapeSelector::storeValue() const +bool ModuleBase_WidgetShapeSelector::storeValueCustom() const { - FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject); + // the value is stored on the selection changed signal processing + return true; +} + +//******************************************************************** +bool ModuleBase_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject, + GeomShapePtr theShape) const +{ + bool isChanged = false; + FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject); if (aSelectedFeature == myFeature) // In order to avoid selection of the same object - return false; + return isChanged; DataPtr aData = myFeature->data(); - if (myUseSubShapes) { - boost::shared_ptr aSelect = - boost::dynamic_pointer_cast(aData->attribute(attributeID())); - - ResultPtr aBody = boost::dynamic_pointer_cast(mySelectedObject); - if (aBody) { - aSelect->setValue(aBody, myShape); - updateObject(myFeature); - return true; + AttributeReferencePtr aRef = aData->reference(attributeID()); + if (aRef) { + ObjectPtr aObject = aRef->value(); + if (!(aObject && aObject->isSame(theSelectedObject))) { + aRef->setValue(theSelectedObject); + isChanged = true; } } else { - boost::shared_ptr aRef = - boost::dynamic_pointer_cast(aData->attribute(attributeID())); - - ObjectPtr aObject = aRef->value(); - if (!(aObject && aObject->isSame(mySelectedObject))) { - aRef->setValue(mySelectedObject); - updateObject(myFeature); - return true; + AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID()); + if (aRefAttr) { + ObjectPtr aObject = aRefAttr->object(); + if (!(aObject && aObject->isSame(theSelectedObject))) { + aRefAttr->setObject(theSelectedObject); + isChanged = true; + } + } else { + AttributeSelectionPtr aSelectAttr = aData->selection(attributeID()); + ResultPtr aBody = std::dynamic_pointer_cast(theSelectedObject); + if (aSelectAttr && aBody && (theShape.get() != NULL)) { + aSelectAttr->setValue(aBody, theShape); + isChanged = true; + } } } - return false; + return isChanged; } //******************************************************************** -bool ModuleBase_WidgetShapeSelector::restoreValue() +void ModuleBase_WidgetShapeSelector::clearAttribute() { DataPtr aData = myFeature->data(); - bool isBlocked = this->blockSignals(true); - if (myUseSubShapes) { - boost::shared_ptr aSelect = aData->selection(attributeID()); - if (aSelect) { - mySelectedObject = aSelect->context(); - myShape = aSelect->value(); - } - } else { - boost::shared_ptr aRef = aData->reference(attributeID()); - mySelectedObject = aRef->value(); + AttributeSelectionPtr aSelect = aData->selection(attributeID()); + if (aSelect) { + aSelect->setValue(ResultPtr(), std::shared_ptr(new GeomAPI_Shape())); + return; } - updateSelectionName(); + AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID()); + if (aRefAttr) { + aRefAttr->setObject(ObjectPtr()); + return; + } + AttributeReferencePtr aRef = aData->reference(attributeID()); + if (aRef) { + aRef->setObject(ObjectPtr()); + } +} +//******************************************************************** +bool ModuleBase_WidgetShapeSelector::restoreValue() +{ + bool isBlocked = this->blockSignals(true); + updateSelectionName(); this->blockSignals(isBlocked); + return true; } @@ -182,114 +209,52 @@ QList ModuleBase_WidgetShapeSelector::getControls() const //******************************************************************** void ModuleBase_WidgetShapeSelector::onSelectionChanged() { - QList aObjects = myWorkshop->selection()->selectedObjects(); - if (aObjects.size() > 0) { - ObjectPtr aObject = aObjects.first(); - if ((!mySelectedObject) && (!aObject)) - return; - - // Check that the selected object is result (others can not be accepted) - ResultPtr aRes = boost::dynamic_pointer_cast(aObject); - if (!aRes) - return; - - if (myFeature) { - // We can not select a result of our feature - const std::list>& aResList = myFeature->results(); - std::list >::const_iterator aIt; - for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) { - if ((*aIt) == aRes) - return; - } + // In order to make reselection possible + // TODO: check with MPV clearAttribute(); + + //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations(); + QList aSelected = myWorkshop->selection()->getSelected(); + if (aSelected.size() > 0) { + Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner(); + if (isValid(anOwner)) { + setSelection(anOwner); + emit focusOutWidget(this); } - // Check that object belongs to active document or PartSet - DocumentPtr aDoc = aRes->document(); - SessionPtr aMgr = ModelAPI_Session::get(); - if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument())) - return; - - // Check that the result has a shape - GeomShapePtr aShape = ModelAPI_Tools::shape(aRes); - if (!aShape) - return; - - /// Check that object has acceptable type - if (!acceptObjectType(aObject)) - return; - - // Get sub-shapes from local selection - if (myUseSubShapes) { - NCollection_List aShapeList; - std::list aOwners; - myWorkshop->selection()->selectedShapes(aShapeList, aOwners); - if (aShapeList.Extent() > 0) { - aShape = boost::shared_ptr(new GeomAPI_Shape()); - aShape->setImpl(new TopoDS_Shape(aShapeList.First())); - } - } - - // Check that the selection corresponds to selection type - if (myUseSubShapes) { - if (!acceptSubShape(aShape)) - return; - } else { - if (!acceptObjectShape(aObject)) - return; - } - setObject(aObject, aShape); - //activateSelection(false); - emit focusOutWidget(this); } } //******************************************************************** -void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr theShape) -{ - mySelectedObject = theObj; - myShape = theShape; - if (mySelectedObject) { - raisePanel(); - if (!myUseSubShapes) { - static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE); - ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent); - } - } - updateSelectionName(); - activateSelection(false); - emit valuesChanged(); -} +//bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const +//{ +// ResultPtr aResult = std::dynamic_pointer_cast(theResult); +// +// // Check that the shape of necessary type +// std::shared_ptr aShapePtr = ModelAPI_Tools::shape(aResult); +// if (!aShapePtr) +// return false; +// TopoDS_Shape aShape = aShapePtr->impl(); +// if (aShape.IsNull()) +// return false; +// +// TopAbs_ShapeEnum aShapeType = aShape.ShapeType(); +// if (aShapeType == TopAbs_COMPOUND) { +// foreach (QString aType, +// myShapeTypes) { +// TopExp_Explorer aEx(aShape, shapeType(aType)); +// if (aEx.More()) +// return true; +// } +// } else { +// foreach (QString aType, myShapeTypes) { +// if (shapeType(aType) == aShapeType) +// return true; +// } +// } +// return false; +//} //******************************************************************** -bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const -{ - ResultPtr aResult = boost::dynamic_pointer_cast(theResult); - - // Check that the shape of necessary type - boost::shared_ptr aShapePtr = ModelAPI_Tools::shape(aResult); - if (!aShapePtr) - return false; - TopoDS_Shape aShape = aShapePtr->impl(); - if (aShape.IsNull()) - return false; - - TopAbs_ShapeEnum aShapeType = aShape.ShapeType(); - if (aShapeType == TopAbs_COMPOUND) { - foreach (QString aType, myShapeTypes) { - TopExp_Explorer aEx(aShape, shapeType(aType)); - if (aEx.More()) - return true; - } - } else { - foreach (QString aType, myShapeTypes) { - if (shapeType(aType) == aShapeType) - return true; - } - } - return false; -} - -//******************************************************************** -bool ModuleBase_WidgetShapeSelector::acceptSubShape(boost::shared_ptr theShape) const +bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr theShape) const { TopoDS_Shape aShape = theShape->impl(); foreach (QString aType, myShapeTypes) { @@ -299,35 +264,76 @@ bool ModuleBase_WidgetShapeSelector::acceptSubShape(boost::shared_ptr(theObject); - return (aConstr != NULL); - } // ToDo: Process other types of objects + ObjectPtr anObject; + std::string anAttrType = theAttribute->attributeType(); + if (anAttrType == ModelAPI_AttributeRefAttr::type()) { + AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast(theAttribute); + if (anAttr != NULL && anAttr->isObject()) + anObject = anAttr->object(); } - // Object type is defined but not found - return false; + if (anAttrType == ModelAPI_AttributeSelection::type()) { + AttributeSelectionPtr anAttr = std::dynamic_pointer_cast(theAttribute); + if (anAttr != NULL && anAttr->isInitialized()) + anObject = anAttr->context(); + } + if (anAttrType == ModelAPI_AttributeReference::type()) { + AttributeReferencePtr anAttr = std::dynamic_pointer_cast(theAttribute); + if (anAttr.get() != NULL && anAttr->isInitialized()) + anObject = anAttr->value(); + } + return anObject; } +//******************************************************************** +GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const +{ + GeomShapePtr aShape; + DataPtr aData = myFeature->data(); + if (aData.get() == NULL) + return aShape; + + AttributeSelectionPtr aSelect = aData->selection(attributeID()); + if (aSelect) + aShape = aSelect->value(); + + return aShape; +} + //******************************************************************** void ModuleBase_WidgetShapeSelector::updateSelectionName() { - if (mySelectedObject) { - std::string aName = mySelectedObject->data()->name(); - myTextLine->setText(QString::fromStdString(aName)); - } else { - if (myIsActive) { - myTextLine->setText(""); + DataPtr aData = myFeature->data(); + if (aData.get() == NULL) + return; + + bool isNameUpdated = false; + AttributeSelectionPtr aSelect = aData->selection(attributeID()); + if (aSelect) { + myTextLine->setText(QString::fromStdString(aSelect->namingName())); + isNameUpdated = true; + } + if (!isNameUpdated) { + ObjectPtr anObject = getObject(myFeature->attribute(attributeID())); + if (anObject.get() != NULL) { + std::string aName = anObject->data()->name(); + myTextLine->setText(QString::fromStdString(aName)); + } else { + AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID()); + if (aRefAttr && aRefAttr->attr().get() != NULL) { + //myIsObject = aRefAttr->isObject(); + AttributePtr anAttr = aRefAttr->attr(); + if (anAttr.get() != NULL) { + std::stringstream aName; + aName <owner()->data()->name()<<"/"<id(); + myTextLine->setText(QString::fromStdString(aName.str())); + } + } + else if (myIsActive) { + myTextLine->setText(""); + } } } } @@ -336,39 +342,28 @@ void ModuleBase_WidgetShapeSelector::updateSelectionName() //******************************************************************** void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate) { + if (myIsActive == toActivate) + return; myIsActive = toActivate; updateSelectionName(); if (myIsActive) { - connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - if (myUseSubShapes) { - - QIntList aList; - foreach (QString aType, myShapeTypes) - aList.append(shapeType(aType)); - myWorkshop->activateSubShapesSelection(aList); - if (!myObjectTypes.isEmpty()) { - myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes); - myWorkshop->viewer()->clearSelectionFilters(); - myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter); - } + QIntList aList; + foreach (QString aType, myShapeTypes) { + aList.append(shapeType(aType)); } + myWorkshop->activateSubShapesSelection(aList); } else { - disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - if (myUseSubShapes) { - if (!myObjTypeFilter.IsNull()) { - myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter); - myObjTypeFilter.Nullify(); - } - myWorkshop->deactivateSubShapesSelection(); - } + myWorkshop->deactivateSubShapesSelection(); } + + activateFilters(myWorkshop, myIsActive); } //******************************************************************** void ModuleBase_WidgetShapeSelector::raisePanel() const { - QWidget* aParent = myContainer->parentWidget(); + QWidget* aParent = this->parentWidget(); QWidget* aLastPanel = 0; while (!aParent->inherits("QDockWidget")) { aLastPanel = aParent; @@ -383,36 +378,92 @@ void ModuleBase_WidgetShapeSelector::raisePanel() const } //******************************************************************** -bool ModuleBase_WidgetShapeSelector::focusTo() +void ModuleBase_WidgetShapeSelector::activateCustom() { + connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); activateSelection(true); - return ModuleBase_ModelWidget::focusTo(); } //******************************************************************** -bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent) +void ModuleBase_WidgetShapeSelector::backupAttributeValue(const bool isBackup) { - if (theObj == myTextLine) { - if (theEvent->type() == QEvent::FocusIn) - activateSelection(true); + DataPtr aData = myFeature->data(); + AttributePtr anAttribute = myFeature->attribute(attributeID()); + + if (isBackup) { + myObject = getObject(anAttribute); + myShape = getShape(); + myRefAttribute = NULL; + myIsObject = false; + AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID()); + if (aRefAttr) { + myIsObject = aRefAttr->isObject(); + myRefAttribute = aRefAttr->attr(); + } + } + else { + storeAttributeValues(myObject, myShape); + AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID()); + if (aRefAttr) { + if (!myIsObject) + aRefAttr->setAttr(myRefAttribute); + } } - return ModuleBase_ModelWidget::eventFilter(theObj, theEvent); } //******************************************************************** -bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue) +bool ModuleBase_WidgetShapeSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner) { - if (theValue) { - ModuleBase_WidgetValueFeature* aFeatureValue = - dynamic_cast(theValue); - if (aFeatureValue && aFeatureValue->object()) { - ObjectPtr aObject = aFeatureValue->object(); - if (acceptObjectShape(aObject)) { - setObject(aObject); - return true; - } + bool isDone = false; + + ModuleBase_ViewerPrs aPrs; + myWorkshop->selection()->fillPresentation(aPrs, theOwner); + ObjectPtr aObject = aPrs.object(); + ObjectPtr aCurrentObject = getObject(myFeature->attribute(attributeID())); + if ((!aCurrentObject) && (!aObject)) + return false; + + // Check that the selected object is result (others can not be accepted) + ResultPtr aRes = std::dynamic_pointer_cast(aObject); + if (!aRes) + return false; + + if (myFeature) { + // We can not select a result of our feature + const std::list>& aResList = myFeature->results(); + std::list >::const_iterator aIt; + for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) { + if ((*aIt) == aRes) + return false; } } - return false; + // Check that object belongs to active document or PartSet + DocumentPtr aDoc = aRes->document(); + SessionPtr aMgr = ModelAPI_Session::get(); + if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument())) + return false; + + // Check that the result has a shape + GeomShapePtr aShape = ModelAPI_Tools::shape(aRes); + if (!aShape) + return false; + + // Get sub-shapes from local selection + if (!aPrs.shape().IsNull()) { + aShape = std::shared_ptr(new GeomAPI_Shape()); + aShape->setImpl(new TopoDS_Shape(aPrs.shape())); + } + // Check that the selection corresponds to selection type + if (!acceptSubShape(aShape)) + return false; + + storeAttributeValues(aObject, aShape); + return true; } +//******************************************************************** +void ModuleBase_WidgetShapeSelector::deactivate() +{ + activateSelection(false); + disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); +}