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_ListView.h>
30 #include <ModuleBase_ResultPrs.h>
31 #include <ModuleBase_Tools.h>
32 #include <ModuleBase_ViewerPrs.h>
33 #include <ModuleBase_SelectionFilterType.h>
35 #include <Config_PropManager.h>
36 #include <Events_Loop.h>
37 #include <GeomAlgoAPI_CompoundBuilder.h>
39 #include <ModelAPI_Events.h>
43 #include <QFocusEvent>
44 #include <QGridLayout>
45 #include <QListWidget>
46 #include <QMainWindow>
48 static const int LayoutMargin = 3;
50 //********************************************************************
51 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
52 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
54 setWindowTitle(tr("Hide Faces"));
55 QAction* aViewAct = toggleViewAction();
56 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
58 QWidget* aContent = new QWidget(this);
59 QGridLayout* aMainLayout = new QGridLayout(aContent);
60 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
63 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
64 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
66 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
67 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
69 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
70 aMainLayout->addWidget(myListView->getControl(), 1, 0);
72 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
73 myListView->getControl()->viewport()->installEventFilter(this);
76 //********************************************************************
77 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
79 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
82 // clear internal containers
83 myListView->getControl()->clear();
86 // restore previous view of presentations
87 bool isModified = redisplayObjects(myItemObjects);
88 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
89 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects) || isModified;
90 if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
93 updateProcessedObjects(myItems, myItemObjects);
94 myHiddenObjects.clear();
95 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
98 //********************************************************************
99 bool XGUI_FacesPanel::isEmpty() const
101 return myItems.size() == 0;
104 //********************************************************************
105 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
107 theModes.append(TopAbs_FACE);
110 //********************************************************************
111 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
113 ModuleBase_IModule* aModule = myWorkshop->module();
114 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
116 theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
117 theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
118 theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
121 //********************************************************************
122 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
124 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
125 if (theEvent->type() == QEvent::MouseButtonRelease)
127 if (myListView->getControl()->viewport() == aWidget)
128 setActivePanel(true);
130 // pass the event on to the parent class
131 return QObject::eventFilter(theObject, theEvent);
134 //********************************************************************
135 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
137 if (myIsActive == theIsActive)
140 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
141 myIsActive = theIsActive;
145 // selection should be cleared after emit of signal to do not process selection change
146 // event by the previous selector
147 // the selection is cleared by activating selection control
148 XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
154 //********************************************************************
155 bool XGUI_FacesPanel::useTransparency() const
157 return myHiddenOrTransparent->isChecked();
160 //********************************************************************
161 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
163 std::set<int> anIndicesToBeRemoved;
164 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
165 anItemsIt != myItems.end(); anItemsIt++)
167 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
168 ObjectPtr anObject = aPrs->object();
169 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
171 anIndicesToBeRemoved.insert(anItemsIt.key());
174 // remove from myItes container
175 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
176 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
178 myItems.remove(*aToBeRemovedIt);
180 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
181 updateProcessedObjects(myItems, myItemObjects);
182 myListView->removeItems(anIndicesToBeRemoved);
184 // remove from container of hidden objects
185 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
186 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
188 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
189 myHiddenObjects.erase(*aHiddenIt);
193 //********************************************************************
194 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
196 switch (theActionType) {
198 // return processEnter();
200 setActivePanel(false);
203 return processDelete();
211 //********************************************************************
212 void XGUI_FacesPanel::processSelection()
214 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
215 ModuleBase_ISelection::Viewer);
216 bool isModified = false;
217 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
219 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
220 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
221 for (int i = 0; i < aSelected.size(); i++) {
222 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
223 ObjectPtr anObject = aPrs->object();
226 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
229 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
230 aPrs->interactive());
231 if (aResultPrs.IsNull())
233 QString aItemName = XGUI_Tools::generateName(aPrs);
234 if (myListView->hasItem(aItemName))
237 myItems.insert(myLastItemIndex, aPrs);
238 myListView->addItem(aItemName, myLastItemIndex);
242 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
243 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
245 NCollection_List<TopoDS_Shape> aListOfShapes;
246 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
247 anObjectToShapes[anObject] = aListOfShapes;
248 anObjectToPrs[anObject] = aResultPrs;
251 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
252 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
253 ObjectPtr anObject = anIt->first;
254 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
256 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
258 if (!aResultPrs->hasSubShapeVisible(anIt->second)) { // redisplay
259 // erase object because it is entirely hidden
260 anObject->setDisplayed(false);
261 myHiddenObjects.insert(anObject);
263 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
266 updateProcessedObjects(myItems, myItemObjects);
269 onTransparencyChanged();
272 //********************************************************************
273 bool XGUI_FacesPanel::processDelete()
275 //appendFirstSelectionInHistory();
276 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
278 std::set<int> aSelectedIds;
279 myListView->getSelectedIndices(aSelectedIds);
280 if (aSelectedIds.empty())
283 bool isModified = false;
284 std::set<ObjectPtr> aRestoredObjects;
285 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
287 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
288 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
289 aRestoredObjects.insert(aPrs->object());
290 myItems.remove(*anIt);
294 bool isRedisplayed = redisplayObjects(aRestoredObjects);
295 isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects)
299 // should be after flush of redisplay to have items object to be updated
300 updateProcessedObjects(myItems, myItemObjects);
303 myListView->removeSelectedItems();
305 myListView->restoreSelection(anIndices);
306 //appendSelectionInHistory();
310 //********************************************************************
311 bool XGUI_FacesPanel::redisplayObjects(
312 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
314 if (theObjects.empty())
317 bool isModified = false;
318 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
319 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
322 ObjectPtr anObject = *anIt;
323 if (!anObject->isDisplayed())
325 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
331 //********************************************************************
332 bool XGUI_FacesPanel::displayHiddenObjects(
333 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
334 std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects)
336 if (theObjects.empty())
339 bool isModified = false;
340 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
342 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
345 ObjectPtr anObject = *anIt;
346 // if the object was hidden by this panel
347 if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
349 theHiddenObjects.erase(anObject);
350 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
351 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
357 //********************************************************************
358 bool XGUI_FacesPanel::hideEmptyObjects()
360 bool isModified = false;
361 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
362 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
363 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
365 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
366 anIt != myItems.end(); anIt++) {
367 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
368 ObjectPtr anObject = aPrs->object();
369 if (!anObject.get() || !anObject->isDisplayed())
372 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
373 aPrs->interactive());
374 if (aResultPrs.IsNull())
377 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
378 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
380 NCollection_List<TopoDS_Shape> aListOfShapes;
381 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
382 anObjectToShapes[anObject] = aListOfShapes;
383 anObjectToPrs[anObject] = aResultPrs;
386 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
387 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
388 ObjectPtr anObject = anIt->first;
389 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
391 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
393 if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
394 // erase object because it is entirely hidden
395 anObject->setDisplayed(false);
396 myHiddenObjects.insert(anObject);
397 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
404 //********************************************************************
405 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
406 std::set<ObjectPtr>& theObjects)
409 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
410 anIt != theItems.end(); anIt++) {
411 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
412 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
413 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
415 theObjects.insert(anObject);
419 //********************************************************************
420 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
422 QDockWidget::closeEvent(theEvent);
426 //********************************************************************
427 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
428 const AISObjectPtr& thePresentation)
430 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
433 if (thePresentation.get() == NULL)
436 if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
439 // if the object is displayed, the hidden faces are collected and set to the presentation
440 bool isModified = false;
441 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
442 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
443 anIt != myItems.end(); anIt++) {
444 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
445 if (aPrs.get() && aPrs->object() != theObject)
447 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
448 if (!aHiddenSubShapes.Contains(aShape))
449 aHiddenSubShapes.Append(aShape);
452 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
453 thePresentation->impl<Handle(AIS_InteractiveObject)>());
454 if (aResultPrs.IsNull())
457 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
459 double aTransparency = !useTransparency() ? 1
460 : Config_PropManager::real("Visualization", "hidden_face_transparency");
461 isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
466 //********************************************************************
467 void XGUI_FacesPanel::onDeleteItem()
472 //********************************************************************
473 void XGUI_FacesPanel::onTransparencyChanged()
475 bool isModified = false;
476 if (useTransparency()) {
477 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
478 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects);
481 isModified = hideEmptyObjects();
483 isModified = redisplayObjects(myItemObjects) || isModified;
488 //********************************************************************
489 void XGUI_FacesPanel::onClosed()
491 setActivePanel(false);
495 //********************************************************************
496 void XGUI_FacesPanel::flushRedisplay() const
498 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
499 // Necessary for update visibility icons in ObjectBrowser
500 XGUI_ObjectsBrowser* anObjectBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
502 anObjectBrowser->updateAllIndexes();