1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ModuleBase_WidgetSelectionFilter.h"
21 #include "ModuleBase_Tools.h"
22 #include "ModuleBase_IWorkshop.h"
23 #include "ModuleBase_ISelectionActivate.h"
24 #include "ModuleBase_IModule.h"
25 #include "ModuleBase_IViewer.h"
26 #include "ModuleBase_IPropertyPanel.h"
27 #include "ModuleBase_PageWidget.h"
28 #include "ModuleBase_WidgetMultiSelector.h"
29 #include "ModuleBase_ResultPrs.h"
30 #include "ModuleBase_WidgetFactory.h"
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_AttributeSelectionList.h>
34 #include <ModelAPI_Events.h>
35 #include <GeomAPI_ShapeExplorer.h>
37 #include <Events_Loop.h>
39 #include <AIS_InteractiveContext.hxx>
40 #include <StdSelect_BRepOwner.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <BRep_Builder.hxx>
45 #include <QPushButton>
50 #include <QToolButton>
53 static FeaturePtr SelectorFeature;
54 static std::string AttributeId;
57 GeomAPI_Shape::ShapeType selectionType(const QString& theType)
59 QString aType = theType.toUpper();
60 if ((aType == "VERTEX") || (aType == "VERTICES"))
61 return GeomAPI_Shape::VERTEX;
62 else if ((aType == "EDGE") || (aType == "EDGES"))
63 return GeomAPI_Shape::EDGE;
64 else if ((aType == "WIRE") || (aType == "WIRES"))
65 return GeomAPI_Shape::WIRE;
66 else if ((aType == "FACE") || (aType == "FACES"))
67 return GeomAPI_Shape::FACE;
68 else if ((aType == "SHELL") || (aType == "SHELLS"))
69 return GeomAPI_Shape::SHELL;
70 else if ((aType == "SOLID") || (aType == "SOLIDS"))
71 return GeomAPI_Shape::SOLID;
72 else if ((aType == "COMPSOLID") || (aType == "COMPSOLIDS"))
73 return GeomAPI_Shape::COMPSOLID;
74 else if ((aType == "COMPOUND") || (aType == "COMPOUNDS"))
75 return GeomAPI_Shape::COMPOUND;
77 return GeomAPI_Shape::SHAPE;
81 ModuleBase_FilterStarter::ModuleBase_FilterStarter(const std::string& theFeature,
82 QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
84 myFeatureName(theFeature),
85 myWorkshop(theWorkshop)
87 QHBoxLayout* aMainLayout = new QHBoxLayout(this);
88 ModuleBase_Tools::adjustMargins(aMainLayout);
90 aMainLayout->addStretch(1);
91 QPushButton* aLaunchBtn = new QPushButton(tr("Selection by filters"), this);
92 connect(aLaunchBtn, SIGNAL(clicked()), SLOT(onFiltersLaunch()));
93 aMainLayout->addWidget(aLaunchBtn);
95 myFilterLbl = new QLabel(this);
96 myFilterLbl->setPixmap(QPixmap(":pictures/filter.png"));
97 aMainLayout->addWidget(myFilterLbl);
99 myModifyLbl = new QLabel(this);
100 myModifyLbl->setPixmap(QPixmap(":pictures/plus_minus.png"));
101 aMainLayout->addWidget(myModifyLbl);
102 aMainLayout->addStretch(1);
108 void ModuleBase_FilterStarter::onFiltersLaunch()
110 QWidget* aParent = parentWidget();
111 ModuleBase_WidgetMultiSelector* aSelector =
112 dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
114 aParent = aParent->parentWidget();
115 aSelector = dynamic_cast<ModuleBase_WidgetMultiSelector*>(aParent);
119 SelectorFeature = aSelector->feature();
120 AttributeId = aSelector->attributeID();
122 // Launch Filters operation
123 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
124 (myWorkshop->module()->createOperation(myFeatureName));
125 myWorkshop->processLaunchOperation(aFOperation);
128 //*****************************************************************************
129 //*****************************************************************************
130 //*****************************************************************************
131 ModuleBase_FilterItem::ModuleBase_FilterItem(
132 const std::string& theFilter, ModuleBase_WidgetSelectionFilter* theParent)
133 : QWidget(theParent->filtersWidget()), myFilterID(theFilter),
134 mySelection(std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(theParent->feature()))
136 std::string aXmlString = ModelAPI_Session::get()->filters()->filter(theFilter)->xmlRepresentation();
137 if (aXmlString.length() == 0)
140 ModuleBase_WidgetFactory aFactory(aXmlString, theParent->workshop());
141 QVBoxLayout* aLayout = new QVBoxLayout(this);
142 ModuleBase_Tools::zeroMargins(aLayout);
144 QWidget* aItemRow = new QWidget(this);
145 addItemRow(aItemRow);
146 aLayout->addWidget(aItemRow);
148 ModuleBase_PageWidget* aParamsWgt = new ModuleBase_PageWidget(this);
149 aParamsWgt->setFrameStyle(QFrame::Box | QFrame::Raised);
150 aFactory.createWidget(aParamsWgt);
151 ModuleBase_Tools::zeroMargins(aParamsWgt->layout());
152 myWidgets = aFactory.getModelWidgets();
153 foreach(ModuleBase_ModelWidget* aWidget, myWidgets) {
154 aWidget->setFeature(theParent->feature());
155 connect(aWidget, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)),
156 theParent, SIGNAL(focusInWidget(ModuleBase_ModelWidget*)));
157 connect(aWidget, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)),
158 theParent, SIGNAL(focusOutWidget(ModuleBase_ModelWidget*)));
160 aLayout->addWidget(aParamsWgt);
164 void ModuleBase_FilterItem::addItemRow(QWidget* theParent)
166 QHBoxLayout* aLayout = new QHBoxLayout(theParent);
167 ModuleBase_Tools::zeroMargins(aLayout);
169 // Reverse filter button
170 myRevBtn = new QToolButton(theParent);
171 myRevBtn->setCheckable(true);
172 myRevBtn->setChecked(false);
173 myRevBtn->setAutoRaise(true);
174 myRevBtn->setIcon(QIcon(":pictures/add.png"));
175 myRevBtn->setToolTip(tr("Reverse the filter"));
176 connect(myRevBtn, SIGNAL(toggled(bool)), SLOT(onReverse(bool)));
177 aLayout->addWidget(myRevBtn);
179 const std::string& aFilterName = ModelAPI_Session::get()->filters()->filter(myFilterID)->name();
180 aLayout->addWidget(new QLabel(aFilterName.c_str(), theParent), 1);
182 QToolButton* aDelBtn = new QToolButton(theParent);
183 aDelBtn->setIcon(QIcon(":pictures/delete.png"));
184 aDelBtn->setAutoRaise(true);
185 aDelBtn->setToolTip(tr("Delete the filter"));
186 connect(aDelBtn, SIGNAL(clicked(bool)), SLOT(onDelete()));
187 aLayout->addWidget(aDelBtn);
190 void ModuleBase_FilterItem::onReverse(bool theCheck)
192 mySelection->setReversed(myFilterID, theCheck);
194 myRevBtn->setIcon(QIcon(":pictures/reverce.png"));
196 myRevBtn->setIcon(QIcon(":pictures/add.png"));
197 emit reversedItem(this);
200 void ModuleBase_FilterItem::onDelete()
202 emit deleteItem(this);
205 QList<QWidget*> ModuleBase_FilterItem::getControls() const
207 QList<QWidget*> aWidgetsList;
208 foreach(ModuleBase_ModelWidget* aWgt, myWidgets) {
209 QList<QWidget*> aSubList = aWgt->getControls();
210 foreach(QWidget* aSub, aSubList) {
211 aWidgetsList.append(aSub);
219 //*****************************************************************************
220 //*****************************************************************************
221 //*****************************************************************************
222 ModuleBase_WidgetSelectionFilter::ModuleBase_WidgetSelectionFilter(QWidget* theParent,
223 ModuleBase_IWorkshop* theWorkshop, const Config_WidgetAPI* theData)
224 : ModuleBase_ModelWidget(theParent, theData),
225 myWorkshop(theWorkshop),
226 mySelectorFeature(SelectorFeature),
227 mySelectorAttribute(AttributeId)
229 // Clear Old selection
230 AttributePtr aAttr = SelectorFeature->attribute(AttributeId);
231 AttributeSelectionListPtr aSelListAttr =
232 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
233 mySelectionType = selectionType(aSelListAttr->selectionType().c_str());
234 aSelListAttr->clear();
237 QVBoxLayout* aMainLayout = new QVBoxLayout(this);
238 ModuleBase_Tools::adjustMargins(aMainLayout);
240 QGroupBox* aFiltersGroup = new QGroupBox(tr("Filters"), this);
241 QVBoxLayout* aGroupLayout = new QVBoxLayout(aFiltersGroup);
242 aGroupLayout->setContentsMargins(0, 0, 0, 0);
243 aGroupLayout->setSpacing(0);
245 myFiltersWgt = new QWidget();
246 myFiltersLayout = new QVBoxLayout(myFiltersWgt);
247 myFiltersLayout->setContentsMargins(0, 0, 0, 0);
248 aGroupLayout->addWidget(myFiltersWgt);
250 myFiltersCombo = new QComboBox(aFiltersGroup);
251 myFiltersCombo->addItem(tr("Add new filter..."));
252 SessionPtr aSession = ModelAPI_Session::get();
253 std::list<FilterPtr> allFilters =
254 aSession->filters()->filters((GeomAPI_Shape::ShapeType) mySelectionType);
256 std::list<FilterPtr>::const_iterator aIt;
257 for (aIt = allFilters.cbegin(); aIt != allFilters.cend(); aIt++) {
258 aItems.push_back((*aIt)->name().c_str());
259 myFilters.push_back(aSession->filters()->id(*aIt));
261 myFiltersCombo->addItems(aItems);
262 connect(myFiltersCombo, SIGNAL(currentIndexChanged(int)), SLOT(onAddFilter(int)));
264 aGroupLayout->addWidget(myFiltersCombo);
265 aMainLayout->addWidget(aFiltersGroup);
268 QWidget* aBtnWgt = new QWidget(this);
269 QHBoxLayout* aBtnLayout = new QHBoxLayout(aBtnWgt);
270 ModuleBase_Tools::adjustMargins(aBtnLayout);
272 aBtnLayout->addStretch(1);
274 mySelectBtn = new QPushButton(tr("Select"), aBtnWgt);
275 connect(mySelectBtn, SIGNAL(clicked()), SLOT(onSelect()));
276 aBtnLayout->addWidget(mySelectBtn);
278 aMainLayout->addWidget(aBtnWgt);
281 QWidget* aLblWgt = new QWidget(this);
282 QHBoxLayout* aLblLayout = new QHBoxLayout(aLblWgt);
283 ModuleBase_Tools::zeroMargins(aLblLayout);
285 aLblLayout->addWidget(new QLabel(tr("Number of selected objects:"), aLblWgt));
287 myNbLbl = new QLabel("0", aLblWgt);
288 aLblLayout->addWidget(myNbLbl);
291 myShowBtn = new QCheckBox(tr("Show only"), this);
292 connect(myShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
293 aLblLayout->addWidget(myShowBtn);
295 aMainLayout->addWidget(aLblWgt);
297 aMainLayout->addStretch(1);
302 ModuleBase_WidgetSelectionFilter::~ModuleBase_WidgetSelectionFilter()
305 if (!myPreview.IsNull()) {
306 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
307 aCtx->Remove(myPreview, false);
309 if (myShowBtn->isChecked()) {
310 AIS_ListOfInteractive::const_iterator aIt;
311 Handle(AIS_Shape) aShapeIO;
312 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
313 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
314 if (!aShapeIO.IsNull()) {
315 aCtx->Display(aShapeIO, false);
319 aCtx->UpdateCurrentViewer();
321 SelectorFeature = FeaturePtr();
325 void ModuleBase_WidgetSelectionFilter::onAddFilter(int theIndex)
330 std::list<std::string>::iterator aIt;
333 for (aIt = myFilters.begin(), i = 0; aIt != myFilters.cend(); i++, aIt++) {
334 if (i == (theIndex - 1)) {
336 myFilters.erase(aIt);
340 if (!aFilter.empty()) {
341 myUseFilters.push_back(aFilter);
342 ModuleBase_FilterItem* aItem = new ModuleBase_FilterItem(aFilter, this);
343 connect(aItem, SIGNAL(deleteItem(ModuleBase_FilterItem*)),
344 SLOT(onDeleteItem(ModuleBase_FilterItem*)));
345 connect(aItem, SIGNAL(reversedItem(ModuleBase_FilterItem*)),
346 SLOT(onReverseItem(ModuleBase_FilterItem*)));
347 myFiltersLayout->addWidget(aItem);
349 FiltersFeaturePtr aFiltersFeature =
350 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
351 aFiltersFeature->addFilter(aFilter);
354 clearCurrentSelection(true);
355 updateNumberSelected();
356 myFiltersCombo->setCurrentIndex(0);
357 myFiltersCombo->removeItem(theIndex);
359 enableFocusProcessing();
362 void ModuleBase_WidgetSelectionFilter::onDeleteItem(ModuleBase_FilterItem* theItem)
364 std::string aFilter = theItem->filter();
365 QList<ModuleBase_ModelWidget*> aWidgets = theItem->widgets();
366 foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
369 myFiltersLayout->removeWidget(theItem);
370 theItem->deleteLater();
372 myUseFilters.remove(aFilter);
373 myFilters.push_back(aFilter);
374 myFiltersCombo->addItem(ModelAPI_Session::get()->filters()->filter(aFilter)->name().c_str());
376 FiltersFeaturePtr aFiltersFeature =
377 std::dynamic_pointer_cast<ModelAPI_FiltersFeature>(myFeature);
378 aFiltersFeature->removeFilter(aFilter);
381 clearCurrentSelection(true);
382 updateNumberSelected();
384 enableFocusProcessing();
385 myWorkshop->deactivateCurrentSelector();
386 myWorkshop->selectionActivate()->updateSelectionModes();
387 myWorkshop->selectionActivate()->updateSelectionFilters();
392 void ModuleBase_WidgetSelectionFilter::redisplayFeature()
394 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
395 ModelAPI_EventCreator::get()->sendUpdated(myFeature, aDispEvent);
396 Events_Loop::loop()->flush(aDispEvent);
399 void ModuleBase_WidgetSelectionFilter::onReverseItem(ModuleBase_FilterItem* theItem)
402 clearCurrentSelection(true);
403 updateNumberSelected();
406 void ModuleBase_WidgetSelectionFilter::onSelect()
408 if (myUseFilters.size() == 0)
410 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
414 clearCurrentSelection();
416 BRep_Builder aBuilder;
417 TopoDS_Compound aComp;
418 aBuilder.MakeCompound(aComp);
420 if (!myShowBtn->isChecked()) {
422 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
423 if (!myPreview.IsNull())
424 myListIO.Remove(myPreview);
426 AIS_ListOfInteractive::const_iterator aIt;
427 Handle(ModuleBase_ResultPrs) aShapeIO;
428 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
429 aShapeIO = Handle(ModuleBase_ResultPrs)::DownCast(*aIt);
430 if (!aShapeIO.IsNull()) {
431 GeomShapePtr aShape(new GeomAPI_Shape);
432 aShape->setImpl(new TopoDS_Shape(aShapeIO->Shape()));
433 std::list<GeomShapePtr> aSubShapes =
434 aShape->subShapes((GeomAPI_Shape::ShapeType)mySelectionType);
435 std::list<GeomShapePtr>::const_iterator aShapesIt;
436 for (aShapesIt = aSubShapes.cbegin(); aShapesIt != aSubShapes.cend(); aShapesIt++) {
437 GeomShapePtr aShape = (*aShapesIt);
438 SessionPtr aSession = ModelAPI_Session::get();
439 bool isValid = aSession->filters()->isValid(myFeature, aShape);
441 TopoDS_Shape aTShape = aShape->impl<TopoDS_Shape>();
442 Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aTShape, aShapeIO, true);
443 aBuilder.Add(aComp, aTShape);
445 ModuleBase_ViewerPrsPtr aValue(new ModuleBase_ViewerPrs(aShapeIO->getResult(), aShape, aOwner));
446 myValues.append(aValue);
451 if (myValues.size() > 0)
452 updatePreview(aComp);
453 updateNumberSelected();
456 void ModuleBase_WidgetSelectionFilter::updatePreview(const TopoDS_Shape& theShape)
458 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
462 if (myPreview.IsNull()) {
463 myPreview = new AIS_Shape(theShape);
464 //myPreview->SetDisplayMode(myShowBtn->isChecked()? AIS_Shaded : AIS_WireFrame);
465 myPreview->SetDisplayMode(AIS_Shaded);
466 myPreview->SetColor(Quantity_NOC_BLUE1);
467 myPreview->SetTransparency();
468 aCtx->Display(myPreview, true);
469 aCtx->Deactivate(myPreview);
472 myPreview->Set(theShape);
473 aCtx->Redisplay(myPreview, true);
478 void ModuleBase_WidgetSelectionFilter::onShowOnly(bool theShow)
480 if (myPreview.IsNull())
482 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
486 aCtx->DisplayedObjects(AIS_KOI_Shape, -1, myListIO);
487 myListIO.Remove(myPreview);
489 AIS_ListOfInteractive::const_iterator aIt;
490 Handle(AIS_Shape) aShapeIO;
491 for (aIt = myListIO.cbegin(); aIt != myListIO.cend(); aIt++) {
492 aShapeIO = Handle(AIS_Shape)::DownCast(*aIt);
493 if (!aShapeIO.IsNull()) {
495 aCtx->Erase(aShapeIO, false);
497 aCtx->Display(aShapeIO, false);
500 aCtx->UpdateCurrentViewer();
503 void ModuleBase_WidgetSelectionFilter::updateSelectBtn()
505 mySelectBtn->setEnabled(myUseFilters.size() > 0);
508 void ModuleBase_WidgetSelectionFilter::updateNumberSelected()
510 myNbLbl->setText(QString::number(myValues.size()));
512 QList<QWidget*> ModuleBase_WidgetSelectionFilter::getControls() const
514 QList<QWidget*> aWidgets;
515 QList<ModuleBase_FilterItem*> aItems = myFiltersWgt->findChildren<ModuleBase_FilterItem*>();
516 foreach(ModuleBase_FilterItem* aItem, aItems) {
517 QList<QWidget*> aSubList = aItem->getControls();
518 foreach(QWidget* aWgt, aSubList) {
519 aWidgets.append(aWgt);
525 void ModuleBase_WidgetSelectionFilter::clearCurrentSelection(bool toUpdate)
528 if (!myPreview.IsNull()) {
529 Handle(AIS_InteractiveContext) aCtx = myWorkshop->viewer()->AISContext();
530 aCtx->Remove(myPreview, toUpdate);
535 void ModuleBase_WidgetSelectionFilter::onFeatureAccepted()
537 AttributePtr aAttr = mySelectorFeature->attribute(mySelectorAttribute);
538 AttributeSelectionListPtr aSelListAttr =
539 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aAttr);
540 aSelListAttr->clear();
541 foreach(ModuleBase_ViewerPrsPtr aPrs, myValues) {
542 aSelListAttr->append(aPrs->object(), aPrs->shape());
546 bool ModuleBase_WidgetSelectionFilter::storeValueCustom()
551 bool ModuleBase_WidgetSelectionFilter::restoreValueCustom()
553 ModuleBase_ModelWidget* aActive = myWorkshop->propertyPanel()->activeWidget();
555 return aActive->restoreValue();