1 // Copyright (C) 2014-2024 CEA, EDF
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 "XGUI_FacesPanel.h"
21 #include "XGUI_ObjectsBrowser.h"
22 #include "XGUI_SelectionMgr.h"
23 #include "XGUI_Selection.h"
24 #include "XGUI_Tools.h"
25 #include "XGUI_Workshop.h"
26 #include "XGUI_Displayer.h"
27 #include "XGUI_ViewerProxy.h"
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_ISelection.h>
31 #include <ModuleBase_IWorkshop.h>
32 #include <ModuleBase_IViewer.h>
33 #include <ModuleBase_ListView.h>
34 #include <ModuleBase_ResultPrs.h>
35 #include <ModuleBase_Tools.h>
36 #include <ModuleBase_ViewerPrs.h>
37 #include <ModuleBase_SelectionFilterType.h>
39 #include <Config_PropManager.h>
40 #include <Events_Loop.h>
41 #include <GeomAlgoAPI_CompoundBuilder.h>
42 #include <GeomAPI_Shape.h>
44 #include <ModelAPI_Events.h>
45 #include <ModelAPI_AttributeSelectionList.h>
47 #include <ModuleBase_Operation.h>
48 #include <ModuleBase_OperationFeature.h>
49 #include <XGUI_OperationMgr.h>
53 #include <QFocusEvent>
54 #include <QGridLayout>
55 #include <QListWidget>
56 #include <QMainWindow>
60 #pragma warning(disable : 4189) // for skipping MAYBE_UNUSED on Win
63 static const int LayoutMargin = 3;
65 //********************************************************************
66 void updateHiddenShapes(Handle(ModuleBase_ResultPrs) thePrs, const TopoDS_ListOfShape& theShapes)
68 TopoDS_ListOfShape aAlreadyHidden = thePrs->hiddenSubShapes();
69 TopoDS_ListOfShape::Iterator aShPIt(theShapes);
70 for (; aShPIt.More(); aShPIt.Next()) {
71 if (aAlreadyHidden.Contains(aShPIt.Value()))
72 aAlreadyHidden.Remove(aShPIt.Value());
74 thePrs->setSubShapeHidden(aAlreadyHidden);
77 //********************************************************************
78 void objectsMapFromPrs(ModuleBase_ViewerPrsPtr thePrs,
79 std::map<ObjectPtr, TopoDS_ListOfShape>& theObjectToShapes,
80 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) >& theObjectToPrs)
82 ObjectPtr anObject = thePrs->object();
87 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
88 thePrs->interactive());
89 if (aResultPrs.IsNull())
92 if (theObjectToShapes.find(anObject) != theObjectToShapes.end())
93 theObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(thePrs));
95 TopoDS_ListOfShape aListOfShapes;
96 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(thePrs));
97 theObjectToShapes[anObject] = aListOfShapes;
98 theObjectToPrs[anObject] = aResultPrs;
102 //********************************************************************
103 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, XGUI_Workshop* theWorkshop)
104 : QDockWidget(theParent), myWorkshop(theWorkshop), myIsActive(false), myLastItemIndex(0)
106 setWindowTitle(tr("Hide Faces"));
107 setObjectName("Hide Faces");
109 MAYBE_UNUSED QAction* aViewAct = toggleViewAction();
110 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
112 QWidget* aContent = new QWidget(this);
113 QGridLayout* aMainLayout = new QGridLayout(aContent);
114 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
117 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
118 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
120 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
121 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
123 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
124 aMainLayout->addWidget(myListView->getControl(), 1, 0);
126 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
127 myListView->getControl()->viewport()->installEventFilter(this);
129 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
130 connect(aDisplayer, SIGNAL(objectDisplayed(ObjectPtr, AISObjectPtr)),
131 SLOT(onObjectDisplay(ObjectPtr, AISObjectPtr)));
134 //********************************************************************
135 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
137 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
140 std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
141 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
142 QMap<int, ModuleBase_ViewerPrsPtr >::const_iterator aIt;
143 for (aIt = myItems.cbegin(); aIt != myItems.cend(); aIt++) {
144 objectsMapFromPrs(aIt.value(), anObjectToShapes, anObjectToPrs);
147 std::set<ObjectPtr> aObjects;
148 TopoDS_ListOfShape anEmpty;
149 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs)>::const_iterator aPrsIt;
150 for (aPrsIt = anObjectToPrs.cbegin(); aPrsIt != anObjectToPrs.cend(); aPrsIt++) {
151 aObjects.insert(aPrsIt->first);
152 aPrsIt->second->setSubShapeHidden(anEmpty);
155 if (redisplayObjects(aObjects))
158 // clear internal containers
159 myListView->getControl()->clear();
161 updateProcessedObjects(myItems, myItemObjects);
162 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
163 myHiddenObjects.clear();
166 //********************************************************************
167 bool XGUI_FacesPanel::isEmpty() const
169 return myItems.size() == 0;
172 //********************************************************************
173 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
175 theModes.append(TopAbs_FACE);
178 //********************************************************************
179 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
181 ModuleBase_IModule* aModule = myWorkshop->module();
182 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
184 // The global filter makes problem for groups selection when any operation is launched
185 // theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
186 theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
187 theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
190 //********************************************************************
191 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
193 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
194 if (theEvent->type() == QEvent::MouseButtonRelease)
196 if (myListView->getControl()->viewport() == aWidget)
197 setActivePanel(true);
199 // pass the event on to the parent class
200 return QObject::eventFilter(theObject, theEvent);
203 //********************************************************************
204 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
206 if (myIsActive == theIsActive)
209 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
210 myIsActive = theIsActive;
214 // selection should be cleared after emit of signal to do not process selection change
215 // event by the previous selector
216 // the selection is cleared by activating selection control
217 myWorkshop->selector()->clearSelection();
225 //********************************************************************
226 bool XGUI_FacesPanel::useTransparency() const
228 return myHiddenOrTransparent->isChecked();
231 //********************************************************************
232 double XGUI_FacesPanel::transparency() const
234 return useTransparency() ?
235 Config_PropManager::real("Visualization", "hidden_face_transparency") : 1;
239 //********************************************************************
240 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
242 std::set<int> anIndicesToBeRemoved;
243 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
244 anItemsIt != myItems.end(); anItemsIt++)
246 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
247 ObjectPtr anObject = aPrs->object();
248 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
250 anIndicesToBeRemoved.insert(anItemsIt.key());
253 // remove from myItes container
254 removeItems(anIndicesToBeRemoved);
255 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
256 updateProcessedObjects(myItems, myItemObjects);
258 // remove from container of hidden objects
259 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
260 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
262 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
263 myHiddenObjects.erase(*aHiddenIt);
267 //********************************************************************
268 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
270 switch (theActionType) {
272 // return processEnter();
274 setActivePanel(false);
277 return processDelete();
286 //********************************************************************
287 void XGUI_FacesPanel::processSelection()
289 QList<ModuleBase_ViewerPrsPtr> aSelected =
290 myWorkshop->selector()->selection()->getSelected(ModuleBase_ISelection::Viewer);
292 if (aSelected.size() == 0)
295 bool isModified = false;
296 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
298 std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
299 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
300 std::set<int> aToRemove;
302 for (int i = 0; i < aSelected.size(); i++) {
303 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
304 ObjectPtr anObject = aPrs->object();
308 GeomShapePtr aShapePtr = aPrs->shape();
309 if (!aShapePtr.get() || !aShapePtr->isFace())
312 QString aItemName = XGUI_Tools::generateName(aPrs);
313 if (myListView->hasItem(aItemName))
316 objectsMapFromPrs(aPrs, anObjectToShapes, anObjectToPrs);
318 // The code is dedicated to remove already selected items if they are selected twice
319 // It can happen in case of groups selection
320 QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator aIt;
321 for (aIt = myItems.cbegin(); aIt != myItems.cend(); aIt++) {
322 ModuleBase_ViewerPrsPtr aCurPrs = aIt.value();
323 ObjectPtr aObject = aCurPrs->object();
324 if (anObjectToShapes.find(aObject) != anObjectToShapes.end()) {
325 TopoDS_ListOfShape aShapes = anObjectToShapes[aObject];
326 GeomShapePtr aShapePtr = aCurPrs->shape();
327 if (aShapes.Contains(aShapePtr->impl<TopoDS_Shape>())) {
328 aToRemove.insert(aIt.key());
333 myItems.insert(myLastItemIndex, aPrs);
334 myListView->addItem(aItemName, myLastItemIndex);
336 myUndoList.push_back(myLastItemIndex);
341 // Hide fully hidden shapes
342 std::map<ObjectPtr, TopoDS_ListOfShape>::const_iterator anIt;
343 for (anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
344 ObjectPtr anObject = anIt->first;
345 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
347 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
349 if (!aResultPrs->hasSubShapeVisible(anIt->second)) { // redisplay
350 // erase object because it is entirely hidden
351 anObject->setDisplayed(false);
352 myHiddenObjects.insert(anObject);
354 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
357 // Process selected presentations
358 double aTransp = transparency();
359 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs)>::iterator aPrsIt;
360 for (aPrsIt = anObjectToPrs.begin(); aPrsIt != anObjectToPrs.end(); aPrsIt++) {
361 ObjectPtr anObject = aPrsIt->first;
362 Handle(ModuleBase_ResultPrs) aPrs = aPrsIt->second;
363 TopoDS_ListOfShape aAlreadyHidden = aPrs->hiddenSubShapes();
364 TopoDS_ListOfShape aListOfShapes = anObjectToShapes[anObject];
365 TopoDS_ListOfShape::Iterator aShapesIt(aListOfShapes);
366 for (; aShapesIt.More(); aShapesIt.Next()) {
367 if (!aAlreadyHidden.Contains(aShapesIt.Value()))
368 aAlreadyHidden.Append(aShapesIt.Value());
370 aPrs->setSubShapeHidden(aAlreadyHidden);
371 aPrs->setHiddenSubShapeTransparency(aTransp);
374 // Remove duplicate items
375 removeItems(aToRemove);
376 myWorkshop->selector()->clearSelection();
378 updateProcessedObjects(myItems, myItemObjects);
383 //********************************************************************
384 bool XGUI_FacesPanel::processDelete()
386 //appendFirstSelectionInHistory();
387 //QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
389 std::set<int> aSelectedIds;
390 myListView->getSelectedIndices(aSelectedIds);
391 if (aSelectedIds.empty())
394 std::set<ModuleBase_ViewerPrsPtr> aRestored;
395 std::set<int>::const_iterator anIt;
396 for (anIt = aSelectedIds.begin(); anIt != aSelectedIds.end(); anIt++) {
397 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
398 if (aRestored.find(aPrs) == aRestored.end()) {
399 aRestored.insert(aPrs);
400 myItems.remove(*anIt);
403 std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
404 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
406 std::set<ModuleBase_ViewerPrsPtr>::const_iterator aIt;
407 for (aIt = aRestored.cbegin(); aIt != aRestored.cend(); aIt++) {
408 objectsMapFromPrs((*aIt), anObjectToShapes, anObjectToPrs);
411 std::set<ObjectPtr> aRestoredObjects;
412 std::map<ObjectPtr, TopoDS_ListOfShape>::const_iterator aShapesIt;
413 for (aShapesIt = anObjectToShapes.begin(); aShapesIt != anObjectToShapes.end(); aShapesIt++) {
414 TopoDS_ListOfShape aShapes = aShapesIt->second;
415 aRestoredObjects.insert(aShapesIt->first);
416 Handle(ModuleBase_ResultPrs) aPrs = anObjectToPrs[aShapesIt->first];
417 TopoDS_ListOfShape aHiddenShapes = aPrs->hiddenSubShapes();
418 TopoDS_ListOfShape::Iterator aSubShapesIt(aShapes);
419 for (; aSubShapesIt.More(); aSubShapesIt.Next()) {
420 if (aHiddenShapes.Contains(aSubShapesIt.Value()))
421 aHiddenShapes.Remove(aSubShapesIt.Value());
423 aPrs->setSubShapeHidden(aHiddenShapes);
425 if (redisplayObjects(aRestoredObjects))
428 myListView->removeSelectedItems();
432 //********************************************************************
433 bool XGUI_FacesPanel::redisplayObjects(
434 const std::set<ObjectPtr >& theObjects)
436 if (theObjects.empty())
439 bool isModified = false;
440 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
441 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
444 ObjectPtr anObject = *anIt;
445 if (!anObject->isDisplayed())
447 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
453 //********************************************************************
454 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
455 std::set<ObjectPtr>& theObjects)
458 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
459 anIt != theItems.end(); anIt++) {
460 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
461 if (theObjects.find(aPrs->object()) == theObjects.end())
462 theObjects.insert(aPrs->object());
466 //********************************************************************
467 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
469 QDockWidget::closeEvent(theEvent);
473 //********************************************************************
474 void XGUI_FacesPanel::onDeleteItem()
479 //********************************************************************
480 void XGUI_FacesPanel::onTransparencyChanged()
482 std::map<ObjectPtr, TopoDS_ListOfShape> anObjectToShapes;
483 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
484 QMap<int, ModuleBase_ViewerPrsPtr >::const_iterator aIt;
485 for (aIt = myItems.cbegin(); aIt != myItems.cend(); aIt++) {
486 objectsMapFromPrs(aIt.value(), anObjectToShapes, anObjectToPrs);
489 double aTransp = Config_PropManager::real("Visualization", "hidden_face_transparency");
490 std::set<ObjectPtr> aObjects;
491 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs)>::const_iterator aPrsIt;
492 for (aPrsIt = anObjectToPrs.cbegin(); aPrsIt != anObjectToPrs.cend(); aPrsIt++) {
493 aObjects.insert(aPrsIt->first);
494 aPrsIt->second->setHiddenSubShapeTransparency(useTransparency()? aTransp : 1);
496 if (redisplayObjects(aObjects))
500 //********************************************************************
501 void XGUI_FacesPanel::onClosed()
503 setActivePanel(false);
507 //********************************************************************
509 void XGUI_FacesPanel::processUndo()
511 if(!myUndoList.size())
514 myListView->selectIndices({myUndoList.back()});
516 myUndoList.pop_back();
519 //********************************************************************
520 void XGUI_FacesPanel::flushRedisplay() const
522 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
523 // Necessary for update visibility icons in ObjectBrowser
524 XGUI_ObjectsBrowser* anObjectBrowser = myWorkshop->objectBrowser();
526 anObjectBrowser->updateAllIndexes();
527 myWorkshop->viewer()->update();
531 //********************************************************************
532 void XGUI_FacesPanel::onObjectDisplay(ObjectPtr theObject, AISObjectPtr theAIS)
534 bool aContains = false;
535 QMap<int, ModuleBase_ViewerPrsPtr>::iterator aIt;
536 for (aIt = myItems.begin(); aIt != myItems.end(); aIt++) {
537 ModuleBase_ViewerPrsPtr aPrs = aIt.value();
538 if (aPrs->object() == theObject) {
544 std::map<ObjectPtr, TopoDS_ListOfShape> aObjectToShapes;
545 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs)> aObjectToPrs;
546 std::set<ObjectPtr> aObjects;
547 std::set<int> aIdsToRem;
549 TopoDS_ListOfShape aHideShapes;
550 std::map<ObjectPtr, TopoDS_ListOfShape>::const_iterator aSIt;
551 for (aIt = myItems.begin(); aIt != myItems.end(); aIt++) {
552 ModuleBase_ViewerPrsPtr aPrs = aIt.value();
553 if (aPrs->object() == theObject) {
554 TopoDS_Shape aShape = aPrs->shape()->impl<TopoDS_Shape>();
555 if (!aShape.IsNull())
556 aHideShapes.Append(aShape);
557 aPrs->setInteractive(theAIS->impl<Handle(AIS_InteractiveObject)>());
560 double aTransp = transparency();
561 if (aHideShapes.Size() > 0) {
562 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
563 theAIS->impl<Handle(AIS_InteractiveObject)>());
564 if (!aResultPrs.IsNull()) {
565 aResultPrs->setSubShapeHidden(aHideShapes);
566 aResultPrs->setHiddenSubShapeTransparency(aTransp);
567 aObjects.insert(theObject);
570 removeItems(aIdsToRem);
571 myWorkshop->selector()->clearSelection();
572 if (redisplayObjects(aObjects))
573 QTimer::singleShot(50, this, SLOT(flushRedisplay()));
577 void XGUI_FacesPanel::removeItems(std::set<int> theIds)
581 myListView->removeItems(theIds);
582 std::set<int>::const_iterator aRIt;
583 for (aRIt = theIds.begin(); aRIt != theIds.end(); aRIt++)
584 myItems.remove(*aRIt);