-// Copyright (C) 2014-2019 CEA/DEN, EDF R&D
+// Copyright (C) 2014-2022 CEA/DEN, EDF R&D
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
#include <ModelAPI_Events.h>
#include <ModelAPI_FiltersFactory.h>
#include <ModelAPI_ResultBody.h>
+
#include <GeomAPI_ShapeExplorer.h>
+#include <GeomAPI_Edge.h>
#include <Events_Loop.h>
#include <Config_ValidatorReader.h>
#include <QCheckBox>
#include <QDir>
-static FeaturePtr SelectorFeature;
-static std::string AttributeId;
+#ifdef WIN32
+#pragma warning(disable : 4456) // for nested foreach
+#endif
+
+FeaturePtr ModuleBase_WidgetSelectionFilter::SelectorFeature;
+std::string ModuleBase_WidgetSelectionFilter::AttributeId;
GeomAPI_Shape::ShapeType selectionType(const QString& theType)
ModuleBase_Tools::adjustMargins(aMainLayout);
aMainLayout->addStretch(1);
- QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
+ QPushButton* aLaunchBtn = new QPushButton(
+ ModuleBase_Tools::translate("FiltersSelection", "Selection by filters"), this);
connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
aMainLayout->addWidget(aLaunchBtn);
}
}
if (!aSelector)
return;
- SelectorFeature = aSelector->feature();
- AttributeId = aSelector->attributeID();
+ ModuleBase_WidgetSelectionFilter::SelectorFeature = aSelector->feature();
+ ModuleBase_WidgetSelectionFilter::AttributeId = aSelector->attributeID();
// Launch Filters operation
ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
(myWorkshop->module()->createOperation(myFeatureName));
- AttributeSelectionListPtr aAttrList = SelectorFeature->selectionList(AttributeId);
+ AttributeSelectionListPtr aAttrList =
+ ModuleBase_WidgetSelectionFilter::SelectorFeature->selectionList(
+ ModuleBase_WidgetSelectionFilter::AttributeId);
FiltersFeaturePtr aFilters = aAttrList->filters();
if (aFilters.get())
aFOperation->setFeature(aFilters);
: QWidget(theParent->filtersWidget()), myFilterID(theFilter),
mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
{
- std::string aXmlString =
- ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
+ FilterPtr aFilter = ModelAPI_Session::get()->filters()->filter(theFilter);
+ std::string aXmlString = aFilter->xmlRepresentation();
if (aXmlString.length() == 0)
addItemRow(this);
else {
- ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
+ std::string anAttrPrefix; // this must be added to the attributes names for multiple filters
+ std::string aFilterKind = ModelAPI_Session::get()->filters()->id(aFilter);
+ if (theFilter != aFilterKind) {
+ anAttrPrefix = theFilter.substr(0, theFilter.size() - aFilterKind.size());
+ }
+ ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop(), anAttrPrefix);
Config_ValidatorReader aValidatorReader(aXmlString, true);
aValidatorReader.setFeatureId(mySelection->getKind());
aValidatorReader.readAll();
connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
connect(aWidget, SIGNAL(objectUpdated()), theParent, SLOT(onObjectUpdated()));
+ aWidget->enableFocusProcessing();
}
aLayout->addWidget(aParamsWgt);
}
void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
{
+ std::string aContext = mySelection->getKind();
QHBoxLayout* aLayout = new QHBoxLayout(theParent);
ModuleBase_Tools::zeroMargins(aLayout);
myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
else
myRevBtn->setIcon(QIcon(":pictures/add.png"));
- myRevBtn->setToolTip(tr("Reverse the filter"));
+ myRevBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Reverse the filter"));
connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
aLayout->addWidget(myRevBtn);
const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
- aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
+ aLayout->addWidget(new QLabel(ModuleBase_Tools::translate(aContext, aFilterName), theParent), 1);
QToolButton* aDelBtn = new QToolButton(theParent);
aDelBtn->setIcon(QIcon(":pictures/delete.png"));
aDelBtn->setAutoRaise(true);
- aDelBtn->setToolTip(tr("Delete the filter"));
+ aDelBtn->setToolTip(ModuleBase_Tools::translate(aContext, "Delete the filter"));
connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
aLayout->addWidget(aDelBtn);
}
//*****************************************************************************
//*****************************************************************************
ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
- ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
+ ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData, bool theReadOnly)
: ModuleBase_ModelWidget(theParent, theData),
myWorkshop(theWorkshop),
- mySelectorFeature(SelectorFeature),
- mySelectorAttribute(AttributeId)
+ mySelectorFeature(ModuleBase_WidgetSelectionFilter::SelectorFeature),
+ mySelectorAttribute(ModuleBase_WidgetSelectionFilter::AttributeId)
{
// Clear Old selection
- AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
- mySelectionType = selectionType(aAttrList->selectionType().c_str());
- aAttrList->clear();
+ AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
+ mySelectionType = selectionType(aAttrList->selectionType().c_str());
+ if (!theReadOnly) {
+ aAttrList->clear();
+ }
// Define widgets
QVBoxLayout* aMainLayout = new QVBoxLayout(this);
ModuleBase_Tools::adjustMargins(aMainLayout);
- QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
+ QGroupBox* aFiltersGroup = new QGroupBox(translate("Filters"), this);
QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
aGroupLayout->setContentsMargins(0, 0, 0, 0);
aGroupLayout->setSpacing(0);
aGroupLayout->addWidget(myFiltersWgt);
myFiltersCombo = new QComboBox(aFiltersGroup);
- myFiltersCombo->addItem(tr("Add new filter..."));
+ myFiltersCombo->addItem(translate("Add new filter..."));
SessionPtr aSession = ModelAPI_Session::get();
std::list<FilterPtr> allFilters =
aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
+ storeFilters(allFilters);
QStringList aItems;
std::list<FilterPtr>::const_iterator aIt;
for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
- aItems.push_back((*aIt)->name().c_str());
- myFilters.push_back(aSession->filters()->id(*aIt));
+ aItems.push_back(translate((*aIt)->name().c_str()));
}
myFiltersCombo->addItems(aItems);
connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
aBtnLayout->addStretch(1);
- mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
+ mySelectBtn = new QPushButton(translate("Select"), aBtnWgt);
connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
aBtnLayout->addWidget(mySelectBtn);
QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
ModuleBase_Tools::zeroMargins(aLblLayout);
- aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
+ aLblLayout->addWidget(new QLabel(translate("Number of selected objects:"), aLblWgt));
myNbLbl = new QLabel("0", aLblWgt);
aLblLayout->addWidget(myNbLbl);
// Show only button
- myShowBtn = new QCheckBox(tr("Show only"), this);
+ myShowBtn = new QCheckBox(translate("Show only"), this);
connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
aLblLayout->addWidget(myShowBtn);
aMainLayout->addStretch(1);
updateSelectBtn();
+ if (theReadOnly) {
+ myFiltersCombo->hide();
+ mySelectBtn->hide();
+ aLblWgt->hide();
+ myShowBtn->hide();
+ }
}
ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
aCtx->Remove(myPreview, false);
myPreview.Nullify();
if (myListIO.Size() > 0) {
- Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
+ aCtx = myWorkshop->viewer()->AISContext();
AIS_ListOfInteractive::const_iterator aIt;
Handle(AIS_Shape) aShapeIO;
for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
if (theIndex == 0)
return;
- std::list<std::string>::iterator aIt;
- int i;
- std::string aFilter;
- for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
- if (i == (theIndex - 1)) {
- aFilter = (*aIt);
- break;
- }
- }
- ModuleBase_FilterItem* aItem = onAddFilter(aFilter);
+ ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
FiltersFeaturePtr aFiltersFeature =
std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
- aFiltersFeature->addFilter(aFilter);
- myFiltersCombo->setCurrentIndex(0);
- myFiltersCombo->removeItem(theIndex);
+ std::string aText = myFiltersCombo->itemText(theIndex).toStdString();
+
+ std::list<FilterPtr>::iterator aIt;
+ std::string aFilter;
+ std::map<std::string, FilterPtr>::const_iterator aFound = myFilters.find(aText);
+ if (aFound == myFilters.end()) {
+ std::list<FilterPtr> aFilters = aFactory->filters((GeomAPI_Shape::ShapeType) mySelectionType);
+ storeFilters(aFilters);
+ aFound = myFilters.find(aText);
+ }
+ if (aFound != myFilters.end())
+ aFilter = aFactory->id(aFound->second);
+
+ aFiltersFeature->addFilter(aFilter);
updateObject(myFeature);
- if (aItem && (aItem->widgets().size() > 0))
- aItem->widgets().first()->emitFocusInWidget();
+ QList<ModuleBase_FilterItem*> aList = itemsList();
+ if (!aList.isEmpty() && (aList.last()->widgets().size() > 0))
+ aList.last()->widgets().first()->emitFocusInWidget();
else
emitFocusInWidget();
}
{
if (theFilter.length() == 0)
return 0;
- std::list<std::string>::const_iterator aIt;
- for (aIt = myUseFilters.cbegin(); aIt != myUseFilters.cend(); aIt++) {
- if (theFilter == (*aIt))
- return 0;
- }
- myFilters.remove(theFilter);
- myUseFilters.push_back(theFilter);
ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(theFilter, this);
connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
SLOT(onDeleteItem(ModuleBase_FilterItem*)));
myFiltersLayout->removeWidget(theItem);
theItem->deleteLater();
- myUseFilters.remove(aFilter);
- myFilters.push_back(aFilter);
- myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
-
+ ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
+ if (!aFactory->filter(aFilter)->isMultiple()) {
+ //myFilters.push_back(aFilter);
+ myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
+ }
FiltersFeaturePtr aFiltersFeature =
std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
aFiltersFeature->removeFilter(aFilter);
void ModuleBase_WidgetSelectionFilter::onSelect()
{
- if (myUseFilters.size() == 0)
- return;
Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
if (aCtx.IsNull())
return;
clearCurrentSelection();
+ FiltersFeaturePtr aFiltersFeature =
+ std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
+ static SessionPtr aSession = ModelAPI_Session::get();
+ std::list< std::pair<ResultPtr, GeomShapePtr> > aResList =
+ aSession->filters()->select(aFiltersFeature, (GeomAPI_Shape::ShapeType)mySelectionType);
+
BRep_Builder aBuilder;
TopoDS_Compound aComp;
aBuilder.MakeCompound(aComp);
- DocumentPtr aDoc = myFeature->document();
- int aNb = aDoc->size(ModelAPI_ResultBody::group());
- ObjectPtr aObj;
- ResultBodyPtr aBody;
- GeomShapePtr aShape;
- for (int i = 0; i < aNb; i++) {
- aObj = aDoc->object(ModelAPI_ResultBody::group(), i);
- aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aObj);
- GeomShapePtr aShape = aBody->shape();
- std::list<GeomShapePtr> aSubShapes =
- aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
- TopTools_MapOfShape alreadyThere;
- std::list<GeomShapePtr>::const_iterator aShapesIt;
- for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
- GeomShapePtr aShape = (*aShapesIt);
- TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
- if (!alreadyThere.Add(aTShape))
- continue;
- static SessionPtr aSession = ModelAPI_Session::get();
- bool isValid = aSession->filters()->isValid(myFeature, aBody, aShape);
- if (isValid) {
- aBuilder.Add(aComp, aTShape);
- ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aObj, aShape));
- myValues.append(aValue);
- }
- }
+ std::list< std::pair<ResultPtr, GeomShapePtr> >::const_iterator itSelected = aResList.cbegin();
+ for (; itSelected != aResList.cend(); itSelected++) {
+ ResultPtr aCurRes = (*itSelected).first;
+ GeomShapePtr aSubShape = (*itSelected).second;
+ TopoDS_Shape aTShape = aSubShape->impl<TopoDS_Shape>();
+ aBuilder.Add(aComp, aTShape);
+ ModuleBase_ViewerPrsPtr aValue (new ModuleBase_ViewerPrs(aCurRes, aSubShape));
+ myValues.append(aValue);
}
if (myValues.size() > 0)
void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
{
- if (myPreview.IsNull())
- return;
Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
-
if (theShow) {
AIS_ListOfInteractive aList;
aCtx->DisplayedObjects(AIS_KOI_Shape, -1, aList);
- aList.Remove(myPreview);
+ if (!myPreview.IsNull())
+ aList.Remove(myPreview);
if (aList.Size() > 0)
myListIO = aList;
}
AIS_ListOfInteractive::const_iterator aIt;
Handle(AIS_Shape) aShapeIO;
+ bool isModified = false;
for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
if (!aShapeIO.IsNull()) {
- if (theShow)
- aCtx->Erase(aShapeIO, false);
+ if (theShow) {
+ if (aCtx->IsDisplayed(aShapeIO)) {
+ aCtx->Erase(aShapeIO, false);
+ isModified = true;
+ }
+ }
else {
- aCtx->Display(aShapeIO, false);
- std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
- anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
- myWorkshop->applyCurrentSelectionModes(anAISObj);
+ if (!aCtx->IsDisplayed(aShapeIO)) {
+ aCtx->Display(aShapeIO, false);
+ std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
+ anAISObj->setImpl(new Handle(AIS_InteractiveObject)(aShapeIO));
+ myWorkshop->applyCurrentSelectionModes(anAISObj);
+ isModified = true;
+ }
}
}
}
- myWorkshop->viewer()->update();
+ if (isModified)
+ myWorkshop->viewer()->update();
}
void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
{
- mySelectBtn->setEnabled(myUseFilters.size() > 0);
+ FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
+ mySelectBtn->setEnabled(aFiltersFeature.get() && (aFiltersFeature->filters().size() > 0));
}
void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
{
int aNb = myValues.size();
myNbLbl->setText(QString::number(aNb));
- //QString aErr = () ? tr("Selection is empty") : "";
- if (aNb == 0)
- myFeature->setError(tr("Selection is empty").toStdString(), false, false);
+ if (aNb == 0) {
+ myFeature->setError(translate("Selection is empty").toStdString(), false, false);
+ myShowBtn->setChecked(false);
+ onShowOnly(false);
+ myShowBtn->setEnabled(false);
+ }
else {
myFeature->setError("", false, false);
myFeature->data()->execState(ModelAPI_StateDone);
+ myShowBtn->setEnabled(true);
}
}
return true;
}
+QList<ModuleBase_FilterItem*> ModuleBase_WidgetSelectionFilter::itemsList() const
+{
+ return myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
+}
+
+
bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
{
ModelAPI_FiltersFactory* aFactory = ModelAPI_Session::get()->filters();
FiltersFeaturePtr aFiltersFeature = std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
- std::list<std::string> aFilters = aFiltersFeature->filters();
- std::list<std::string>::const_iterator aIt;
- for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++) {
- std::string aStr = (*aIt);
- onAddFilter(aStr);
- FilterPtr aFilterObj = aFactory->filter(aStr);
- int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
- if (aId != -1)
- myFiltersCombo->removeItem(aId);
- }
+
// Init filters member of the parent attribute
AttributeSelectionListPtr aAttrList = mySelectorFeature->selectionList(mySelectorAttribute);
if (aAttrList->filters() != aFiltersFeature) {
aAttrList->setFilters(aFiltersFeature);
}
- QList<QWidget*> aWidgets;
- QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
- foreach(ModuleBase_FilterItem* aItem, aItems) {
- QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
- foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
- aWgt->restoreValue();
+ QList<ModuleBase_FilterItem*> aItemsList = itemsList();
+ std::list<std::string> aFilters = aFiltersFeature->filters();
+
+ std::list<std::string>::const_iterator aIt;
+ int i = 0;
+ int aNbItems = aItemsList.size();
+ ModuleBase_FilterItem* aItem = 0;
+ bool isBlocked = myFiltersCombo->blockSignals(true);
+ for (aIt = aFilters.cbegin(); aIt != aFilters.cend(); aIt++, i++) {
+ std::string aStr = (*aIt);
+ aItem = 0;
+ if (i >= aNbItems) {
+ aItem = onAddFilter(aStr);
+ FilterPtr aFilterObj = aFactory->filter(aStr);
+ int aId = myFiltersCombo->findText(aFilterObj->name().c_str());
+ if ((aId != -1) && !aFilterObj->isMultiple())
+ myFiltersCombo->removeItem(aId);
+ if (aItem) {
+ QList<ModuleBase_ModelWidget*> aSubList = aItem->widgets();
+ foreach(ModuleBase_ModelWidget* aWgt, aSubList) {
+ aWgt->restoreValue();
+ }
+ }
}
}
+ myFiltersCombo->setCurrentIndex(0);
+ myFiltersCombo->blockSignals(isBlocked);
return true;
}
QString aErrorMsg = ModuleBase_ModelWidget::getError(theValueStateChecked);
if (aErrorMsg.isEmpty()) {
if (myValues.size() == 0)
- aErrorMsg = tr("Selection is empty");
+ aErrorMsg = translate("Selection is empty");
}
return aErrorMsg;
}
clearCurrentSelection(true);
updateNumberSelected();
- QList<ModuleBase_FilterItem*> aItemsList = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
+ QList<ModuleBase_FilterItem*> aItemsList = itemsList();
foreach(ModuleBase_FilterItem* aItem, aItemsList) {
QList<ModuleBase_ModelWidget*> aWidgetsList = aItem->widgets();
foreach(ModuleBase_ModelWidget* aWidget, aWidgetsList) {
ModelAPI_EventCreator::get()->sendUpdated(myFeature, EVENT_DISP);
Events_Loop::loop()->flush(EVENT_DISP);
}
+
+void ModuleBase_WidgetSelectionFilter::storeFilters(const std::list<FilterPtr>& theFilters)
+{
+ for (std::list<FilterPtr>::const_iterator anIt = theFilters.begin();
+ anIt != theFilters.end(); ++anIt) {
+ std::string aName = translate((*anIt)->name()).toStdString();
+ myFilters[aName] = *anIt;
+ }
+}
+
+QString ModuleBase_WidgetSelectionFilter::translate(const std::string& theString) const
+{
+ return ModuleBase_Tools::translate(myFeatureId, theString);
+}