1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_FacesPanel.h"
23 #include <Config_PropManager.h>
24 #include <Events_Loop.h>
25 #include <GeomAlgoAPI_CompoundBuilder.h>
27 #include <ModelAPI_Events.h>
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_ISelection.h>
31 #include "ModuleBase_IWorkshop.h"
32 #include "ModuleBase_ListView.h"
33 #include "ModuleBase_ResultPrs.h"
34 #include "ModuleBase_Tools.h"
35 #include "ModuleBase_ViewerPrs.h"
37 #include "XGUI_ObjectsBrowser.h"
38 #include "XGUI_SelectionMgr.h"
39 #include "XGUI_SelectionFilterType.h"
40 #include "XGUI_Tools.h"
41 #include "XGUI_Workshop.h"
45 #include <QFocusEvent>
46 #include <QGridLayout>
47 #include <QListWidget>
48 #include <QMainWindow>
50 static const int LayoutMargin = 3;
52 //********************************************************************
53 XGUI_FacesPanel::XGUI_FacesPanel(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop)
54 : QDockWidget(theParent), myIsActive(false), myWorkshop(theWorkshop)
56 setWindowTitle(tr("Hide Faces"));
57 QAction* aViewAct = toggleViewAction();
58 setStyleSheet("::title { position: relative; padding-left: 5px; text-align: left center }");
60 QWidget* aContent = new QWidget(this);
61 QGridLayout* aMainLayout = new QGridLayout(aContent);
62 aMainLayout->setContentsMargins(LayoutMargin, LayoutMargin, LayoutMargin, LayoutMargin);
65 myHiddenOrTransparent = new QCheckBox(tr("Transparent"), aContent);
66 connect(myHiddenOrTransparent, SIGNAL(toggled(bool)), SLOT(onTransparencyChanged()));
68 myListView = new ModuleBase_ListView(aContent, "", "Hidden/transparent faces in 3D view");
69 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
71 aMainLayout->addWidget(myHiddenOrTransparent, 0, 0);
72 aMainLayout->addWidget(myListView->getControl(), 1, 0);
74 myListView->getControl()->setFocusPolicy(Qt::StrongFocus);
75 myListView->getControl()->viewport()->installEventFilter(this);
78 //********************************************************************
79 void XGUI_FacesPanel::reset(const bool isToFlushRedisplay)
81 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
84 // clear internal containers
85 myListView->getControl()->clear();
88 // restore previous view of presentations
89 bool isModified = redisplayObjects(myItemObjects);
90 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
91 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects) || isModified;
92 if (isModified)// && isToFlushRedisplay) // flush signal immediatelly until container is filled
95 updateProcessedObjects(myItems, myItemObjects);
96 myHiddenObjects.clear();
97 myLastItemIndex = 0; // it should be after redisplay as flag used in customize
100 //********************************************************************
101 bool XGUI_FacesPanel::isEmpty() const
103 return myItems.size() == 0;
106 //********************************************************************
107 void XGUI_FacesPanel::selectionModes(QIntList& theModes)
109 theModes.append(TopAbs_FACE);
112 //********************************************************************
113 void XGUI_FacesPanel::selectionFilters(SelectMgr_ListOfFilter& theSelectionFilters)
115 ModuleBase_IModule* aModule = myWorkshop->module();
116 QIntList aModuleSelectionFilters = myWorkshop->module()->selectionFilters();
118 theSelectionFilters.Append(aModule->selectionFilter(SF_GlobalFilter));
119 theSelectionFilters.Append(aModule->selectionFilter(SF_FilterInfinite));
120 theSelectionFilters.Append(aModule->selectionFilter(SF_ResultGroupNameFilter));
123 //********************************************************************
124 bool XGUI_FacesPanel::eventFilter(QObject* theObject, QEvent *theEvent)
126 QWidget* aWidget = qobject_cast<QWidget*>(theObject);
127 if (theEvent->type() == QEvent::MouseButtonRelease)
129 if (myListView->getControl()->viewport() == aWidget)
130 setActivePanel(true);
132 // pass the event on to the parent class
133 return QObject::eventFilter(theObject, theEvent);
136 //********************************************************************
137 void XGUI_FacesPanel::setActivePanel(const bool theIsActive)
139 if (myIsActive == theIsActive)
142 ModuleBase_Tools::setShadowEffect(myListView->getControl(), theIsActive);
143 myIsActive = theIsActive;
147 // selection should be cleared after emit of signal to do not process selection change
148 // event by the previous selector
149 // the selection is cleared by activating selection control
150 XGUI_Tools::workshop(myWorkshop)->selector()->clearSelection();
156 //********************************************************************
157 bool XGUI_FacesPanel::useTransparency() const
159 return myHiddenOrTransparent->isChecked();
162 //********************************************************************
163 void XGUI_FacesPanel::restoreObjects(const std::set<ObjectPtr>& theHiddenObjects)
165 std::set<int> anIndicesToBeRemoved;
166 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anItemsIt = myItems.begin();
167 anItemsIt != myItems.end(); anItemsIt++)
169 ModuleBase_ViewerPrsPtr aPrs = anItemsIt.value();
170 ObjectPtr anObject = aPrs->object();
171 if (theHiddenObjects.find(anObject) == theHiddenObjects.end()) // not found
173 anIndicesToBeRemoved.insert(anItemsIt.key());
176 // remove from myItes container
177 for (std::set<int>::const_iterator aToBeRemovedIt = anIndicesToBeRemoved.begin();
178 aToBeRemovedIt != anIndicesToBeRemoved.end(); aToBeRemovedIt++)
180 myItems.remove(*aToBeRemovedIt);
182 if (!anIndicesToBeRemoved.empty()) // means that myItems has been changed
183 updateProcessedObjects(myItems, myItemObjects);
184 myListView->removeItems(anIndicesToBeRemoved);
186 // remove from container of hidden objects
187 for (std::set<ObjectPtr>::const_iterator aHiddenIt = theHiddenObjects.begin();
188 aHiddenIt != theHiddenObjects.end(); aHiddenIt++)
190 if (myHiddenObjects.find(*aHiddenIt) != myHiddenObjects.end()) /// found objects
191 myHiddenObjects.erase(*aHiddenIt);
195 //********************************************************************
196 bool XGUI_FacesPanel::processAction(ModuleBase_ActionType theActionType)
198 switch (theActionType) {
200 // return processEnter();
202 setActivePanel(false);
205 return processDelete();
213 //********************************************************************
214 void XGUI_FacesPanel::processSelection()
216 QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
217 ModuleBase_ISelection::Viewer);
218 bool isModified = false;
219 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
221 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
222 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
223 for (int i = 0; i < aSelected.size(); i++) {
224 ModuleBase_ViewerPrsPtr aPrs = aSelected[i];
225 ObjectPtr anObject = aPrs->object();
228 if (ModuleBase_Tools::getSelectedShape(aPrs).ShapeType() != TopAbs_FACE)
231 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
232 aPrs->interactive());
233 if (aResultPrs.IsNull())
235 QString aItemName = XGUI_Tools::generateName(aPrs);
236 if (myListView->hasItem(aItemName))
239 myItems.insert(myLastItemIndex, aPrs);
240 myListView->addItem(aItemName, myLastItemIndex);
244 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
245 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
247 NCollection_List<TopoDS_Shape> aListOfShapes;
248 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
249 anObjectToShapes[anObject] = aListOfShapes;
250 anObjectToPrs[anObject] = aResultPrs;
253 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
254 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
255 ObjectPtr anObject = anIt->first;
256 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
258 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
260 if (aResultPrs->hasSubShapeVisible(anIt->second) || useTransparency()) // redisplay
261 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
262 else { // erase object because it is entirely hidden
263 anObject->setDisplayed(false);
264 myHiddenObjects.insert(anObject);
265 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
269 updateProcessedObjects(myItems, myItemObjects);
274 //********************************************************************
275 bool XGUI_FacesPanel::processDelete()
277 //appendFirstSelectionInHistory();
278 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
280 std::set<int> aSelectedIds;
281 myListView->getSelectedIndices(aSelectedIds);
282 if (aSelectedIds.empty())
285 bool isModified = false;
286 std::set<ObjectPtr> aRestoredObjects;
287 for (std::set<int>::const_iterator anIt = aSelectedIds.begin(); anIt != aSelectedIds.end();
289 ModuleBase_ViewerPrsPtr aPrs = myItems[*anIt];
290 if (aRestoredObjects.find(aPrs->object()) == aRestoredObjects.end())
291 aRestoredObjects.insert(aPrs->object());
292 myItems.remove(*anIt);
296 bool isRedisplayed = redisplayObjects(aRestoredObjects);
297 isRedisplayed = displayHiddenObjects(aRestoredObjects, myHiddenObjects)
301 // should be after flush of redisplay to have items object to be updated
302 updateProcessedObjects(myItems, myItemObjects);
305 myListView->removeSelectedItems();
307 myListView->restoreSelection(anIndices);
308 //appendSelectionInHistory();
312 //********************************************************************
313 bool XGUI_FacesPanel::redisplayObjects(
314 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects)
316 if (theObjects.empty())
319 bool isModified = false;
320 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
321 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
324 ObjectPtr anObject = *anIt;
325 if (!anObject->isDisplayed())
327 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
333 //********************************************************************
334 bool XGUI_FacesPanel::displayHiddenObjects(
335 const std::set<std::shared_ptr<ModelAPI_Object> >& theObjects,
336 std::set<std::shared_ptr<ModelAPI_Object> >& theHiddenObjects)
338 if (theObjects.empty())
341 bool isModified = false;
342 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
344 for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin(); anIt != theObjects.end();
347 ObjectPtr anObject = *anIt;
348 // if the object was hidden by this panel
349 if (anObject->isDisplayed() || theHiddenObjects.find(anObject) == theHiddenObjects.end())
351 theHiddenObjects.erase(anObject);
352 anObject->setDisplayed(true); // it means that the object is hidden by hide all faces
353 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
359 //********************************************************************
360 bool XGUI_FacesPanel::hideEmptyObjects()
362 bool isModified = false;
363 static Events_ID aDispEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
364 std::map<ObjectPtr, NCollection_List<TopoDS_Shape> > anObjectToShapes;
365 std::map<ObjectPtr, Handle(ModuleBase_ResultPrs) > anObjectToPrs;
367 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
368 anIt != myItems.end(); anIt++) {
369 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
370 ObjectPtr anObject = aPrs->object();
371 if (!anObject.get() || !anObject->isDisplayed())
374 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
375 aPrs->interactive());
376 if (aResultPrs.IsNull())
379 if (anObjectToShapes.find(anObject) != anObjectToShapes.end())
380 anObjectToShapes.at(anObject).Append(ModuleBase_Tools::getSelectedShape(aPrs));
382 NCollection_List<TopoDS_Shape> aListOfShapes;
383 aListOfShapes.Append(ModuleBase_Tools::getSelectedShape(aPrs));
384 anObjectToShapes[anObject] = aListOfShapes;
385 anObjectToPrs[anObject] = aResultPrs;
388 for (std::map<ObjectPtr, NCollection_List<TopoDS_Shape> >::const_iterator
389 anIt = anObjectToShapes.begin(); anIt != anObjectToShapes.end(); anIt++) {
390 ObjectPtr anObject = anIt->first;
391 if (!anObject.get() || anObjectToPrs.find(anObject) == anObjectToPrs.end())
393 Handle(ModuleBase_ResultPrs) aResultPrs = anObjectToPrs.at(anObject);
395 if (!aResultPrs->hasSubShapeVisible(anIt->second)) {
396 // erase object because it is entirely hidden
397 anObject->setDisplayed(false);
398 myHiddenObjects.insert(anObject);
399 ModelAPI_EventCreator::get()->sendUpdated(anObject, aDispEvent);
406 //********************************************************************
407 void XGUI_FacesPanel::updateProcessedObjects(QMap<int, ModuleBase_ViewerPrsPtr> theItems,
408 std::set<ObjectPtr>& theObjects)
411 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = theItems.begin();
412 anIt != theItems.end(); anIt++) {
413 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
414 ObjectPtr anObject = aPrs.get() ? aPrs->object() : ObjectPtr();
415 if (anObject.get() && theObjects.find(anObject) != theObjects.end())
417 theObjects.insert(anObject);
421 //********************************************************************
422 void XGUI_FacesPanel::closeEvent(QCloseEvent* theEvent)
424 QDockWidget::closeEvent(theEvent);
428 //********************************************************************
429 bool XGUI_FacesPanel::customizeObject(const ObjectPtr& theObject,
430 const AISObjectPtr& thePresentation)
432 if (myLastItemIndex == 0) // do nothing because there was no activity in the pane after reset
435 if (thePresentation.get() == NULL)
438 if (myItemObjects.find(theObject) == myItemObjects.end()) // not found
441 // if the object is displayed, the hidden faces are collected and set to the presentation
442 bool isModified = false;
443 NCollection_List<TopoDS_Shape> aHiddenSubShapes;
444 for (QMap<int, ModuleBase_ViewerPrsPtr>::const_iterator anIt = myItems.begin();
445 anIt != myItems.end(); anIt++) {
446 ModuleBase_ViewerPrsPtr aPrs = anIt.value();
447 if (aPrs.get() && aPrs->object() != theObject)
449 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(aPrs);
450 if (!aHiddenSubShapes.Contains(aShape))
451 aHiddenSubShapes.Append(aShape);
454 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(
455 thePresentation->impl<Handle(AIS_InteractiveObject)>());
456 if (aResultPrs.IsNull())
459 isModified = aResultPrs->setSubShapeHidden(aHiddenSubShapes);
461 double aTransparency = !useTransparency() ? 1
462 : Config_PropManager::real("Visualization", "hidden_face_transparency");
463 isModified = aResultPrs->setHiddenSubShapeTransparency(aTransparency) || isModified;
468 //********************************************************************
469 void XGUI_FacesPanel::onDeleteItem()
474 //********************************************************************
475 void XGUI_FacesPanel::onTransparencyChanged()
477 bool isModified = false;
478 if (useTransparency()) {
479 std::set<std::shared_ptr<ModelAPI_Object> > aHiddenObjects = myHiddenObjects;
480 isModified = displayHiddenObjects(aHiddenObjects, myHiddenObjects);
483 isModified = hideEmptyObjects();
485 isModified = redisplayObjects(myItemObjects) || isModified;
490 //********************************************************************
491 void XGUI_FacesPanel::onClosed()
493 setActivePanel(false);
497 //********************************************************************
498 void XGUI_FacesPanel::flushRedisplay() const
500 Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
501 // Necessary for update visibility icons in ObjectBrowser
502 XGUI_ObjectsBrowser* anObjectBrowser = XGUI_Tools::workshop(myWorkshop)->objectBrowser();
504 anObjectBrowser->updateAllIndexes();