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 "XGUI_FacesPanel.h"
21 #include "XGUI_ObjectsBrowser.h"
22 #include "XGUI_SelectionMgr.h"
23 #include "XGUI_Tools.h"
24 #include "XGUI_Workshop.h"
26 #include <ModuleBase_IModule.h>
27 #include <ModuleBase_ISelection.h>
28 #include <ModuleBase_IWorkshop.h>
29 #include <ModuleBase_IViewer.h>
30 #include <ModuleBase_ListView.h>
31 #include <ModuleBase_ResultPrs.h>
32 #include <ModuleBase_Tools.h>
33 #include <ModuleBase_ViewerPrs.h>
34 #include <ModuleBase_SelectionFilterType.h>
36 #include <Config_PropManager.h>
37 #include <Events_Loop.h>
38 #include <GeomAlgoAPI_CompoundBuilder.h>
40 #include <ModelAPI_Events.h>
44 #include <QFocusEvent>
45 #include <QGridLayout>
46 #include <QListWidget>
47 #include <QMainWindow>
49 static const int LayoutMargin = 3;
51 //********************************************************************
52 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
53 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
55 setWindowTitle(tr("Hide Faces"));
56 QAction* aViewAct = toggleViewAction();
57 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
59 QWidget* aContent = new QWidget(this);
60 QGridLayout* aMainLayout = new QGridLayout(aContent);
61 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
64 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
65 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
67 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
68 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
70 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
71 aMainLayout->addWidget(myListView->getControl(), 1, 0);
73 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
74 myListView->getControl()->viewport()->installEventFilter(this);
77 //********************************************************************
78 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
80 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
83 // clear internal containers
84 myListView->getControl()->clear();
87 // restore previous view of presentations
88 bool isModified = redisplayObjects(myItemObjects);
89 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
90 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects) || isModified;
91 if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
94 updateProcessedObjects(myItems, myItemObjects);
95 myHiddenObjects.clear();
96 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
99 //********************************************************************
100 bool XGUI_FacesPanel::isEmpty() const
102 return myItems.size() == 0;
105 //********************************************************************
106 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
108 theModes.append(TopAbs_FACE);
111 //********************************************************************
112 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
114 ModuleBase_IModule* aModule = myWorkshop->module();
115 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
117 theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
118 theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
119 theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
122 //********************************************************************
123 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
125 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
126 if (theEvent->type() == QEvent::MouseButtonRelease)
128 if (myListView->getControl()->viewport() == aWidget)
129 setActivePanel(true);
131 // pass the event on to the parent class
132 return QObject::eventFilter(theObject, theEvent);
135 //********************************************************************
136 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
138 if (myIsActive == theIsActive)
141 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
142 myIsActive = theIsActive;
146 // selection should be cleared after emit of signal to do not process selection change
147 // event by the previous selector
148 // the selection is cleared by activating selection control
149 XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
155 //********************************************************************
156 bool XGUI_FacesPanel::useTransparency() const
158 return myHiddenOrTransparent->isChecked();
161 //********************************************************************
162 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
164 std::set<int> anIndicesToBeRemoved;
165 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
166 anItemsIt != myItems.end(); anItemsIt++)
168 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
169 ObjectPtr anObject = aPrs->object();
170 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
172 anIndicesToBeRemoved.insert(anItemsIt.key());
175 // remove from myItes container
176 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
177 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
179 myItems.remove(*aToBeRemovedIt);
181 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
182 updateProcessedObjects(myItems, myItemObjects);
183 myListView->removeItems(anIndicesToBeRemoved);
185 // remove from container of hidden objects
186 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
187 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
189 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
190 myHiddenObjects.erase(*aHiddenIt);
194 //********************************************************************
195 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
197 switch (theActionType) {
199 // return processEnter();
201 setActivePanel(false);
204 return processDelete();
212 //********************************************************************
213 void XGUI_FacesPanel::processSelection()
215 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
216 ModuleBase_ISelection::Viewer);
217 bool isModified = false;
218 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
220 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
221 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
222 for (int i = 0; i < aSelected.size(); i++) {
223 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
224 ObjectPtr anObject = aPrs->object();
227 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
230 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
231 aPrs->interactive());
232 if (aResultPrs.IsNull())
234 QString aItemName = XGUI_Tools::generateName(aPrs);
235 if (myListView->hasItem(aItemName))
238 myItems.insert(myLastItemIndex, aPrs);
239 myListView->addItem(aItemName, myLastItemIndex);
243 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
244 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
246 NCollection_List<TopoDS_Shape> aListOfShapes;
247 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
248 anObjectToShapes[anObject] = aListOfShapes;
249 anObjectToPrs[anObject] = aResultPrs;
252 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
253 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
254 ObjectPtr anObject = anIt->first;
255 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
257 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
259 if (!aResultPrs->hasSubShapeVisible(anIt->second)) { // redisplay
260 // erase object because it is entirely hidden
261 anObject->setDisplayed(false);
262 myHiddenObjects.insert(anObject);
264 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
267 updateProcessedObjects(myItems, myItemObjects);
270 onTransparencyChanged();
273 //********************************************************************
274 bool XGUI_FacesPanel::processDelete()
276 //appendFirstSelectionInHistory();
277 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
279 std::set<int> aSelectedIds;
280 myListView->getSelectedIndices(aSelectedIds);
281 if (aSelectedIds.empty())
284 bool isModified = false;
285 std::set<ObjectPtr> aRestoredObjects;
286 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
288 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
289 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
290 aRestoredObjects.insert(aPrs->object());
291 myItems.remove(*anIt);
295 bool isRedisplayed = redisplayObjects(aRestoredObjects);
296 isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects)
300 myWorkshop->viewer()->update();
302 // should be after flush of redisplay to have items object to be updated
303 updateProcessedObjects(myItems, myItemObjects);
307 myListView->removeSelectedItems();
309 myListView->restoreSelection(anIndices);
310 //appendSelectionInHistory();
314 //********************************************************************
315 bool XGUI_FacesPanel::redisplayObjects(
316 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
318 if (theObjects.empty())
321 bool isModified = false;
322 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
323 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
326 ObjectPtr anObject = *anIt;
327 if (!anObject->isDisplayed())
329 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
335 //********************************************************************
336 bool XGUI_FacesPanel::displayHiddenObjects(
337 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
338 std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects)
340 if (theObjects.empty())
343 bool isModified = false;
344 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
346 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
349 ObjectPtr anObject = *anIt;
350 // if the object was hidden by this panel
351 if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
353 theHiddenObjects.erase(anObject);
354 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
355 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
361 //********************************************************************
362 bool XGUI_FacesPanel::hideEmptyObjects()
364 bool isModified = false;
365 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
366 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
367 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
369 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
370 anIt != myItems.end(); anIt++) {
371 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
372 ObjectPtr anObject = aPrs->object();
373 if (!anObject.get() || !anObject->isDisplayed())
376 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
377 aPrs->interactive());
378 if (aResultPrs.IsNull())
381 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
382 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
384 NCollection_List<TopoDS_Shape> aListOfShapes;
385 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
386 anObjectToShapes[anObject] = aListOfShapes;
387 anObjectToPrs[anObject] = aResultPrs;
390 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
391 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
392 ObjectPtr anObject = anIt->first;
393 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
395 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
397 if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
398 // erase object because it is entirely hidden
399 anObject->setDisplayed(false);
400 myHiddenObjects.insert(anObject);
401 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
408 //********************************************************************
409 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
410 std::set<ObjectPtr>& theObjects)
413 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
414 anIt != theItems.end(); anIt++) {
415 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
416 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
417 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
419 theObjects.insert(anObject);
423 //********************************************************************
424 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
426 QDockWidget::closeEvent(theEvent);
430 //********************************************************************
431 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
432 const AISObjectPtr& thePresentation)
434 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
437 if (thePresentation.get() == NULL)
440 if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
443 // if the object is displayed, the hidden faces are collected and set to the presentation
444 bool isModified = false;
445 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
446 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
447 anIt != myItems.end(); anIt++) {
448 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
449 if (aPrs.get() && aPrs->object() != theObject)
451 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
452 if (!aHiddenSubShapes.Contains(aShape))
453 aHiddenSubShapes.Append(aShape);
456 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
457 thePresentation->impl<Handle(AIS_InteractiveObject)>());
458 if (aResultPrs.IsNull())
461 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
463 double aTransparency = !useTransparency() ? 1
464 : Config_PropManager::real("Visualization", "hidden_face_transparency");
465 isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
470 //********************************************************************
471 void XGUI_FacesPanel::onDeleteItem()
476 //********************************************************************
477 void XGUI_FacesPanel::onTransparencyChanged()
479 bool isModified = false;
480 if (useTransparency()) {
481 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
482 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects);
485 isModified = hideEmptyObjects();
487 isModified = redisplayObjects(myItemObjects) || isModified;
492 //********************************************************************
493 void XGUI_FacesPanel::onClosed()
495 setActivePanel(false);
499 //********************************************************************
500 void XGUI_FacesPanel::flushRedisplay() const
502 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
503 // Necessary for update visibility icons in ObjectBrowser
504 XGUI_ObjectsBrowser* anObjectBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
506 anObjectBrowser->updateAllIndexes();