X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FModuleBase%2FModuleBase_WidgetShapeSelector.cpp;h=86f7a3471d935804915ccdb23045586cee2abd45;hb=28c90c232ffe159b88edd156286a398bfa3bb73b;hp=3fe721135cce046f132df3a5058f1db4335f12f8;hpb=a39242db16d62e2706d4902437e011bfd447ea88;p=modules%2Fshaper.git diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp index 3fe721135..86f7a3471 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp @@ -2,18 +2,33 @@ // Created: 2 June 2014 // Author: Vitaly Smetannikov -#include "ModuleBase_WidgetShapeSelector.h" -#include "ModuleBase_IWorkshop.h" +#include +#include +#include +#include +#include +#include +#include +#include #include -#include -#include - +#include +#include +#include +#include #include -#include +#include +#include +#include #include +#include #include +#include +#include +#include +#include #include +#include #include @@ -29,7 +44,13 @@ #include #include -#include +#include +#include + +#include + +#include +#include typedef QMap ShapeTypes; static ShapeTypes MyShapeTypes; @@ -38,15 +59,21 @@ TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theTyp { if (MyShapeTypes.count() == 0) { MyShapeTypes["face"] = TopAbs_FACE; + MyShapeTypes["faces"] = TopAbs_FACE; MyShapeTypes["vertex"] = TopAbs_VERTEX; + MyShapeTypes["vertices"] = TopAbs_VERTEX; MyShapeTypes["wire"] = TopAbs_WIRE; MyShapeTypes["edge"] = TopAbs_EDGE; + MyShapeTypes["edges"] = TopAbs_EDGE; MyShapeTypes["shell"] = TopAbs_SHELL; MyShapeTypes["solid"] = TopAbs_SOLID; + MyShapeTypes["solids"] = TopAbs_SOLID; } - if (MyShapeTypes.contains(theType)) - return MyShapeTypes[theType]; - throw std::invalid_argument("Shape type defined in XML is not implemented!"); + QString aType = theType.toLower(); + if (MyShapeTypes.contains(aType)) + return MyShapeTypes[aType]; + Events_Error::send("Shape type defined in XML is not implemented!"); + return TopAbs_SHAPE; } ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent, @@ -54,12 +81,12 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen const Config_WidgetAPI* theData, const std::string& theParentId) : ModuleBase_ModelWidget(theParent, theData, theParentId), - myWorkshop(theWorkshop) + myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false) { myContainer = new QWidget(theParent); QHBoxLayout* aLayout = new QHBoxLayout(myContainer); + ModuleBase_Tools::adjustMargins(aLayout); - aLayout->setContentsMargins(0, 0, 0, 0); QString aLabelText = QString::fromStdString(theData->widgetLabel()); QString aLabelIcon = QString::fromStdString(theData->widgetIcon()); myLabel = new QLabel(aLabelText, myContainer); @@ -81,21 +108,19 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen aLayout->addWidget(myTextLine, 1); - myActivateBtn = new QToolButton(myContainer); - myActivateBtn->setIcon(QIcon(":icons/hand_point.png")); - myActivateBtn->setCheckable(true); - myActivateBtn->setToolTip(tr("Activate/Deactivate selection")); - connect(myActivateBtn, SIGNAL(toggled(bool)), this, SLOT(activateSelection(bool))); + std::string aTypes = theData->getProperty("shape_types"); + myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts); - aLayout->addWidget(myActivateBtn); + std::string aObjTypes = theData->getProperty("object_types"); + myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts); - std::string aTypes = theData->getProperty("shape_types"); - myShapeTypes = QString(aTypes.c_str()).split(' '); + myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); } //******************************************************************** ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector() { + activateSelection(false); } //******************************************************************** @@ -106,25 +131,45 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const return false; DataPtr aData = myFeature->data(); - boost::shared_ptr aRef = boost::dynamic_pointer_cast< - ModelAPI_AttributeReference>(aData->attribute(attributeID())); - - ObjectPtr aObject = aRef->value(); - if (!(aObject && aObject->isSame(mySelectedObject))) { - aRef->setValue(mySelectedObject); - updateObject(myFeature); + 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; + } + } 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; + } } - return true; + return false; } //******************************************************************** bool ModuleBase_WidgetShapeSelector::restoreValue() { DataPtr aData = myFeature->data(); - boost::shared_ptr aRef = aData->reference(attributeID()); - bool isBlocked = this->blockSignals(true); - mySelectedObject = aRef->value(); + 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(); + } updateSelectionName(); this->blockSignals(isBlocked); @@ -137,42 +182,95 @@ QList ModuleBase_WidgetShapeSelector::getControls() const QList aControls; aControls.append(myLabel); aControls.append(myTextLine); - aControls.append(myActivateBtn); return aControls; } //******************************************************************** void ModuleBase_WidgetShapeSelector::onSelectionChanged() { - QList aObjects = myWorkshop->selectedObjects(); + QList aObjects = myWorkshop->selection()->selectedObjects(); if (aObjects.size() > 0) { ObjectPtr aObject = aObjects.first(); if ((!mySelectedObject) && (!aObject)) return; - if (mySelectedObject && aObject && mySelectedObject->isSame(aObject)) + + // Check that the selected object is result (others can not be accepted) + ResultPtr aRes = boost::dynamic_pointer_cast(aObject); + if (!aRes) return; - // Check that the selection corresponds to selection type - if (!isAccepted(aObject)) + 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; + } + } + // 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; - mySelectedObject = aObject; - if (mySelectedObject) { - updateSelectionName(); - myActivateBtn->setChecked(false); - raisePanel(); + // 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 { - myTextLine->setText(""); + if (!acceptObjectShape(aObject)) + return; } - emit valuesChanged(); + setObject(aObject, aShape); + //activateSelection(false); emit focusOutWidget(this); } } //******************************************************************** -bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const +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 = boost::dynamic_pointer_cast(theResult); + + // Check that the shape of necessary type boost::shared_ptr aShapePtr = ModelAPI_Tools::shape(aResult); if (!aShapePtr) return false; @@ -197,45 +295,93 @@ bool ModuleBase_WidgetShapeSelector::isAccepted(const ObjectPtr theResult) const } //******************************************************************** -void ModuleBase_WidgetShapeSelector::updateSelectionName() +bool ModuleBase_WidgetShapeSelector::acceptSubShape(boost::shared_ptr theShape) const { - if (mySelectedObject) { - std::string aName = mySelectedObject->data()->name(); + TopoDS_Shape aShape = theShape->impl(); + foreach (QString aType, myShapeTypes) { + if (aShape.ShapeType() == shapeType(aType)) + return true; + } + return false; +} - myTextLine->setText(QString::fromStdString(aName)); - } else - myTextLine->setText(""); +//******************************************************************** +bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const +{ + // Definition of types is not obligatory. If types are not defined then + // it means that accepted any type + if (myObjectTypes.isEmpty()) + return true; + + foreach (QString aType, myObjectTypes) { + if (aType.toLower() == "construction") { + ResultConstructionPtr aConstr = + boost::dynamic_pointer_cast(theObject); + return (aConstr != NULL); + } // ToDo: Process other types of objects + } + // Object type is defined but not found + return false; } + //******************************************************************** -void ModuleBase_WidgetShapeSelector::enableOthersControls(bool toEnable) const +void ModuleBase_WidgetShapeSelector::updateSelectionName() { - QWidget* aParent = myContainer->parentWidget(); - QList aChldList = aParent->findChildren(); - foreach(QWidget* aWgt, aChldList) - { - if ((aWgt != myLabel) && (aWgt != myActivateBtn) && (aWgt != myTextLine) - && (aWgt != myContainer)) - aWgt->setEnabled(toEnable); + if (mySelectedObject) { + std::string aName = mySelectedObject->data()->name(); + myTextLine->setText(QString::fromStdString(aName)); + } else { + if (myIsActive) { + QString aMsg = tr("Select a "); + int i = 0; + foreach (QString aType, myShapeTypes) { + if (i > 0) + aMsg += " or "; + aMsg += aType; + i++; + } + myTextLine->setText(aMsg); + } else + myTextLine->setText(tr("No object selected")); } } + //******************************************************************** void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate) { - enableOthersControls(!toActivate); - //myTextLine->setEnabled(toActivate); - if (toActivate) + myIsActive = toActivate; + if (myIsActive) myTextLine->setPalette(myBasePalet); else myTextLine->setPalette(myInactivePalet); + updateSelectionName(); - if (toActivate) + if (myIsActive) { connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - else + 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); + } + } + } else { disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); - - myActivateBtn->setDown(toActivate); + if (myUseSubShapes) { + if (!myObjTypeFilter.IsNull()) { + myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter); + myObjTypeFilter.Nullify(); + } + myWorkshop->deactivateSubShapesSelection(); + } + } } //******************************************************************** @@ -258,6 +404,34 @@ void ModuleBase_WidgetShapeSelector::raisePanel() const //******************************************************************** bool ModuleBase_WidgetShapeSelector::focusTo() { - myActivateBtn->setChecked(true); + activateSelection(true); return true; } + +//******************************************************************** +bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent) +{ + if (theObj == myTextLine) { + if (theEvent->type() == QEvent::FocusIn) + activateSelection(true); + } + return ModuleBase_ModelWidget::eventFilter(theObj, theEvent); +} + +//******************************************************************** +bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue) +{ + if (theValue) { + ModuleBase_WidgetValueFeature* aFeatureValue = + dynamic_cast(theValue); + if (aFeatureValue && aFeatureValue->object()) { + ObjectPtr aObject = aFeatureValue->object(); + if (acceptObjectShape(aObject)) { + setObject(aObject); + return true; + } + } + } + return false; +} +